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 java.io.Serializable;
12 import java.net.URISyntaxException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * The QName from XML consists of local name of element and XML namespace, but
22 * for our use, we added module revision to it.
24 * In YANG context QName is full name of defined node, type, procedure or
25 * notification. QName consists of XML namespace, YANG model revision and local
26 * name of defined type. It is used to prevent name clashes between nodes with
27 * same local name, but from different schemas.
30 * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
31 * defined element, type, procedure or notification.</li>
32 * <li><b>Revision</b> - the revision of the YANG module which describes the
34 * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
35 * node in the YANG module</li>
40 public final class QName implements Immutable,Serializable {
42 private static final long serialVersionUID = 5398411242927766414L;
44 protected static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
46 private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
48 private final URI namespace;
49 private final String localName;
50 private final String prefix;
51 private final Date revision;
57 * the namespace assigned to the YANG module
59 * the revision of the YANG module
61 * locally defined prefix assigned to local name
63 * YANG schema identifier
65 public QName(URI namespace, Date revision, String prefix, String localName) {
66 this.namespace = namespace;
67 this.localName = localName;
68 this.revision = revision;
76 * the namespace assigned to the YANG module
78 * YANG schema identifier
80 public QName(URI namespace, String localName) {
81 this(namespace, null, "", localName);
88 * the namespace assigned to the YANG module
90 * the revision of the YANG module
92 * YANG schema identifier
94 public QName(URI namespace, Date revision, String localName) {
95 this(namespace, revision, null, localName);
98 public QName(QName base, String localName) {
99 this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
103 * Returns XMLNamespace assigned to the YANG module.
105 * @return XMLNamespace assigned to the YANG module.
107 public URI getNamespace() {
112 * Returns YANG schema identifier which were defined for this node in the
115 * @return YANG schema identifier which were defined for this node in the
118 public String getLocalName() {
123 * Returns revision of the YANG module if the module has defined revision,
124 * otherwise returns <code>null</code>
126 * @return revision of the YANG module if the module has defined revision,
127 * otherwise returns <code>null</code>
129 public Date getRevision() {
134 * Returns locally defined prefix assigned to local name
136 * @return locally defined prefix assigned to local name
138 public String getPrefix() {
143 public int hashCode() {
144 final int prime = 31;
146 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
147 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
148 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
153 public boolean equals(Object obj) {
160 if (getClass() != obj.getClass()) {
163 QName other = (QName) obj;
164 if (localName == null) {
165 if (other.localName != null) {
168 } else if (!localName.equals(other.localName)) {
171 if (namespace == null) {
172 if (other.namespace != null) {
175 } else if (!namespace.equals(other.namespace)) {
178 if (revision == null) {
179 if (other.revision != null) {
182 } else if (!revision.equals(other.revision)) {
189 public String toString() {
190 StringBuilder sb = new StringBuilder();
191 if (namespace != null) {
192 sb.append("(" + namespace);
194 if (revision != null) {
195 sb.append("?revision=" + revisionFormat.format(revision));
199 sb.append(localName);
200 return sb.toString();
204 * Returns a namespace in form defined by section 5.6.4. of {@link https
205 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
206 * the method will return <code>null</code> <br>
207 * example "http://example.acme.com/system?revision=2008-04-01"
209 * @return namespace in form defined by section 5.6.4. of {@link https
210 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
211 * defined, the method will return <code>null</code>
214 URI getRevisionNamespace() {
216 if (namespace == null) {
221 if (revision != null) {
222 query = "revision=" + revisionFormat.format(revision);
225 URI compositeURI = null;
227 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
228 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
229 } catch (URISyntaxException e) {