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.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.text.ParseException;
20 import java.util.Date;
21 import java.util.Objects;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import javax.annotation.Nonnull;
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);
69 private static final String QNAME_STRING_NO_NAMESPACE_NO_REVISION = "^(.+)$";
70 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION =
71 Pattern.compile(QNAME_STRING_NO_NAMESPACE_NO_REVISION);
73 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&', ':' };
76 private final QNameModule module;
78 private final String localName;
79 private transient int hash;
81 private QName(final QNameModule module, final String localName) {
82 this.localName = checkLocalName(localName);
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 Preconditions.checkArgument(localName != null, "Parameter 'localName' may not be null.");
100 Preconditions.checkArgument(!Strings.isNullOrEmpty(localName),
101 "Parameter 'localName' must be a non-empty string.");
103 for (final char c : ILLEGAL_CHARACTERS) {
104 if (localName.indexOf(c) != -1) {
105 throw new IllegalArgumentException(String.format(
106 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
112 public static QName create(final String input) {
113 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
114 if (matcher.matches()) {
115 final String namespace = matcher.group(1);
116 final String revision = matcher.group(2);
117 final String localName = matcher.group(3);
118 return create(namespace, revision, localName);
120 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
121 if (matcher.matches()) {
122 final URI namespace = URI.create(matcher.group(1));
123 final String localName = matcher.group(2);
124 return new QName(namespace, localName);
126 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
127 if (matcher.matches()) {
128 final String localName = matcher.group(1);
129 return new QName((URI) null, localName);
131 throw new IllegalArgumentException("Invalid input:" + input);
134 public static QName create(final QName base, final String localName) {
135 return create(base.getModule(), localName);
142 * Namespace and revision enclosed as a QNameModule
144 * Local name part of QName. MUST NOT BE null.
145 * @return Instance of QName
147 public static QName create(final QNameModule qnameModule, final String localName) {
148 return new QName(Preconditions.checkNotNull(qnameModule,"module may not be null"), localName);
155 * Namespace of QName or null if namespace is undefined.
157 * Revision of namespace or null if revision is unspecified.
159 * Local name part of QName. MUST NOT BE null.
160 * @return Instance of QName
162 public static QName create(final URI namespace, final Date revision, final String localName) {
163 return create(QNameModule.create(namespace, revision), localName);
170 * Namespace of QName or null if namespace is undefined.
172 * Revision of namespace or null if revision is unspecified.
174 * Local name part of QName. MUST NOT BE null.
175 * @return Instance of QName
177 public static QName create(final String namespace, final String localName, final Date revision) {
178 final URI namespaceUri = parseNamespace(namespace);
179 return create(QNameModule.create(namespaceUri, revision), localName);
186 * Namespace of QName, MUST NOT BE Null.
188 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
189 * in format <code>YYYY-mm-dd</code>.
191 * Local name part of QName. MUST NOT BE null.
192 * @return A new QName
193 * @throws NullPointerException
194 * If any of parameters is null.
195 * @throws IllegalArgumentException
196 * If <code>namespace</code> is not valid URI or
197 * <code>revision</code> is not according to format
198 * <code>YYYY-mm-dd</code>.
200 public static QName create(final String namespace, final String revision, final String localName) {
201 final URI namespaceUri = parseNamespace(namespace);
202 final Date revisionDate = parseRevision(revision);
203 return create(namespaceUri, revisionDate, localName);
210 * Namespace of QName, MUST NOT BE Null.
212 * Local name part of QName. MUST NOT BE null.
213 * @return A new QName
214 * @throws NullPointerException
215 * If any of parameters is null.
216 * @throws IllegalArgumentException
217 * If <code>namespace</code> is not valid URI.
219 public static QName create(final String namespace, final String localName) {
220 return create(parseNamespace(namespace), null, localName);
224 * Get the module component of the QName.
226 * @return Module component
228 public QNameModule getModule() {
233 * Returns XMLNamespace assigned to the YANG module.
235 * @return XMLNamespace assigned to the YANG module.
237 public URI getNamespace() {
238 return module.getNamespace();
242 * Returns YANG schema identifier which were defined for this node in the
245 * @return YANG schema identifier which were defined for this node in the
248 public String getLocalName() {
253 * Returns revision of the YANG module if the module has defined revision,
254 * otherwise returns <code>null</code>.
256 * @return revision of the YANG module if the module has defined revision,
257 * otherwise returns <code>null</code>
259 public Date getRevision() {
260 return module.getRevision();
264 * Return an interned reference to a equivalent QName.
266 * @return Interned reference, or this object if it was interned.
268 public QName intern() {
269 // We also want to make sure we keep the QNameModule cached
270 final QNameModule cacheMod = module.intern();
272 // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
273 // interner. It is important that it does not hold user-supplied reference (such a String instance from
274 // parsing of an XML document).
275 final QName template = cacheMod == module ? this : QName.create(cacheMod, localName.intern());
277 return INTERNER.intern(template);
281 public int hashCode() {
283 hash = Objects.hash(module, localName);
289 * Compares the specified object with this list for equality. Returns
290 * <tt>true</tt> if and only if the specified object is also instance of
291 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
292 * {@link #getRevision()} are equals to same properties of this instance.
294 * @param obj the object to be compared for equality with this QName
295 * @return <tt>true</tt> if the specified object is equal to this QName
299 public boolean equals(final Object obj) {
303 if (!(obj instanceof QName)) {
306 final QName other = (QName) obj;
307 return Objects.equals(localName, other.localName) && module.equals(other.module);
310 private static URI parseNamespace(final String namespace) {
312 return new URI(namespace);
313 } catch (final URISyntaxException ue) {
314 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
319 public String toString() {
320 final StringBuilder sb = new StringBuilder();
321 if (getNamespace() != null) {
322 sb.append(QNAME_LEFT_PARENTHESIS).append(getNamespace());
324 if (getFormattedRevision() != null) {
325 sb.append(QNAME_REVISION_DELIMITER).append(getFormattedRevision());
327 sb.append(QNAME_RIGHT_PARENTHESIS);
329 sb.append(localName);
330 return sb.toString();
334 * Return string representation of revision in format <code>YYYY-mm-dd</code>
337 * YANG Specification defines format for <code>revision</code> as
338 * YYYY-mm-dd. This format for revision is reused accross multiple places
339 * such as capabilities URI, YANG modules, etc.
341 * @return String representation of revision or null, if revision is not
344 public String getFormattedRevision() {
345 return module.getFormattedRevision();
349 * Creates copy of this with revision and prefix unset.
351 * @return copy of this QName with revision and prefix unset.
353 public QName withoutRevision() {
354 return create(getNamespace(), null, localName);
357 @SuppressWarnings("checkstyle:illegalCatch")
358 public static Date parseRevision(final String formatedDate) {
360 return getRevisionFormat().parse(formatedDate);
361 } catch (ParseException | RuntimeException e) {
362 throw new IllegalArgumentException(
363 String.format("Revision '%s'is not in a supported format", formatedDate), e);
368 * Formats {@link Date} representing revision to format
369 * <code>YYYY-mm-dd</code>
372 * YANG Specification defines format for <code>revision</code> as
373 * YYYY-mm-dd. This format for revision is reused accross multiple places
374 * such as capabilities URI, YANG modules, etc.
377 * Date object to format or null
378 * @return String representation or null if the input was null.
380 public static String formattedRevision(final Date revision) {
381 if (revision == null) {
384 return getRevisionFormat().format(revision);
388 * Compares this QName to other, without comparing revision.
391 * Compares instance of this to other instance of QName and returns true if
392 * both instances have equal <code>localName</code> ({@link #getLocalName()}
393 * ) and <code>namespace</code> ({@link #getNamespace()}).
396 * Other QName. Must not be null.
397 * @return true if this instance and other have equals localName and
399 * @throws NullPointerException
400 * if <code>other</code> is null.
402 public boolean isEqualWithoutRevision(final QName other) {
403 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
407 public int compareTo(@Nonnull final QName other) {
408 // compare mandatory localName parameter
409 int result = localName.compareTo(other.localName);
414 // compare nullable namespace parameter
415 if (getNamespace() == null) {
416 if (other.getNamespace() != null) {
420 if (other.getNamespace() == null) {
423 result = getNamespace().compareTo(other.getNamespace());
429 // compare nullable revision parameter
430 if (getRevision() == null) {
431 if (other.getRevision() != null) {
435 if (other.getRevision() == null) {
438 result = getRevision().compareTo(other.getRevision());