Moving to root below branch label

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2005-12-08 07:13:07 +00:00
commit e1e6508fec
1095 changed files with 230566 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import junit.framework.TestCase;
public class DynamicNameSpaceResolverTest extends TestCase
{
public DynamicNameSpaceResolverTest()
{
super();
}
public void testOne()
{
DynamicNamespacePrefixResolver dnpr = new DynamicNamespacePrefixResolver(null);
dnpr.registerNamespace("one", "http:/namespace/one");
dnpr.registerNamespace("two", "http:/namespace/two");
dnpr.registerNamespace("three", "http:/namespace/three");
dnpr.registerNamespace("oneagain", "http:/namespace/one");
dnpr.registerNamespace("four", "http:/namespace/one");
dnpr.registerNamespace("four", "http:/namespace/four");
assertEquals("http:/namespace/one", dnpr.getNamespaceURI("one"));
assertEquals("http:/namespace/two", dnpr.getNamespaceURI("two"));
assertEquals("http:/namespace/three", dnpr.getNamespaceURI("three"));
assertEquals("http:/namespace/one", dnpr.getNamespaceURI("oneagain"));
assertEquals("http:/namespace/four", dnpr.getNamespaceURI("four"));
assertEquals(null, dnpr.getNamespaceURI("five"));
dnpr.unregisterNamespace("four");
assertEquals(null, dnpr.getNamespaceURI("four"));
assertEquals(0, dnpr.getPrefixes("http:/namespace/four").size());
assertEquals(1, dnpr.getPrefixes("http:/namespace/two").size());
assertEquals(2, dnpr.getPrefixes("http:/namespace/one").size());
}
public void testTwo()
{
DynamicNamespacePrefixResolver dnpr1 = new DynamicNamespacePrefixResolver(null);
dnpr1.registerNamespace("one", "http:/namespace/one");
dnpr1.registerNamespace("two", "http:/namespace/two");
dnpr1.registerNamespace("three", "http:/namespace/three");
dnpr1.registerNamespace("oneagain", "http:/namespace/one");
dnpr1.registerNamespace("four", "http:/namespace/one");
dnpr1.registerNamespace("four", "http:/namespace/four");
dnpr1.registerNamespace("five", "http:/namespace/five");
dnpr1.registerNamespace("six", "http:/namespace/six");
DynamicNamespacePrefixResolver dnpr2 = new DynamicNamespacePrefixResolver(dnpr1);
dnpr2.registerNamespace("a", "http:/namespace/one");
dnpr2.registerNamespace("b", "http:/namespace/two");
dnpr2.registerNamespace("c", "http:/namespace/three");
dnpr2.registerNamespace("d", "http:/namespace/one");
dnpr2.registerNamespace("e", "http:/namespace/one");
dnpr2.registerNamespace("f", "http:/namespace/four");
dnpr2.registerNamespace("five", "http:/namespace/one");
dnpr2.registerNamespace("six", "http:/namespace/seven");
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("one"));
assertEquals("http:/namespace/two", dnpr2.getNamespaceURI("two"));
assertEquals("http:/namespace/three", dnpr2.getNamespaceURI("three"));
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("oneagain"));
assertEquals("http:/namespace/four", dnpr2.getNamespaceURI("four"));
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("five"));
dnpr2.unregisterNamespace("five");
assertEquals("http:/namespace/five", dnpr2.getNamespaceURI("five"));
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("a"));
assertEquals("http:/namespace/two", dnpr2.getNamespaceURI("b"));
assertEquals("http:/namespace/three", dnpr2.getNamespaceURI("c"));
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("d"));
assertEquals("http:/namespace/one", dnpr2.getNamespaceURI("e"));
assertEquals("http:/namespace/four", dnpr2.getNamespaceURI("f"));
assertEquals(5, dnpr2.getPrefixes("http:/namespace/one").size());
assertEquals(2, dnpr2.getPrefixes("http:/namespace/two").size());
assertEquals(2, dnpr2.getPrefixes("http:/namespace/three").size());
assertEquals(2, dnpr2.getPrefixes("http:/namespace/four").size());
assertEquals(1, dnpr2.getPrefixes("http:/namespace/five").size());
assertEquals(0, dnpr2.getPrefixes("http:/namespace/six").size());
assertEquals(1, dnpr2.getPrefixes("http:/namespace/seven").size());
}
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/**
* A delegating namespace prefix resolver which allows local over rides from the
* delegate. Allows standard/default prefixes to be available but over ridden as
* required.
*
* @author andyh
*
*/
public class DynamicNamespacePrefixResolver implements NamespaceService
{
/**
* The delegate
*/
private NamespacePrefixResolver delegate;
/**
* The map uris keyed by prefix
*/
private HashMap<String, String> map = new HashMap<String, String>();
public DynamicNamespacePrefixResolver(NamespacePrefixResolver delegate)
{
super();
this.delegate = delegate;
}
public DynamicNamespacePrefixResolver()
{
this(null);
}
/**
* Add prefix to name space mapping override
*
* @param prefix
* @param uri
*/
public void registerNamespace(String prefix, String uri)
{
map.put(prefix, uri);
}
/**
* Remove a prefix to namespace mapping
*
* @param prefix
*/
public void unregisterNamespace(String prefix)
{
map.remove(prefix);
}
// NameSpacePrefix Resolver
public String getNamespaceURI(String prefix) throws NamespaceException
{
String uri = map.get(prefix);
if ((uri == null) && (delegate != null))
{
uri = delegate.getNamespaceURI(prefix);
}
return uri;
}
public Collection<String> getPrefixes(String namespaceURI) throws NamespaceException
{
Collection<String> prefixes = new ArrayList<String>();
for (String key : map.keySet())
{
String uri = map.get(key);
if ((uri != null) && (uri.equals(namespaceURI)))
{
prefixes.add(key);
}
}
// Only add if not over ridden here (if identical already added)
if (delegate != null)
{
for (String prefix : delegate.getPrefixes(namespaceURI))
{
if (!map.containsKey(prefix))
{
prefixes.add(prefix);
}
}
}
return prefixes;
}
public Collection<String> getPrefixes()
{
Set<String> prefixes = new HashSet<String>();
if(delegate != null)
{
prefixes.addAll(delegate.getPrefixes());
}
prefixes.addAll(map.keySet());
return prefixes;
}
public Collection<String> getURIs()
{
Set<String> uris = new HashSet<String>();
if(delegate != null)
{
uris.addAll(delegate.getURIs());
}
uris.addAll(map.keySet());
return uris;
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
public class InvalidQNameException extends NamespaceException
{
private static final long serialVersionUID = 7851788938794302629L;
public InvalidQNameException(String msg)
{
super(msg);
}
public InvalidQNameException(String msg, Throwable cause)
{
super(msg, cause);
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
public class NamespaceException extends RuntimeException
{
private static final long serialVersionUID = 7851788938794302629L;
public NamespaceException(String msg)
{
super(msg);
}
public NamespaceException(String msg, Throwable cause)
{
super(msg, cause);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import java.util.Collection;
/**
* The <code>NamespacePrefixResolver</code> provides a mapping between
* namespace prefixes and namespace URIs.
*
* @author David Caruana
*/
public interface NamespacePrefixResolver
{
/**
* Gets the namespace URI registered for the given prefix
*
* @param prefix prefix to lookup
* @return the namespace
* @throws NamespaceException if prefix has not been registered
*/
public String getNamespaceURI(String prefix)
throws NamespaceException;
/**
* Gets the registered prefixes for the given namespace URI
*
* @param namespaceURI namespace URI to lookup
* @return the prefixes (or empty collection, if no prefixes registered against URI)
* @throws NamespaceException if URI has not been registered
*/
public Collection<String> getPrefixes(String namespaceURI)
throws NamespaceException;
/**
* Gets all registered Prefixes
*
* @return collection of all registered namespace prefixes
*/
Collection<String> getPrefixes();
/**
* Gets all registered Uris
*
* @return collection of all registered namespace uris
*/
Collection<String> getURIs();
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
/**
* Namespace Service.
*
* The Namespace Service provides access to and definition of namespace
* URIs and Prefixes.
*
* @author David Caruana
*/
public interface NamespaceService extends NamespacePrefixResolver
{
/** Default Namespace URI */
public static final String DEFAULT_URI = "";
/** Default Namespace Prefix */
public static final String DEFAULT_PREFIX = "";
/** Default Alfresco URI */
public static final String ALFRESCO_URI = "http://www.alfresco.org";
/** Default Alfresco Prefix */
public static final String ALFRESCO_PREFIX = "alf";
/** Dictionary Model URI */
public static final String DICTIONARY_MODEL_1_0_URI = "http://www.alfresco.org/model/dictionary/1.0";
/** Dictionary Model Prefix */
public static final String DICTIONARY_MODEL_PREFIX = "d";
/** System Model URI */
public static final String SYSTEM_MODEL_1_0_URI = "http://www.alfresco.org/model/system/1.0";
/** System Model Prefix */
public static final String SYSTEM_MODEL_PREFIX = "sys";
/** Content Model URI */
public static final String CONTENT_MODEL_1_0_URI = "http://www.alfresco.org/model/content/1.0";
/** Content Model Prefix */
public static final String CONTENT_MODEL_PREFIX = "cm";
/** Application Model URI */
public static final String APP_MODEL_1_0_URI = "http://www.alfresco.org/model/application/1.0";
/** Application Model Prefix */
public static final String APP_MODEL_PREFIX = "app";
/** Alfresco View Namespace URI */
public static final String REPOSITORY_VIEW_1_0_URI = "http://www.alfresco.org/view/repository/1.0";
/** Alfresco View Namespace Prefix */
public static final String REPOSITORY_VIEW_PREFIX = "view";
/** Alfresco security URI */
public static final String SECURITY_MODEL_1_0_URI = "http://www.alfresco.org/model/security/1.0";
/** Alfresco security Prefix */
public static final String SECURITY_MODEL_PREFIX = "security";
/**
* Register a prefix for namespace uri.
*
* @param prefix
* @param uri
*/
public void registerNamespace(String prefix, String uri);
/**
* Unregister a prefix.
*
* @param prefix
*/
public void unregisterNamespace(String prefix);
}

View File

@@ -0,0 +1,476 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import java.io.Serializable;
import java.util.Collection;
/**
* <code>QName</code> represents the qualified name of a Repository item. Each
* QName consists of a local name qualified by a namespace.
* <p>
* The {@link org.alfresco.service.namespace.QNamePattern QNamePattern} is implemented
* to allow instances of this class to be used for direct pattern matching where
* required on interfaces.
*
* @author David Caruana
*
*/
public final class QName implements QNamePattern, Serializable, Cloneable
{
private static final long serialVersionUID = 3977016258204348976L;
private String namespaceURI; // never null
private String localName; // never null
private int hashCode;
private String prefix;
public static final char NAMESPACE_PREFIX = ':';
public static final char NAMESPACE_BEGIN = '{';
public static final char NAMESPACE_END = '}';
public static final int MAX_LENGTH = 100;
/**
* Create a QName
*
* @param namespaceURI the qualifying namespace (maybe null or empty string)
* @param localName the qualified name
* @return the QName
*/
public static QName createQName(String namespaceURI, String localName)
throws InvalidQNameException
{
if (localName == null || localName.length() == 0)
{
throw new InvalidQNameException("A QName must consist of a local name");
}
return new QName(namespaceURI, localName, null);
}
/**
* Create a QName
*
* @param prefix namespace prefix (maybe null or empty string)
* @param localName local name
* @param prefixResolver lookup to resolve mappings between prefix and namespace
* @return the QName
*/
public static QName createQName(String prefix, String localName, NamespacePrefixResolver prefixResolver)
throws InvalidQNameException, NamespaceException
{
// Validate Arguments
if (localName == null || localName.length() == 0)
{
throw new InvalidQNameException("A QName must consist of a local name");
}
if (prefixResolver == null)
{
throw new IllegalArgumentException("A Prefix Resolver must be specified");
}
if (prefix == null)
{
prefix = NamespaceService.DEFAULT_PREFIX;
}
// Calculate namespace URI and create QName
String uri = prefixResolver.getNamespaceURI(prefix);
if (uri == null)
{
throw new NamespaceException("Namespace prefix " + prefix + " is not mapped to a namespace URI");
}
return new QName(uri, localName, prefix);
}
/**
* Create a QName
*
* @param qname qualified name of the following format <code>prefix:localName</code>
* @param prefixResolver lookup to resolve mappings between prefix and namespace
* @return the QName
*/
public static QName createQName(String qname, NamespacePrefixResolver prefixResolver)
throws InvalidQNameException, NamespaceException
{
QName name = null;
if (qname != null)
{
int colonIndex = qname.indexOf(NAMESPACE_PREFIX);
String prefix = (colonIndex == -1) ? NamespaceService.DEFAULT_PREFIX : qname.substring(0, colonIndex);
String localName = (colonIndex == -1) ? qname : qname.substring(colonIndex +1);
name = createQName(prefix, localName, prefixResolver);
}
return name;
}
/**
* Create a QName from its internal string representation of the following format:
*
* <code>{namespaceURI}localName</code>
*
* @param qname the string representation of the QName
* @return the QName
* @throws IllegalArgumentException
* @throws InvalidQNameException
*/
public static QName createQName(String qname)
throws InvalidQNameException
{
if (qname == null || qname.length() == 0)
{
throw new InvalidQNameException("Argument qname is mandatory");
}
String namespaceURI = null;
String localName = null;
// Parse namespace
int namespaceBegin = qname.indexOf(NAMESPACE_BEGIN);
int namespaceEnd = -1;
if (namespaceBegin != -1)
{
if (namespaceBegin != 0)
{
throw new InvalidQNameException("QName '" + qname + "' must start with a namespaceURI");
}
namespaceEnd = qname.indexOf(NAMESPACE_END, namespaceBegin + 1);
if (namespaceEnd == -1)
{
throw new InvalidQNameException("QName '" + qname + "' is missing the closing namespace " + NAMESPACE_END + " token");
}
namespaceURI = qname.substring(namespaceBegin + 1, namespaceEnd);
}
// Parse name
localName = qname.substring(namespaceEnd + 1);
if (localName == null || localName.length() == 0)
{
throw new InvalidQNameException("QName '" + qname + "' must consist of a local name");
}
// Construct QName
return new QName(namespaceURI, localName, null);
}
/**
* Create a valid local name from the specified name
*
* @param name name to create valid local name from
* @return valid local name
*/
public static String createValidLocalName(String name)
{
// Validate length
if (name == null || name.length() == 0)
{
throw new IllegalArgumentException("Local name cannot be null or empty.");
}
if (name.length() > MAX_LENGTH)
{
name = name.substring(0, MAX_LENGTH);
}
return name;
}
/**
* Create a QName
*
* @param qname qualified name of the following format <code>prefix:localName</code>
* @return string array where index 0 => prefix and index 1 => local name
*/
public static String[] splitPrefixedQName(String qname)
throws InvalidQNameException, NamespaceException
{
if (qname != null)
{
int colonIndex = qname.indexOf(NAMESPACE_PREFIX);
String prefix = (colonIndex == -1) ? NamespaceService.DEFAULT_PREFIX : qname.substring(0, colonIndex);
String localName = (colonIndex == -1) ? qname : qname.substring(colonIndex +1);
return new String[] { prefix, localName };
}
return null;
}
/**
* Construct QName
*
* @param namespace qualifying namespace (maybe null or empty string)
* @param name qualified name
* @param prefix prefix (maybe null or empty string)
*/
private QName(String namespace, String name, String prefix)
{
this.namespaceURI = (namespace == null) ? NamespaceService.DEFAULT_URI : namespace;
this.prefix = prefix;
this.localName = name;
this.hashCode = 0;
}
@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
/**
* Gets the name
*
* @return the name
*/
public String getLocalName()
{
return this.localName;
}
/**
* Gets the namespace
*
* @return the namespace (empty string when not specified, but never null)
*/
public String getNamespaceURI()
{
return this.namespaceURI;
}
/**
* Gets a prefix resolved version of this QName
*
* @param resolver namespace prefix resolver
* @return QName with prefix resolved
*/
public QName getPrefixedQName(NamespacePrefixResolver resolver)
{
Collection<String> prefixes = resolver.getPrefixes(namespaceURI);
if (prefixes.size() == 0)
{
throw new NamespaceException("A namespace prefix is not registered for uri " + namespaceURI);
}
String resolvedPrefix = prefixes.iterator().next();
if (prefix != null && prefix.equals(resolvedPrefix))
{
return this;
}
return new QName(namespaceURI, localName, resolvedPrefix);
}
/**
* Two QNames are equal only when both their name and namespace match.
*
* Note: The prefix is ignored during the comparison.
*/
public boolean equals(Object object)
{
if (this == object)
{
return true;
}
else if (object == null)
{
return false;
}
if (object instanceof QName)
{
QName other = (QName) object;
// namespaceURI and localname are not allowed to be null
return (this.namespaceURI.equals(other.namespaceURI) &&
this.localName.equals(other.localName));
}
else
{
return false;
}
}
/**
* Performs a direct comparison between qnames.
*
* @see #equals(Object)
*/
public boolean isMatch(QName qname)
{
return this.equals(qname);
}
/**
* Calculate hashCode. Follows pattern used by String where hashCode is
* cached (QName is immutable).
*/
public int hashCode()
{
if (this.hashCode == 0)
{
// the hashcode assignment is atomic - it is only an integer
this.hashCode = ((37 * localName.hashCode()) + namespaceURI.hashCode());
}
return this.hashCode;
}
/**
* Render string representation of QName using format:
*
* <code>{namespace}name</code>
*
* @return the string representation
*/
public String toString()
{
return NAMESPACE_BEGIN + namespaceURI + NAMESPACE_END + localName;
}
/**
* Render string representation of QName using format:
*
* <code>prefix:name</code>
*
* @return the string representation
*/
public String toPrefixString()
{
return (prefix == null) ? localName : prefix + NAMESPACE_PREFIX + localName;
}
/**
* Render string representation of QName using format:
*
* <code>prefix:name</code>
*
* according to namespace prefix mappings of specified namespace resolver.
*
* @param prefixResolver namespace prefix resolver
*
* @return the string representation
*/
public String toPrefixString(NamespacePrefixResolver prefixResolver)
{
Collection<String> prefixes = prefixResolver.getPrefixes(namespaceURI);
if (prefixes.size() == 0)
{
throw new NamespaceException("A namespace prefix is not registered for uri " + namespaceURI);
}
String prefix = prefixes.iterator().next();
if (prefix.equals(NamespaceService.DEFAULT_PREFIX))
{
return localName;
}
else
{
return prefix + NAMESPACE_PREFIX + localName;
}
}
/**
* Creates a QName representation for the given String. If the String has no namespace the Alfresco namespace is
* added. If the String has a prefix an attempt to resolve the prefix to the full URI will be made.
*
* @param str The string to convert
* @return A QName representation of the given string
*/
public static QName resolveToQName(NamespacePrefixResolver prefixResolver, String str)
{
QName qname = null;
if (str == null && str.length() == 0)
{
throw new IllegalArgumentException("str parameter is mandatory");
}
if (str.charAt(0) == (NAMESPACE_BEGIN))
{
// create QName directly
qname = createQName(str);
}
else if (str.indexOf(NAMESPACE_PREFIX) != -1)
{
// extract the prefix and try and resolve using the
// namespace service
int end = str.indexOf(NAMESPACE_PREFIX);
String prefix = str.substring(0, end);
String localName = str.substring(end + 1);
String uri = prefixResolver.getNamespaceURI(prefix);
if (uri != null)
{
qname = createQName(uri, localName);
}
}
else
{
// there's no namespace so prefix with Alfresco's Content Model
qname = createQName(NamespaceService.CONTENT_MODEL_1_0_URI, str);
}
return qname;
}
/**
* Creates a string representation of a QName for the given string. If the given string already has a namespace,
* either a URL or a prefix, nothing the given string is returned. If it does not have a namespace the Alfresco
* namespace is added.
*
* @param str
* The string to convert
*
* @return A QName String representation of the given string
*/
public static String resolveToQNameString(NamespacePrefixResolver prefixResolver, String str)
{
String result = str;
if (str == null && str.length() == 0)
{
throw new IllegalArgumentException("str parameter is mandatory");
}
if (str.charAt(0) != NAMESPACE_BEGIN && str.indexOf(NAMESPACE_PREFIX) != -1)
{
// get the prefix and resolve to the uri
int end = str.indexOf(NAMESPACE_PREFIX);
String prefix = str.substring(0, end);
String localName = str.substring(end + 1);
String uri = prefixResolver.getNamespaceURI(prefix);
if (uri != null)
{
result = new StringBuilder(64).append(NAMESPACE_BEGIN).append(uri).append(NAMESPACE_END).append(
localName).toString();
}
}
else if (str.charAt(0) != NAMESPACE_BEGIN)
{
// there's no namespace so prefix with Alfresco's Content Model
result = new StringBuilder(64).append(NAMESPACE_BEGIN).append(NamespaceService.CONTENT_MODEL_1_0_URI)
.append(NAMESPACE_END).append(str).toString();
}
return result;
}
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A Map that holds as it's key a QName stored in it's internal String representation.
* Calls to get and put automatically map the key to and from the QName representation.
*
* @author gavinc
*/
public class QNameMap<K,V> implements Map, Cloneable
{
protected static Log logger = LogFactory.getLog(QNameMap.class);
protected Map<String, Object> contents = new HashMap<String, Object>(11, 1.0f);
protected NamespacePrefixResolver resolver = null;
/**
* Constructor
*
* @param resolver Mandatory NamespacePrefixResolver helper
*/
public QNameMap(NamespacePrefixResolver resolver)
{
if (resolver == null)
{
throw new IllegalArgumentException("NamespacePrefixResolver is mandatory.");
}
this.resolver = resolver;
}
/**
* @see java.util.Map#size()
*/
public final int size()
{
return this.contents.size();
}
/**
* @see java.util.Map#isEmpty()
*/
public boolean isEmpty()
{
return this.contents.isEmpty();
}
/**
* @see java.util.Map#containsKey(java.lang.Object)
*/
public boolean containsKey(Object key)
{
return (this.contents.containsKey(QName.resolveToQNameString(resolver, (String)key)));
}
/**
* @see java.util.Map#containsValue(java.lang.Object)
*/
public boolean containsValue(Object value)
{
return this.contents.containsValue(value);
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public Object get(Object key)
{
String qnameKey = QName.resolveToQNameString(resolver, key.toString());
Object obj = this.contents.get(qnameKey);
return obj;
}
/**
* @see java.util.Map#put(K, V)
*/
public Object put(Object key, Object value)
{
return this.contents.put(QName.resolveToQNameString(resolver, (String)key), value);
}
/**
* @see java.util.Map#remove(java.lang.Object)
*/
public Object remove(Object key)
{
return this.contents.remove(QName.resolveToQNameString(resolver, (String)key));
}
/**
* @see java.util.Map#putAll(java.util.Map)
*/
public void putAll(Map t)
{
for (Object key : t.keySet())
{
this.put(key, t.get(key));
}
}
/**
* @see java.util.Map#clear()
*/
public void clear()
{
this.contents.clear();
}
/**
* @see java.util.Map#keySet()
*/
public Set keySet()
{
return this.contents.keySet();
}
/**
* @see java.util.Map#values()
*/
public Collection values()
{
return this.contents.values();
}
/**
* @see java.util.Map#entrySet()
*/
public Set entrySet()
{
return this.contents.entrySet();
}
/**
* Override Object.toString() to provide useful debug output
*/
public String toString()
{
return this.contents.toString();
}
/**
* Shallow copy the map by copying keys and values into a new QNameMap
*/
public Object clone()
{
QNameMap map = new QNameMap(resolver);
map.putAll(this);
return map;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
/**
* Provides pattern matching against {@link org.alfresco.service.namespace.QName qnames}.
* <p>
* Implementations will use different mechanisms to match against the
* {@link org.alfresco.service.namespace.QName#getNamespaceURI() namespace} and
* {@link org.alfresco.service.namespace.QName#getLocalName()() localname}.
*
* @see org.alfresco.service.namespace.QName
*
* @author Derek Hulley
*/
public interface QNamePattern
{
/**
* Checks if the given qualified name matches the pattern represented
* by this instance
*
* @param qname the instance to check
* @return Returns true if the qname matches this pattern
*/
public boolean isMatch(QName qname);
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import junit.framework.TestCase;
/**
* Tests the various implementations of the
* {@link org.alfresco.service.namespace.QNamePattern}.
*
* @author Derek Hulley
*/
public class QNamePatternTest extends TestCase
{
private static final String TEST_NAMESPACE = "http://www.alfresco.org/QNamePatternTest";
QName check1;
QName check2;
QName check3;
public QNamePatternTest(String name)
{
super(name);
}
public void setUp() throws Exception
{
check1 = QName.createQName(null, "ABC");
check2 = QName.createQName(TEST_NAMESPACE, "XYZ");
check3 = QName.createQName(TEST_NAMESPACE, "ABC");
}
public void testSimpleQNamePattern() throws Exception
{
QNamePattern pattern = QName.createQName(TEST_NAMESPACE, "ABC");
// check
assertFalse("Simple match failed: " + check1, pattern.isMatch(check1));
assertFalse("Simple match failed: " + check2, pattern.isMatch(check2));
assertTrue("Simple match failed: " + check3, pattern.isMatch(check3));
}
public void testRegexQNamePatternMatcher() throws Exception
{
QNamePattern pattern = new RegexQNamePattern(".*alfresco.*", "A.?C");
// check
assertFalse("Regex match failed: " + check1, pattern.isMatch(check1));
assertFalse("Regex match failed: " + check2, pattern.isMatch(check2));
assertTrue("Regex match failed: " + check3, pattern.isMatch(check3));
assertTrue("All match failed: " + check1, RegexQNamePattern.MATCH_ALL.isMatch(check1));
assertTrue("All match failed: " + check2, RegexQNamePattern.MATCH_ALL.isMatch(check2));
assertTrue("All match failed: " + check3, RegexQNamePattern.MATCH_ALL.isMatch(check3));
}
}

View File

@@ -0,0 +1,262 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import java.util.Collection;
import java.util.HashSet;
import junit.framework.TestCase;
/**
* @see org.alfresco.service.namespace.QName
*
* @author David Caruana
*/
public class QNameTest extends TestCase
{
public QNameTest(String name)
{
super(name);
}
public void testInvalidQName() throws Exception
{
try
{
QName qname = QName.createQName("");
fail("Missing local name was not caught");
}
catch (InvalidQNameException e)
{
}
try
{
QName qname = QName.createQName("invalid{}name");
fail("Namespace not at start was not caught");
}
catch (InvalidQNameException e)
{
}
try
{
QName qname = QName.createQName("{name");
fail("Missing closing namespace token was not caught");
}
catch (InvalidQNameException e)
{
}
try
{
QName qname = QName.createQName("{}");
fail("Missing local name after namespace was not caught");
}
catch (InvalidQNameException e)
{
}
try
{
QName qname = QName.createQName("{}name");
}
catch (InvalidQNameException e)
{
fail("Empty namespace is valid");
}
try
{
QName qname = QName.createQName("{namespace}name");
assertEquals("namespace", qname.getNamespaceURI());
assertEquals("name", qname.getLocalName());
}
catch (InvalidQNameException e)
{
fail("Valid namespace has been thrown out");
}
try
{
QName qname = QName.createQName((String) null, (String) null);
fail("Null name was not caught");
}
catch (InvalidQNameException e)
{
}
try
{
QName qname = QName.createQName((String) null, "");
fail("Empty name was not caught");
}
catch (InvalidQNameException e)
{
}
}
public void testConstruction()
{
QName qname1 = QName.createQName("namespace1", "name1");
assertEquals("namespace1", qname1.getNamespaceURI());
assertEquals("name1", qname1.getLocalName());
QName qname2 = QName.createQName("{namespace2}name2");
assertEquals("namespace2", qname2.getNamespaceURI());
assertEquals("name2", qname2.getLocalName());
QName qname3 = QName.createQName(null, "name3");
assertEquals("", qname3.getNamespaceURI());
QName qname4 = QName.createQName("", "name4");
assertEquals("", qname4.getNamespaceURI());
QName qname5 = QName.createQName("{}name5");
assertEquals("", qname5.getNamespaceURI());
QName qname6 = QName.createQName("name6");
assertEquals("", qname6.getNamespaceURI());
}
public void testStringRepresentation()
{
QName qname1 = QName.createQName("namespace", "name1");
assertEquals("{namespace}name1", qname1.toString());
QName qname2 = QName.createQName("", "name2");
assertEquals("{}name2", qname2.toString());
QName qname3 = QName.createQName("{namespace}name3");
assertEquals("{namespace}name3", qname3.toString());
QName qname4 = QName.createQName("{}name4");
assertEquals("{}name4", qname4.toString());
QName qname5 = QName.createQName("name5");
assertEquals("{}name5", qname5.toString());
}
public void testEquality()
{
QName qname1 = QName.createQName("namespace", "name");
QName qname2 = QName.createQName("namespace", "name");
QName qname3 = QName.createQName("{namespace}name");
assertEquals(qname1, qname2);
assertEquals(qname1, qname3);
assertEquals(qname1.hashCode(), qname2.hashCode());
assertEquals(qname1.hashCode(), qname3.hashCode());
QName qname4 = QName.createQName("", "name");
QName qname5 = QName.createQName("", "name");
QName qname6 = QName.createQName(null, "name");
assertEquals(qname4, qname5);
assertEquals(qname4, qname6);
assertEquals(qname4.hashCode(), qname5.hashCode());
assertEquals(qname4.hashCode(), qname6.hashCode());
QName qname7 = QName.createQName("namespace", "name");
QName qname8 = QName.createQName("namespace", "differentname");
assertFalse(qname7.equals(qname8));
assertFalse(qname7.hashCode() == qname8.hashCode());
QName qname9 = QName.createQName("namespace", "name");
QName qname10 = QName.createQName("differentnamespace", "name");
assertFalse(qname9.equals(qname10));
assertFalse(qname9.hashCode() == qname10.hashCode());
}
public void testPrefix()
{
try
{
QName noResolver = QName.createQName(NamespaceService.ALFRESCO_PREFIX, "alfresco prefix", null);
fail("Null resolver was not caught");
}
catch (IllegalArgumentException e)
{
}
NamespacePrefixResolver mockResolver = new MockNamespacePrefixResolver();
QName qname1 = QName.createQName(NamespaceService.ALFRESCO_PREFIX, "alfresco prefix", mockResolver);
assertEquals(NamespaceService.ALFRESCO_URI, qname1.getNamespaceURI());
QName qname2 = QName.createQName("", "default prefix", mockResolver);
assertEquals(NamespaceService.DEFAULT_URI, qname2.getNamespaceURI());
QName qname3 = QName.createQName(null, "null default prefix", mockResolver);
assertEquals(NamespaceService.DEFAULT_URI, qname3.getNamespaceURI());
try
{
QName qname4 = QName.createQName("garbage", "garbage prefix", mockResolver);
fail("Invalid Prefix was not caught");
}
catch (NamespaceException e)
{
}
}
private static class MockNamespacePrefixResolver
implements NamespacePrefixResolver
{
public String getNamespaceURI(String prefix)
{
if (prefix.equals(NamespaceService.DEFAULT_PREFIX))
{
return NamespaceService.DEFAULT_URI;
}
else if (prefix.equals(NamespaceService.ALFRESCO_PREFIX))
{
return NamespaceService.ALFRESCO_URI;
}
throw new NamespaceException("Prefix " + prefix + " not registered");
}
public Collection<String> getPrefixes(String namespaceURI)
{
throw new NamespaceException("URI " + namespaceURI + " not registered");
}
public Collection<String> getPrefixes()
{
HashSet<String> prefixes = new HashSet<String>();
prefixes.add(NamespaceService.DEFAULT_PREFIX);
prefixes.add(NamespaceService.ALFRESCO_PREFIX);
return prefixes;
}
public Collection<String> getURIs()
{
HashSet<String> uris = new HashSet<String>();
uris.add(NamespaceService.DEFAULT_URI);
uris.add(NamespaceService.ALFRESCO_URI);
return uris;
}
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.service.namespace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Provides matching between {@link org.alfresco.service.namespace.QName qnames} using
* regular expression matching.
* <p>
* A simple {@link #MATCH_ALL convenience} pattern matcher is also provided that
* will match any qname.
*
* @see java.lang.String#matches(java.lang.String)
*
* @author Derek Hulley
*/
public class RegexQNamePattern implements QNamePattern
{
private static final Log logger = LogFactory.getLog(RegexQNamePattern.class);
/** A helper pattern matcher that will match <i>all</i> qnames */
public static final QNamePattern MATCH_ALL = new QNamePattern()
{
public boolean isMatch(QName qname)
{
return true;
}
};
private String namespaceUriPattern;
private String localNamePattern;
private String combinedPattern;
/**
* @param namespaceUriPattern a regex pattern that will be applied to the namespace URI
* @param localNamePattern a regex pattern that will be applied to the local name
*/
public RegexQNamePattern(String namespaceUriPattern, String localNamePattern)
{
this.namespaceUriPattern = namespaceUriPattern;
this.localNamePattern = localNamePattern;
this.combinedPattern = null;
}
/**
* @param combinedPattern a regex pattern that will be applied to the full qname
* string representation
*
* @see QName#toString()
*/
public RegexQNamePattern(String combinedPattern)
{
this.combinedPattern = combinedPattern;
this.namespaceUriPattern = null;
this.localNamePattern = null;
}
public String toString()
{
StringBuilder sb = new StringBuilder(56);
sb.append("RegexQNamePattern[");
if (combinedPattern != null)
{
sb.append(" pattern=").append(combinedPattern);
}
else
{
sb.append(" uri=").append(namespaceUriPattern);
sb.append(", localname=").append(namespaceUriPattern);
}
sb.append(" ]");
return sb.toString();
}
/**
* @param qname the value to check against this pattern
* @return Returns true if the regex pattern provided match thos of the provided qname
*/
public boolean isMatch(QName qname)
{
boolean match = false;
if (combinedPattern != null)
{
String qnameStr = qname.toString();
match = qnameStr.matches(combinedPattern);
}
else
{
match = (qname.getNamespaceURI().matches(namespaceUriPattern) &&
qname.getLocalName().matches(localNamePattern));
}
// done
if (logger.isDebugEnabled())
{
logger.debug("QName matching: \n" +
" matcher: " + this + "\n" +
" qname: " + qname + "\n" +
" result: " + match);
}
return match;
}
}