Search in sources :

Example 36 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project sling by apache.

the class NodeTypeConfigurationPrinter method printConfiguration.

/**
     * {@inheritDoc}
     */
public void printConfiguration(PrintWriter pw, String mode) {
    if (slingRepository != null) {
        Session session = null;
        try {
            session = slingRepository.loginAdministrative(null);
            NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
            NodeTypeIterator it = ntm.getAllNodeTypes();
            List<NodeType> sortedTypes = sortTypes(it);
            for (NodeType nt : sortedTypes) {
                pw.printf("[%s]", nt.getName());
                printSuperTypes(pw, nt);
                if (nt.hasOrderableChildNodes()) {
                    pw.print(" orderable");
                }
                if (nt.isMixin()) {
                    pw.print(" mixin");
                }
                linebreak(pw, mode);
                for (PropertyDefinition prop : nt.getPropertyDefinitions()) {
                    if (prop.getDeclaringNodeType() == nt) {
                        startBold(pw, mode);
                    }
                    pw.printf("- %s", prop.getName());
                    printDefaultValues(pw, prop);
                    if (prop.getName().equals(nt.getPrimaryItemName())) {
                        pw.print(" primary");
                    }
                    if (prop.isMandatory()) {
                        pw.print(" mandatory");
                    }
                    if (prop.isAutoCreated()) {
                        pw.print(" autocreated");
                    }
                    if (prop.isProtected()) {
                        pw.print(" protected");
                    }
                    if (prop.isMultiple()) {
                        pw.print(" multiple");
                    }
                    pw.printf(" %s", OnParentVersionAction.nameFromValue(prop.getOnParentVersion()));
                    printConstraints(pw, prop);
                    if (prop.getDeclaringNodeType() == nt) {
                        stopBold(pw, mode);
                    }
                    linebreak(pw, mode);
                }
                for (NodeDefinition child : nt.getChildNodeDefinitions()) {
                    if (child.getDeclaringNodeType() == nt) {
                        startBold(pw, mode);
                    }
                    pw.printf("+ %s", child.getName());
                    printRequiredChildTypes(pw, child);
                    if (child.getDefaultPrimaryType() != null) {
                        pw.printf(" = %s", child.getDefaultPrimaryType().getName());
                    }
                    if (child.isMandatory()) {
                        pw.print(" mandatory");
                    }
                    if (child.isAutoCreated()) {
                        pw.print(" autocreated");
                    }
                    if (child.isProtected()) {
                        pw.print(" protected");
                    }
                    if (child.allowsSameNameSiblings()) {
                        pw.print(" multiple");
                    }
                    pw.printf(" %s", OnParentVersionAction.nameFromValue(child.getOnParentVersion()));
                    if (child.getDeclaringNodeType() == nt) {
                        stopBold(pw, mode);
                    }
                    linebreak(pw, mode);
                }
                linebreak(pw, mode);
            }
        } catch (RepositoryException e) {
            pw.println("Unable to output namespace mappings.");
            e.printStackTrace(pw);
        } finally {
            if (session != null) {
                session.logout();
            }
        }
    } else {
        pw.println("SlingRepository is not available.");
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session)

Example 37 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project sling by apache.

the class NodeTypesJSONServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json; charset=UTF-8");
    PrintWriter writer = response.getWriter();
    Resource resource = request.getResource();
    Node currentNode = resource.adaptTo(Node.class);
    try {
        NodeTypeIterator nodeTypeIterator = currentNode.getSession().getWorkspace().getNodeTypeManager().getAllNodeTypes();
        JSONObject nodeTypes = new JSONObject();
        while (nodeTypeIterator.hasNext()) {
            NodeType nodeType = nodeTypeIterator.nextNodeType();
            if (nodeType.getName() != null) {
                JSONNodeType jsonNodeType = new JSONNodeType(nodeType);
                nodeTypes.put(nodeType.getName(), jsonNodeType.getJson());
            }
        }
        writer.println(nodeTypes.toString(2));
        writer.flush();
        writer.close();
    } catch (RepositoryException e) {
        log.error("Could not generate the node types.", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } catch (JSONException e) {
        log.error("Could not generate the node types.", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Resource(org.apache.sling.api.resource.Resource) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) PrintWriter(java.io.PrintWriter)

Example 38 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.

the class LuceneQueryFactory method create.

/**
     * Creates a lucene query for the given QOM selector.
     *
     * @param selector the selector.
     * @return a lucene query for the given selector.
     * @throws RepositoryException if an error occurs while creating the query.
     */
public Query create(Selector selector) throws RepositoryException {
    List<Term> terms = new ArrayList<Term>();
    String name = selector.getNodeTypeName();
    NodeTypeIterator allTypes = ntManager.getAllNodeTypes();
    while (allTypes.hasNext()) {
        NodeType nt = allTypes.nextNodeType();
        if (nt.isNodeType(name)) {
            terms.add(createNodeTypeTerm(nt));
        }
    }
    if (terms.size() == 1) {
        return new JackrabbitTermQuery(terms.get(0));
    } else {
        BooleanQuery b = new BooleanQuery();
        for (Term term : terms) {
            b.add(new JackrabbitTermQuery(term), SHOULD);
        }
        return b;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) Term(org.apache.lucene.index.Term)

Example 39 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.

the class GQL method resolveChildNodeName.

/**
     * Resolves the given node name. If the name has a prefix then the name
     * is returned immediately as is. Otherwise the node type manager is
     * searched for a node definition that defines a named child node with
     * a local name that matches the provided <code>name</code>. If such a match
     * is found the name of the node definition is returned.
     *
     * @param name the name of a node (optionally without a prefix).
     * @return the resolved node name.
     * @throws RepositoryException if an error occurs while reading from the
     *                             node type manager.
     */
private String resolveChildNodeName(String name) throws RepositoryException {
    if (isPrefixed(name)) {
        return name;
    }
    if (childNodeNames == null) {
        childNodeNames = new HashMap<String, String>();
        NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
        NodeTypeIterator it = ntMgr.getAllNodeTypes();
        while (it.hasNext()) {
            NodeType nt = it.nextNodeType();
            NodeDefinition[] defs = nt.getDeclaredChildNodeDefinitions();
            for (NodeDefinition def : defs) {
                String cnn = def.getName();
                if (!cnn.equals("*")) {
                    String localName = cnn;
                    int idx = cnn.indexOf(':');
                    if (idx != -1) {
                        localName = cnn.substring(idx + 1);
                    }
                    childNodeNames.put(localName, cnn);
                }
            }
        }
    }
    String cnn = childNodeNames.get(name);
    if (cnn != null) {
        return cnn;
    } else {
        return name;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 40 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.

the class NodeMixinUtil method getNonExistingMixinName.

/**
     * @return a string that is not the name of a mixin type
     */
public static String getNonExistingMixinName(Session session) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator mixins = manager.getMixinNodeTypes();
    StringBuffer s = new StringBuffer("X");
    while (mixins.hasNext()) {
        s.append(mixins.nextNodeType().getName());
    }
    return s.toString().replaceAll(":", "");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Aggregations

NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)65 NodeType (javax.jcr.nodetype.NodeType)53 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)28 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Node (javax.jcr.Node)9 RepositoryException (javax.jcr.RepositoryException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)8 ArrayList (java.util.ArrayList)6 Session (javax.jcr.Session)6 HashSet (java.util.HashSet)4 Value (javax.jcr.Value)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Property (javax.jcr.Property)2