Files
alfresco-community-repo/source/java/org/alfresco/repo/virtual/config/NodeRefPathExpression.java
Raluca Munteanu 8674e2bfc8 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 12:48:49 +00:00

220 lines
5.9 KiB
Java

package org.alfresco.repo.virtual.config;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.cmr.repository.NodeRef;
/**
* A String name or qname path expression that resolves to a {@link NodeRef}.<br>
* The given name or qname path is relative to a {@link NodeRefContext}
* repository location. The default context is set to
* {@link CompanyHomeContext#COMPANY_HOME_CONTEXT_NAME}. Other contexts can be
* set using their name with {@link #setContext(String)}.<br>
* The set path is automatically detected and checked for consistency.
*/
public class NodeRefPathExpression implements NodeRefExpression
{
private static final String NAMESPACE_DELIMITER = ":";
private static final String PATH_DELIMITER = "/";
private Map<String, NodeRefContext> contexts = new HashMap<String, NodeRefContext>();
private NodeRefResolver resolver;
private String context = CompanyHomeContext.COMPANY_HOME_CONTEXT_NAME;
private String[] createNamePath;
private String[] namePath;
private String[] qNamePath;
public NodeRefPathExpression(NodeRefResolver resolver, Map<String, NodeRefContext> contexts)
{
this(resolver,
contexts,
CompanyHomeContext.COMPANY_HOME_CONTEXT_NAME,
null);
}
public NodeRefPathExpression(NodeRefResolver resolver, Map<String, NodeRefContext> contexts, String context,
String path)
{
super();
this.resolver = resolver;
this.contexts = contexts;
this.context = context;
setPath(path);
}
public void setContext(String context)
{
this.context = context;
}
public void setCreatedPathName(String createNamePath)
{
this.createNamePath = createNamePath.split(",");
}
/**
* Path setter.<br>
* The type of path is automatically detected and checked for consistency.
*
* @param path the string path to be resolved later
* @throws AlfrescoRuntimeException if the given path is inconsistent (i.e.
* a combination of qnames and names)
*/
public void setPath(String path) throws AlfrescoRuntimeException
{
String[] pathElements = splitAndNormalizePath(path);
if (isQNamePath(pathElements))
{
this.qNamePath = pathElements;
}
else if (isNamePath(pathElements))
{
this.namePath = pathElements;
}
else
{
throw new AlfrescoRuntimeException("Invalid path format : " + path);
}
}
private boolean isQNamePath(String[] pathElements)
{
for (int i = 0; i < pathElements.length; i++)
{
if (!pathElements[i].contains(NAMESPACE_DELIMITER))
{
return false;
}
}
return true;
}
private boolean isNamePath(String[] pathElements)
{
for (int i = 0; i < pathElements.length; i++)
{
if (pathElements[i].contains(NAMESPACE_DELIMITER))
{
return false;
}
}
return true;
}
public static String[] splitAndNormalizePath(String path)
{
if (path == null || path.trim().length() == 0)
{
return new String[] {};
}
String[] splitPath = path.split(PATH_DELIMITER);
// remove blank entries resulted from misplaced delimiters
int shift = 0;
for (int i = 0; i < splitPath.length; i++)
{
if (splitPath[i] == null || splitPath[i].trim().isEmpty())
{
shift++;
}
else if (shift > 0)
{
splitPath[i - shift] = splitPath[i];
}
}
if (shift > 0)
{
String[] noBlanksSplitPath = new String[splitPath.length - shift];
if (noBlanksSplitPath.length > 0)
{
System.arraycopy(splitPath,
0,
noBlanksSplitPath,
0,
noBlanksSplitPath.length);
}
splitPath = noBlanksSplitPath;
}
return splitPath;
}
@Override
public NodeRef resolve()
{
NodeRefContext theContext = contexts.get(context);
if (this.namePath != null)
{
return theContext.resolveNamePath(this.namePath,
resolver);
}
else
{
return theContext.resolveQNamePath(this.qNamePath,
resolver);
}
}
@Override
public NodeRef resolve(boolean createIfNotFound)
{
NodeRef nodeRef = resolve();
if (nodeRef == null && createIfNotFound)
{
NodeRefContext theContext = contexts.get(context);
if (this.namePath != null)
{
return theContext.createNamePath(this.namePath,
resolver);
}
else
{
return theContext.createQNamePath(this.qNamePath,
this.createNamePath,
resolver);
}
}
return nodeRef;
}
@Override
public String toString()
{
StringBuilder pathString = new StringBuilder();
pathString.append("<");
pathString.append(this.context);
pathString.append(">/");
if (this.namePath != null)
{
pathString.append(Arrays.toString(this.namePath));
}
if (this.qNamePath != null)
{
pathString.append(Arrays.toString(this.qNamePath));
}
return "<null path expression>";
}
}