use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeTypeCreationTest method testPropertyDefinitionTemplate.
public void testPropertyDefinitionTemplate() throws Exception {
PropertyDefinitionTemplate pdt = createBooleanPropTemplate();
assertEquals(jcrPropName, pdt.getName());
try {
pdt.setName(null);
fail("null isn't a valid JCR name");
} catch (ConstraintViolationException e) {
// success
}
assertEquals(false, pdt.isAutoCreated());
assertEquals(false, pdt.isMandatory());
assertEquals(OnParentVersionAction.IGNORE, pdt.getOnParentVersion());
assertEquals(false, pdt.isProtected());
assertEquals(PropertyType.BOOLEAN, pdt.getRequiredType());
assertEquals(null, pdt.getValueConstraints());
assertEquals(null, pdt.getDefaultValues());
assertEquals(false, pdt.isMultiple());
String[] qo = pdt.getAvailableQueryOperators();
assertEquals(1, qo.length);
assertEquals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, qo[0]);
assertEquals(false, pdt.isFullTextSearchable());
assertEquals(false, pdt.isQueryOrderable());
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeTypeCreationTest method testInvalidJCRNames.
public void testInvalidJCRNames() throws Exception {
String invalidName = ":ab[2]";
// invalid name(s) passed to NT-template methods
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
try {
ntt.setName(invalidName);
fail("ConstraintViolationException expected. Nt-name is invalid");
} catch (ConstraintViolationException e) {
// success
}
try {
ntt.setDeclaredSuperTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
fail("ConstraintViolationException expected. One of the super type names is invalid");
} catch (ConstraintViolationException e) {
// success
}
try {
ntt.setPrimaryItemName(invalidName);
fail("ConstraintViolationException expected. Primary item name is invalid");
} catch (ConstraintViolationException e) {
// success
}
// invalid name(s) passed to NodeDefinitionTemplate
NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
try {
ndt.setName(invalidName);
fail("ConstraintViolationException expected. Name is invalid");
} catch (ConstraintViolationException e) {
// success
}
try {
ndt.setRequiredPrimaryTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
fail("ConstraintViolationException expected. One of the required primary type names is invalid");
} catch (ConstraintViolationException e) {
// success
}
try {
ndt.setDefaultPrimaryTypeName(invalidName);
fail("ConstraintViolationException expected. Default primary type name is invalid");
} catch (ConstraintViolationException e) {
// success
}
// invalid name(s) passed to PropertyDefinitionTemplate
PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
try {
pdt.setName(invalidName);
fail("ConstraintViolationException expected. Name is invalid");
} catch (ConstraintViolationException e) {
// success
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeTypeRegistryImpl method getEffectiveNodeType.
/**
* @see EffectiveNodeTypeProvider#getEffectiveNodeType(QNodeTypeDefinition, Map)
*/
public EffectiveNodeType getEffectiveNodeType(QNodeTypeDefinition ntd, Map<Name, QNodeTypeDefinition> ntdMap) throws ConstraintViolationException, NoSuchNodeTypeException {
TreeSet<Name> mergedNodeTypes = new TreeSet<Name>();
TreeSet<Name> inheritedNodeTypes = new TreeSet<Name>();
TreeSet<Name> allNodeTypes = new TreeSet<Name>();
Map<Name, List<QItemDefinition>> namedItemDefs = new HashMap<Name, List<QItemDefinition>>();
List<QItemDefinition> unnamedItemDefs = new ArrayList<QItemDefinition>();
Set<Name> supportedMixins = null;
Name ntName = ntd.getName();
// prepare new instance
mergedNodeTypes.add(ntName);
allNodeTypes.add(ntName);
Name[] smixins = ntd.getSupportedMixinTypes();
if (smixins != null) {
supportedMixins = new HashSet<Name>();
supportedMixins.addAll(Arrays.asList(smixins));
}
// map of all item definitions (maps id to definition)
// used to effectively detect ambiguous child definitions where
// ambiguity is defined in terms of definition identity
Set<QItemDefinition> itemDefIds = new HashSet<QItemDefinition>();
for (QNodeDefinition nd : ntd.getChildNodeDefs()) {
// this node type definition
if (itemDefIds.contains(nd)) {
// conflict
String msg;
if (nd.definesResidual()) {
msg = ntName + " contains ambiguous residual child node definitions";
} else {
msg = ntName + " contains ambiguous definitions for child node named " + nd.getName();
}
log.debug(msg);
throw new ConstraintViolationException(msg);
} else {
itemDefIds.add(nd);
}
if (nd.definesResidual()) {
// residual node definition
unnamedItemDefs.add(nd);
} else {
// named node definition
Name name = nd.getName();
List<QItemDefinition> defs = namedItemDefs.get(name);
if (defs == null) {
defs = new ArrayList<QItemDefinition>();
namedItemDefs.put(name, defs);
}
if (defs.size() > 0) {
/**
* there already exists at least one definition with that
* name; make sure none of them is auto-create
*/
for (QItemDefinition qDef : defs) {
if (nd.isAutoCreated() || qDef.isAutoCreated()) {
// conflict
String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
}
defs.add(nd);
}
}
for (QPropertyDefinition pd : ntd.getPropertyDefs()) {
// this node type definition
if (itemDefIds.contains(pd)) {
// conflict
String msg;
if (pd.definesResidual()) {
msg = ntName + " contains ambiguous residual property definitions";
} else {
msg = ntName + " contains ambiguous definitions for property named " + pd.getName();
}
log.debug(msg);
throw new ConstraintViolationException(msg);
} else {
itemDefIds.add(pd);
}
if (pd.definesResidual()) {
// residual property definition
unnamedItemDefs.add(pd);
} else {
// named property definition
Name name = pd.getName();
List<QItemDefinition> defs = namedItemDefs.get(name);
if (defs == null) {
defs = new ArrayList<QItemDefinition>();
namedItemDefs.put(name, defs);
}
if (defs.size() > 0) {
/**
* there already exists at least one definition with that
* name; make sure none of them is auto-create
*/
for (QItemDefinition qDef : defs) {
if (pd.isAutoCreated() || qDef.isAutoCreated()) {
// conflict
String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
}
defs.add(pd);
}
}
// create empty effective node type instance
EffectiveNodeTypeImpl ent = new EffectiveNodeTypeImpl(mergedNodeTypes, inheritedNodeTypes, allNodeTypes, namedItemDefs, unnamedItemDefs, supportedMixins);
// resolve supertypes recursively
Name[] supertypes = ntd.getSupertypes();
if (supertypes.length > 0) {
EffectiveNodeTypeImpl effSuperType = (EffectiveNodeTypeImpl) getEffectiveNodeType(supertypes, ntdMap);
ent.internalMerge(effSuperType, true);
}
return ent;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class ItemDefinitionProviderImpl method getMatchingPropDef.
private static QPropertyDefinition getMatchingPropDef(QPropertyDefinition[] defs, int type, boolean multiValued, boolean throwWhenAmbiguous) throws ConstraintViolationException {
QPropertyDefinition match = null;
for (int i = 0; i < defs.length; i++) {
QItemDefinition qDef = defs[i];
if (!qDef.definesNode()) {
QPropertyDefinition pd = (QPropertyDefinition) qDef;
int reqType = pd.getRequiredType();
// match type
if (reqType == PropertyType.UNDEFINED || type == PropertyType.UNDEFINED || reqType == type) {
// match multiValued flag
if (multiValued == pd.isMultiple()) {
// found match
if (pd.getRequiredType() != PropertyType.UNDEFINED) {
if (match != null && throwWhenAmbiguous) {
throw new ConstraintViolationException("ambiguous property definitions found: " + match + " vs " + pd);
}
// thus obviously specified a String.
if (match == null || match.getRequiredType() != PropertyType.STRING) {
// found best possible match
match = pd;
}
} else {
if (match == null) {
match = pd;
}
}
}
}
}
}
return match;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class StringConstraint method check.
/**
* @see org.apache.jackrabbit.spi.QValueConstraint#check(QValue)
*/
public void check(QValue value) throws ConstraintViolationException, RepositoryException {
if (value == null) {
throw new ConstraintViolationException("null value does not satisfy the constraint '" + getString() + "'");
}
switch(value.getType()) {
case PropertyType.STRING:
case PropertyType.URI:
String text = value.getString();
Matcher matcher = pattern.matcher(text);
if (!matcher.matches()) {
throw new ConstraintViolationException("'" + text + "' does not satisfy the constraint '" + getString() + "'");
}
return;
default:
String msg = "String constraint can not be applied to value of type: " + PropertyType.nameFromValue(value.getType());
log.debug(msg);
throw new RepositoryException(msg);
}
}
Aggregations