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 com.google.common.base.Preconditions;
12 import com.google.common.collect.Interner;
13 import com.google.common.collect.Interners;
14 import java.io.Serializable;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import java.util.Date;
19 import java.util.Objects;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.objcache.ObjectCache;
24 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
27 * The QName from XML consists of local name of element and XML namespace, but
28 * for our use, we added module revision to it.
30 * In YANG context QName is full name of defined node, type, procedure or
31 * notification. QName consists of XML namespace, YANG model revision and local
32 * name of defined type. It is used to prevent name clashes between nodes with
33 * same local name, but from different schemas.
36 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
37 * defined element, type, procedure or notification.</li>
38 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
40 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
41 * node in the YANG module</li>
44 * QName may also have <code>prefix</code> assigned, but prefix does not
45 * affect equality and identity of two QNames and carry only information
46 * which may be useful for serializers / deserializers.
50 public final class QName implements Immutable, Serializable, Comparable<QName> {
51 private static final Interner<QName> INTERNER = Interners.newWeakInterner();
52 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
53 private static final long serialVersionUID = 5398411242927766414L;
55 static final String QNAME_REVISION_DELIMITER = "?revision=";
56 static final String QNAME_LEFT_PARENTHESIS = "(";
57 static final String QNAME_RIGHT_PARENTHESIS = ")";
59 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
61 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
62 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
63 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
66 private final QNameModule module;
68 private final String localName;
70 private QName(final QNameModule module, final String localName) {
71 this.localName = checkLocalName(localName);
76 * Look up specified QName in the global cache and return a shared reference.
78 * @param qname QName instance
79 * @return Cached instance, according to {@link ObjectCache} policy.
81 * @deprecated Use {@link #intern()} instead.
84 public static QName cachedReference(final QName qname) {
85 // We also want to make sure we keep the QNameModule cached
86 final QNameModule myMod = qname.getModule();
87 final QNameModule cacheMod = QNameModule.cachedReference(myMod);
90 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname'
91 // into cache. It is important that it does not hold user-supplied reference (such a String instance from
93 if (cacheMod == myMod) {
96 what = QName.create(cacheMod, qname.localName);
99 return CACHE.getReference(what);
106 * the namespace assigned to the YANG module
108 * YANG schema identifier
110 public QName(final URI namespace, final String localName) {
111 this(QNameModule.create(namespace, null), localName);
114 private static String checkLocalName(final String localName) {
115 if (localName == null) {
116 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
118 if (localName.length() == 0) {
119 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
122 for (final char c : ILLEGAL_CHARACTERS) {
123 if (localName.indexOf(c) != -1) {
124 throw new IllegalArgumentException(String.format(
125 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
131 public static QName create(final String input) {
132 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
133 if (matcher.matches()) {
134 final String namespace = matcher.group(1);
135 final String revision = matcher.group(2);
136 final String localName = matcher.group(3);
137 return create(namespace, revision, localName);
139 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
140 if (matcher.matches()) {
141 final URI namespace = URI.create(matcher.group(1));
142 final String localName = matcher.group(2);
143 return new QName(namespace, localName);
145 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
146 if (matcher.matches()) {
147 final String localName = matcher.group(1);
148 return new QName((URI) null, localName);
150 throw new IllegalArgumentException("Invalid input:" + input);
154 * Get the module component of the QName.
156 * @return Module component
158 public QNameModule getModule() {
163 * Returns XMLNamespace assigned to the YANG module.
165 * @return XMLNamespace assigned to the YANG module.
167 public URI getNamespace() {
168 return module.getNamespace();
172 * Returns YANG schema identifier which were defined for this node in the
175 * @return YANG schema identifier which were defined for this node in the
178 public String getLocalName() {
183 * Returns revision of the YANG module if the module has defined revision,
184 * otherwise returns <code>null</code>
186 * @return revision of the YANG module if the module has defined revision,
187 * otherwise returns <code>null</code>
189 public Date getRevision() {
190 return module.getRevision();
194 * Return an interned reference to a equivalent QName.
196 * @return Interned reference, or this object if it was interned.
198 public QName intern() {
199 // We also want to make sure we keep the QNameModule cached
200 final QNameModule cacheMod = module.intern();
202 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
203 // interner. It is important that it does not hold user-supplied reference (such a String instance from
204 // parsing of an XML document).
205 final QName template = cacheMod == module ? this : QName.create(cacheMod, localName);
207 return INTERNER.intern(template);
211 public int hashCode() {
212 final int prime = 31;
214 result = prime * result + Objects.hashCode(localName);
215 result = prime * result + module.hashCode();
221 * Compares the specified object with this list for equality. Returns
222 * <tt>true</tt> if and only if the specified object is also instance of
223 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
224 * {@link #getRevision()} are equals to same properties of this instance.
226 * @param obj the object to be compared for equality with this QName
227 * @return <tt>true</tt> if the specified object is equal to this QName
231 public boolean equals(final Object obj) {
235 if (!(obj instanceof QName)) {
238 final QName other = (QName) obj;
239 return Objects.equals(localName, other.localName) && module.equals(other.module);
242 public static QName create(final QName base, final String localName) {
243 return create(base.getModule(), localName);
250 * Namespace and revision enclosed as a QNameModule
252 * Local name part of QName. MUST NOT BE null.
253 * @return Instance of QName
255 public static QName create(final QNameModule qnameModule, final String localName) {
256 return new QName(Preconditions.checkNotNull(qnameModule,"module may not be null"), localName);
263 * Namespace of QName or null if namespace is undefined.
265 * Revision of namespace or null if revision is unspecified.
267 * Local name part of QName. MUST NOT BE null.
268 * @return Instance of QName
270 public static QName create(final URI namespace, final Date revision, final String localName) {
271 return create(QNameModule.create(namespace, revision), localName);
279 * Namespace of QName, MUST NOT BE Null.
281 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
282 * in format <code>YYYY-mm-dd</code>.
284 * Local name part of QName. MUST NOT BE null.
286 * @throws NullPointerException
287 * If any of parameters is null.
288 * @throws IllegalArgumentException
289 * If <code>namespace</code> is not valid URI or
290 * <code>revision</code> is not according to format
291 * <code>YYYY-mm-dd</code>.
293 public static QName create(final String namespace, final String revision, final String localName) {
294 final URI namespaceUri = parseNamespace(namespace);
295 final Date revisionDate = parseRevision(revision);
296 return create(namespaceUri, revisionDate, localName);
299 private static URI parseNamespace(final String namespace) {
301 return new URI(namespace);
302 } catch (final URISyntaxException ue) {
303 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
311 * Namespace of QName, MUST NOT BE Null.
313 * Local name part of QName. MUST NOT BE null.
315 * @throws NullPointerException
316 * If any of parameters is null.
317 * @throws IllegalArgumentException
318 * If <code>namespace</code> is not valid URI.
320 public static QName create(final String namespace, final String localName) {
321 return create(parseNamespace(namespace), null, localName);
325 public String toString() {
326 final StringBuilder sb = new StringBuilder();
327 if (getNamespace() != null) {
328 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
330 if (getFormattedRevision() != null) {
331 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
333 sb.append(QNAME_RIGHT_PARENTHESIS);
335 sb.append(localName);
336 return sb.toString();
340 * Return string representation of revision in format
341 * <code>YYYY-mm-dd</code>
343 * YANG Specification defines format for <code>revision</code> as
344 * YYYY-mm-dd. This format for revision is reused accross multiple places
345 * such as capabilities URI, YANG modules, etc.
347 * @return String representation of revision or null, if revision is not
350 public String getFormattedRevision() {
351 return module.getFormattedRevision();
355 * Creates copy of this with revision and prefix unset.
357 * @return copy of this QName with revision and prefix unset.
359 public QName withoutRevision() {
360 return create(getNamespace(), null, localName);
363 public static Date parseRevision(final String formatedDate) {
365 return getRevisionFormat().parse(formatedDate);
366 } catch (ParseException | RuntimeException e) {
367 throw new IllegalArgumentException(
368 String.format("Revision '%s'is not in a supported format", formatedDate), e);
373 * Formats {@link Date} representing revision to format
374 * <code>YYYY-mm-dd</code>
376 * YANG Specification defines format for <code>revision</code> as
377 * YYYY-mm-dd. This format for revision is reused accross multiple places
378 * such as capabilities URI, YANG modules, etc.
381 * Date object to format or null
382 * @return String representation or null if the input was null.
384 public static String formattedRevision(final Date revision) {
385 if (revision == null) {
388 return getRevisionFormat().format(revision);
393 * Compares this QName to other, without comparing revision.
395 * Compares instance of this to other instance of QName and returns true if
396 * both instances have equal <code>localName</code> ({@link #getLocalName()}
397 * ) and <code>namespace</code> ({@link #getNamespace()}).
400 * Other QName. Must not be null.
401 * @return true if this instance and other have equals localName and
403 * @throws NullPointerException
404 * if <code>other</code> is null.
406 public boolean isEqualWithoutRevision(final QName other) {
407 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
411 public int compareTo(final QName other) {
412 // compare mandatory localName parameter
413 int result = localName.compareTo(other.localName);
418 // compare nullable namespace parameter
419 if (getNamespace() == null) {
420 if (other.getNamespace() != null) {
424 if (other.getNamespace() == null) {
427 result = getNamespace().compareTo(other.getNamespace());
433 // compare nullable revision parameter
434 if (getRevision() == null) {
435 if (other.getRevision() != null) {
439 if (other.getRevision() == null) {
442 result = getRevision().compareTo(other.getRevision());