
com.squeakysand.jcr.Name Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2010-2011 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.squeakysand.jcr;
import com.squeakysand.commons.lang.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Name {
private static final Logger LOG = LoggerFactory.getLogger(Name.class);
private static final Pattern NAMESPACE_PATTERN = Pattern.compile("\\{(.*)\\}(.+)");
private static final Pattern PREFIX_PATTERN = Pattern.compile("([^\\{\\}]*):(.+)");
private String name;
private String namespace;
private String prefix;
private String baseName;
private boolean containsNamespace;
private boolean containsPrefix;
public Name(String name, NamespaceRegistry registry) {
if (StringUtils.isNullOrEmpty(name)) {
throw new IllegalArgumentException("name cannot be null or empty");
}
if (registry == null) {
throw new IllegalArgumentException("registry cannot be null");
}
this.name = name;
try {
Matcher namespaceMatcher = NAMESPACE_PATTERN.matcher(name);
Matcher prefixMatcher = PREFIX_PATTERN.matcher(name);
if (namespaceMatcher.matches()) {
containsNamespace = true;
namespace = namespaceMatcher.group(1);
baseName = namespaceMatcher.group(2);
prefix = registry.getPrefix(namespace);
} else if (prefixMatcher.matches()) {
containsPrefix = true;
prefix = prefixMatcher.group(1);
baseName = prefixMatcher.group(2);
namespace = registry.getURI(prefix);
}
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} - namespace: {}, prefix: {}, base: {}", new Object[] {name, namespace, prefix, baseName});
}
}
public boolean containsNamespace() {
return containsNamespace;
}
public boolean containsPrefix() {
return containsPrefix;
}
public String getNamespace() {
return namespace;
}
public String getPrefix() {
return prefix;
}
public String getBaseName() {
return baseName;
}
public String getSimpleName() {
return String.format("%s:%s", getPrefix(), getBaseName());
}
public String getQualifiedName() {
return String.format("{%s}%s", getNamespace(), getBaseName());
}
@Override
public String toString() {
return name;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy