2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.common;
10 import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat;
11 import java.io.Serializable;
13 import java.net.URISyntaxException;
14 import java.text.ParseException;
15 import java.util.Date;
16 import java.util.Objects;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.objcache.ObjectCache;
21 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
24 * The QName from XML consists of local name of element and XML namespace, but
25 * for our use, we added module revision to it.
27 * In YANG context QName is full name of defined node, type, procedure or
28 * notification. QName consists of XML namespace, YANG model revision and local
29 * name of defined type. It is used to prevent name clashes between nodes with
30 * same local name, but from different schemas.
33 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
34 * defined element, type, procedure or notification.</li>
35 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
37 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
38 * node in the YANG module</li>
41 * QName may also have <code>prefix</code> assigned, but prefix does not
42 * affect equality and identity of two QNames and carry only information
43 * which may be useful for serializers / deserializers.
47 public final class QName implements Immutable, Serializable, Comparable<QName> {
48 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
49 private static final long serialVersionUID = 5398411242927766414L;
51 static final String QNAME_REVISION_DELIMITER = "?revision=";
52 static final String QNAME_LEFT_PARENTHESIS = "(";
53 static final String QNAME_RIGHT_PARENTHESIS = ")";
55 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
57 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
58 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
59 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
62 private final QNameModule module;
64 private final String localName;
66 private QName(final QNameModule module, final String localName) {
67 this.localName = checkLocalName(localName);
72 * Look up specified QName in the global cache and return a shared reference.
74 * @param qname QName instance
75 * @return Cached instance, according to {@link ObjectCache} policy.
77 public static QName cachedReference(final QName qname) {
78 // We also want to make sure we keep the QNameModule cached
79 final QNameModule myMod = qname.getModule();
80 final QNameModule cacheMod = QNameModule.cachedReference(myMod);
83 if (cacheMod == myMod) {
86 what = QName.create(cacheMod, qname.localName);
89 return CACHE.getReference(what);
96 * the namespace assigned to the YANG module
98 * YANG schema identifier
100 public QName(final URI namespace, final String localName) {
101 this(QNameModule.create(namespace, null), localName);
104 private static String checkLocalName(final String localName) {
105 if (localName == null) {
106 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
108 if (localName.length() == 0) {
109 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
112 for (final char c : ILLEGAL_CHARACTERS) {
113 if (localName.indexOf(c) != -1) {
114 throw new IllegalArgumentException(String.format(
115 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
121 public static QName create(final String input) {
122 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
123 if (matcher.matches()) {
124 final String namespace = matcher.group(1);
125 final String revision = matcher.group(2);
126 final String localName = matcher.group(3);
127 return create(namespace, revision, localName);
129 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
130 if (matcher.matches()) {
131 final URI namespace = URI.create(matcher.group(1));
132 final String localName = matcher.group(2);
133 return new QName(namespace, localName);
135 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
136 if (matcher.matches()) {
137 final String localName = matcher.group(1);
138 return new QName((URI) null, localName);
140 throw new IllegalArgumentException("Invalid input:" + input);
144 * Get the module component of the QName.
146 * @return Module component
148 public QNameModule getModule() {
153 * Returns XMLNamespace assigned to the YANG module.
155 * @return XMLNamespace assigned to the YANG module.
157 public URI getNamespace() {
158 return module.getNamespace();
162 * Returns YANG schema identifier which were defined for this node in the
165 * @return YANG schema identifier which were defined for this node in the
168 public String getLocalName() {
173 * Returns revision of the YANG module if the module has defined revision,
174 * otherwise returns <code>null</code>
176 * @return revision of the YANG module if the module has defined revision,
177 * otherwise returns <code>null</code>
179 public Date getRevision() {
180 return module.getRevision();
184 public int hashCode() {
185 final int prime = 31;
187 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
188 result = prime * result + module.hashCode();
194 * Compares the specified object with this list for equality. Returns
195 * <tt>true</tt> if and only if the specified object is also instance of
196 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
197 * {@link #getRevision()} are equals to same properties of this instance.
199 * @param obj the object to be compared for equality with this QName
200 * @return <tt>true</tt> if the specified object is equal to this QName
204 public boolean equals(final Object obj) {
208 if (!(obj instanceof QName)) {
211 final QName other = (QName) obj;
212 if (localName == null) {
213 if (other.localName != null) {
216 } else if (!localName.equals(other.localName)) {
219 return module.equals(other.module);
222 public static QName create(final QName base, final String localName) {
223 return create(base.getModule(), localName);
230 * Namespace and revision enclosed as a QNameModule
232 * Local name part of QName. MUST NOT BE null.
233 * @return Instance of QName
235 public static QName create(final QNameModule qnameModule, final String localName) {
236 if (qnameModule == null) {
237 throw new NullPointerException("module may not be null");
239 return new QName(qnameModule, localName);
246 * Namespace of QName or null if namespace is undefined.
248 * Revision of namespace or null if revision is unspecified.
250 * Local name part of QName. MUST NOT BE null.
251 * @return Instance of QName
253 public static QName create(final URI namespace, final Date revision, final String localName) {
254 return create(QNameModule.create(namespace, revision), localName);
262 * Namespace of QName, MUST NOT BE Null.
264 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
265 * in format <code>YYYY-mm-dd</code>.
267 * Local name part of QName. MUST NOT BE null.
269 * @throws NullPointerException
270 * If any of parameters is null.
271 * @throws IllegalArgumentException
272 * If <code>namespace</code> is not valid URI or
273 * <code>revision</code> is not according to format
274 * <code>YYYY-mm-dd</code>.
276 public static QName create(final String namespace, final String revision, final String localName) {
277 final URI namespaceUri = parseNamespace(namespace);
278 final Date revisionDate = parseRevision(revision);
279 return create(namespaceUri, revisionDate, localName);
282 private static URI parseNamespace(final String namespace) {
284 return new URI(namespace);
285 } catch (final URISyntaxException ue) {
286 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
294 * Namespace of QName, MUST NOT BE Null.
296 * Local name part of QName. MUST NOT BE null.
298 * @throws NullPointerException
299 * If any of parameters is null.
300 * @throws IllegalArgumentException
301 * If <code>namespace</code> is not valid URI.
303 public static QName create(final String namespace, final String localName) {
304 return create(parseNamespace(namespace), null, localName);
308 public String toString() {
309 final StringBuilder sb = new StringBuilder();
310 if (getNamespace() != null) {
311 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
313 if (getFormattedRevision() != null) {
314 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
316 sb.append(QNAME_RIGHT_PARENTHESIS);
318 sb.append(localName);
319 return sb.toString();
323 * Return string representation of revision in format
324 * <code>YYYY-mm-dd</code>
326 * YANG Specification defines format for <code>revision</code> as
327 * YYYY-mm-dd. This format for revision is reused accross multiple places
328 * such as capabilities URI, YANG modules, etc.
330 * @return String representation of revision or null, if revision is not
333 public String getFormattedRevision() {
334 return module.getFormattedRevision();
338 * Creates copy of this with revision and prefix unset.
340 * @return copy of this QName with revision and prefix unset.
342 public QName withoutRevision() {
343 return create(getNamespace(), null, localName);
346 public static Date parseRevision(final String formatedDate) {
348 return getRevisionFormat().parse(formatedDate);
349 } catch (ParseException | RuntimeException e) {
350 throw new IllegalArgumentException(
351 String.format("Revision '%s'is not in a supported format", formatedDate), e);
356 * Formats {@link Date} representing revision to format
357 * <code>YYYY-mm-dd</code>
359 * YANG Specification defines format for <code>revision</code> as
360 * YYYY-mm-dd. This format for revision is reused accross multiple places
361 * such as capabilities URI, YANG modules, etc.
364 * Date object to format or null
365 * @return String representation or null if the input was null.
367 public static String formattedRevision(final Date revision) {
368 if (revision == null) {
371 return getRevisionFormat().format(revision);
376 * Compares this QName to other, without comparing revision.
378 * Compares instance of this to other instance of QName and returns true if
379 * both instances have equal <code>localName</code> ({@link #getLocalName()}
380 * ) and <code>namespace</code> ({@link #getNamespace()}).
383 * Other QName. Must not be null.
384 * @return true if this instance and other have equals localName and
386 * @throws NullPointerException
387 * if <code>other</code> is null.
389 public boolean isEqualWithoutRevision(final QName other) {
390 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
394 public int compareTo(final QName other) {
395 // compare mandatory localName parameter
396 int result = localName.compareTo(other.localName);
401 // compare nullable namespace parameter
402 if (getNamespace() == null) {
403 if (other.getNamespace() != null) {
407 if (other.getNamespace() == null) {
410 result = getNamespace().compareTo(other.getNamespace());
416 // compare nullable revision parameter
417 if (getRevision() == null) {
418 if (other.getRevision() != null) {
422 if (other.getRevision() == null) {
425 result = getRevision().compareTo(other.getRevision());