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 com.google.common.base.Preconditions;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.io.Serializable;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.Date;
20 import java.util.Objects;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import org.opendaylight.yangtools.concepts.Immutable;
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 Interner<QName> INTERNER = Interners.newWeakInterner();
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 QName(final QNameModule module, final String localName) {
69 this.localName = checkLocalName(localName);
74 * Look up specified QName in the global cache and return a shared reference.
76 * @param qname QName instance
77 * @return Cached instance, according to {@link org.opendaylight.yangtools.objcache.ObjectCache} policy.
79 * @deprecated Use {@link #intern()} instead.
82 public static QName cachedReference(final QName qname) {
83 return qname.intern();
90 * the namespace assigned to the YANG module
92 * YANG schema identifier
94 public QName(final URI namespace, final String localName) {
95 this(QNameModule.create(namespace, null), localName);
98 private static String checkLocalName(final String localName) {
99 if (localName == null) {
100 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
102 if (localName.length() == 0) {
103 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
106 for (final char c : ILLEGAL_CHARACTERS) {
107 if (localName.indexOf(c) != -1) {
108 throw new IllegalArgumentException(String.format(
109 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
115 public static QName create(final String input) {
116 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
117 if (matcher.matches()) {
118 final String namespace = matcher.group(1);
119 final String revision = matcher.group(2);
120 final String localName = matcher.group(3);
121 return create(namespace, revision, localName);
123 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
124 if (matcher.matches()) {
125 final URI namespace = URI.create(matcher.group(1));
126 final String localName = matcher.group(2);
127 return new QName(namespace, localName);
129 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
130 if (matcher.matches()) {
131 final String localName = matcher.group(1);
132 return new QName((URI) null, localName);
134 throw new IllegalArgumentException("Invalid input:" + input);
138 * Get the module component of the QName.
140 * @return Module component
142 public QNameModule getModule() {
147 * Returns XMLNamespace assigned to the YANG module.
149 * @return XMLNamespace assigned to the YANG module.
151 public URI getNamespace() {
152 return module.getNamespace();
156 * Returns YANG schema identifier which were defined for this node in the
159 * @return YANG schema identifier which were defined for this node in the
162 public String getLocalName() {
167 * Returns revision of the YANG module if the module has defined revision,
168 * otherwise returns <code>null</code>
170 * @return revision of the YANG module if the module has defined revision,
171 * otherwise returns <code>null</code>
173 public Date getRevision() {
174 return module.getRevision();
178 * Return an interned reference to a equivalent QName.
180 * @return Interned reference, or this object if it was interned.
182 public QName intern() {
183 // We also want to make sure we keep the QNameModule cached
184 final QNameModule cacheMod = module.intern();
186 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
187 // interner. It is important that it does not hold user-supplied reference (such a String instance from
188 // parsing of an XML document).
189 final QName template = cacheMod == module ? this : QName.create(cacheMod, localName);
191 return INTERNER.intern(template);
195 public int hashCode() {
196 final int prime = 31;
198 result = prime * result + Objects.hashCode(localName);
199 result = prime * result + module.hashCode();
205 * Compares the specified object with this list for equality. Returns
206 * <tt>true</tt> if and only if the specified object is also instance of
207 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
208 * {@link #getRevision()} are equals to same properties of this instance.
210 * @param obj the object to be compared for equality with this QName
211 * @return <tt>true</tt> if the specified object is equal to this QName
215 public boolean equals(final Object obj) {
219 if (!(obj instanceof QName)) {
222 final QName other = (QName) obj;
223 return Objects.equals(localName, other.localName) && module.equals(other.module);
226 public static QName create(final QName base, final String localName) {
227 return create(base.getModule(), localName);
234 * Namespace and revision enclosed as a QNameModule
236 * Local name part of QName. MUST NOT BE null.
237 * @return Instance of QName
239 public static QName create(final QNameModule qnameModule, final String localName) {
240 return new QName(Preconditions.checkNotNull(qnameModule,"module may not be null"), localName);
247 * Namespace of QName or null if namespace is undefined.
249 * Revision of namespace or null if revision is unspecified.
251 * Local name part of QName. MUST NOT BE null.
252 * @return Instance of QName
254 public static QName create(final URI namespace, final Date revision, final String localName) {
255 return create(QNameModule.create(namespace, revision), localName);
263 * Namespace of QName, MUST NOT BE Null.
265 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
266 * in format <code>YYYY-mm-dd</code>.
268 * Local name part of QName. MUST NOT BE null.
270 * @throws NullPointerException
271 * If any of parameters is null.
272 * @throws IllegalArgumentException
273 * If <code>namespace</code> is not valid URI or
274 * <code>revision</code> is not according to format
275 * <code>YYYY-mm-dd</code>.
277 public static QName create(final String namespace, final String revision, final String localName) {
278 final URI namespaceUri = parseNamespace(namespace);
279 final Date revisionDate = parseRevision(revision);
280 return create(namespaceUri, revisionDate, localName);
283 private static URI parseNamespace(final String namespace) {
285 return new URI(namespace);
286 } catch (final URISyntaxException ue) {
287 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
295 * Namespace of QName, MUST NOT BE Null.
297 * Local name part of QName. MUST NOT BE null.
299 * @throws NullPointerException
300 * If any of parameters is null.
301 * @throws IllegalArgumentException
302 * If <code>namespace</code> is not valid URI.
304 public static QName create(final String namespace, final String localName) {
305 return create(parseNamespace(namespace), null, localName);
309 public String toString() {
310 final StringBuilder sb = new StringBuilder();
311 if (getNamespace() != null) {
312 sb.append(QNAME_LEFT_PARENTHESIS).append(getNamespace());
314 if (getFormattedRevision() != null) {
315 sb.append(QNAME_REVISION_DELIMITER).append(getFormattedRevision());
317 sb.append(QNAME_RIGHT_PARENTHESIS);
319 sb.append(localName);
320 return sb.toString();
324 * Return string representation of revision in format
325 * <code>YYYY-mm-dd</code>
327 * YANG Specification defines format for <code>revision</code> as
328 * YYYY-mm-dd. This format for revision is reused accross multiple places
329 * such as capabilities URI, YANG modules, etc.
331 * @return String representation of revision or null, if revision is not
334 public String getFormattedRevision() {
335 return module.getFormattedRevision();
339 * Creates copy of this with revision and prefix unset.
341 * @return copy of this QName with revision and prefix unset.
343 public QName withoutRevision() {
344 return create(getNamespace(), null, localName);
347 public static Date parseRevision(final String formatedDate) {
349 return getRevisionFormat().parse(formatedDate);
350 } catch (ParseException | RuntimeException e) {
351 throw new IllegalArgumentException(
352 String.format("Revision '%s'is not in a supported format", formatedDate), e);
357 * Formats {@link Date} representing revision to format
358 * <code>YYYY-mm-dd</code>
360 * YANG Specification defines format for <code>revision</code> as
361 * YYYY-mm-dd. This format for revision is reused accross multiple places
362 * such as capabilities URI, YANG modules, etc.
365 * Date object to format or null
366 * @return String representation or null if the input was null.
368 public static String formattedRevision(final Date revision) {
369 if (revision == null) {
372 return getRevisionFormat().format(revision);
377 * Compares this QName to other, without comparing revision.
379 * Compares instance of this to other instance of QName and returns true if
380 * both instances have equal <code>localName</code> ({@link #getLocalName()}
381 * ) and <code>namespace</code> ({@link #getNamespace()}).
384 * Other QName. Must not be null.
385 * @return true if this instance and other have equals localName and
387 * @throws NullPointerException
388 * if <code>other</code> is null.
390 public boolean isEqualWithoutRevision(final QName other) {
391 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
395 public int compareTo(final QName other) {
396 // compare mandatory localName parameter
397 int result = localName.compareTo(other.localName);
402 // compare nullable namespace parameter
403 if (getNamespace() == null) {
404 if (other.getNamespace() != null) {
408 if (other.getNamespace() == null) {
411 result = getNamespace().compareTo(other.getNamespace());
417 // compare nullable revision parameter
418 if (getRevision() == null) {
419 if (other.getRevision() != null) {
423 if (other.getRevision() == null) {
426 result = getRevision().compareTo(other.getRevision());