use of javax.jcr.NamespaceException in project sling by apache.
the class JcrNamespaceMangler method mangleNamespaces.
public String mangleNamespaces(ResourceResolver resolver, Logger logger, String absPath) {
if (absPath.contains(MANGLE_NAMESPACE_OUT_SUFFIX)) {
final Session session = resolver.adaptTo(Session.class);
if (session != null) {
final Matcher m = MANLE_NAMESPACE_OUT_PATTERN.matcher(absPath);
final StringBuffer buf = new StringBuffer();
while (m.find()) {
final String namespace = m.group(1);
try {
// throws if "namespace" is not a registered
// namespace prefix
session.getNamespaceURI(namespace);
final String replacement = MANGLE_NAMESPACE_IN_PREFIX + namespace + MANGLE_NAMESPACE_IN_SUFFIX;
m.appendReplacement(buf, replacement);
} catch (final NamespaceException ne) {
// not a valid prefix
logger.debug("mangleNamespaces: '{}' is not a prefix, not mangling", namespace);
} catch (final RepositoryException re) {
logger.warn("mangleNamespaces: Problem checking namespace '{}'", namespace, re);
}
}
m.appendTail(buf);
absPath = buf.toString();
}
}
return absPath;
}
use of javax.jcr.NamespaceException in project sling by apache.
the class JcrNamespaceMangler method unmangleNamespaces.
public String unmangleNamespaces(ResourceResolver resolver, Logger logger, String absPath) {
if (absPath.contains(MANGLE_NAMESPACE_IN_PREFIX)) {
final Session session = resolver.adaptTo(Session.class);
if (session != null) {
final Matcher m = MANGLE_NAMESPACE_IN_PATTERN.matcher(absPath);
final StringBuffer buf = new StringBuffer();
while (m.find()) {
final String namespace = m.group(1);
try {
// throws if "namespace" is not a registered
// namespace prefix
session.getNamespaceURI(namespace);
final String replacement = MANGLE_NAMESPACE_OUT_PREFIX + namespace + MANGLE_NAMESPACE_OUT_SUFFIX;
m.appendReplacement(buf, replacement);
} catch (final NamespaceException ne) {
// not a valid prefix
logger.debug("unmangleNamespaces: '{}' is not a prefix, not unmangling", namespace);
} catch (final RepositoryException re) {
logger.warn("unmangleNamespaces: Problem checking namespace '{}'", namespace, re);
}
}
m.appendTail(buf);
absPath = buf.toString();
}
}
return absPath;
}
use of javax.jcr.NamespaceException in project jackrabbit by apache.
the class NodeImpl method orderBefore.
/**
* Same as <code>{@link Node#orderBefore(String, String)}</code> except that
* this method takes a <code>Path.Element</code> arguments instead of
* <code>String</code>s.
*
* @param srcName
* @param dstName
* @throws UnsupportedRepositoryOperationException
* @throws VersionException
* @throws ConstraintViolationException
* @throws ItemNotFoundException
* @throws LockException
* @throws RepositoryException
*/
public synchronized void orderBefore(Path.Element srcName, Path.Element dstName) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
// check state of this instance
sanityCheck();
if (!getPrimaryNodeType().hasOrderableChildNodes()) {
throw new UnsupportedRepositoryOperationException("child node ordering not supported on " + this);
}
// check arguments
if (srcName.equals(dstName)) {
// there's nothing to do
return;
}
// check existence
if (!hasNode(srcName.getName(), srcName.getIndex())) {
String name;
try {
Path.Element[] path = new Path.Element[] { srcName };
name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
} catch (NameException e) {
name = srcName.toString();
} catch (NamespaceException e) {
name = srcName.toString();
}
throw new ItemNotFoundException(this + " has no child node with name " + name);
}
if (dstName != null && !hasNode(dstName.getName(), dstName.getIndex())) {
String name;
try {
Path.Element[] path = new Path.Element[] { dstName };
name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
} catch (NameException e) {
name = dstName.toString();
} catch (NamespaceException e) {
name = dstName.toString();
}
throw new ItemNotFoundException(this + " has no child node with name " + name);
}
// make sure this node is checked-out and neither protected nor locked
int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
/*
make sure the session is allowed to reorder child nodes.
since there is no specific privilege for reordering child nodes,
test if the the node to be reordered can be removed and added,
i.e. treating reorder similar to a move.
TODO: properly deal with sns in which case the index would change upon reorder.
*/
AccessManager acMgr = sessionContext.getAccessManager();
PathBuilder pb = new PathBuilder(getPrimaryPath());
pb.addLast(srcName.getName(), srcName.getIndex());
Path childPath = pb.getPath();
if (!acMgr.isGranted(childPath, Permission.MODIFY_CHILD_NODE_COLLECTION)) {
String msg = "Not allowed to reorder child node " + sessionContext.getJCRPath(childPath) + ".";
log.debug(msg);
throw new AccessDeniedException(msg);
}
ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(data.getNodeState().getChildNodeEntries());
int srcInd = -1, destInd = -1;
for (int i = 0; i < list.size(); i++) {
ChildNodeEntry entry = list.get(i);
if (srcInd == -1) {
if (entry.getName().equals(srcName.getName()) && (entry.getIndex() == srcName.getIndex() || srcName.getIndex() == 0 && entry.getIndex() == 1)) {
srcInd = i;
}
}
if (destInd == -1 && dstName != null) {
if (entry.getName().equals(dstName.getName()) && (entry.getIndex() == dstName.getIndex() || dstName.getIndex() == 0 && entry.getIndex() == 1)) {
destInd = i;
if (srcInd != -1) {
break;
}
}
} else {
if (srcInd != -1) {
break;
}
}
}
// check if resulting order would be different to current order
if (destInd == -1) {
if (srcInd == list.size() - 1) {
// no change, we're done
return;
}
} else {
if ((destInd - srcInd) == 1) {
// no change, we're done
return;
}
}
// reorder list
if (destInd == -1) {
list.add(list.remove(srcInd));
} else {
if (srcInd < destInd) {
list.add(destInd, list.get(srcInd));
list.remove(srcInd);
} else {
list.add(destInd, list.remove(srcInd));
}
}
// modify the state of 'this', i.e. the parent node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
thisState.setChildNodeEntries(list);
}
use of javax.jcr.NamespaceException in project jackrabbit by apache.
the class LostFromCacheIssueTest method setUp.
public void setUp() throws Exception {
super.setUp();
Workspace workspace = superuser.getWorkspace();
NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
NodeTypeManager ntmgr = workspace.getNodeTypeManager();
NodeTypeRegistry nodetypeRegistry = ((NodeTypeManagerImpl) ntmgr).getNodeTypeRegistry();
try {
namespaceRegistry.registerNamespace(NAMESPACE_PREFIX, NAMESPACE_URI);
} catch (NamespaceException ignore) {
//already exists
}
QNodeTypeDefinition nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_1), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
try {
nodetypeRegistry.registerNodeType(nodeTypeDefinition);
} catch (InvalidNodeTypeDefException ignore) {
//already exists
}
nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_2), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
try {
nodetypeRegistry.registerNodeType(nodeTypeDefinition);
} catch (InvalidNodeTypeDefException ignore) {
//already exists
}
getOrCreate(superuser.getRootNode(), TESTNODE_PATH);
superuser.save();
}
use of javax.jcr.NamespaceException in project jackrabbit by apache.
the class PropertyUtil method checkNameFormat.
/**
* checks if the given name follows the NAME syntax rules and if a present
* prefix is mapped to a registered namespace
*
* @param name the string to test
*/
public static boolean checkNameFormat(String name, Session session) throws RepositoryException {
if (name == null || name.length() == 0) {
return false;
} else {
NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
boolean prefixOk = true;
// validate name element
Matcher matcher = PATTERN_NAME.matcher(name);
// validate namespace prefixes if present
String[] split = name.split(":");
if (split.length > 1) {
String prefix = split[0];
try {
nsr.getURI(prefix);
} catch (NamespaceException nse) {
prefixOk = false;
}
}
return matcher.matches() && prefixOk;
}
}
Aggregations