net.nand.util.i18n
Class PropsFileParser

java.lang.Object
  extended by net.nand.util.i18n.PropsFileParser

public class PropsFileParser
extends java.lang.Object

Represents a parsed properties file.

Usage:

See ParsedPropsFilePair to work with two related property files with similar sets of keys.

Remember that .properties bundle files are encoded not in UTF-8 but in ISO-8859-1:

Characters outside that encoding must use \uXXXX code escapes.

Author:
Jeremy D Monin <jeremy@nand.net>
See Also:
PropsFileWriter

Nested Class Summary
static class PropsFileParser.KeyPairLine
          Parsed key-pair line from one properties file; includes its preceding PropsFileParser.KeyPairLine.comment lines, if any.
 
Field Summary
static java.lang.String KEY_PREFIX_NO_LOCALIZE
          If a key starts with this prefix "_nolocaliz", it should be present only in the source language (something.properties), and never localized (something_lang.properties).
 
Constructor Summary
PropsFileParser()
           
 
Method Summary
static void findDuplicateKeys(java.util.List<PropsFileParser.KeyPairLine> kpLines, java.util.Map<java.lang.String,java.lang.String> dupeKeys)
          Search a list of key-pair lines for any duplicates, filling the dupeKeys map.
static java.util.List<PropsFileParser.KeyPairLine> parseOneFile(java.io.File pFile)
          Parse one properties file.
static java.util.List<PropsFileParser.KeyPairLine> parseOneFile(java.io.File pFile, java.util.Map<java.lang.String,java.lang.String> dupeKeys)
          Parse one properties file.
private static java.lang.String unescapeUnicodes(java.lang.String valStr)
          Un-escape \uXXXX sequences into unicode characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_PREFIX_NO_LOCALIZE

public static final java.lang.String KEY_PREFIX_NO_LOCALIZE
If a key starts with this prefix "_nolocaliz", it should be present only in the source language (something.properties), and never localized (something_lang.properties).

See Also:
Constant Field Values
Constructor Detail

PropsFileParser

public PropsFileParser()
Method Detail

parseOneFile

public static java.util.List<PropsFileParser.KeyPairLine> parseOneFile(java.io.File pFile)
                                                                throws java.io.IOException,
                                                                       java.lang.SecurityException,
                                                                       java.io.UnsupportedEncodingException
Parse one properties file. May include a header comment (separated by blank line(s) from the first key line), may include an ending comment after the last key line.

Parameters:
pFile - File to parse
Returns:
the file entries as a List. If the file starts with a header comment, the first list entry will have a comment and null key and value. If the file ends in a comment, the last list entry will have a comment and null key and value.
Throws:
java.io.IOException - If file not found, cannot be read, etc.
java.lang.SecurityException - if read access is denied
java.io.UnsupportedEncodingException - if the "ISO-8859-1" file encoding is somehow not supported; this is the encoding used by Java properties files, so it should be available; this error is not expected to occur.

parseOneFile

public static java.util.List<PropsFileParser.KeyPairLine> parseOneFile(java.io.File pFile,
                                                                       java.util.Map<java.lang.String,java.lang.String> dupeKeys)
                                                                throws java.io.IOException,
                                                                       java.lang.SecurityException,
                                                                       java.io.UnsupportedEncodingException
Parse one properties file. May include a header comment (separated by blank line(s) from the first key line), may include an ending comment after the last key line.

Parameters:
pFile - File to parse
dupeKeys - Output param for noting any duplicate key names and their values, or null. Key = each key seen more than once while parsing, value = values for that key.

For structure details see findDuplicateKeys(List, Map), including special case of 'duplicates' with same value.

Returns:
the file entries as a List. If the file starts with a header comment, the first list entry will have a comment and null key and value. If the file ends in a comment, the last list entry will have a comment and null key and value.
Throws:
java.io.IOException - If file not found, cannot be read, etc.
java.lang.SecurityException - if read access is denied
java.io.UnsupportedEncodingException - if the "ISO-8859-1" file encoding is somehow not supported; this is the encoding used by Java properties files, so it should be available; this error is not expected to occur.

findDuplicateKeys

public static void findDuplicateKeys(java.util.List<PropsFileParser.KeyPairLine> kpLines,
                                     java.util.Map<java.lang.String,java.lang.String> dupeKeys)
Search a list of key-pair lines for any duplicates, filling the dupeKeys map.

Parameters:
kpLines - Key-pair lines to search for duplicates, as returned by parseOneFile(File).
dupeKeys - Output param for noting any duplicate key names and their values. This should be empty when called.
Key = each key seen more than once while parsing, value = values for that key.
Because this is only for display/warning purposes, to simplify structure the duplicate values are kept as a string separated by ",".

Special case: If a key appears more than once with exactly the same value, it won't be considered a dupe and won't be placed in this map.


unescapeUnicodes

private static java.lang.String unescapeUnicodes(java.lang.String valStr)
Un-escape \uXXXX sequences into unicode characters.

Parameters:
valStr - String containing unicode escape sequences
Returns:
Un-escaped string