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 return CACHE.getReference(qname);
90 * the namespace assigned to the YANG module
92 * the revision of the YANG module
94 * locally defined prefix assigned to local name
96 * YANG schema identifier
98 * @deprecated Prefix storage in QNames is deprecated.
101 public QName(final URI namespace, final Date revision, final String prefix, final String localName) {
102 this(QNameModule.create(namespace, revision), prefix, localName);
109 * the namespace assigned to the YANG module
111 * YANG schema identifier
113 public QName(final URI namespace, final String localName) {
114 this(namespace, null, "", localName);
117 private static String checkLocalName(final String localName) {
118 if (localName == null) {
119 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
121 if (localName.length() == 0) {
122 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
125 for (char c : ILLEGAL_CHARACTERS) {
126 if (localName.indexOf(c) != -1) {
127 throw new IllegalArgumentException(String.format(
128 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
134 public static QName create(final String input) {
135 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
136 if (matcher.matches()) {
137 String namespace = matcher.group(1);
138 String revision = matcher.group(2);
139 String localName = matcher.group(3);
140 return create(namespace, revision, localName);
142 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
143 if (matcher.matches()) {
144 URI namespace = URI.create(matcher.group(1));
145 String localName = matcher.group(2);
146 return new QName(namespace, localName);
148 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
149 if (matcher.matches()) {
150 String localName = matcher.group(1);
151 return new QName((URI) null, localName);
153 throw new IllegalArgumentException("Invalid input:" + input);
157 * Get the module component of the QName.
159 * @return Module component
161 public QNameModule getModule() {
166 * Returns XMLNamespace assigned to the YANG module.
168 * @return XMLNamespace assigned to the YANG module.
170 public URI getNamespace() {
171 return module.getNamespace();
175 * Returns YANG schema identifier which were defined for this node in the
178 * @return YANG schema identifier which were defined for this node in the
181 public String getLocalName() {
186 * Returns revision of the YANG module if the module has defined revision,
187 * otherwise returns <code>null</code>
189 * @return revision of the YANG module if the module has defined revision,
190 * otherwise returns <code>null</code>
192 public Date getRevision() {
193 return module.getRevision();
197 * Returns locally defined prefix assigned to local name
199 * @return locally defined prefix assigned to local name
201 * @deprecated Prefix storage in QNames is deprecated.
204 public String getPrefix() {
209 public int hashCode() {
210 final int prime = 31;
212 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
213 result = prime * result + module.hashCode();
219 * Compares the specified object with this list for equality. Returns
220 * <tt>true</tt> if and only if the specified object is also instance of
221 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
222 * {@link #getRevision()} are equals to same properties of this instance.
224 * @param o the object to be compared for equality with this QName
225 * @return <tt>true</tt> if the specified object is equal to this QName
229 public boolean equals(final Object obj) {
233 if (!(obj instanceof QName)) {
236 final QName other = (QName) obj;
237 if (localName == null) {
238 if (other.localName != null) {
241 } else if (!localName.equals(other.localName)) {
244 return module.equals(other.module);
247 public static QName create(final QName base, final String localName) {
248 return create(base.getModule(), base.getPrefix(), localName);
255 * Namespace and revision enclosed as a QNameModule
259 * Local name part of QName. MUST NOT BE null.
260 * @return Instance of QName
262 * @deprecated Prefix storage in QNames is deprecated.
265 public static QName create(final QNameModule module, final String prefix, final String localName) {
266 if (module == null) {
267 throw new NullPointerException("module may not be null");
269 return new QName(module, prefix, localName);
276 * Namespace and revision enclosed as a QNameModule
278 * Local name part of QName. MUST NOT BE null.
279 * @return Instance of QName
281 public static QName create(final QNameModule qnameModule, final String localName) {
282 return create(qnameModule, null, localName);
289 * Namespace of QName or null if namespace is undefined.
291 * Revision of namespace or null if revision is unspecified.
293 * Local name part of QName. MUST NOT BE null.
294 * @return Instance of QName
296 public static QName create(final URI namespace, final Date revision, final String localName) {
297 return create(QNameModule.create(namespace, revision), null, localName);
305 * Namespace of QName, MUST NOT BE Null.
307 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
308 * in format <code>YYYY-mm-dd</code>.
310 * Local name part of QName. MUST NOT BE null.
312 * @throws NullPointerException
313 * If any of paramaters is null.
314 * @throws IllegalArgumentException
315 * If <code>namespace</code> is not valid URI or
316 * <code>revision</code> is not according to format
317 * <code>YYYY-mm-dd</code>.
319 public static QName create(final String namespace, final String revision, final String localName)
320 throws IllegalArgumentException {
321 final URI namespaceUri;
323 namespaceUri = new URI(namespace);
324 } catch (URISyntaxException ue) {
325 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
328 Date revisionDate = parseRevision(revision);
329 return create(namespaceUri, revisionDate, localName);
333 public String toString() {
334 StringBuilder sb = new StringBuilder();
335 if (getNamespace() != null) {
336 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
338 if (getFormattedRevision() != null) {
339 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
341 sb.append(QNAME_RIGHT_PARENTHESIS);
343 sb.append(localName);
344 return sb.toString();
348 * Return string representation of revision in format
349 * <code>YYYY-mm-dd</code>
351 * YANG Specification defines format for <code>revision</code> as
352 * YYYY-mm-dd. This format for revision is reused accross multiple places
353 * such as capabilities URI, YANG modules, etc.
355 * @return String representation of revision or null, if revision is not
358 public String getFormattedRevision() {
359 return module.getFormattedRevision();
363 * Creates copy of this with revision and prefix unset.
365 * @return copy of this QName with revision and prefix unset.
367 public QName withoutRevision() {
368 return create(getNamespace(), null, localName);
371 public static Date parseRevision(final String formatedDate) {
373 return getRevisionFormat().parse(formatedDate);
374 } catch (ParseException | RuntimeException e) {
375 throw new IllegalArgumentException(
376 String.format("Revision '%s'is not in a supported format", formatedDate), e);
381 * Formats {@link Date} representing revision to format
382 * <code>YYYY-mm-dd</code>
384 * YANG Specification defines format for <code>revision</code> as
385 * YYYY-mm-dd. This format for revision is reused accross multiple places
386 * such as capabilities URI, YANG modules, etc.
389 * Date object to format or null
390 * @return String representation or null if the input was null.
392 public static String formattedRevision(final Date revision) {
393 if (revision == null) {
396 return getRevisionFormat().format(revision);
401 * Compares this QName to other, without comparing revision.
403 * Compares instance of this to other instance of QName and returns true if
404 * both instances have equal <code>localName</code> ({@link #getLocalName()}
405 * ) and <code>namespace</code> ({@link #getNamespace()}).
408 * Other QName. Must not be null.
409 * @return true if this instance and other have equals localName and
411 * @throws NullPointerException
412 * if <code>other</code> is null.
414 public boolean isEqualWithoutRevision(final QName other) {
415 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
419 public int compareTo(final QName other) {
420 // compare mandatory localName parameter
421 int result = localName.compareTo(other.localName);
426 // compare nullable namespace parameter
427 if (getNamespace() == null) {
428 if (other.getNamespace() != null) {
432 if (other.getNamespace() == null) {
435 result = getNamespace().compareTo(other.getNamespace());
441 // compare nullable revision parameter
442 if (getRevision() == null) {
443 if (other.getRevision() != null) {
447 if (other.getRevision() == null) {
450 result = getRevision().compareTo(other.getRevision());