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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.Interner;
15 import com.google.common.collect.Interners;
16 import java.io.Serializable;
18 import java.net.URISyntaxException;
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import javax.annotation.RegEx;
26 import org.opendaylight.yangtools.concepts.Immutable;
29 * The QName from XML consists of local name of element and XML namespace, but
30 * for our use, we added module revision to it.
33 * In YANG context QName is full name of defined node, type, procedure or
34 * notification. QName consists of XML namespace, YANG model revision and local
35 * name of defined type. It is used to prevent name clashes between nodes with
36 * same local name, but from different schemas.
39 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
40 * defined element, type, procedure or notification.</li>
41 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
43 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
44 * node in the YANG module</li>
48 * QName may also have <code>prefix</code> assigned, but prefix does not
49 * affect equality and identity of two QNames and carry only information
50 * which may be useful for serializers / deserializers.
52 public final class QName implements Immutable, Serializable, Comparable<QName> {
53 private static final Interner<QName> INTERNER = Interners.newWeakInterner();
54 private static final long serialVersionUID = 5398411242927766414L;
56 static final String QNAME_REVISION_DELIMITER = "?revision=";
57 static final String QNAME_LEFT_PARENTHESIS = "(";
58 static final String QNAME_RIGHT_PARENTHESIS = ")";
61 private static final String QNAME_STRING_FULL = "^\\((.+)\\?revision=(.+)\\)(.+)$";
62 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile(QNAME_STRING_FULL);
65 private static final String QNAME_STRING_NO_REVISION = "^\\((.+)\\)(.+)$";
66 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile(QNAME_STRING_NO_REVISION);
68 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&', ':' };
71 private final QNameModule module;
73 private final String localName;
74 private transient int hash;
76 private QName(final QNameModule module, final String localName) {
77 this.localName = checkLocalName(localName);
85 * the namespace assigned to the YANG module
87 * YANG schema identifier
89 private QName(final URI namespace, final String localName) {
90 this(QNameModule.create(namespace), localName);
93 private static String checkLocalName(final String localName) {
94 checkArgument(localName != null, "Parameter 'localName' may not be null.");
95 checkArgument(!Strings.isNullOrEmpty(localName), "Parameter 'localName' must be a non-empty string.");
97 for (final char c : ILLEGAL_CHARACTERS) {
98 if (localName.indexOf(c) != -1) {
99 throw new IllegalArgumentException(String.format(
100 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
106 public static QName create(final String input) {
107 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
108 if (matcher.matches()) {
109 final String namespace = matcher.group(1);
110 final String revision = matcher.group(2);
111 final String localName = matcher.group(3);
112 return create(namespace, revision, localName);
114 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
115 if (matcher.matches()) {
116 final URI namespace = URI.create(matcher.group(1));
117 final String localName = matcher.group(2);
118 return new QName(namespace, localName);
120 throw new IllegalArgumentException("Invalid input: " + input);
123 public static QName create(final QName base, final String localName) {
124 return create(base.getModule(), localName);
131 * Namespace and revision enclosed as a QNameModule
133 * Local name part of QName. MUST NOT BE null.
134 * @return Instance of QName
136 public static QName create(final QNameModule qnameModule, final String localName) {
137 return new QName(requireNonNull(qnameModule, "module may not be null"), localName);
144 * Namespace of QName or null if namespace is undefined.
146 * Revision of namespace or null if revision is unspecified.
148 * Local name part of QName. MUST NOT BE null.
149 * @return Instance of QName
151 public static QName create(final URI namespace, @Nullable final Revision revision, final String localName) {
152 return create(QNameModule.create(namespace, revision), localName);
159 * Namespace of QName or null if namespace is undefined.
161 * Revision of namespace.
163 * Local name part of QName. MUST NOT BE null.
164 * @return Instance of QName
166 public static QName create(final URI namespace, final Optional<Revision> revision, final String localName) {
167 return create(QNameModule.create(namespace, revision), localName);
174 * Namespace of QName or null if namespace is undefined.
176 * Revision of namespace or null if revision is unspecified.
178 * Local name part of QName. MUST NOT BE null.
179 * @return Instance of QName
181 public static QName create(final String namespace, final String localName, final Revision revision) {
182 final URI namespaceUri = parseNamespace(namespace);
183 return create(QNameModule.create(namespaceUri, revision), localName);
190 * Namespace of QName, MUST NOT BE Null.
192 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
193 * in format <code>YYYY-mm-dd</code>.
195 * Local name part of QName. MUST NOT BE null.
196 * @return A new QName
197 * @throws NullPointerException
198 * If any of parameters is null.
199 * @throws IllegalArgumentException
200 * If <code>namespace</code> is not valid URI or
201 * <code>revision</code> is not according to format
202 * <code>YYYY-mm-dd</code>.
204 public static QName create(final String namespace, final String revision, final String localName) {
205 final URI namespaceUri = parseNamespace(namespace);
206 final Revision revisionDate = Revision.valueOf(revision);
207 return create(namespaceUri, revisionDate, localName);
214 * Namespace of QName, MUST NOT BE Null.
216 * Local name part of QName. MUST NOT BE null.
217 * @return A new QName
218 * @throws NullPointerException
219 * If any of parameters is null.
220 * @throws IllegalArgumentException
221 * If <code>namespace</code> is not valid URI.
223 public static QName create(final String namespace, final String localName) {
224 return create(parseNamespace(namespace), localName);
231 * Namespace of QName, MUST NOT BE null.
233 * Local name part of QName. MUST NOT BE null.
234 * @return A new QName
235 * @throws NullPointerException
236 * If any of parameters is null.
237 * @throws IllegalArgumentException
238 * If <code>namespace</code> is not valid URI.
240 public static QName create(final URI namespace, final String localName) {
241 return new QName(namespace, localName);
245 * Get the module component of the QName.
247 * @return Module component
249 public QNameModule getModule() {
254 * Returns XMLNamespace assigned to the YANG module.
256 * @return XMLNamespace assigned to the YANG module.
258 public URI getNamespace() {
259 return module.getNamespace();
263 * Returns YANG schema identifier which were defined for this node in the
266 * @return YANG schema identifier which were defined for this node in the
269 public String getLocalName() {
274 * Returns revision of the YANG module if the module has defined revision.
276 * @return revision of the YANG module if the module has defined revision.
278 public Optional<Revision> getRevision() {
279 return module.getRevision();
283 * Return an interned reference to a equivalent QName.
285 * @return Interned reference, or this object if it was interned.
287 public QName intern() {
288 // We also want to make sure we keep the QNameModule cached
289 final QNameModule cacheMod = module.intern();
291 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
292 // interner. It is important that it does not hold user-supplied reference (such a String instance from
293 // parsing of an XML document).
294 final QName template = cacheMod == module ? this : QName.create(cacheMod, localName.intern());
296 return INTERNER.intern(template);
300 public int hashCode() {
302 hash = Objects.hash(module, localName);
308 * Compares the specified object with this list for equality. Returns
309 * <tt>true</tt> if and only if the specified object is also instance of
310 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
311 * {@link #getRevision()} are equals to same properties of this instance.
313 * @param obj the object to be compared for equality with this QName
314 * @return <tt>true</tt> if the specified object is equal to this QName
318 public boolean equals(final Object obj) {
322 if (!(obj instanceof QName)) {
325 final QName other = (QName) obj;
326 return Objects.equals(localName, other.localName) && module.equals(other.module);
329 private static URI parseNamespace(final String namespace) {
331 return new URI(namespace);
332 } catch (final URISyntaxException ue) {
333 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
338 public String toString() {
339 final StringBuilder sb = new StringBuilder();
340 if (getNamespace() != null) {
341 sb.append(QNAME_LEFT_PARENTHESIS).append(getNamespace());
343 final Optional<Revision> rev = getRevision();
344 if (rev.isPresent()) {
345 sb.append(QNAME_REVISION_DELIMITER).append(rev.get());
347 sb.append(QNAME_RIGHT_PARENTHESIS);
349 sb.append(localName);
350 return sb.toString();
354 * Creates copy of this with revision and prefix unset.
356 * @return copy of this QName with revision and prefix unset.
358 public QName withoutRevision() {
359 return create(getNamespace(), localName);
363 * Formats {@link Revision} representing revision to format <code>YYYY-mm-dd</code>
366 * YANG Specification defines format for <code>revision</code> as
367 * YYYY-mm-dd. This format for revision is reused accross multiple places
368 * such as capabilities URI, YANG modules, etc.
371 * Date object to format
372 * @return String representation or null if the input was null.
374 public static String formattedRevision(final Optional<Revision> revision) {
375 return revision.map(Revision::toString).orElse(null);
379 * Compares this QName to other, without comparing revision.
382 * Compares instance of this to other instance of QName and returns true if
383 * both instances have equal <code>localName</code> ({@link #getLocalName()}
384 * ) and <code>namespace</code> ({@link #getNamespace()}).
387 * Other QName. Must not be null.
388 * @return true if this instance and other have equals localName and
390 * @throws NullPointerException
391 * if <code>other</code> is null.
393 public boolean isEqualWithoutRevision(final QName other) {
394 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
398 public int compareTo(@Nonnull final QName other) {
399 // compare mandatory localName parameter
400 int result = localName.compareTo(other.localName);
404 result = getNamespace().compareTo(other.getNamespace());
409 return Revision.compare(getRevision(), other.getRevision());