use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class AbstractNodeType method getSubtypes.
//--------------------------------------------------------------------------
/**
* Returns the node types derived from this node type.
*
* @param directOnly if <code>true</code> only direct subtypes will be considered
*
* @return an <code>NodeTypeIterator</code>.
* @see NodeType#getSubtypes
* @see NodeType#getDeclaredSubtypes
*/
public NodeTypeIterator getSubtypes(boolean directOnly) {
NodeTypeIterator iter;
try {
iter = ntMgr.getAllNodeTypes();
} catch (RepositoryException e) {
// should never get here
log.error("failed to retrieve registered node types", e);
return NodeTypeIteratorAdapter.EMPTY;
}
ArrayList<NodeType> result = new ArrayList<NodeType>();
String thisName = getName();
while (iter.hasNext()) {
NodeType nt = iter.nextNodeType();
if (!nt.getName().equals(thisName)) {
if (directOnly) {
// direct subtypes only
for (String name : nt.getDeclaredSupertypeNames()) {
if (name.equals(thisName)) {
result.add(nt);
break;
}
}
} else {
// direct and indirect subtypes
if (nt.isNodeType(thisName)) {
result.add(nt);
}
}
}
}
return new NodeTypeIteratorAdapter(result);
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.
the class PropertyDefTest method testGetValueConstraints.
/**
* Tests if value constraints match the pattern specified by the required
* property type.
* <p>
* The test runs for all value constraints of all properties of all
* available node types.
*/
public void testGetValueConstraints() throws RepositoryException {
NodeTypeIterator types = manager.getAllNodeTypes();
// loop all node types
while (types.hasNext()) {
NodeType type = types.nextNodeType();
PropertyDefinition[] defs = type.getPropertyDefinitions();
for (int i = 0; i < defs.length; i++) {
PropertyDefinition def = defs[i];
String[] constraints = def.getValueConstraints();
if (constraints != null) {
for (int j = 0; j < constraints.length; j++) {
Matcher matcher;
switch(defs[i].getRequiredType()) {
case PropertyType.STRING:
case PropertyType.UNDEFINED:
// any value matches
break;
case PropertyType.BINARY:
matcher = CONSTRAINTSPATTERN_BINARY.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.BINARY ", matcher.matches());
break;
case PropertyType.DATE:
matcher = CONSTRAINTSPATTERN_DATE.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.DATE ", matcher.matches());
break;
case PropertyType.LONG:
matcher = CONSTRAINTSPATTERN_LONG.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.LONG", matcher.matches());
break;
case PropertyType.DOUBLE:
matcher = CONSTRAINTSPATTERN_DOUBLE.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.DOUBLE", matcher.matches());
break;
case PropertyType.NAME:
matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.NAME", matcher.matches());
checkPrefix(constraints[j]);
break;
case PropertyType.PATH:
matcher = CONSTRAINTSPATTERN_PATH.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.PATH", matcher.matches());
String[] elems = constraints[j].split("/");
for (int k = 0; k < elems.length; k++) {
checkPrefix(elems[k]);
}
break;
case PropertyType.REFERENCE:
matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
assertTrue("Value constraint does not match " + "the pattern of PropertyType.REFERENCE", matcher.matches());
checkPrefix(constraints[j]);
break;
case PropertyType.BOOLEAN:
assertTrue("Value constraint does not match " + "the pattern of PropertyType.BOOLEAN", constraints[j].equals("true") || constraints[j].equals("false"));
break;
}
}
}
}
}
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit-oak by apache.
the class IndexDefinition method getAllNodeTypes.
private static List<String> getAllNodeTypes(ReadOnlyNodeTypeManager ntReg) {
try {
List<String> typeNames = newArrayList();
NodeTypeIterator ntItr = ntReg.getAllNodeTypes();
while (ntItr.hasNext()) {
typeNames.add(ntItr.nextNodeType().getName());
}
return typeNames;
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit-oak by apache.
the class NodeTypeIndexingUtils method createPrimaryTypeSynonymsFile.
public static File createPrimaryTypeSynonymsFile(String path, Session session) throws Exception {
File file = new File(path);
StringWriter stringWriter = new StringWriter();
NodeTypeIterator allNodeTypes = session.getWorkspace().getNodeTypeManager().getAllNodeTypes();
while (allNodeTypes.hasNext()) {
NodeType nodeType = allNodeTypes.nextNodeType();
NodeType[] superTypes = nodeType.getSupertypes();
if (superTypes != null && superTypes.length > 0) {
stringWriter.append(nodeType.getName()).append(" => ");
for (int i = 0; i < superTypes.length; i++) {
stringWriter.append(superTypes[i].getName());
if (i < superTypes.length - 1) {
stringWriter.append(',');
}
stringWriter.append(' ');
}
stringWriter.append('\n');
}
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(stringWriter.toString().getBytes("UTF-8"));
fileOutputStream.flush();
fileOutputStream.close();
if (file.exists() || file.createNewFile()) {
return file;
} else {
throw new IOException("primary types synonyms file could not be created");
}
}
use of javax.jcr.nodetype.NodeTypeIterator in project sling by apache.
the class MockNodeTypeGenerator method setUp.
public void setUp() throws RepositoryException, IOException {
// create mocks
request = mock(SlingHttpServletRequest.class);
response = mock(SlingHttpServletResponse.class);
resource = mock(Resource.class);
currentNode = mock(Node.class);
session = mock(Session.class);
workspace = mock(Workspace.class);
ntManager = mock(NodeTypeManager.class);
nodeTypeIterator = mock(NodeTypeIterator.class);
outStream = new ByteArrayOutputStream();
// stubbing
when(request.getResource()).thenReturn(resource);
when(request.getMethod()).thenReturn(HttpConstants.METHOD_GET);
when(response.getWriter()).thenReturn(new PrintWriter(outStream, true));
when(resource.adaptTo(Node.class)).thenReturn(currentNode);
when(currentNode.getSession()).thenReturn(session);
when(session.getWorkspace()).thenReturn(workspace);
when(workspace.getNodeTypeManager()).thenReturn(ntManager);
when(ntManager.getAllNodeTypes()).thenReturn(nodeTypeIterator);
}
Aggregations