FileDocCategorySizeDatePackage
JadProperties.javaAPI DocphoneME MR2 API (J2ME)12423Wed May 02 18:00:04 BST 2007com.sun.midp.installer

JadProperties

public class JadProperties extends com.sun.midp.util.Properties
This class represents a set of properties loaded from a MIDP Java Application Descriptor. The parsing of descriptor is more relaxed than the MIDP 1.0 specification. First, the white space that is required before and after the property value is optional. Second, any extra carriage returns and end of file characters are ignored. Third, the key of the property is only checked for whitespace. Fourth, blanklines are allowed so the save code when parsing JAR manifests.

The set of properties, though not strictly ordered, will be stored in the same order it was read in or created, with modifications being appended to the end of the list by default.

If an alternate encoding type is not given, when saving properties to a stream or loading them from a stream, the ISO 8859-1 character encoding is used.

Fields Summary
protected static final int
HT
Horizontal Tab - Unicode character 0x09.
protected static final int
LF
Line Feed - Unicode character 0x0A.
protected static final int
CR
Carriage Return - Unicode character 0x0D.
protected static final int
EOF
End Of File - Unicode character 0x1A.
protected static final int
SP
SPace - Unicode character 0x20.
protected char[]
lineBuffer
Buffers one line from the stream.
Constructors Summary
public JadProperties()
Constructor - creates an empty property list.


                
      
    
Methods Summary
protected booleancheckKeyChars(java.lang.String key)
Check to see if all the chars in the key of a property are valid.

param
key key to check
return
false if a character is not valid for a key

        char[] temp = key.toCharArray();
        int len = temp.length;

        for (int i = 0; i < len; i++) {
            char current = temp[i];

            if (current <= 0x1F ||
                current == 0x7F ||
                current == '(" ||
                current == ')" ||
                current == '<" ||
                current == '>" ||
                current == '@" ||
                current == '," ||
                current == ';" ||
                current == '\'" ||
                current == '"" ||
                current == '/" ||
                current == '[" ||
                current == ']" ||
                current == '?" ||
                current == '=" ||
                current == '{" ||
                current == '}" ||
                current == SP ||
                current == HT) {

                return false;
            }
        }

        return true;
    
protected booleancheckValueChars(java.lang.String value)
Check to see if all the chars in the value of a property are valid.

param
value value to check
return
false if a character is not valid for a value

        char[] temp = value.toCharArray();
        int len = temp.length;

        // assume whitespace and newlines are trimmed
        for (int i = 0; i < len; i++) {
            char current = temp[i];
             
            // if current is a CTL character, throw exception
            if ((current <= 0x1F || current == 0x7F) && 
                (current != HT)) {
                return false;
            }
        }

        return true;
    
public synchronized voidload(java.io.InputStream inStream, java.lang.String enc)
Reads a JAD (key and element pairs) from the input stream. The stream uses the character encoding specified by enc

Every property occupies one line of the input stream. Each line is terminated by a line terminator which can be a LF, or (CR LF). Lines from the input stream are processed until end of file is reached on the input stream.

Every line describes one property to be added to the table. The key consists of all the characters from beginning of the line up to, but not including the first ASCII :. All remaining characters on the line become part of the associated element. The element is also trimmed of leading and trailing whitespace.

As an example, each of the following line specifies the key "Truth" and the associated element value "Beauty":

Truth: Beauty

This method will try to continue after a format error and load as many properties it can, but throw the last error encountered.

param
inStream the input stream.
param
enc character encoding used on input stream, can be null to get the default (UTF-8)
exception
IOException if an error occurred when reading from the input stream.
exception
InvalidJadException if the JAD is not formatted correctly.

            partialLoad(inStream, enc, Integer.MAX_VALUE);
    
public synchronized voidload(java.io.InputStream inStream)
Loads properties from the input stream using the default character encoding. Currently the default encoding is UTF8.

see
#load(InputStream inStream, String enc)
param
inStream the input stream.
exception
IOException if an error occurred when reading from the input stream.
exception
InvalidJadException if the JAD is not formatted correctly.

	load(inStream, null);
    
public voidpartialLoad(java.io.InputStream inStream, java.lang.String enc, int propertiesToLoad)
Loads up a given number of properties from a JAD. Used when authenticating a JAD.

param
inStream the input stream.
param
enc character encoding used on input stream, null for the default encoding (UTF-8)
param
propertiesToLoad maximum number of properties to load
exception
IOException if an error occurred when reading from the input stream.
exception
InvalidJadException if the JAD is not formatted correctly.

        Reader in;
        String line;
        int endOfKey;
        String key = null;
        int startOfValue;
        String value = null;
        InvalidJadException jadException = null;

        if (enc == null) {
            in = new InputStreamReader(inStream, "UTF-8");
        } else {
            in = new InputStreamReader(inStream, enc);
        }

	lineBuffer = new char[512];

	for (int i = 0; i < propertiesToLoad; i++) {
            // Get next line
            line = readLine(in);
            if (line == null) {
                break;
            }

            // blank line separate groups of properties
            if (line.length() == 0) {
                continue;
            }

            endOfKey = line.indexOf(":");
            if (endOfKey == -1) {
                jadException =
                    new InvalidJadException(InvalidJadException.INVALID_KEY,
                                            line);
                continue;
            }

            key = line.substring(0, endOfKey);

            if (key == null || key.length() == 0) {
                jadException =
                    new InvalidJadException(InvalidJadException.INVALID_KEY,
                                            line);
                continue;
            }

            if (!checkKeyChars(key)) {
                jadException =
                    new InvalidJadException(InvalidJadException.INVALID_KEY,
                                            line);
                continue;
            }

            startOfValue = endOfKey + 1;
            value = line.substring(startOfValue, line.length());
            value = value.trim();
            if (value == null) {
                jadException =
                    new InvalidJadException(InvalidJadException.INVALID_VALUE,
                                            key);
                continue;
            }

            if (!checkValueChars(value)) {
                jadException = new
                    InvalidJadException(InvalidJadException.INVALID_VALUE,
                                        key);
                continue;
            }

            putProperty(key, value);
	}

        // we only need the line buffer while loading, so let it be reclaimed
	lineBuffer = null;

        if (jadException != null) {
            throw jadException;
        }
    
protected voidputProperty(java.lang.String key, java.lang.String value)
Store key:value pair.

param
key the key to be placed into this property list.
param
value the value corresponding to key.
see
#getProperty

	setProperty(key, value);
    
protected java.lang.StringreadLine(java.io.Reader in)
Reads one using a given reader. LF or CR LF end a line. The end of line and end of file characters are dropped.

param
in reader for a JAD
return
one line of the JAD or null at the end of the JAD
exception
IOException thrown by the reader

	int room;
	int offset = 0;
	int c = 0;
        char[] temp;

	room = lineBuffer.length;

        for (;;) {
            c = in.read();
            if (c == -1 || c == LF) {
                // LF or CR LF ends a line
                break;
            }

            /*
             * throw away carriage returns and the end of file character.
             */
            if (c == CR || c == EOF) {
                continue;
            }

            if (--room < 0) {
                temp = new char[offset + 128];
                room = temp.length - offset - 1;
                System.arraycopy(lineBuffer, 0, temp, 0, offset);
                lineBuffer = temp;
            }

            lineBuffer[offset++] = (char) c;
	}

	if ((c == -1) && (offset <= 0)) {
	    return null;
	}

        return new String(lineBuffer, 0, offset);