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 * Returns XMLNamespace assigned to the YANG module.
198 * @return XMLNamespace assigned to the YANG module.
200 public URI getNamespace() {
201 return module.getNamespace();
205 * Returns YANG schema identifier which were defined for this node in the
208 * @return YANG schema identifier which were defined for this node in the
211 public String getLocalName() {
216 * Returns revision of the YANG module if the module has defined revision,
217 * otherwise returns <code>null</code>
219 * @return revision of the YANG module if the module has defined revision,
220 * otherwise returns <code>null</code>
222 public Date getRevision() {
223 return module.getRevision();
227 * Returns locally defined prefix assigned to local name
229 * @return locally defined prefix assigned to local name
231 public String getPrefix() {
236 public int hashCode() {
237 final int prime = 31;
239 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
240 result = prime * result + ((getNamespace() == null) ? 0 : getNamespace().hashCode());
241 result = prime * result + ((getFormattedRevision() == null) ? 0 : getFormattedRevision().hashCode());
247 * Compares the specified object with this list for equality. Returns
248 * <tt>true</tt> if and only if the specified object is also instance of
249 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
250 * {@link #getRevision()} are equals to same properties of this instance.
252 * @param o the object to be compared for equality with this QName
253 * @return <tt>true</tt> if the specified object is equal to this QName
257 public boolean equals(final Object obj) {
261 if (!(obj instanceof QName)) {
264 final QName other = (QName) obj;
265 if (localName == null) {
266 if (other.localName != null) {
269 } else if (!localName.equals(other.localName)) {
272 if (getNamespace() == null) {
273 if (other.getNamespace() != null) {
276 } else if (!getNamespace().equals(other.getNamespace())) {
279 if (getFormattedRevision() == null) {
280 if (other.getFormattedRevision() != null) {
283 } else if (!getRevision().equals(other.getRevision())) {
289 public static QName create(final QName base, final String localName) {
290 return new QName(base, localName);
297 * @param namespace Namespace of QName or null if namespace is undefined.
298 * @param revision Revision of namespace or null if revision is unspecified.
299 * @param localName Local name part of QName. MUST NOT BE null.
300 * @return Instance of QName
302 public static QName create(final URI namespace, final Date revision, final String localName) {
303 return new QName(QNameModule.create(namespace, revision), null,localName);
311 * Namespace of QName, MUST NOT BE Null.
313 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
314 * in format <code>YYYY-mm-dd</code>.
316 * Local name part of QName. MUST NOT BE null.
318 * @throws NullPointerException
319 * If any of paramaters is null.
320 * @throws IllegalArgumentException
321 * If <code>namespace</code> is not valid URI or
322 * <code>revision</code> is not according to format
323 * <code>YYYY-mm-dd</code>.
325 public static QName create(final String namespace, final String revision, final String localName)
326 throws IllegalArgumentException {
327 final URI namespaceUri;
329 namespaceUri = new URI(namespace);
330 } catch (URISyntaxException ue) {
331 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
334 Date revisionDate = parseRevision(revision);
335 return create(namespaceUri, revisionDate, localName);
339 public String toString() {
340 StringBuilder sb = new StringBuilder();
341 if (getNamespace() != null) {
342 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
344 if (getFormattedRevision() != null) {
345 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
347 sb.append(QNAME_RIGHT_PARENTHESIS);
349 sb.append(localName);
350 return sb.toString();
354 * Return string representation of revision in format
355 * <code>YYYY-mm-dd</code>
357 * YANG Specification defines format for <code>revision</code> as
358 * YYYY-mm-dd. This format for revision is reused accross multiple places
359 * such as capabilities URI, YANG modules, etc.
361 * @return String representation of revision or null, if revision is not
364 public String getFormattedRevision() {
365 return module.getFormattedRevision();
369 * Creates copy of this with revision and prefix unset.
371 * @return copy of this QName with revision and prefix unset.
373 public QName withoutRevision() {
374 return QName.create(getNamespace(), null, localName);
377 public static Date parseRevision(final String formatedDate) {
379 return getRevisionFormat().parse(formatedDate);
380 } catch (ParseException | RuntimeException e) {
381 throw new IllegalArgumentException(
382 String.format("Revision '%s'is not in a supported format", formatedDate), e);
387 * Formats {@link Date} representing revision to format
388 * <code>YYYY-mm-dd</code>
390 * YANG Specification defines format for <code>revision</code> as
391 * YYYY-mm-dd. This format for revision is reused accross multiple places
392 * such as capabilities URI, YANG modules, etc.
395 * Date object to format or null
396 * @return String representation or null if the input was null.
398 public static String formattedRevision(final Date revision) {
399 if (revision == null) {
402 return getRevisionFormat().format(revision);
407 * Compares this QName to other, without comparing revision.
409 * Compares instance of this to other instance of QName and returns true if
410 * both instances have equal <code>localName</code> ({@link #getLocalName()}
411 * ) and <code>namespace</code> ({@link #getNamespace()}).
414 * Other QName. Must not be null.
415 * @return true if this instance and other have equals localName and
417 * @throws NullPointerException
418 * if <code>other</code> is null.
420 public boolean isEqualWithoutRevision(final QName other) {
421 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
425 public int compareTo(final QName other) {
426 // compare mandatory localName parameter
427 int result = localName.compareTo(other.localName);
432 // compare nullable namespace parameter
433 if (getNamespace() == null) {
434 if (other.getNamespace() != null) {
438 if (other.getNamespace() == null) {
441 result = getNamespace().compareTo(other.getNamespace());
447 // compare nullable revision parameter
448 if (getRevision() == null) {
449 if (other.getRevision() != null) {
453 if (other.getRevision() == null) {
456 result = getRevision().compareTo(other.getRevision());