compactnodetypedefreader.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 692 行 · 第 1/2 页

JAVA
692
字号
                ndi.setDefaultPrimaryType(null);                ndi.setRequiredPrimaryTypes(new QName[]{QName.NT_BASE});                nextToken();                doChildNodeDefinition(ndi, ntd);                nodeDefinitions.add(ndi);            }        }        if (propertyDefinitions.size() > 0) {            ntd.setPropertyDefs((PropDef[]) propertyDefinitions.toArray(new PropDef[0]));        }        if (nodeDefinitions.size() > 0) {            ntd.setChildNodeDefs((NodeDef[]) nodeDefinitions.toArray(new NodeDef[0]));        }    }    /**     * processes the property definition     *     * @param pdi     * @param ntd     * @throws ParseException     */    private void doPropertyDefinition(PropDefImpl pdi, NodeTypeDef ntd)            throws ParseException {        if (currentToken.equals("*")) {            pdi.setName(ItemDef.ANY_NAME);        } else {            pdi.setName(toQName(currentToken));        }        nextToken();        doPropertyType(pdi);        doPropertyDefaultValue(pdi);        doPropertyAttributes(pdi, ntd);        doPropertyValueConstraints(pdi);    }    /**     * processes the property type     *     * @param pdi     * @throws ParseException     */    private void doPropertyType(PropDefImpl pdi) throws ParseException {        if (!currentTokenEquals(Lexer.BEGIN_TYPE)) {            return;        }        nextToken();        if (currentTokenEquals(Lexer.STRING)) {            pdi.setRequiredType(PropertyType.STRING);        } else if (currentTokenEquals(Lexer.BINARY)) {            pdi.setRequiredType(PropertyType.BINARY);        } else if (currentTokenEquals(Lexer.LONG)) {            pdi.setRequiredType(PropertyType.LONG);        } else if (currentTokenEquals(Lexer.DOUBLE)) {            pdi.setRequiredType(PropertyType.DOUBLE);        } else if (currentTokenEquals(Lexer.BOOLEAN)) {            pdi.setRequiredType(PropertyType.BOOLEAN);        } else if (currentTokenEquals(Lexer.DATE)) {            pdi.setRequiredType(PropertyType.DATE);        } else if (currentTokenEquals(Lexer.NAME)) {            pdi.setRequiredType(PropertyType.NAME);        } else if (currentTokenEquals(Lexer.PATH)) {            pdi.setRequiredType(PropertyType.PATH);        } else if (currentTokenEquals(Lexer.REFERENCE)) {            pdi.setRequiredType(PropertyType.REFERENCE);        } else if (currentTokenEquals(Lexer.UNDEFINED)) {            pdi.setRequiredType(PropertyType.UNDEFINED);        } else {            lexer.fail("Unkown property type '" + currentToken + "' specified");        }        nextToken();        if (!currentTokenEquals(Lexer.END_TYPE)) {            lexer.fail("Missing '" + Lexer.END_TYPE + "' delimiter for end of property type");        }        nextToken();    }    /**     * processes the property attributes     *     * @param pdi     * @param ntd     * @throws ParseException     */    private void doPropertyAttributes(PropDefImpl pdi, NodeTypeDef ntd) throws ParseException {        while (currentTokenEquals(Lexer.ATTRIBUTE)) {            if (currentTokenEquals(Lexer.PRIMARY)) {                if (ntd.getPrimaryItemName() != null) {                    String name = null;                    try {                        name = NameFormat.format(ntd.getName(), nsMapping);                    } catch (NoPrefixDeclaredException e) {                        // Should never happen, checked earlier                    }                    lexer.fail("More than one primary item specified in node type '" + name + "'");                }                ntd.setPrimaryItemName(pdi.getName());            } else if (currentTokenEquals(Lexer.AUTOCREATED)) {                pdi.setAutoCreated(true);            } else if (currentTokenEquals(Lexer.MANDATORY)) {                pdi.setMandatory(true);            } else if (currentTokenEquals(Lexer.PROTECTED)) {                pdi.setProtected(true);            } else if (currentTokenEquals(Lexer.MULTIPLE)) {                pdi.setMultiple(true);            } else if (currentTokenEquals(Lexer.COPY)) {                pdi.setOnParentVersion(OnParentVersionAction.COPY);            } else if (currentTokenEquals(Lexer.VERSION)) {                pdi.setOnParentVersion(OnParentVersionAction.VERSION);            } else if (currentTokenEquals(Lexer.INITIALIZE)) {                pdi.setOnParentVersion(OnParentVersionAction.INITIALIZE);            } else if (currentTokenEquals(Lexer.COMPUTE)) {                pdi.setOnParentVersion(OnParentVersionAction.COMPUTE);            } else if (currentTokenEquals(Lexer.IGNORE)) {                pdi.setOnParentVersion(OnParentVersionAction.IGNORE);            } else if (currentTokenEquals(Lexer.ABORT)) {                pdi.setOnParentVersion(OnParentVersionAction.ABORT);            }            nextToken();        }    }    /**     * processes the property default values     *     * @param pdi     * @throws ParseException     */    private void doPropertyDefaultValue(PropDefImpl pdi) throws ParseException {        if (!currentTokenEquals(Lexer.DEFAULT)) {            return;        }        List defaultValues = new ArrayList();        do {            nextToken();            InternalValue value = null;            try {                value = InternalValue.create(ValueHelper.convert(                        currentToken, pdi.getRequiredType(),                        ValueFactoryImpl.getInstance()), nsMapping);            } catch (ValueFormatException e) {                lexer.fail("'" + currentToken + "' is not a valid string representation of a value of type " + pdi.getRequiredType());            } catch (RepositoryException e) {                lexer.fail("An error occured during value conversion of '" + currentToken + "'");            }            defaultValues.add(value);            nextToken();        } while (currentTokenEquals(Lexer.LIST_DELIMITER));        pdi.setDefaultValues((InternalValue[]) defaultValues.toArray(new InternalValue[0]));    }    /**     * processes the property value constraints     *     * @param pdi     * @throws ParseException     */    private void doPropertyValueConstraints(PropDefImpl pdi) throws ParseException {        if (!currentTokenEquals(Lexer.CONSTRAINT)) {            return;        }        List constraints = new ArrayList();        do {            nextToken();            ValueConstraint constraint = null;            try {                constraint = ValueConstraint.create(pdi.getRequiredType(), currentToken, nsMapping);            } catch (InvalidConstraintException e) {                lexer.fail("'" + currentToken + "' is not a valid constraint expression for a value of type " + pdi.getRequiredType());            }            constraints.add(constraint);            nextToken();        } while (currentTokenEquals(Lexer.LIST_DELIMITER));        pdi.setValueConstraints((ValueConstraint[]) constraints.toArray(new ValueConstraint[0]));    }    /**     * processes the childnode definition     *     * @param ndi     * @param ntd     * @throws ParseException     */    private void doChildNodeDefinition(NodeDefImpl ndi, NodeTypeDef ntd)            throws ParseException {        if (currentTokenEquals('*')) {            ndi.setName(ItemDef.ANY_NAME);        } else {            ndi.setName(toQName(currentToken));        }        nextToken();        doChildNodeRequiredTypes(ndi);        doChildNodeDefaultType(ndi);        doChildNodeAttributes(ndi, ntd);    }    /**     * processes the childnode required types     *     * @param ndi     * @throws ParseException     */    private void doChildNodeRequiredTypes(NodeDefImpl ndi) throws ParseException {        if (!currentTokenEquals(Lexer.BEGIN_TYPE)) {            return;        }        List types = new ArrayList();        do {            nextToken();            types.add(toQName(currentToken));            nextToken();        } while (currentTokenEquals(Lexer.LIST_DELIMITER));        ndi.setRequiredPrimaryTypes((QName[]) types.toArray(new QName[0]));        nextToken();    }    /**     * processes the childnode default types     *     * @param ndi     * @throws ParseException     */    private void doChildNodeDefaultType(NodeDefImpl ndi) throws ParseException {        if (!currentTokenEquals(Lexer.DEFAULT)) {            return;        }        nextToken();        ndi.setDefaultPrimaryType(toQName(currentToken));        nextToken();    }    /**     * processes the childnode attributes     *     * @param ndi     * @param ntd     * @throws ParseException     */    private void doChildNodeAttributes(NodeDefImpl ndi, NodeTypeDef ntd) throws ParseException {        while (currentTokenEquals(Lexer.ATTRIBUTE)) {            if (currentTokenEquals(Lexer.PRIMARY)) {                if (ntd.getPrimaryItemName() != null) {                    String name = null;                    try {                        name = NameFormat.format(ntd.getName(), nsMapping);                    } catch (NoPrefixDeclaredException e) {                        // Should never happen, checked earlier                    }                    lexer.fail("More than one primary item specified in node type '" + name + "'");                }                ntd.setPrimaryItemName(ndi.getName());            } else if (currentTokenEquals(Lexer.AUTOCREATED)) {                ndi.setAutoCreated(true);            } else if (currentTokenEquals(Lexer.MANDATORY)) {                ndi.setMandatory(true);            } else if (currentTokenEquals(Lexer.PROTECTED)) {                ndi.setProtected(true);            } else if (currentTokenEquals(Lexer.MULTIPLE)) {                ndi.setAllowsSameNameSiblings(true);            } else if (currentTokenEquals(Lexer.COPY)) {                ndi.setOnParentVersion(OnParentVersionAction.COPY);            } else if (currentTokenEquals(Lexer.VERSION)) {                ndi.setOnParentVersion(OnParentVersionAction.VERSION);            } else if (currentTokenEquals(Lexer.INITIALIZE)) {                ndi.setOnParentVersion(OnParentVersionAction.INITIALIZE);            } else if (currentTokenEquals(Lexer.COMPUTE)) {                ndi.setOnParentVersion(OnParentVersionAction.COMPUTE);            } else if (currentTokenEquals(Lexer.IGNORE)) {                ndi.setOnParentVersion(OnParentVersionAction.IGNORE);            } else if (currentTokenEquals(Lexer.ABORT)) {                ndi.setOnParentVersion(OnParentVersionAction.ABORT);            }            nextToken();        }    }    /**     * Converts the given string into a qualified name using the current     * namespace mapping.     *     * @param stringName     * @return the qualified name     * @throws ParseException if the conversion fails     */    private QName toQName(String stringName) throws ParseException {        try {            return ISO9075.decode(NameFormat.parse(stringName, nsMapping));        } catch (NameException e) {            lexer.fail("Error while parsing '" + stringName + "'", e);            return null;        }    }    /**     * Gets the next token from the underlying lexer.     *     * @see Lexer#getNextToken()     * @throws ParseException if the lexer fails to get the next token.     */    private void nextToken() throws ParseException {        currentToken = lexer.getNextToken();    }    /**     * Checks if the {@link #currentToken} is semantically equal to the given     * argument.     *     * @param s the tokens to compare with     * @return <code>true</code> if equals; <code>false</code> otherwise.     */    private boolean currentTokenEquals(String[] s) {        for (int i = 0; i < s.length; i++) {            if (currentToken.equals(s[i])) {                return true;            }        }        return false;    }    /**     * Checks if the {@link #currentToken} is semantically equal to the given     * argument.     *     * @param c the tokens to compare with     * @return <code>true</code> if equals; <code>false</code> otherwise.     */    private boolean currentTokenEquals(char c) {        return currentToken.length() == 1 && currentToken.charAt(0) == c;    }    /**     * Checks if the {@link #currentToken} is semantically equal to the given     * argument.     *     * @param s the tokens to compare with     * @return <code>true</code> if equals; <code>false</code> otherwise.     */    private boolean currentTokenEquals(String s) {        return currentToken.equals(s);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?