use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class NodeTypeRegistry method validateNodeTypeDef.
/**
* Validates the specified <code>NodeTypeDef</code> within the context of
* the two other given collections and returns an <code>EffectiveNodeType</code>.
*
* @param ntd node type definition
* @param entCache effective node type cache
* @param ntdCache cache of 'known' node type definitions, used to resolve dependencies
* @param nsReg namespace registry used for validatingatch names
* @param lenient flag governing whether validation can be lenient or has to be strict
* @return an effective node type representation of the specified <code>QNodeTypeDefinition</code>
* @throws InvalidNodeTypeDefException if the node type is not valid
* @throws RepositoryException if another error occurs
*/
private static EffectiveNodeType validateNodeTypeDef(QNodeTypeDefinition ntd, EffectiveNodeTypeCache entCache, Map<Name, QNodeTypeDefinition> ntdCache, NamespaceRegistry nsReg, boolean lenient) throws InvalidNodeTypeDefException, RepositoryException {
/**
* the effective (i.e. merged and resolved) node type resulting from
* the specified node type definition;
* the effective node type will finally be created after the definition
* has been verified and checked for conflicts etc.; in some cases it
* will be created already at an earlier stage during the validation
* of child node definitions
*/
EffectiveNodeType ent = null;
Name name = ntd.getName();
if (name == null) {
String msg = "no name specified";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
checkNamespace(name, nsReg);
// validate supertypes
Name[] supertypes = ntd.getSupertypes();
if (supertypes.length > 0) {
for (Name supertype : supertypes) {
checkNamespace(supertype, nsReg);
/**
* simple check for infinite recursion
* (won't trap recursion on a deeper inheritance level)
*/
if (name.equals(supertype)) {
String msg = "[" + name + "] invalid supertype: " + supertype + " (infinite recursion))";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
if (!ntdCache.containsKey(supertype)) {
String msg = "[" + name + "] invalid supertype: " + supertype;
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
}
/**
* check for circularity in inheritance chain
* ('a' extends 'b' extends 'a')
*/
Stack<Name> inheritanceChain = new Stack<Name>();
inheritanceChain.push(name);
checkForCircularInheritance(supertypes, inheritanceChain, ntdCache);
}
/**
* build effective (i.e. merged and resolved) node type from supertypes
* and check for conflicts
*/
if (supertypes.length > 0) {
try {
EffectiveNodeType est = getEffectiveNodeType(supertypes, entCache, ntdCache);
// a supertypes's primaryItem -> illegal (JCR-1947)
if (ntd.getPrimaryItemName() != null && est.getPrimaryItemName() != null) {
String msg = "[" + name + "] primaryItemName is already specified by a supertype and must therefore not be overridden.";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
} catch (NodeTypeConflictException ntce) {
String msg = "[" + name + "] failed to validate supertypes";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, ntce);
} catch (NoSuchNodeTypeException nsnte) {
String msg = "[" + name + "] failed to validate supertypes";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, nsnte);
}
}
checkNamespace(ntd.getPrimaryItemName(), nsReg);
// validate property definitions
QPropertyDefinition[] pda = ntd.getPropertyDefs();
for (QPropertyDefinition pd : pda) {
/**
* sanity check:
* make sure declaring node type matches name of node type definition
*/
if (!name.equals(pd.getDeclaringNodeType())) {
String msg = "[" + name + "#" + pd.getName() + "] invalid declaring node type specified";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
checkNamespace(pd.getName(), nsReg);
// check that auto-created properties specify a name
if (pd.definesResidual() && pd.isAutoCreated()) {
String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a name";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
// check that auto-created properties specify a type
if (pd.getRequiredType() == PropertyType.UNDEFINED && pd.isAutoCreated()) {
String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a type";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
/**
* check default values:
* make sure type of value is consistent with required property type
*/
QValue[] defVals = pd.getDefaultValues();
if (defVals != null && defVals.length != 0) {
int reqType = pd.getRequiredType();
for (QValue defVal : defVals) {
if (reqType == PropertyType.UNDEFINED) {
reqType = defVal.getType();
} else {
if (defVal.getType() != reqType) {
String msg = "[" + name + "#" + pd.getName() + "] type of default value(s) is not consistent with required property type";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
}
}
} else {
// no default values specified
if (!lenient) {
// auto-created properties must have a default value
if (pd.isAutoCreated()) {
String msg = "[" + name + "#" + pd.getName() + "] auto-created property must have a default value";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
}
}
// check that default values satisfy value constraints
QValueConstraint[] constraints = pd.getValueConstraints();
if (constraints != null && constraints.length > 0) {
if (defVals != null && defVals.length > 0) {
// check value constraints on every value
for (QValue defVal : defVals) {
// constraints are OR-ed together
boolean satisfied = false;
ConstraintViolationException cve = null;
for (QValueConstraint constraint : constraints) {
try {
constraint.check(defVal);
// at least one constraint is satisfied
satisfied = true;
break;
} catch (ConstraintViolationException e) {
cve = e;
}
}
if (!satisfied) {
// report last exception we encountered
String msg = "[" + name + "#" + pd.getName() + "] default value does not satisfy value constraint";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, cve);
}
}
}
/**
* ReferenceConstraint:
* the specified node type must be registered, with one notable
* exception: the node type just being registered
*/
if (pd.getRequiredType() == PropertyType.REFERENCE || pd.getRequiredType() == PropertyType.WEAKREFERENCE) {
for (QValueConstraint constraint : constraints) {
Name ntName = NameFactoryImpl.getInstance().create(constraint.getString());
if (!name.equals(ntName) && !ntdCache.containsKey(ntName)) {
String msg = "[" + name + "#" + pd.getName() + "] invalid " + (pd.getRequiredType() == PropertyType.REFERENCE ? "REFERENCE" : "WEAKREFERENCE") + " value constraint '" + ntName + "' (unknown node type)";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
}
}
}
}
// validate child-node definitions
QNodeDefinition[] cnda = ntd.getChildNodeDefs();
for (QNodeDefinition cnd : cnda) {
/**
* sanity check:
* make sure declaring node type matches name of node type definition
*/
if (!name.equals(cnd.getDeclaringNodeType())) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid declaring node type specified";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
checkNamespace(cnd.getName(), nsReg);
// check that auto-created child-nodes specify a name
if (cnd.definesResidual() && cnd.isAutoCreated()) {
String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a name";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
// check that auto-created child-nodes specify a default primary type
if (cnd.getDefaultPrimaryType() == null && cnd.isAutoCreated()) {
String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
// check default primary type
Name dpt = cnd.getDefaultPrimaryType();
checkNamespace(dpt, nsReg);
boolean referenceToSelf = false;
EffectiveNodeType defaultENT = null;
if (dpt != null) {
// check if this node type specifies itself as default primary type
if (name.equals(dpt)) {
referenceToSelf = true;
}
/**
* the default primary type must be registered, with one notable
* exception: the node type just being registered
*/
if (!name.equals(dpt) && !ntdCache.containsKey(dpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid default primary type '" + dpt + "'";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
/**
* build effective (i.e. merged and resolved) node type from
* default primary type and check for conflicts
*/
try {
if (!referenceToSelf) {
defaultENT = getEffectiveNodeType(dpt, entCache, ntdCache);
} else {
/**
* the default primary type is identical with the node
* type just being registered; we have to instantiate it
* 'manually'
*/
ent = EffectiveNodeType.create(ntd, entCache, ntdCache);
defaultENT = ent;
}
if (cnd.isAutoCreated()) {
/**
* check for circularity through default primary types
* of auto-created child nodes (node type 'a' defines
* auto-created child node with default primary type 'a')
*/
Stack<Name> definingNTs = new Stack<Name>();
definingNTs.push(name);
checkForCircularNodeAutoCreation(defaultENT, definingNTs, entCache, ntdCache);
}
} catch (NodeTypeConflictException ntce) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, ntce);
} catch (NoSuchNodeTypeException nsnte) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, nsnte);
}
}
// check required primary types
Name[] reqTypes = cnd.getRequiredPrimaryTypes();
if (reqTypes != null && reqTypes.length > 0) {
for (Name rpt : reqTypes) {
// skip nt:base required types
if (NameConstants.NT_BASE.equals(rpt)) {
continue;
}
checkNamespace(rpt, nsReg);
referenceToSelf = false;
/**
* check if this node type specifies itself as required
* primary type
*/
if (name.equals(rpt)) {
referenceToSelf = true;
}
/**
* the required primary type must be registered, with one
* notable exception: the node type just being registered
*/
if (!name.equals(rpt) && !ntdCache.containsKey(rpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid required primary type: " + rpt;
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
/**
* check if default primary type satisfies the required
* primary type constraint
*/
if (defaultENT != null && !defaultENT.includesNodeType(rpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] default primary type does not satisfy required primary type constraint " + rpt;
log.debug(msg);
throw new InvalidNodeTypeDefException(msg);
}
/**
* build effective (i.e. merged and resolved) node type from
* required primary type constraint and check for conflicts
*/
try {
if (!referenceToSelf) {
getEffectiveNodeType(rpt, entCache, ntdCache);
} else {
/**
* the required primary type is identical with the
* node type just being registered; we have to
* instantiate it 'manually'
*/
if (ent == null) {
ent = EffectiveNodeType.create(ntd, entCache, ntdCache);
}
}
} catch (NodeTypeConflictException ntce) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, ntce);
} catch (NoSuchNodeTypeException nsnte) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, nsnte);
}
}
}
}
/**
* now build effective (i.e. merged and resolved) node type from
* this node type definition; this will potentially detect more
* conflicts or problems
*/
if (ent == null) {
try {
ent = EffectiveNodeType.create(ntd, entCache, ntdCache);
} catch (NodeTypeConflictException ntce) {
String msg = "[" + name + "] failed to resolve node type definition";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, ntce);
} catch (NoSuchNodeTypeException nsnte) {
String msg = "[" + name + "] failed to resolve node type definition";
log.debug(msg);
throw new InvalidNodeTypeDefException(msg, nsnte);
}
}
return ent;
}
use of org.apache.jackrabbit.spi.QValueConstraint 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;
}
use of org.apache.jackrabbit.spi.QValueConstraint 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.QValueConstraint in project jackrabbit by apache.
the class NodeTypeDefinitionFactory method createValueConstraints.
private String[] createValueConstraints(int type, QValueConstraint[] qv) throws RepositoryException {
String[] ret = new String[qv.length];
for (int i = 0; i < ret.length; i++) {
try {
ValueConstraint c = ValueConstraint.create(type, qv[i].getString());
ret[i] = c.getDefinition(resolver);
} catch (InvalidConstraintException e) {
throw new RepositoryException("Internal error while converting value constraints.", e);
}
}
return ret;
}
use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class PropertyDefinitionImpl method getValueConstraints.
/**
* {@inheritDoc}
*/
public String[] getValueConstraints() {
QPropertyDefinition pd = (QPropertyDefinition) itemDef;
QValueConstraint[] constraints = pd.getValueConstraints();
if (constraints == null || constraints.length == 0) {
return new String[0];
}
String[] vca = new String[constraints.length];
for (int i = 0; i < constraints.length; i++) {
try {
ValueConstraint vc = ValueConstraint.create(pd.getRequiredType(), constraints[i].getString());
vca[i] = vc.getDefinition(resolver);
} catch (InvalidConstraintException e) {
log.warn("Internal error during conversion of constraint.", e);
vca[i] = constraints[i].getString();
}
}
return vca;
}
Aggregations