use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class AbstractNode method getPrimaryNodeType.
/**
* Returns the primary node type of this node.
* <p>
* The default implementation uses the value of the
* <code>jcr:primaryType</code> property to look up the primary
* node type from the {@link NodeTypeManager} of the current workspace.
*
* @return primary node type
* @throws RepositoryException if an error occurs
*/
public NodeType getPrimaryNodeType() throws RepositoryException {
NodeTypeManager manager = getSession().getWorkspace().getNodeTypeManager();
Property property = getProperty(getName("jcr:primaryType"));
return manager.getNodeType(property.getString());
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class AbstractNode method getMixinNodeTypes.
//----------------------------------------------------------------< Node >
/**
* Returns the declared mixin node types of this node.
* <p>
* The default implementation uses the values of the
* <code>jcr:mixinTypes</code> property to look up the mixin node types
* from the {@link NodeTypeManager} of the current workspace.
*
* @return mixin node types
* @throws RepositoryException if an error occurs
*/
public NodeType[] getMixinNodeTypes() throws RepositoryException {
try {
NodeTypeManager manager = getSession().getWorkspace().getNodeTypeManager();
Property property = getProperty(getName("jcr:mixinTypes"));
Value[] values = property.getValues();
NodeType[] types = new NodeType[values.length];
for (int i = 0; i < values.length; i++) {
types[i] = manager.getNodeType(values[i].getString());
}
return types;
} catch (PathNotFoundException e) {
// jcr:mixinTypes does not exist, i.e. no mixin types on this node
return new NodeType[0];
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class WorkspaceResourceImpl method setProperty.
/**
* Allows to alter the registered namespaces ({@link ItemResourceConstants#JCR_NAMESPACES})
* or register node types {@link ItemResourceConstants#JCR_NODETYPES_CND}
* where the passed value is a cnd string containing the definition
* and forwards any other property to the super class.
* <p>
* Note that again no property status is set. Any failure while setting
* a property results in an exception (violating RFC 2518).
*
* @param property
* @throws DavException
* @see DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty)
*/
@Override
public void setProperty(DavProperty<?> property) throws DavException {
if (ItemResourceConstants.JCR_NAMESPACES.equals(property.getName())) {
NamespacesProperty nsp = new NamespacesProperty(property);
try {
Map<String, String> changes = new HashMap<String, String>(nsp.getNamespaces());
NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
for (String prefix : nsReg.getPrefixes()) {
if (!changes.containsKey(prefix)) {
// prefix not present amongst the new values any more > unregister
nsReg.unregisterNamespace(prefix);
} else if (changes.get(prefix).equals(nsReg.getURI(prefix))) {
// present with same uri-value >> no action required
changes.remove(prefix);
}
}
// it has not been present before.
for (String prefix : changes.keySet()) {
String uri = changes.get(prefix);
nsReg.registerNamespace(prefix, uri);
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else if (ItemResourceConstants.JCR_NODETYPES_CND.equals(property.getName())) {
try {
Object value = property.getValue();
List<?> cmds;
if (value instanceof List) {
cmds = (List) value;
} else if (value instanceof Element) {
cmds = Collections.singletonList(value);
} else {
log.warn("Unexpected structure of dcr:nodetypes-cnd property.");
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
}
String registerCnd = null;
boolean allowUpdate = false;
List<String> unregisterNames = new ArrayList<String>();
for (Object listEntry : cmds) {
if (listEntry instanceof Element) {
Element e = (Element) listEntry;
String localName = e.getLocalName();
if (ItemResourceConstants.XML_CND.equals(localName)) {
registerCnd = DomUtil.getText(e);
} else if (ItemResourceConstants.XML_ALLOWUPDATE.equals(localName)) {
String allow = DomUtil.getTextTrim(e);
allowUpdate = Boolean.parseBoolean(allow);
} else if (ItemResourceConstants.XML_NODETYPENAME.equals(localName)) {
unregisterNames.add(DomUtil.getTextTrim(e));
}
}
}
// TODO: for simplicity it's currently either registration or unregistration as nt-modifications are immediately persisted.
Session s = getRepositorySession();
NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
if (registerCnd != null) {
StringReader reader = new StringReader(registerCnd);
DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, s.getValueFactory(), s.getWorkspace().getNamespaceRegistry());
CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, "davex", factory);
List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
ntMgr.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]), allowUpdate);
} else if (!unregisterNames.isEmpty()) {
ntMgr.unregisterNodeTypes(unregisterNames.toArray(new String[unregisterNames.size()]));
}
} catch (ParseException e) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, e);
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else {
// only jcr:namespace or node types can be modified
throw new DavException(DavServletResponse.SC_CONFLICT);
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class NodeTypesReport method getNodeTypes.
/**
* Parse the Xml element in the info object an return an interator over
* the specified node types.
*
* @return
* @throws RepositoryException
* @throws DavException
*/
private static NodeTypeIterator getNodeTypes(Session session, ReportInfo info) throws RepositoryException, DavException {
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
// check the simple types first...
if (info.containsContentElement(XML_REPORT_ALLNODETYPES, ItemResourceConstants.NAMESPACE)) {
return ntMgr.getAllNodeTypes();
} else if (info.containsContentElement(XML_REPORT_MIXINNODETYPES, ItemResourceConstants.NAMESPACE)) {
return ntMgr.getMixinNodeTypes();
} else if (info.containsContentElement(XML_REPORT_PRIMARYNODETYPES, ItemResourceConstants.NAMESPACE)) {
return ntMgr.getPrimaryNodeTypes();
} else {
// None of the simple types. test if a report for individual
// nodetype was request. If not, the request body is not valid.
List<Element> elemList = info.getContentElements(XML_NODETYPE, ItemResourceConstants.NAMESPACE);
if (elemList.isEmpty()) {
// throw exception if the request body does not contain a single nodetype element
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "NodeTypes report: request body has invalid format.");
}
// todo: find better solution...
List<NodeType> ntList = new ArrayList<NodeType>();
for (Element el : elemList) {
String nodetypeName = DomUtil.getChildTextTrim(el, XML_NODETYPENAME, ItemResourceConstants.NAMESPACE);
if (nodetypeName != null) {
ntList.add(ntMgr.getNodeType(nodetypeName));
}
}
return new NodeTypeIteratorAdapter(ntList);
}
}
use of javax.jcr.nodetype.NodeTypeManager 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(":", "");
}
Aggregations