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);
}
}
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;
}
}
}
}
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;
}
Aggregations