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;
11 import java.net.URISyntaxException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
15 import javax.management.ImmutableDescriptor;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * The QName from XML consists of local name of element and XML namespace, but
23 * for our use, we added module revision to it.
25 * In YANG context QName is full name of defined node, type, procedure or
26 * notification. QName consists of XML namespace, YANG model revision and local
27 * name of defined type. It is used to prevent name clashes between nodes with
28 * same local name, but from different schemas.
31 * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
32 * defined element, type, procedure or notification.</li>
33 * <li><b>Revision</b> - the revision of the YANG module which describes the
35 * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
36 * node in the YANG module</li>
41 public final class QName implements Immutable {
42 protected static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
44 private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
46 private final URI namespace;
47 private final String localName;
48 private final String prefix;
49 private final Date revision;
55 * the namespace assigned to the YANG module
57 * the revision of the YANG module
59 * locally defined prefix assigned to local name
61 * YANG schema identifier
63 public QName(URI namespace, Date revision, String prefix, String localName) {
64 this.namespace = namespace;
65 this.localName = localName;
66 this.revision = revision;
74 * the namespace assigned to the YANG module
76 * YANG schema identifier
78 public QName(URI namespace, String localName) {
79 this(namespace, null, "", localName);
86 * the namespace assigned to the YANG module
88 * the revision of the YANG module
90 * YANG schema identifier
92 public QName(URI namespace, Date revision, String localName) {
93 this(namespace, revision, null, localName);
96 public QName(QName base, String localName) {
97 this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
101 * Returns XMLNamespace assigned to the YANG module.
103 * @return XMLNamespace assigned to the YANG module.
105 public URI getNamespace() {
110 * Returns YANG schema identifier which were defined for this node in the
113 * @return YANG schema identifier which were defined for this node in the
116 public String getLocalName() {
121 * Returns revision of the YANG module if the module has defined revision,
122 * otherwise returns <code>null</code>
124 * @return revision of the YANG module if the module has defined revision,
125 * otherwise returns <code>null</code>
127 public Date getRevision() {
132 * Returns locally defined prefix assigned to local name
134 * @return locally defined prefix assigned to local name
136 public String getPrefix() {
141 public int hashCode() {
142 final int prime = 31;
144 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
145 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
146 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
151 public boolean equals(Object obj) {
158 if (getClass() != obj.getClass()) {
161 QName other = (QName) obj;
162 if (localName == null) {
163 if (other.localName != null) {
166 } else if (!localName.equals(other.localName)) {
169 if (namespace == null) {
170 if (other.namespace != null) {
173 } else if (!namespace.equals(other.namespace)) {
176 if (revision == null) {
177 if (other.revision != null) {
180 } else if (!revision.equals(other.revision)) {
187 public String toString() {
188 StringBuilder sb = new StringBuilder();
189 if (namespace != null) {
190 sb.append("(" + namespace);
192 if (revision != null) {
193 sb.append("?revision=" + revisionFormat.format(revision));
197 sb.append(localName);
198 return sb.toString();
202 * Returns a namespace in form defined by section 5.6.4. of {@link https
203 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
204 * the method will return <code>null</code> <br>
205 * example "http://example.acme.com/system?revision=2008-04-01"
207 * @return namespace in form defined by section 5.6.4. of {@link https
208 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
209 * defined, the method will return <code>null</code>
212 URI getRevisionNamespace() {
214 if (namespace == null) {
219 if (revision != null) {
220 query = "revision=" + revisionFormat.format(revision);
223 URI compositeURI = null;
225 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
226 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
227 } catch (URISyntaxException e) {