use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class NodeTypeTest method testGetDeclaredChildNodeDefs.
/**
* Test if all node defs returned by getDeclaredChildNodeDefs() are also
* returned by getChildNodeDefs(). All existing node types are tested.
*/
public void testGetDeclaredChildNodeDefs() throws RepositoryException {
NodeTypeIterator types = manager.getAllNodeTypes();
while (types.hasNext()) {
NodeType type = types.nextNodeType();
NodeDefinition[] declaredDefs = type.getDeclaredChildNodeDefinitions();
NodeDefinition[] defs = type.getChildNodeDefinitions();
try {
for (int i = 0; i < declaredDefs.length; i++) {
boolean exists = false;
for (int j = 0; j < defs.length; j++) {
if (defs[j].getName().equals(declaredDefs[i].getName())) {
exists = true;
break;
}
}
assertTrue("All node defs returned by " + "getDeclaredChildNodeDefs() must also be " + "returned by getChildNodeDefs().", exists);
}
} catch (ArrayIndexOutOfBoundsException e) {
fail("The array returned by " + "getDeclaredChildNodeDefs() must not exceed " + "the one returned by getChildNodeDefs()");
}
}
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class NodeTypeTest method testGetPropertyDefs.
/**
* Test if getPropertyDefs() of a primary node returns also "jcr:primaryType"
* which is inherited from "nt:base".
*/
public void testGetPropertyDefs() throws NotExecutableException, RepositoryException {
// find a primary node type but not "nt:base"
NodeTypeIterator types = manager.getPrimaryNodeTypes();
while (types.hasNext()) {
NodeType type = types.nextNodeType();
PropertyDefinition[] defs = type.getPropertyDefinitions();
boolean hasJCRPrimaryType = false;
for (int i = 0; i < defs.length; i++) {
if (defs[i].getName().equals(jcrPrimaryType)) {
hasJCRPrimaryType = true;
break;
}
}
assertTrue("getPropertyDefs() of a primary node type " + "must return also \"jcr:primaryType\".", hasJCRPrimaryType);
}
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class SessionImportTest method testEmptyMixins2.
/**
* Test case for issue <a href="https://issues.apache.org/jira/browse/JCR-1857">JCR-1857</href>
*
* @throws IOException
* @throws RepositoryException
*/
public void testEmptyMixins2() throws IOException, RepositoryException, NotExecutableException {
/*
look for a a node type that includes mix:referenceable but isn't any
of the known internal nodetypes that ev. cannot be created through a
session-import
*/
String referenceableNt = null;
NodeTypeIterator it = superuser.getWorkspace().getNodeTypeManager().getPrimaryNodeTypes();
while (it.hasNext() && referenceableNt == null) {
NodeType nt = it.nextNodeType();
String ntName = nt.getName();
if (nt.isNodeType(mixReferenceable) && !nt.isAbstract() && // ignore are built-in nodetypes (mostly version related)
!ntName.startsWith("nt:") && // also skip all internal node types...
!ntName.startsWith("rep:")) {
referenceableNt = ntName;
}
}
if (referenceableNt == null) {
throw new NotExecutableException("No primary type found that extends from mix:referenceable.");
}
/*
TODO: retrieve valid jcr:uuid value from test-properties.
*/
String uuid = UUID.randomUUID().toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sv:node xmlns:nt=\"http://www.jcp.org/jcr/nt/1.0\"\n" + " xmlns:sv=\"http://www.jcp.org/jcr/sv/1.0\"\n" + " xmlns:mix=\"http://www.jcp.org/jcr/mix/1.0\"\n" + " xmlns:jcr=\"http://www.jcp.org/jcr/1.0\"\n" + " sv:name=\"testnode1\">\n" + " <sv:property sv:name=\"jcr:primaryType\" sv:type=\"Name\">\n" + " <sv:value>" + referenceableNt + "</sv:value>\n" + " </sv:property>\n" + " <sv:property sv:name=\"jcr:uuid\" sv:type=\"String\">\n" + " <sv:value>" + uuid + "</sv:value>\n" + " </sv:property>\n" + "</sv:node>";
InputStream in = new ByteArrayInputStream(xml.getBytes());
superuser.importXML(testRootNode.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class GQL method resolveNodeTypeName.
/**
* Resolves the given <code>ntName</code> and returns all node type names
* where the local name matches <code>ntName</code>.
*
* @param ntName the name of a node type (optionally without prefix).
* @return the matching node type names.
* @throws RepositoryException if an error occurs while reading from the
* node type manager.
*/
private String[] resolveNodeTypeName(String ntName) throws RepositoryException {
String[] names;
if (isPrefixed(ntName)) {
names = new String[] { ntName };
} else {
if (ntNames == null) {
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
ntNames = new HashMap<String, String[]>();
NodeTypeIterator it = ntMgr.getAllNodeTypes();
while (it.hasNext()) {
String name = it.nextNodeType().getName();
String localName = name;
int idx = name.indexOf(':');
if (idx != -1) {
localName = name.substring(idx + 1);
}
String[] nts = ntNames.get(localName);
if (nts == null) {
nts = new String[] { name };
} else {
String[] tmp = new String[nts.length + 1];
System.arraycopy(nts, 0, tmp, 0, nts.length);
tmp[nts.length] = name;
nts = tmp;
}
ntNames.put(localName, nts);
}
}
names = ntNames.get(ntName);
if (names == null) {
names = new String[] { ntName };
}
}
return names;
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class GQL method collectNodeTypes.
/**
* Resolves and collects all node types that match <code>ntName</code>.
*
* @param ntName the name of a node type (optionally without prefix).
* @throws RepositoryException if an error occurs while reading from the
* node type manager.
*/
private void collectNodeTypes(String ntName) throws RepositoryException {
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
String[] resolvedNames = resolveNodeTypeName(ntName);
// now resolve node type hierarchy
for (String resolvedName : resolvedNames) {
try {
NodeType base = ntMgr.getNodeType(resolvedName);
if (base.isMixin()) {
// search for nodes where jcr:mixinTypes is set to this mixin
addTypeConstraint(new MixinComparision(resolvedName));
} else {
// search for nodes where jcr:primaryType is set to this type
addTypeConstraint(new PrimaryTypeComparision(resolvedName));
}
// now search for all node types that are derived from base
NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
while (allTypes.hasNext()) {
NodeType nt = allTypes.nextNodeType();
NodeType[] superTypes = nt.getSupertypes();
if (Arrays.asList(superTypes).contains(base)) {
if (nt.isMixin()) {
addTypeConstraint(new MixinComparision(nt.getName()));
} else {
addTypeConstraint(new PrimaryTypeComparision(nt.getName()));
}
}
}
} catch (NoSuchNodeTypeException e) {
// add anyway -> will not match anything
addTypeConstraint(new PrimaryTypeComparision(resolvedName));
}
}
}
Aggregations