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));
120 public static QName create(final String input) {
121 Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
122 if (matcher.matches()) {
123 String namespace = matcher.group(1);
124 String revision = matcher.group(2);
125 String localName = matcher.group(3);
126 return create(namespace, revision, localName);
128 matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
129 if (matcher.matches()) {
130 URI namespace = URI.create(matcher.group(1));
131 String localName = matcher.group(2);
132 return new QName(namespace, localName);
134 matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
135 if (matcher.matches()) {
136 String localName = matcher.group(1);
137 return new QName((URI) null, localName);
139 throw new IllegalArgumentException("Invalid input:" + input);
143 * Get the module component of the QName.
145 * @return Module component
147 public QNameModule getModule() {
152 * Returns XMLNamespace assigned to the YANG module.
154 * @return XMLNamespace assigned to the YANG module.
156 public URI getNamespace() {
157 return module.getNamespace();
161 * Returns YANG schema identifier which were defined for this node in the
164 * @return YANG schema identifier which were defined for this node in the
167 public String getLocalName() {
172 * Returns revision of the YANG module if the module has defined revision,
173 * otherwise returns <code>null</code>
175 * @return revision of the YANG module if the module has defined revision,
176 * otherwise returns <code>null</code>
178 public Date getRevision() {
179 return module.getRevision();
183 * Returns locally defined prefix assigned to local name
185 * @return locally defined prefix assigned to local name
187 public String getPrefix() {
192 public int hashCode() {
193 final int prime = 31;
195 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
196 result = prime * result + module.hashCode();
202 * Compares the specified object with this list for equality. Returns
203 * <tt>true</tt> if and only if the specified object is also instance of
204 * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
205 * {@link #getRevision()} are equals to same properties of this instance.
207 * @param o the object to be compared for equality with this QName
208 * @return <tt>true</tt> if the specified object is equal to this QName
212 public boolean equals(final Object obj) {
216 if (!(obj instanceof QName)) {
219 final QName other = (QName) obj;
220 if (localName == null) {
221 if (other.localName != null) {
224 } else if (!localName.equals(other.localName)) {
227 return module.equals(other.module);
230 public static QName create(final QName base, final String localName) {
231 return new QName(base.getModule(), base.getPrefix(), localName);
238 * Namespace and revision enclosed as a QNameModule
242 * Local name part of QName. MUST NOT BE null.
243 * @return Instance of QName
245 public static QName create(final QNameModule module, final String prefix, final String localName) {
246 if (module == null) {
247 throw new NullPointerException("module may not be null");
249 return new QName(module, prefix, localName);
256 * Namespace and revision enclosed as a QNameModule
258 * Local name part of QName. MUST NOT BE null.
259 * @return Instance of QName
261 public static QName create(final QNameModule qnameModule, final String localName) {
262 return new QName(qnameModule, null, localName);
269 * Namespace of QName or null if namespace is undefined.
271 * Revision of namespace or null if revision is unspecified.
273 * Local name part of QName. MUST NOT BE null.
274 * @return Instance of QName
276 public static QName create(final URI namespace, final Date revision, final String localName) {
277 return new QName(QNameModule.create(namespace, revision), null, localName);
285 * Namespace of QName, MUST NOT BE Null.
287 * Revision of namespace / YANG module. MUST NOT BE null, MUST BE
288 * in format <code>YYYY-mm-dd</code>.
290 * Local name part of QName. MUST NOT BE null.
292 * @throws NullPointerException
293 * If any of paramaters is null.
294 * @throws IllegalArgumentException
295 * If <code>namespace</code> is not valid URI or
296 * <code>revision</code> is not according to format
297 * <code>YYYY-mm-dd</code>.
299 public static QName create(final String namespace, final String revision, final String localName)
300 throws IllegalArgumentException {
301 final URI namespaceUri;
303 namespaceUri = new URI(namespace);
304 } catch (URISyntaxException ue) {
305 throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
308 Date revisionDate = parseRevision(revision);
309 return create(namespaceUri, revisionDate, localName);
313 public String toString() {
314 StringBuilder sb = new StringBuilder();
315 if (getNamespace() != null) {
316 sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
318 if (getFormattedRevision() != null) {
319 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
321 sb.append(QNAME_RIGHT_PARENTHESIS);
323 sb.append(localName);
324 return sb.toString();
328 * Return string representation of revision in format
329 * <code>YYYY-mm-dd</code>
331 * YANG Specification defines format for <code>revision</code> as
332 * YYYY-mm-dd. This format for revision is reused accross multiple places
333 * such as capabilities URI, YANG modules, etc.
335 * @return String representation of revision or null, if revision is not
338 public String getFormattedRevision() {
339 return module.getFormattedRevision();
343 * Creates copy of this with revision and prefix unset.
345 * @return copy of this QName with revision and prefix unset.
347 public QName withoutRevision() {
348 return QName.create(getNamespace(), null, localName);
351 public static Date parseRevision(final String formatedDate) {
353 return getRevisionFormat().parse(formatedDate);
354 } catch (ParseException | RuntimeException e) {
355 throw new IllegalArgumentException(
356 String.format("Revision '%s'is not in a supported format", formatedDate), e);
361 * Formats {@link Date} representing revision to format
362 * <code>YYYY-mm-dd</code>
364 * YANG Specification defines format for <code>revision</code> as
365 * YYYY-mm-dd. This format for revision is reused accross multiple places
366 * such as capabilities URI, YANG modules, etc.
369 * Date object to format or null
370 * @return String representation or null if the input was null.
372 public static String formattedRevision(final Date revision) {
373 if (revision == null) {
376 return getRevisionFormat().format(revision);
381 * Compares this QName to other, without comparing revision.
383 * Compares instance of this to other instance of QName and returns true if
384 * both instances have equal <code>localName</code> ({@link #getLocalName()}
385 * ) and <code>namespace</code> ({@link #getNamespace()}).
388 * Other QName. Must not be null.
389 * @return true if this instance and other have equals localName and
391 * @throws NullPointerException
392 * if <code>other</code> is null.
394 public boolean isEqualWithoutRevision(final QName other) {
395 return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
399 public int compareTo(final QName other) {
400 // compare mandatory localName parameter
401 int result = localName.compareTo(other.localName);
406 // compare nullable namespace parameter
407 if (getNamespace() == null) {
408 if (other.getNamespace() != null) {
412 if (other.getNamespace() == null) {
415 result = getNamespace().compareTo(other.getNamespace());
421 // compare nullable revision parameter
422 if (getRevision() == null) {
423 if (other.getRevision() != null) {
427 if (other.getRevision() == null) {
430 result = getRevision().compareTo(other.getRevision());