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;
12 import java.io.Serializable;
14 import java.net.URISyntaxException;
15 import java.text.ParseException;
16 import java.util.Date;
17 import java.util.Objects;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 import org.opendaylight.yangtools.concepts.Immutable;
22 import org.opendaylight.yangtools.objcache.ObjectCache;
23 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
26 * The QName from XML consists of local name of element and XML namespace, but
27 * for our use, we added module revision to it.
29 * In YANG context QName is full name of defined node, type, procedure or
30 * notification. QName consists of XML namespace, YANG model revision and local
31 * name of defined type. It is used to prevent name clashes between nodes with
32 * same local name, but from different schemas.
35 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
36 * defined element, type, procedure or notification.</li>
37 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
39 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
40 * node in the YANG module</li>
43 * QName may also have <code>prefix</code> assigned, but prefix does not
44 * affect equality and identity of two QNames and carry only information
45 * which may be useful for serializers / deserializers.
49 public final class QName implements Immutable, Serializable, Comparable<QName> {
50 private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
51 private static final long serialVersionUID = 5398411242927766414L;
53 static final String QNAME_REVISION_DELIMITER = "?revision=";
54 static final String QNAME_LEFT_PARENTHESIS = "(";
55 static final String QNAME_RIGHT_PARENTHESIS = ")";
57 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
59 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
60 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
61 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
64 private final QNameModule module;
66 private final String localName;
68 private final String prefix;
70 private QName(final QNameModule module, final String prefix, final String localName) {
71 this.localName = checkLocalName(localName);
77 * Look up specified QName in the global cache and return a shared reference.
79 * @param module QName instance
80 * @return Cached instance, according to {@link ObjectCache} policy.
82 public static QName cachedReference(final QName qname) {
83 // We also want to make sure we keep the QNameModule cached
84 final QNameModule myMod = qname.getModule();
85 final QNameModule cacheMod = QNameModule.cachedReference(myMod);
88 if (cacheMod == myMod) {
91 what = QName.create(cacheMod, qname.localName);
94 return CACHE.getReference(what);
101 * the namespace assigned to the YANG module
103 * the revision of the YANG module
105 * locally defined prefix assigned to local name
107 * YANG schema identifier
109 * @deprecated Prefix storage in QNames is deprecated.
112 public QName(final URI namespace, final Date revision, final String prefix, final String localName) {
113 this(QNameModule.create(namespace, revision), prefix, localName);
120 * the namespace assigned to the YANG module
122 * YANG schema identifier
124 public QName(final URI namespace, final String localName) {
125 this(namespace, null, "", localName);
128 private static String checkLocalName(final String localName) {
129 if (localName == null) {
130 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
132 if (localName.length() == 0) {
133 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
136 for (char c : ILLEGAL_CHARACTERS) {
137 if (localName.indexOf(c) != -1) {
138 throw new IllegalArgumentException(String.format(
139 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
145 public static QName create(final String input) {
146 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
147 if (matcher.matches()) {
148 String namespace = matcher.group(1);
149 String revision = matcher.group(2);
150 String localName = matcher.group(3);
151 return create(namespace, revision, localName);
153 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
154 if (matcher.matches()) {
155 URI namespace = URI.create(matcher.group(1));
156 String localName = matcher.group(2);
157 return new QName(namespace, localName);
159 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
160 if (matcher.matches()) {
161 String localName = matcher.group(1);
162 return new QName((URI) null, localName);
164 throw new IllegalArgumentException("Invalid input:" + input);
168 * Get the module component of the QName.
170 * @return Module component
172 public QNameModule getModule() {
177 * Returns XMLNamespace assigned to the YANG module.
179 * @return XMLNamespace assigned to the YANG module.
181 public URI getNamespace() {
182 return module.getNamespace();
186 * Returns YANG schema identifier which were defined for this node in the
189 * @return YANG schema identifier which were defined for this node in the
192 public String getLocalName() {
197 * Returns revision of the YANG module if the module has defined revision,
198 * otherwise returns <code>null</code>
200 * @return revision of the YANG module if the module has defined revision,
201 * otherwise returns <code>null</code>
203 public Date getRevision() {
204 return module.getRevision();
208 * Returns locally defined prefix assigned to local name
210 * @return locally defined prefix assigned to local name
212 * @deprecated Prefix storage in QNames is deprecated.
215 public String getPrefix() {
220 public int hashCode() {
221 final int prime = 31;
223 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
224 result = prime * result + module.hashCode();
230 * Compares the specified object with this list for equality. Returns
231 * <tt>true</tt> if and only if the specified object is also instance of
232 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
233 * {@link #getRevision()} are equals to same properties of this instance.
235 * @param o the object to be compared for equality with this QName
236 * @return <tt>true</tt> if the specified object is equal to this QName
240 public boolean equals(final Object obj) {
244 if (!(obj instanceof QName)) {
247 final QName other = (QName) obj;
248 if (localName == null) {
249 if (other.localName != null) {
252 } else if (!localName.equals(other.localName)) {
255 return module.equals(other.module);
258 public static QName create(final QName base, final String localName) {
259 return create(base.getModule(), base.getPrefix(), localName);
266 * Namespace and revision enclosed as a QNameModule
270 * Local name part of QName. MUST NOT BE null.
271 * @return Instance of QName
273 * @deprecated Prefix storage in QNames is deprecated.
276 public static QName create(final QNameModule module, final String prefix, final String localName) {
277 if (module == null) {
278 throw new NullPointerException("module may not be null");
280 return new QName(module, prefix, localName);
287 * Namespace and revision enclosed as a QNameModule
289 * Local name part of QName. MUST NOT BE null.
290 * @return Instance of QName
292 public static QName create(final QNameModule qnameModule, final String localName) {
293 return create(qnameModule, null, localName);
300 * Namespace of QName or null if namespace is undefined.
302 * Revision of namespace or null if revision is unspecified.
304 * Local name part of QName. MUST NOT BE null.
305 * @return Instance of QName
307 public static QName create(final URI namespace, final Date revision, final String localName) {
308 return create(QNameModule.create(namespace, revision), null, localName);
316 * Namespace of QName, MUST NOT BE Null.
318 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
319 * in format <code>YYYY-mm-dd</code>.
321 * Local name part of QName. MUST NOT BE null.
323 * @throws NullPointerException
324 * If any of paramaters is null.
325 * @throws IllegalArgumentException
326 * If <code>namespace</code> is not valid URI or
327 * <code>revision</code> is not according to format
328 * <code>YYYY-mm-dd</code>.
330 public static QName create(final String namespace, final String revision, final String localName)
331 throws IllegalArgumentException {
332 final URI namespaceUri;
334 namespaceUri = new URI(namespace);
335 } catch (URISyntaxException ue) {
336 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
339 Date revisionDate = parseRevision(revision);
340 return create(namespaceUri, revisionDate, localName);
344 public String toString() {
345 StringBuilder sb = new StringBuilder();
346 if (getNamespace() != null) {
347 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
349 if (getFormattedRevision() != null) {
350 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
352 sb.append(QNAME_RIGHT_PARENTHESIS);
354 sb.append(localName);
355 return sb.toString();
359 * Return string representation of revision in format
360 * <code>YYYY-mm-dd</code>
362 * YANG Specification defines format for <code>revision</code> as
363 * YYYY-mm-dd. This format for revision is reused accross multiple places
364 * such as capabilities URI, YANG modules, etc.
366 * @return String representation of revision or null, if revision is not
369 public String getFormattedRevision() {
370 return module.getFormattedRevision();
374 * Creates copy of this with revision and prefix unset.
376 * @return copy of this QName with revision and prefix unset.
378 public QName withoutRevision() {
379 return create(getNamespace(), null, localName);
382 public static Date parseRevision(final String formatedDate) {
384 return getRevisionFormat().parse(formatedDate);
385 } catch (ParseException | RuntimeException e) {
386 throw new IllegalArgumentException(
387 String.format("Revision '%s'is not in a supported format", formatedDate), e);
392 * Formats {@link Date} representing revision to format
393 * <code>YYYY-mm-dd</code>
395 * YANG Specification defines format for <code>revision</code> as
396 * YYYY-mm-dd. This format for revision is reused accross multiple places
397 * such as capabilities URI, YANG modules, etc.
400 * Date object to format or null
401 * @return String representation or null if the input was null.
403 public static String formattedRevision(final Date revision) {
404 if (revision == null) {
407 return getRevisionFormat().format(revision);
412 * Compares this QName to other, without comparing revision.
414 * Compares instance of this to other instance of QName and returns true if
415 * both instances have equal <code>localName</code> ({@link #getLocalName()}
416 * ) and <code>namespace</code> ({@link #getNamespace()}).
419 * Other QName. Must not be null.
420 * @return true if this instance and other have equals localName and
422 * @throws NullPointerException
423 * if <code>other</code> is null.
425 public boolean isEqualWithoutRevision(final QName other) {
426 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
430 public int compareTo(final QName other) {
431 // compare mandatory localName parameter
432 int result = localName.compareTo(other.localName);
437 // compare nullable namespace parameter
438 if (getNamespace() == null) {
439 if (other.getNamespace() != null) {
443 if (other.getNamespace() == null) {
446 result = getNamespace().compareTo(other.getNamespace());
452 // compare nullable revision parameter
453 if (getRevision() == null) {
454 if (other.getRevision() != null) {
458 if (other.getRevision() == null) {
461 result = getRevision().compareTo(other.getRevision());