2111 lines · 81.6 KB · Java
RawDownload
        keys = new String[10];        _kp_array = new Property[10];        _property_list_pattern = false;        _property_value_pattern = false;         while (index < len) {            c = name_chars[index];             // case of pattern properties            if (c == '*') {                if (_property_list_pattern)                    throw new MalformedObjectNameException(                              "Cannot have several '*' characters in pattern " +                              "property list");                else {                    _property_list_pattern = true;                    if ((++index < len ) && (name_chars[index] != ','))                        throw new MalformedObjectNameException(                                  "Invalid character found after '*': end of " +                                  "name or ',' expected");                    else if (index == len) {                        if (property_index == 0) {                            // empty properties case                            _kp_array = _Empty_property_array;                            _ca_array = _Empty_property_array;                            _propertyList = Collections.emptyMap();                        }                        break;                    } else {                        // correct pattern spec in props, continue                        index++;                        continue;                    }                }            }             // standard property case, key part            in_index = index;            key_index = in_index;            if (name_chars[in_index] == '=')                throw new MalformedObjectNameException("Invalid key (empty)");            while ((in_index < len) && ((c1 = name_chars[in_index++]) != '='))                switch (c1) {                    // '=' considered to introduce value part                    case  '*' :                    case  '?' :                    case  ',' :                    case  ':' :                    case  '\n' :                        final String ichar = ((c1=='\n')?"\\n":""+c1);                        throw new MalformedObjectNameException(                                  "Invalid character '" + ichar +                                  "' in key part of property");                }            if (name_chars[in_index - 1] != '=')                throw new MalformedObjectNameException(                                             "Unterminated key property part");            value_index = in_index; // in_index pointing after '=' char            key_length = value_index - key_index - 1; // found end of key             // standard property case, value part            boolean value_pattern = false;            if (in_index < len && name_chars[in_index] == '\"') {                quoted_value = true;                // the case of quoted value part            quoted_value_parsing:                while ((++in_index < len) &&                       ((c1 = name_chars[in_index]) != '\"')) {                    // the case of an escaped character                    if (c1 == '\\') {                        if (++in_index == len)                            throw new MalformedObjectNameException(                                               "Unterminated quoted value");                        switch (c1 = name_chars[in_index]) {                            case '\\' :                            case '\"' :                            case '?' :                            case '*' :                            case 'n' :                                break; // valid character                            default :                                throw new MalformedObjectNameException(                                          "Invalid escape sequence '\\" +                                          c1 + "' in quoted value");                        }                    } else if (c1 == '\n') {                        throw new MalformedObjectNameException(                                                     "Newline in quoted value");                    } else {                        switch (c1) {                            case '?' :                            case '*' :                                value_pattern = true;                                break;                        }                    }                }                if (in_index == len)                    throw new MalformedObjectNameException(                                                 "Unterminated quoted value");                else value_length = ++in_index - value_index;            } else {                // the case of standard value part                quoted_value = false;                while ((in_index < len) && ((c1 = name_chars[in_index]) != ','))                switch (c1) {                    // ',' considered to be the value separator                    case '*' :                    case '?' :                        value_pattern = true;                        in_index++;                        break;                    case '=' :                    case ':' :                    case '"' :                    case '\n' :                        final String ichar = ((c1=='\n')?"\\n":""+c1);                        throw new MalformedObjectNameException(                                                 "Invalid character '" + ichar +                                                 "' in value part of property");                    default :                        in_index++;                        break;                }                value_length = in_index - value_index;            }             // Parsed property, checks the end of name            if (in_index == len - 1) {                if (quoted_value)                    throw new MalformedObjectNameException(                                             "Invalid ending character `" +                                             name_chars[in_index] + "'");                else throw new MalformedObjectNameException(                                                  "Invalid ending comma");            } else in_index++;             // we got the key and value part, prepare a property for this            if (!value_pattern) {                prop = new Property(key_index, key_length, value_length);            } else {                _property_value_pattern = true;                prop = new PatternProperty(key_index, key_length, value_length);            }            key_name = name.substring(key_index, key_index + key_length);             if (property_index == keys.length) {                String[] tmp_string_array = new String[property_index + 10];                System.arraycopy(keys, 0, tmp_string_array, 0, property_index);                keys = tmp_string_array;            }            keys[property_index] = key_name;             addProperty(prop, property_index, keys_map, key_name);            property_index++;            index = in_index;        }         // computes and set canonical name        setCanonicalName(name_chars, canonical_chars, keys,                         keys_map, cname_index, property_index);    }     /**     * Construct an ObjectName from a domain and a Hashtable.     *     * @param domain Domain of the ObjectName.     * @param props  Map containing couples <i>key</i> -> <i>value</i>.     *     * @exception MalformedObjectNameException The <code>domain</code>     * contains an illegal character, or one of the keys or values in     * <code>table</code> contains an illegal character, or one of the     * values in <code>table</code> does not follow the rules for quoting.     * @exception NullPointerException One of the parameters is null.     */    private void construct(String domain, Map<String,String> props)        throws MalformedObjectNameException {         // The domain cannot be null        if (domain == null)            throw new NullPointerException("domain cannot be null");         // The key property list cannot be null        if (props == null)            throw new NullPointerException("key property list cannot be null");         // The key property list cannot be empty        if (props.isEmpty())            throw new MalformedObjectNameException(                                         "key property list cannot be empty");         // checks domain validity        if (!isDomain(domain))            throw new MalformedObjectNameException("Invalid domain: " + domain);         // init canonicalname        final StringBuilder sb = new StringBuilder();        sb.append(domain).append(':');        _domain_length = domain.length();         // allocates the property array        int nb_props = props.size();        _kp_array = new Property[nb_props];         String[] keys = new String[nb_props];        final Map<String,Property> keys_map = new HashMap<String,Property>();        Property prop;        int key_index;        int i = 0;        for (Map.Entry<String,String> entry : props.entrySet()) {            if (sb.length() > 0)                sb.append(",");            String key = entry.getKey();            String value;            try {                value = entry.getValue();            } catch (ClassCastException e) {                throw new MalformedObjectNameException(e.getMessage());            }            key_index = sb.length();            checkKey(key);            sb.append(key);            keys[i] = key;            sb.append("=");            boolean value_pattern = checkValue(value);            sb.append(value);            if (!value_pattern) {                prop = new Property(key_index,                                    key.length(),                                    value.length());            } else {                _property_value_pattern = true;                prop = new PatternProperty(key_index,                                           key.length(),                                           value.length());            }            addProperty(prop, i, keys_map, key);            i++;        }         // initialize canonical name and data structure        int len = sb.length();        char[] initial_chars = new char[len];        sb.getChars(0, len, initial_chars, 0);        char[] canonical_chars = new char[len];        System.arraycopy(initial_chars, 0, canonical_chars, 0,                         _domain_length + 1);        setCanonicalName(initial_chars, canonical_chars, keys, keys_map,                         _domain_length + 1, _kp_array.length);    }    // Category : Instance construction <==============================     // Category : Internal utilities ------------------------------>     /**     * Add passed property to the list at the given index     * for the passed key name     */    private void addProperty(Property prop, int index,                             Map<String,Property> keys_map, String key_name)        throws MalformedObjectNameException {         if (keys_map.containsKey(key_name)) throw new                MalformedObjectNameException("key `" +                                         key_name +"' already defined");         // if no more space for property arrays, have to increase it        if (index == _kp_array.length) {            Property[] tmp_prop_array = new Property[index + 10];            System.arraycopy(_kp_array, 0, tmp_prop_array, 0, index);            _kp_array = tmp_prop_array;        }        _kp_array[index] = prop;        keys_map.put(key_name, prop);    }     /**     * Sets the canonical name of receiver from input 'specified_chars'     * array, by filling 'canonical_chars' array with found 'nb-props'     * properties starting at position 'prop_index'.     */    private void setCanonicalName(char[] specified_chars,                                  char[] canonical_chars,                                  String[] keys, Map<String,Property> keys_map,                                  int prop_index, int nb_props) {         // Sort the list of found properties        if (_kp_array != _Empty_property_array) {            String[] tmp_keys = new String[nb_props];            Property[] tmp_props = new Property[nb_props];             System.arraycopy(keys, 0, tmp_keys, 0, nb_props);            Arrays.sort(tmp_keys);            keys = tmp_keys;            System.arraycopy(_kp_array, 0, tmp_props, 0 , nb_props);            _kp_array = tmp_props;            _ca_array = new Property[nb_props];             // now assigns _ca_array to the sorted list of keys            // (there cannot be two identical keys in an objectname.            for (int i = 0; i < nb_props; i++)                _ca_array[i] = keys_map.get(keys[i]);             // now we build the canonical name and set begin indexes of            // properties to reflect canonical form            int last_index = nb_props - 1;            int prop_len;            Property prop;            for (int i = 0; i <= last_index; i++) {                prop = _ca_array[i];                // length of prop including '=' char                prop_len = prop._key_length + prop._value_length + 1;                System.arraycopy(specified_chars, prop._key_index,                                 canonical_chars, prop_index, prop_len);                prop.setKeyIndex(prop_index);                prop_index += prop_len;                if (i != last_index) {                    canonical_chars[prop_index] = ',';                    prop_index++;                }            }        }         // terminate canonicalname with '*' in case of pattern        if (_property_list_pattern) {            if (_kp_array != _Empty_property_array)                canonical_chars[prop_index++] = ',';            canonical_chars[prop_index++] = '*';        }         // we now build the canonicalname string        _canonicalName = (new String(canonical_chars, 0, prop_index)).intern();    }     /**     * Parse a key.     * <pre>final int endKey=parseKey(s,startKey);</pre>     * <p>key starts at startKey (included), and ends at endKey (excluded).     * If (startKey == endKey), then the key is empty.     *     * @param s The char array of the original string.     * @param startKey index at which to begin parsing.     * @return The index following the last character of the key.     **/    private static int parseKey(final char[] s, final int startKey)        throws MalformedObjectNameException {        int next   = startKey;        int endKey = startKey;        final int len = s.length;        while (next < len) {            final char k = s[next++];            switch (k) {            case '*':            case '?':            case ',':            case ':':            case '\n':                final String ichar = ((k=='\n')?"\\n":""+k);                throw new                    MalformedObjectNameException("Invalid character in key: `"                                                 + ichar + "'");            case '=':                // we got the key.                endKey = next-1;                break;            default:                if (next < len) continue;                else endKey=next;            }            break;        }        return endKey;    }     /**     * Parse a value.     * <pre>final int endVal=parseValue(s,startVal);</pre>     * <p>value starts at startVal (included), and ends at endVal (excluded).     * If (startVal == endVal), then the key is empty.     *     * @param s The char array of the original string.     * @param startValue index at which to begin parsing.     * @return The first element of the int array indicates the index     *         following the last character of the value. The second     *         element of the int array indicates that the value is     *         a pattern when its value equals 1.     **/    private static int[] parseValue(final char[] s, final int startValue)        throws MalformedObjectNameException {         boolean value_pattern = false;         int next   = startValue;        int endValue = startValue;         final int len = s.length;        final char q=s[startValue];         if (q == '"') {            // quoted value            if (++next == len) throw new                MalformedObjectNameException("Invalid quote");            while (next < len) {                char last = s[next];                if (last == '\\') {                    if (++next == len) throw new                        MalformedObjectNameException(                           "Invalid unterminated quoted character sequence");                    last = s[next];                    switch (last) {                        case '\\' :                        case '?' :                        case '*' :                        case 'n' :                            break;                        case '\"' :                            // We have an escaped quote. If this escaped                            // quote is the last character, it does not                            // qualify as a valid termination quote.                            //                            if (next+1 == len) throw new                                MalformedObjectNameException(                                                 "Missing termination quote");                            break;                        default:                            throw new                                MalformedObjectNameException(                                "Invalid quoted character sequence '\\" +                                last + "'");                    }                } else if (last == '\n') {                    throw new MalformedObjectNameException(                                                 "Newline in quoted value");                } else if (last == '\"') {                    next++;                    break;                } else {                    switch (last) {                        case '?' :                        case '*' :                            value_pattern = true;                            break;                    }                }                next++;                 // Check that last character is a termination quote.                // We have already handled the case were the last                // character is an escaped quote earlier.                //                if ((next >= len) && (last != '\"')) throw new                    MalformedObjectNameException("Missing termination quote");            }            endValue = next;            if (next < len) {                if (s[next++] != ',') throw new                    MalformedObjectNameException("Invalid quote");            }        } else {            // Non quoted value.            while (next < len) {                final char v=s[next++];                switch(v) {                    case '*':                    case '?':                        value_pattern = true;                        if (next < len) continue;                        else endValue=next;                        break;                    case '=':                    case ':':                    case '\n' :                        final String ichar = ((v=='\n')?"\\n":""+v);                        throw new                            MalformedObjectNameException("Invalid character `" +                                                         ichar + "' in value");                    case ',':                        endValue = next-1;                        break;                    default:                        if (next < len) continue;                        else endValue=next;                }                break;            }        }        return new int[] { endValue, value_pattern ? 1 : 0 };    }     /**     * Check if the supplied value is a valid value.     *     * @return true if the value is a pattern, otherwise false.     */    private static boolean checkValue(String val)        throws MalformedObjectNameException {         if (val == null) throw new            NullPointerException("Invalid value (null)"); 

Lines 501–1000 of 2111. Previous lines