Search in sources :

Example 1 with InvalidConstraintException

use of org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException in project jackrabbit by apache.

the class PathConstraint method create.

static PathConstraint create(String jcrPath, PathResolver resolver) throws InvalidConstraintException {
    try {
        // constraint format: absolute or relative path with optional
        // trailing wild card
        boolean deep = jcrPath.endsWith(JCR_WILDCARD);
        Path path;
        if (JCR_WILDCARD.equals(jcrPath)) {
            path = PATH_FACTORY.getRootPath();
        } else {
            if (deep) {
                // trim trailing wild card before building path
                jcrPath = jcrPath.substring(0, jcrPath.length() - JCR_WILDCARD.length());
            }
            path = resolver.getQPath(jcrPath);
        }
        StringBuffer definition = new StringBuffer(path.getString());
        if (deep) {
            definition.append(WILDCARD);
        }
        return new PathConstraint(definition.toString(), path, deep);
    } catch (NameException e) {
        String msg = "Invalid path expression specified as value constraint: " + jcrPath;
        log.debug(msg);
        throw new InvalidConstraintException(msg, e);
    } catch (NamespaceException e) {
        String msg = "Invalid path expression specified as value constraint: " + jcrPath;
        log.debug(msg);
        throw new InvalidConstraintException(msg, e);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) InvalidConstraintException(org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException) NamespaceException(javax.jcr.NamespaceException)

Example 2 with InvalidConstraintException

use of org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException in project jackrabbit by apache.

the class ValueConstraint method checkValueConstraints.

/**
 * Tests if the value constraints defined in the property definition
 * <code>pd</code> are satisfied by the the specified <code>values</code>.
 * <p>
 * Note that the <i>protected</i> flag is not checked. Also note that no
 * type conversions are attempted if the type of the given values does not
 * match the required type as specified in the given definition.
 *
 * @param pd property definition
 * @param values values to check
 * @throws ConstraintViolationException if the constraints are violated
 */
public static void checkValueConstraints(QPropertyDefinition pd, QValue[] values) throws ConstraintViolationException, RepositoryException {
    // check multi-value flag
    if (!pd.isMultiple() && values != null && values.length > 1) {
        throw new ConstraintViolationException("the property is not multi-valued");
    }
    QValueConstraint[] constraints = pd.getValueConstraints();
    if (constraints == null || constraints.length == 0) {
        // no constraints to check
        return;
    }
    if (values != null && values.length > 0) {
        // check value constraints on every value
        for (QValue value : values) {
            // constraints are OR-ed together
            boolean satisfied = false;
            ConstraintViolationException cve = null;
            for (int j = 0; j < constraints.length && !satisfied; j++) {
                try {
                    constraints[j].check(value);
                    satisfied = true;
                } catch (ConstraintViolationException e) {
                    cve = e;
                } catch (InvalidConstraintException e) {
                    cve = new ConstraintViolationException(e.getMessage(), e);
                }
            }
            if (!satisfied) {
                // re-throw last exception we encountered
                throw cve;
            }
        }
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) InvalidConstraintException(org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint)

Example 3 with InvalidConstraintException

use of org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException in project jackrabbit by apache.

the class NodeTypeReader method getPropDef.

/**
 * Returns the property definition specified by the current element.
 *
 * @return property definition
 * @throws InvalidNodeTypeDefException if the definition is invalid
 * @throws NameException               if the definition contains an
 *                                     illegal name
 * @throws NamespaceException if a namespace is not defined
 */
private QPropertyDefinitionBuilder getPropDef() throws InvalidNodeTypeDefException, NameException, NamespaceException {
    QPropertyDefinitionBuilder def = new QPropertyDefinitionBuilder();
    String name = walker.getAttribute(Constants.NAME_ATTRIBUTE);
    if (name.equals("*")) {
        def.setName(NameConstants.ANY_NAME);
    } else {
        def.setName(resolver.getQName(name));
    }
    // simple attributes
    def.setAutoCreated(Boolean.valueOf(walker.getAttribute(Constants.AUTOCREATED_ATTRIBUTE)));
    def.setMandatory(Boolean.valueOf(walker.getAttribute(Constants.MANDATORY_ATTRIBUTE)));
    def.setProtected(Boolean.valueOf(walker.getAttribute(Constants.PROTECTED_ATTRIBUTE)));
    def.setOnParentVersion(OnParentVersionAction.valueFromName(walker.getAttribute(Constants.ONPARENTVERSION_ATTRIBUTE)));
    def.setMultiple(Boolean.valueOf(walker.getAttribute(Constants.MULTIPLE_ATTRIBUTE)));
    def.setFullTextSearchable(Boolean.valueOf(walker.getAttribute(Constants.ISFULLTEXTSEARCHABLE_ATTRIBUTE)));
    def.setQueryOrderable(Boolean.valueOf(walker.getAttribute(Constants.ISQUERYORDERABLE_ATTRIBUTE)));
    String s = walker.getAttribute(Constants.AVAILABLEQUERYOPERATORS_ATTRIBUTE);
    if (s != null && s.length() > 0) {
        String[] ops = s.split(" ");
        List<String> queryOps = new ArrayList<String>();
        for (String op1 : ops) {
            String op = op1.trim();
            if (op.equals(Constants.EQ_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO);
            } else if (op.equals(Constants.NE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_NOT_EQUAL_TO);
            } else if (op.equals(Constants.LT_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN);
            } else if (op.equals(Constants.LE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO);
            } else if (op.equals(Constants.GT_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN);
            } else if (op.equals(Constants.GE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO);
            } else if (op.equals(Constants.LIKE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LIKE);
            } else {
                throw new InvalidNodeTypeDefException("'" + op + "' is not a valid query operator");
            }
        }
        def.setAvailableQueryOperators(queryOps.toArray(new String[queryOps.size()]));
    }
    def.setRequiredType(PropertyType.valueFromName(walker.getAttribute(Constants.REQUIREDTYPE_ATTRIBUTE)));
    // value constraints
    if (walker.enterElement(Constants.VALUECONSTRAINTS_ELEMENT)) {
        List<QValueConstraint> constraints = new ArrayList<QValueConstraint>();
        int type = def.getRequiredType();
        while (walker.iterateElements(Constants.VALUECONSTRAINT_ELEMENT)) {
            String constraint = walker.getContent();
            try {
                constraints.add(ValueConstraint.create(type, constraint.trim(), resolver));
            } catch (InvalidConstraintException e) {
                throw new InvalidNodeTypeDefException("Invalid value constraint " + constraint, e);
            }
        }
        def.setValueConstraints(constraints.toArray(new QValueConstraint[constraints.size()]));
        walker.leaveElement();
    }
    // default values
    if (walker.enterElement(Constants.DEFAULTVALUES_ELEMENT)) {
        List<InternalValue> values = new ArrayList<InternalValue>();
        int type = def.getRequiredType();
        if (type == PropertyType.UNDEFINED) {
            type = PropertyType.STRING;
        }
        while (walker.iterateElements(Constants.DEFAULTVALUE_ELEMENT)) {
            String value = walker.getContent();
            try {
                Value v = ValueHelper.convert(value, type, valueFactory);
                values.add((InternalValue) ValueFormat.getQValue(v, resolver, qValueFactory));
            } catch (RepositoryException e) {
                throw new InvalidNodeTypeDefException("Unable to create default value: " + value, e);
            }
        }
        def.setDefaultValues(values.toArray(new InternalValue[values.size()]));
        walker.leaveElement();
    }
    return def;
}
Also used : InvalidConstraintException(org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException) QPropertyDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) InternalValue(org.apache.jackrabbit.core.value.InternalValue) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value)

Aggregations

InvalidConstraintException (org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException)3 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)2 ArrayList (java.util.ArrayList)1 NamespaceException (javax.jcr.NamespaceException)1 RepositoryException (javax.jcr.RepositoryException)1 Value (javax.jcr.Value)1 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)1 InvalidNodeTypeDefException (org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException)1 InternalValue (org.apache.jackrabbit.core.value.InternalValue)1 Path (org.apache.jackrabbit.spi.Path)1 QValue (org.apache.jackrabbit.spi.QValue)1 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)1 QPropertyDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder)1 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)1