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;
25 * The QName from XML consists of local name of element and XML namespace, but
26 * for our use, we added module revision to it.
28 * In YANG context QName is full name of defined node, type, procedure or
29 * notification. QName consists of XML namespace, YANG model revision and local
30 * name of defined type. It is used to prevent name clashes between nodes with
31 * same local name, but from different schemas.
34 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
35 * defined element, type, procedure or notification.</li>
36 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
38 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
39 * node in the YANG module</li>
42 * QName may also have <code>prefix</code> assigned, but prefix does not
43 * affect equality and identity of two QNames and carry only information
44 * which may be useful for serializers / deserializers.
48 public final class QName implements Immutable, Serializable, Comparable<QName> {
49 private static final Interner<QName> INTERNER = Interners.newWeakInterner();
50 private static final long serialVersionUID = 5398411242927766414L;
52 static final String QNAME_REVISION_DELIMITER = "?revision=";
53 static final String QNAME_LEFT_PARENTHESIS = "(";
54 static final String QNAME_RIGHT_PARENTHESIS = ")";
56 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
58 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
59 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
60 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
63 private final QNameModule module;
65 private final String localName;
67 private QName(final QNameModule module, final String localName) {
68 this.localName = checkLocalName(localName);
73 * Look up specified QName in the global cache and return a shared reference.
75 * @param qname QName instance
76 * @return Cached instance, according to {@link org.opendaylight.yangtools.objcache.ObjectCache} policy.
78 * @deprecated Use {@link #intern()} instead.
81 public static QName cachedReference(final QName qname) {
82 return qname.intern();
89 * the namespace assigned to the YANG module
91 * YANG schema identifier
93 public QName(final URI namespace, final String localName) {
94 this(QNameModule.create(namespace, null), localName);
97 private static String checkLocalName(final String localName) {
98 if (localName == null) {
99 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
101 if (localName.length() == 0) {
102 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
105 for (final char c : ILLEGAL_CHARACTERS) {
106 if (localName.indexOf(c) != -1) {
107 throw new IllegalArgumentException(String.format(
108 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
114 public static QName create(final String input) {
115 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
116 if (matcher.matches()) {
117 final String namespace = matcher.group(1);
118 final String revision = matcher.group(2);
119 final String localName = matcher.group(3);
120 return create(namespace, revision, localName);
122 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
123 if (matcher.matches()) {
124 final URI namespace = URI.create(matcher.group(1));
125 final String localName = matcher.group(2);
126 return new QName(namespace, localName);
128 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
129 if (matcher.matches()) {
130 final String localName = matcher.group(1);
131 return new QName((URI) null, localName);
133 throw new IllegalArgumentException("Invalid input:" + input);
137 * Get the module component of the QName.
139 * @return Module component
141 public QNameModule getModule() {
146 * Returns XMLNamespace assigned to the YANG module.
148 * @return XMLNamespace assigned to the YANG module.
150 public URI getNamespace() {
151 return module.getNamespace();
155 * Returns YANG schema identifier which were defined for this node in the
158 * @return YANG schema identifier which were defined for this node in the
161 public String getLocalName() {
166 * Returns revision of the YANG module if the module has defined revision,
167 * otherwise returns <code>null</code>
169 * @return revision of the YANG module if the module has defined revision,
170 * otherwise returns <code>null</code>
172 public Date getRevision() {
173 return module.getRevision();
177 * Return an interned reference to a equivalent QName.
179 * @return Interned reference, or this object if it was interned.
181 public QName intern() {
182 // We also want to make sure we keep the QNameModule cached
183 final QNameModule cacheMod = module.intern();
185 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
186 // interner. It is important that it does not hold user-supplied reference (such a String instance from
187 // parsing of an XML document).
188 final QName template = cacheMod == module ? this : QName.create(cacheMod, localName);
190 return INTERNER.intern(template);
194 public int hashCode() {
195 final int prime = 31;
197 result = prime * result + Objects.hashCode(localName);
198 result = prime * result + module.hashCode();
204 * Compares the specified object with this list for equality. Returns
205 * <tt>true</tt> if and only if the specified object is also instance of
206 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
207 * {@link #getRevision()} are equals to same properties of this instance.
209 * @param obj the object to be compared for equality with this QName
210 * @return <tt>true</tt> if the specified object is equal to this QName
214 public boolean equals(final Object obj) {
218 if (!(obj instanceof QName)) {
221 final QName other = (QName) obj;
222 return Objects.equals(localName, other.localName) && module.equals(other.module);
225 public static QName create(final QName base, final String localName) {
226 return create(base.getModule(), localName);
233 * Namespace and revision enclosed as a QNameModule
235 * Local name part of QName. MUST NOT BE null.
236 * @return Instance of QName
238 public static QName create(final QNameModule qnameModule, final String localName) {
239 return new QName(Preconditions.checkNotNull(qnameModule,"module may not be null"), 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());