nodetypeutil.java

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

JAVA
845
字号
                        continue;                    }                }                return propDef;            }        }        return null;    }    /**     * Returns the number of residual property definitions of <code>type</code>     * including its base types.     * @param type the node type     * @return the number of residual property definitions.     */    private static int getNumResidualPropDefs(NodeType type) {        PropertyDefinition[] pDefs = type.getPropertyDefinitions();        int residuals = 0;        for (int j = 0; j < pDefs.length; j++) {            PropertyDefinition pDef = pDefs[j];            if (pDef.getName().equals("*")) {                residuals++;            }        }        return residuals;    }    /**     * Locate a property def parsing all node types     *     * @param session     the session to access the node types     * @param isProtected if true, the returned <code>PropertyDef</code> is     *                    protected, else not     * @param mandatory   if true, the returned <code>PropertyDef</code> is     *                    mandatory, else not     * @return the first <code>PropertyDef</code> found fitting the     *         requirements     */    public static PropertyDefinition locatePropertyDef(Session session,                                                       boolean isProtected,                                                       boolean mandatory)            throws RepositoryException {        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();        NodeTypeIterator types = manager.getAllNodeTypes();        while (types.hasNext()) {            NodeType type = types.nextNodeType();            PropertyDefinition propDefs[] = type.getDeclaredPropertyDefinitions();            for (int i = 0; i < propDefs.length; i++) {                PropertyDefinition propDef = propDefs[i];                if (propDef.getName().equals("*")) {                    continue;                }                if (isProtected && !propDef.isProtected()) {                    continue;                }                if (!isProtected && propDef.isProtected()) {                    continue;                }                if (mandatory && !propDef.isMandatory()) {                    continue;                }                if (!mandatory && propDef.isMandatory()) {                    continue;                }                return propDef;            }        }        return null;    }    /**     * Returns a name that is not defined by the nodeType's child node def     */    public static String getUndefinedChildNodeName(NodeType nodeType) {        NodeDefinition nodeDefs[] = nodeType.getChildNodeDefinitions();        StringBuffer s = new StringBuffer("X");        for (int i = 0; i < nodeDefs.length; i++) {            s.append(nodeDefs[i].getName());        }        String undefinedName = s.toString();        undefinedName = undefinedName.replaceAll("\\*", "");        undefinedName = undefinedName.replaceAll(":", "");        return undefinedName;    }    /**     * Returns a node type that is nor legalType nor a sub type of of     */    public static String getIllegalChildNodeType(NodeTypeManager manager,                                                 String legalType)            throws RepositoryException {        NodeTypeIterator types = manager.getAllNodeTypes();        while (types.hasNext()) {            NodeType type = types.nextNodeType();            if (!type.getName().equals(legalType)) {                NodeType superTypes[] = type.getSupertypes();                boolean isSubType = false;                for (int i = 0; i < superTypes.length; i++) {                    String name = superTypes[i].getName();                    if (name.equals(legalType)) {                        isSubType = true;                        break;                    }                }                if (!isSubType) {                    return type.getName();                }            }        }        return null;    }    /**     * Returns any value of the requested type     */    public static Value getValueOfType(Session session, int type)            throws ValueFormatException, UnsupportedOperationException, RepositoryException {        switch (type) {            case (PropertyType.BINARY):                // note: If binary is not UTF-8 behavior is implementation-specific                return session.getValueFactory().createValue("abc", PropertyType.BINARY);            case (PropertyType.BOOLEAN):                return session.getValueFactory().createValue(true);            case (PropertyType.DATE):                return session.getValueFactory().createValue(Calendar.getInstance());            case (PropertyType.DOUBLE):                return session.getValueFactory().createValue(1.0);            case (PropertyType.LONG):                return session.getValueFactory().createValue(1);            case (PropertyType.NAME):                return session.getValueFactory().createValue("abc", PropertyType.NAME);            case (PropertyType.PATH):                return session.getValueFactory().createValue("/abc", PropertyType.PATH);            default:                // STRING and UNDEFINED                // note: REFERENCE is not testable since its format is implementation-specific                return session.getValueFactory().createValue("abc");        }    }    /**     * Returns a value according to the value contraints of a     * <code>PropertyDefinition</code>     *     * @param propDef   The <code>PropertyDefinition</code> whose constraints     *                  will be regarded     * @param satisfied If true, the returned <code>Value</code> will satisfying     *                  the constraints - If false, the returned     *                  <code>Value</code> will not satisfying the constraints.     * @return Depending on param <code>satisfied</code> a <code>Value</code>     *         satisfying or not satistying the constraints of     *         <code>propDef</code> will be returned. Null will be returned if     *         no accordant <code>Value</code> could be build.     */    public static Value getValueAccordingToValueConstraints(Session session,                                                            PropertyDefinition propDef,                                                            boolean satisfied)            throws ValueFormatException, RepositoryException {        int type = propDef.getRequiredType();        String constraints[] = propDef.getValueConstraints();        if (constraints == null || constraints.length == 0) {            return null;        }        switch (type) {            case (PropertyType.BINARY):                {                    long absMin = 0;                    long absMax = 0;                    // indicate if absMin and absMax are already set                    boolean absMinSet = false;                    boolean absMaxSet = false;                    // boundless vars indicate min/max without bounds,                    // if constraint is e.g.(min,) or [,max]                    boolean maxBoundless = false;                    boolean minBoundless = false;                    // find smallest min and largest max value                    for (int i = 0; i < constraints.length; i++) {                        if (!minBoundless) {                            String minStr = getConstraintMin(constraints[i]);                            if (minStr == null) {                                minBoundless = true;                            } else {                                long min = Long.valueOf(minStr).longValue();                                if (!absMinSet) {                                    absMin = min;                                    absMinSet = true;                                } else if (min < absMin) {                                    absMin = min;                                }                            }                        }                        if (!maxBoundless) {                            String maxStr = getConstraintMax(constraints[i]);                            if (maxStr == null) {                                maxBoundless = true;                            } else {                                long max = Long.valueOf(maxStr).longValue();                                if (!absMaxSet) {                                    absMax = max;                                    absMaxSet = true;                                } else if (max > absMax) {                                    absMin = max;                                }                            }                        }                    }                    if (satisfied) {                        // build a binary value absMin < size > absMax                        StringBuffer content = new StringBuffer();                        for (int i = 0; i <= absMin + 1; i++) {                            content.append("X");                        }                        if (!maxBoundless && content.length() >= absMax) {                            return null;                        } else {                            return session.getValueFactory().createValue(content.toString(), PropertyType.BINARY);                        }                    } else {                        if (!minBoundless && absMin > 1) {                            // return a value of size < absMin                            return session.getValueFactory().createValue("0", PropertyType.BINARY);                        } else if (!maxBoundless) {                            // build a binary value of size > absMax                            StringBuffer content = new StringBuffer();                            for (int i = 0; i <= absMax; i = i + 10) {                                content.append("0123456789");                            }                            return session.getValueFactory().createValue(content.toString(), PropertyType.BINARY);                        } else {                            return null;                        }                    }                }            case (PropertyType.BOOLEAN):                {                    if (constraints.length > 1) {                        return null; // silly constraint                    }                    boolean value = Boolean.valueOf(constraints[0]).booleanValue();                    if (satisfied) {                        return session.getValueFactory().createValue(value);                    } else {                        return session.getValueFactory().createValue(!value);                    }                }            case (PropertyType.DATE):                {                    Calendar absMin = null;                    Calendar absMax = null;                    // boundless vars indicate min/max without bounds,                    // if constraint is e.g.(min,) or [,max]                    boolean maxBoundless = false;                    boolean minBoundless = false;                    // find smallest min and largest max value                    for (int i = 0; i < constraints.length; i++) {                        if (!minBoundless) {                            String minStr = getConstraintMin(constraints[i]);                            if (minStr == null) {                                minBoundless = true;                            } else {                                Calendar min = ISO8601.parse(minStr);                                if (absMin == null || min.before(absMin)) {

⌨️ 快捷键说明

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