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;
24 * The QName from XML consists of local name of element and XML namespace, but
25 * for our use, we added module revision to it.
27 * In YANG context QName is full name of defined node, type, procedure or
28 * notification. QName consists of XML namespace, YANG model revision and local
29 * name of defined type. It is used to prevent name clashes between nodes with
30 * same local name, but from different schemas.
33 * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
34 * defined element, type, procedure or notification.</li>
35 * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
37 * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
38 * node in the YANG module</li>
41 * QName may also have <code>prefix</code> assigned, but prefix does not
42 * affect equality and identity of two QNames and carry only information
43 * which may be useful for serializers / deserializers.
47 public final class QName implements Immutable, Serializable, Comparable<QName> {
48 private static final long serialVersionUID = 5398411242927766414L;
50 static final String QNAME_REVISION_DELIMITER = "?revision=";
51 static final String QNAME_LEFT_PARENTHESIS = "(";
52 static final String QNAME_RIGHT_PARENTHESIS = ")";
54 private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
56 private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
57 private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
59 private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
62 private final QNameModule module;
64 private final String localName;
66 private final String prefix;
68 private QName(final QNameModule module, final String prefix, final String localName) {
69 this.localName = checkLocalName(localName);
78 * the namespace assigned to the YANG module
80 * the revision of the YANG module
82 * locally defined prefix assigned to local name
84 * YANG schema identifier
87 public QName(final URI namespace, final Date revision, final String prefix, final String localName) {
88 this(QNameModule.create(namespace, revision), prefix, localName);
95 * the namespace assigned to the YANG module
97 * YANG schema identifier
99 public QName(final URI namespace, final String localName) {
100 this(namespace, null, "", localName);
103 private static String checkLocalName(final String localName) {
104 if (localName == null) {
105 throw new IllegalArgumentException("Parameter 'localName' may not be null.");
107 if (localName.length() == 0) {
108 throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
111 for (char c : ILLEGAL_CHARACTERS) {
112 if (localName.indexOf(c) != -1) {
113 throw new IllegalArgumentException(String.format(
114 "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
124 * the namespace assigned to the YANG module
126 * the revision of the YANG module
128 * YANG schema identifier
130 * @deprecated Use {@link #create(URI, Date, String)} instead.
133 public QName(final URI namespace, final Date revision, final String localName) {
134 this(QNameModule.create(namespace, revision), null, localName);
138 * Construct new QName which reuses namespace, revision and prefix from
143 * @deprecated Use {@link #create(QName, String)} instead.
146 public QName(final QName base, final String localName) {
147 this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
151 * @deprecated Use {@link #create(String)} instead. This implementation is
155 public QName(final String input) throws ParseException {
156 final String nsAndRev = input.substring(input.indexOf("(") + 1, input.indexOf(")"));
159 if (nsAndRev.contains("?")) {
160 String[] splitted = nsAndRev.split("\\?");
161 namespace = URI.create(splitted[0]);
162 revision = getRevisionFormat().parse(splitted[1]);
164 namespace = URI.create(nsAndRev);
168 this.localName = checkLocalName(input.substring(input.indexOf(")") + 1));
170 this.module = QNameModule.create(namespace, revision);
173 public static QName create(final String input) {
174 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
175 if (matcher.matches()) {
176 String namespace = matcher.group(1);
177 String revision = matcher.group(2);
178 String localName = matcher.group(3);
179 return create(namespace, revision, localName);
181 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
182 if (matcher.matches()) {
183 URI namespace = URI.create(matcher.group(1));
184 String localName = matcher.group(2);
185 return new QName(namespace, localName);
187 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
188 if (matcher.matches()) {
189 String localName = matcher.group(1);
190 return new QName((URI) null, localName);
192 throw new IllegalArgumentException("Invalid input:" + input);
196 * Get the module component of the QName.
198 * @return Module component
200 public QNameModule getModule() {
205 * Returns XMLNamespace assigned to the YANG module.
207 * @return XMLNamespace assigned to the YANG module.
209 public URI getNamespace() {
210 return module.getNamespace();
214 * Returns YANG schema identifier which were defined for this node in the
217 * @return YANG schema identifier which were defined for this node in the
220 public String getLocalName() {
225 * Returns revision of the YANG module if the module has defined revision,
226 * otherwise returns <code>null</code>
228 * @return revision of the YANG module if the module has defined revision,
229 * otherwise returns <code>null</code>
231 public Date getRevision() {
232 return module.getRevision();
236 * Returns locally defined prefix assigned to local name
238 * @return locally defined prefix assigned to local name
240 public String getPrefix() {
245 public int hashCode() {
246 final int prime = 31;
248 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
249 result = prime * result + module.hashCode();
255 * Compares the specified object with this list for equality. Returns
256 * <tt>true</tt> if and only if the specified object is also instance of
257 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
258 * {@link #getRevision()} are equals to same properties of this instance.
260 * @param o the object to be compared for equality with this QName
261 * @return <tt>true</tt> if the specified object is equal to this QName
265 public boolean equals(final Object obj) {
269 if (!(obj instanceof QName)) {
272 final QName other = (QName) obj;
273 if (localName == null) {
274 if (other.localName != null) {
277 } else if (!localName.equals(other.localName)) {
280 return module.equals(other.module);
283 public static QName create(final QName base, final String localName) {
284 return new QName(base, localName);
287 public static QName create(final QNameModule module, final String prefix, final String localName) {
288 if (module == null) {
289 throw new NullPointerException("module may not be null");
291 return new QName(module, prefix, localName);
298 * Namespace and revision enclosed as a QNameModule
300 * Local name part of QName. MUST NOT BE null.
301 * @return Instance of QName
303 public static QName create(final QNameModule qnameModule, final String localName) {
304 return new QName(qnameModule, null, localName);
311 * Namespace of QName or null if namespace is undefined.
313 * Revision of namespace or null if revision is unspecified.
315 * Local name part of QName. MUST NOT BE null.
316 * @return Instance of QName
318 public static QName create(final URI namespace, final Date revision, final String localName) {
319 return new QName(QNameModule.create(namespace, revision), null, localName);
327 * Namespace of QName, MUST NOT BE Null.
329 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
330 * in format <code>YYYY-mm-dd</code>.
332 * Local name part of QName. MUST NOT BE null.
334 * @throws NullPointerException
335 * If any of paramaters is null.
336 * @throws IllegalArgumentException
337 * If <code>namespace</code> is not valid URI or
338 * <code>revision</code> is not according to format
339 * <code>YYYY-mm-dd</code>.
341 public static QName create(final String namespace, final String revision, final String localName)
342 throws IllegalArgumentException {
343 final URI namespaceUri;
345 namespaceUri = new URI(namespace);
346 } catch (URISyntaxException ue) {
347 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
350 Date revisionDate = parseRevision(revision);
351 return create(namespaceUri, revisionDate, localName);
355 public String toString() {
356 StringBuilder sb = new StringBuilder();
357 if (getNamespace() != null) {
358 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
360 if (getFormattedRevision() != null) {
361 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
363 sb.append(QNAME_RIGHT_PARENTHESIS);
365 sb.append(localName);
366 return sb.toString();
370 * Return string representation of revision in format
371 * <code>YYYY-mm-dd</code>
373 * YANG Specification defines format for <code>revision</code> as
374 * YYYY-mm-dd. This format for revision is reused accross multiple places
375 * such as capabilities URI, YANG modules, etc.
377 * @return String representation of revision or null, if revision is not
380 public String getFormattedRevision() {
381 return module.getFormattedRevision();
385 * Creates copy of this with revision and prefix unset.
387 * @return copy of this QName with revision and prefix unset.
389 public QName withoutRevision() {
390 return QName.create(getNamespace(), null, localName);
393 public static Date parseRevision(final String formatedDate) {
395 return getRevisionFormat().parse(formatedDate);
396 } catch (ParseException | RuntimeException e) {
397 throw new IllegalArgumentException(
398 String.format("Revision '%s'is not in a supported format", formatedDate), e);
403 * Formats {@link Date} representing revision to format
404 * <code>YYYY-mm-dd</code>
406 * YANG Specification defines format for <code>revision</code> as
407 * YYYY-mm-dd. This format for revision is reused accross multiple places
408 * such as capabilities URI, YANG modules, etc.
411 * Date object to format or null
412 * @return String representation or null if the input was null.
414 public static String formattedRevision(final Date revision) {
415 if (revision == null) {
418 return getRevisionFormat().format(revision);
423 * Compares this QName to other, without comparing revision.
425 * Compares instance of this to other instance of QName and returns true if
426 * both instances have equal <code>localName</code> ({@link #getLocalName()}
427 * ) and <code>namespace</code> ({@link #getNamespace()}).
430 * Other QName. Must not be null.
431 * @return true if this instance and other have equals localName and
433 * @throws NullPointerException
434 * if <code>other</code> is null.
436 public boolean isEqualWithoutRevision(final QName other) {
437 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
441 public int compareTo(final QName other) {
442 // compare mandatory localName parameter
443 int result = localName.compareTo(other.localName);
448 // compare nullable namespace parameter
449 if (getNamespace() == null) {
450 if (other.getNamespace() != null) {
454 if (other.getNamespace() == null) {
457 result = getNamespace().compareTo(other.getNamespace());
463 // compare nullable revision parameter
464 if (getRevision() == null) {
465 if (other.getRevision() != null) {
469 if (other.getRevision() == null) {
472 result = getRevision().compareTo(other.getRevision());