mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
03.20.2014 0648122458a3731325efa3c0625a95b445e9bf4a
StringPrepProfile.java:
Made code more readable by extracting method canMapToSpace().
In prepareUnicode(), extracted local variable 'b' earlier in the code.
Inlined map() method.
Code cleanup.

SchemaUtils.java:
Removed used of temporary LocalizableMessage variables + useless comments.

SchemaUtilsTest.java:
Added testNormalizeNumericString().
3 files modified
249 ■■■■■ changed files
opendj-core/src/main/java/com/forgerock/opendj/util/StringPrepProfile.java 84 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaUtils.java 148 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaUtilsTest.java 17 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/com/forgerock/opendj/util/StringPrepProfile.java
@@ -25,7 +25,6 @@
 */
package com.forgerock.opendj.util;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.HashMap;
@@ -44,15 +43,17 @@
     * A Table defining the mapped code-points as per RFC 3454.
     */
    private static final class MappingTable {
        // Set of chars which are deleted from the incoming value.
        private final static HashSet<Character> MAP_2_NULL = new HashSet<Character>();
        /** Set of chars which are deleted from the incoming value. */
        private static final HashSet<Character> MAP_2_NULL = new HashSet<Character>();
        // Set of chars which are replaced by a SPACE when found.
        private final static HashSet<Character> MAP_2_SPACE = new HashSet<Character>();
        /** Set of chars which are replaced by a SPACE when found. */
        private static final HashSet<Character> MAP_2_SPACE = new HashSet<Character>();
        // Table for case-folding. Map of Character and String containing
        // uppercase and lowercase value as the key-value pair.
        private final static HashMap<Character, String> CASE_MAP_TABLE =
        /**
         * Table for case-folding. Map of Character and String containing
         * uppercase and lowercase value as the key-value pair.
         */
        private static final HashMap<Character, String> CASE_MAP_TABLE =
                new HashMap<Character, String>();
        static {
@@ -88,8 +89,7 @@
            // Appendix B.2 RFC 3454.
            // Build an uppercase array and a lowercase array and create a map
            // of both
            // values.
            // of both values.
            final char[] upperCaseArr =
                    new char[] { '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046',
                        '\u0047', '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D',
@@ -369,7 +369,7 @@
            }
        }
        // Gets the mapped String.
        /** Checks each character and replaces it with its mapping. */
        private static void map(final StringBuilder buffer, final ByteSequence sequence,
                final boolean trim, final boolean foldCase) {
            final String value = sequence.toString();
@@ -380,17 +380,9 @@
                }
                if (MAP_2_SPACE.contains(c)) {
                    final int buffLen = buffer.length();
                    if (trim && buffLen == 0 || buffLen > 0
                            && buffer.charAt(buffLen - 1) == SPACE_CHAR) {
                        /**
                         * Do not map this character into a space if: a .
                         * trimming is wanted and this was the first char. b.
                         * The last character was a space.
                         **/
                        continue;
                    if (canMapToSpace(buffer, trim)) {
                        buffer.append(SPACE_CHAR);
                    }
                    buffer.append(SPACE_CHAR);
                    continue;
                }
@@ -458,46 +450,36 @@
        // common case.
        final int length = sequence.length();
        for (int i = 0; i < length; i++) {
            if ((sequence.byteAt(i) & 0x7F) != sequence.byteAt(i)) {
            final byte b = sequence.byteAt(i);
            if ((b & 0x7F) != b) {
                // Map the attribute value.
                map(buffer, sequence.subSequence(i, length), trim, foldCase);
                MappingTable.map(buffer, sequence.subSequence(i, length), trim, foldCase);
                // Normalize the attribute value.
                String normalizedForm = Normalizer.normalize(buffer, Form.NFKD);
                buffer.setLength(0);
                buffer.append(normalizedForm);
                break;
            }
            int buffLen = buffer.length();
            switch (sequence.byteAt(i)) {
            switch (b) {
            case ' ':
                if (trim && buffLen == 0 || buffLen > 0 && buffer.charAt(buffLen - 1) == SPACE_CHAR) {
                    break;
                if (canMapToSpace(buffer, trim)) {
                    buffer.append(' ');
                }
                buffer.append(' ');
                break;
            default:
                final byte b = sequence.byteAt(i);
                // Perform mapping.
                if (b >= '\u0009' && b < '\u000E') {
                    // These characters are mapped to a SPACE.
                    buffLen = buffer.length();
                    if (trim && buffLen == 0 || buffLen > 0 && buffer.charAt(buffLen - 1) == ' ') {
                        /**
                         * Do not map this character into a space if: a .
                         * trimming is desired and this was the leading char. b.
                         * The last character was a space.
                         **/
                        break;
                    } else {
                    if (canMapToSpace(buffer, trim)) {
                        buffer.append(SPACE_CHAR);
                    }
                } else if (b >= '\u0000' && b <= '\u0008' || b >= '\u000E' && b <= '\u001F'
                } else if ((b >= '\u0000' && b <= '\u0008') || (b >= '\u000E' && b <= '\u001F')
                        || b == '\u007F') {
                    // These characters are mapped to nothing and hence not
                    // copied
                    // over..
                    // copied over..
                    break;
                } else if (foldCase && b >= 65 && b <= 90) {
                } else if (foldCase && b >= 'A' && b <= 'Z') {
                    // If case-folding is allowed then map to the lower case.
                    buffer.append((char) (b + 32));
                } else {
@@ -518,13 +500,21 @@
        }
    }
    // Checks each character and replaces it with its mapping.
    private static void map(final StringBuilder buffer, final ByteSequence value,
            final boolean trim, final boolean foldCase) {
        MappingTable.map(buffer, value, trim, foldCase);
    /**
     * Do not map this character into a space if:
     * <ol>
     * <li>trimming is desired and this was the leading char.</li>
     * <li>The last character was a space.</li>
     * </ol>
     */
    private static boolean canMapToSpace(final StringBuilder buffer, final boolean trim) {
        final int buffLen = buffer.length();
        final boolean doNotMap = (trim && buffLen == 0)
                || (buffLen > 0 && buffer.charAt(buffLen - 1) == SPACE_CHAR);
        return !doNotMap;
    }
    // Prevent instantiation.
    /** Prevent instantiation. */
    private StringPrepProfile() {
        // Nothing to do.
    }
opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaUtils.java
@@ -39,7 +39,6 @@
import java.util.Map;
import java.util.Set;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.ByteSequence;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DecodeException;
@@ -154,10 +153,8 @@
                    values = Collections.unmodifiableList(values);
                }
            } else {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c), reader
                                .pos() - 1);
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
            }
            return values;
@@ -213,19 +210,13 @@
                        && !(c == '\'' && enclosingQuote)) {
                    if (c == '.') {
                        if (lastWasPeriod) {
                            final LocalizableMessage message =
                                    ERR_ATTR_SYNTAX_OID_CONSECUTIVE_PERIODS1.get(reader.pos() - 1);
                            throw DecodeException.error(message);
                        } else {
                            lastWasPeriod = true;
                            throw DecodeException.error(
                                    ERR_ATTR_SYNTAX_OID_CONSECUTIVE_PERIODS1.get(reader.pos() - 1));
                        }
                        lastWasPeriod = true;
                    } else if (!isDigit(c)) {
                        // This must be an illegal character.
                        // This must have been an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_OID_ILLEGAL_CHARACTER1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_OID_ILLEGAL_CHARACTER1.get(c, reader.pos() - 1));
                    } else {
                        lastWasPeriod = false;
                    }
@@ -234,9 +225,8 @@
                }
                if (lastWasPeriod) {
                    final LocalizableMessage message =
                            ERR_ATTR_SYNTAX_OID_ENDS_WITH_PERIOD1.get(reader.pos() - 1);
                    throw DecodeException.error(message);
                    throw DecodeException.error(
                            ERR_ATTR_SYNTAX_OID_ENDS_WITH_PERIOD1.get(reader.pos() - 1));
                }
            } else if (isAlpha(c)) {
                // This must be an attribute description. In this case, we will
@@ -245,35 +235,25 @@
                while (reader.remaining() > 0 && (c = reader.read()) != ' ' && c != ')'
                        && !(c == '\'' && enclosingQuote)) {
                    if (length == 0 && !isAlpha(c)) {
                        // This is an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                    }
                    if (!isKeyChar(c, allowCompatChars)) {
                        // This is an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                    }
                    length++;
                }
            } else {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c), reader
                                .pos() - 1);
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
            }
            if (enclosingQuote && c != '\'') {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, String
                                .valueOf(c));
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, c));
            }
        }
@@ -326,28 +306,24 @@
                while ((c = reader.read()) != ' ' && c != '{' && !(c == '\'' && enclosingQuote)) {
                    if (c == '.') {
                        if (lastWasPeriod) {
                            final LocalizableMessage message =
                                    ERR_ATTR_SYNTAX_OID_CONSECUTIVE_PERIODS1.get(reader.pos() - 1);
                            throw DecodeException.error(message);
                        } else {
                            lastWasPeriod = true;
                            throw DecodeException.error(
                                    ERR_ATTR_SYNTAX_OID_CONSECUTIVE_PERIODS1.get(reader.pos() - 1));
                        }
                        lastWasPeriod = true;
                    } else if (!isDigit(c)) {
                        // Technically, this must be an illegal character.
                        // However, it is possible that someone just got sloppy
                        // and did not
                        // include a space between the name/OID and a closing
                        // parenthesis. In that case, we'll assume it's the end
                        // of the value.
                        /*
                         * Technically, this must be an illegal character.
                         * However, it is possible that someone just got sloppy
                         * and did not include a space between the name/OID and
                         * a closing parenthesis.
                         * In that case, we'll assume it's the end of the value.
                         */
                        if (c == ')') {
                            break;
                        }
                        // This must have been an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_OID_ILLEGAL_CHARACTER1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_OID_ILLEGAL_CHARACTER1.get(c, reader.pos() - 1));
                    } else {
                        lastWasPeriod = false;
                    }
@@ -355,9 +331,8 @@
                }
                if (length == 0) {
                    final LocalizableMessage message =
                            ERR_ATTR_SYNTAX_OID_NO_VALUE1.get(reader.pos() - 1);
                    throw DecodeException.error(message);
                    throw DecodeException.error(
                            ERR_ATTR_SYNTAX_OID_NO_VALUE1.get(reader.pos() - 1));
                }
            } else if (isAlpha(c)) {
                // This must be an attribute description. In this case, we will
@@ -366,28 +341,20 @@
                while ((c = reader.read()) != ' ' && c != ')' && c != '{'
                        && !(c == '\'' && enclosingQuote)) {
                    if (length == 0 && !isAlpha(c)) {
                        // This is an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                    }
                    if (!isKeyChar(c, allowCompatChars)) {
                        // This is an illegal character.
                        final LocalizableMessage message =
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                    }
                    length++;
                }
            } else {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c), reader
                                .pos() - 1);
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
            }
            reader.reset();
@@ -403,10 +370,9 @@
                // the closing curly brace.
                while ((c = reader.read()) != '}') {
                    if (!isDigit(c)) {
                        final LocalizableMessage message =
                        throw DecodeException.error(
                                ERR_ATTR_SYNTAX_OID_ILLEGAL_CHARACTER1.get(reader.getString(),
                                        reader.pos() - 1);
                        throw DecodeException.error(message);
                                        reader.pos() - 1));
                    }
                }
            } else if (c == '\'') {
@@ -471,10 +437,8 @@
            // The next character must be a single quote.
            final char c = reader.read();
            if (c != '\'') {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, String
                                .valueOf(c));
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, c));
            }
            // Read until we find the closing quote.
@@ -518,9 +482,8 @@
            }
            if (length == 0) {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_RULE_ID_NO_VALUE1.get(reader.pos() - 1);
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_RULE_ID_NO_VALUE1.get(reader.pos() - 1));
            }
            reader.reset();
@@ -600,9 +563,8 @@
            if (token == null && reader.remaining() > 0) {
                reader.reset();
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_UNEXPECTED_CLOSE_PARENTHESIS1.get(length);
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_UNEXPECTED_CLOSE_PARENTHESIS1.get(length));
            }
            return token;
@@ -680,29 +642,21 @@
            // The next character must be a single quote.
            char c = reader.read();
            if (c != '\'') {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, String
                                .valueOf(c));
                throw DecodeException.error(message);
                throw DecodeException.error(
                        ERR_ATTR_SYNTAX_EXPECTED_QUOTE_AT_POS1.get(reader.pos() - 1, c));
            }
            // Read until we find the closing quote.
            reader.mark();
            while ((c = reader.read()) != '\'') {
                if (length == 0 && !isAlpha(c)) {
                    // This is an illegal character.
                    final LocalizableMessage message =
                            ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                    reader.pos() - 1);
                    throw DecodeException.error(message);
                    throw DecodeException.error(
                            ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                }
                if (!isKeyChar(c, allowCompatChars)) {
                    // This is an illegal character.
                    final LocalizableMessage message =
                            ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(String.valueOf(c),
                                    reader.pos() - 1);
                    throw DecodeException.error(message);
                    throw DecodeException.error(
                            ERR_ATTR_SYNTAX_ILLEGAL_CHAR_IN_STRING_OID1.get(c, reader.pos() - 1));
                }
                length++;
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaUtilsTest.java
@@ -244,4 +244,21 @@
            throws Exception {
        testNormalizeStringList(value, trim, foldCase, expected);
    }
    @DataProvider
    public Object[][] numericStringProvider() throws Exception {
        return new Object[][] {
            { "", "" },
            { "   ", "" },
            { " 123  ", "123" },
            { " 123  456  ", "123 456" },
        };
    }
    @Test(dataProvider = "numericStringProvider")
    public void testNormalizeNumericString(String value, String expected) throws Exception {
        ByteString val = ByteString.valueOf(value);
        ByteString normValue = SchemaUtils.normalizeNumericStringAttributeValue(val);
        Assertions.assertThat(normValue.toString()).isEqualTo(expected);
    }
}