2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.yangtools.yang.common;
\r
10 import java.net.URI;
\r
11 import java.net.URISyntaxException;
\r
13 import java.text.SimpleDateFormat;
\r
14 import java.util.Date;
\r
16 import org.slf4j.Logger;
\r
17 import org.slf4j.LoggerFactory;
\r
20 * The QName from XML consists of local name of element and XML namespace, but
\r
21 * for our use, we added module revision to it.
\r
23 * In YANG context QName is full name of defined node, type, procedure or
\r
24 * notification. QName consists of XML namespace, YANG model revision and local
\r
25 * name of defined type. It is used to prevent name clashes between nodes with
\r
26 * same local name, but from different schemas.
\r
29 * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
\r
30 * defined element, type, procedure or notification.</li>
\r
31 * <li><b>Revision</b> - the revision of the YANG module which describes the
\r
33 * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
\r
34 * node in the YANG module</li>
\r
39 public final class QName {
\r
40 protected static final Logger logger = LoggerFactory
\r
41 .getLogger(QName.class);
\r
43 private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
\r
45 final URI namespace;
\r
46 final String localName;
\r
47 final String prefix;
\r
48 final Date revision;
\r
51 * QName Constructor.
\r
54 * the namespace assigned to the YANG module
\r
56 * the revision of the YANG module
\r
58 * locally defined prefix assigned to local name
\r
60 * YANG schema identifier
\r
62 public QName(URI namespace, Date revision, String prefix, String localName) {
\r
63 this.namespace = namespace;
\r
64 this.localName = localName;
\r
65 this.revision = revision;
\r
66 this.prefix = prefix;
\r
70 * QName Constructor.
\r
73 * the namespace assigned to the YANG module
\r
75 * YANG schema identifier
\r
77 public QName(URI namespace, String localName) {
\r
78 this(namespace, null, "", localName);
\r
82 * QName Constructor.
\r
85 * the namespace assigned to the YANG module
\r
87 * the revision of the YANG module
\r
89 * YANG schema identifier
\r
91 public QName(URI namespace, Date revision, String localName) {
\r
92 this(namespace, revision, null, localName);
\r
95 public QName(QName base, String localName) {
\r
96 this(base.getNamespace(), base.getRevision(), base.getPrefix(),
\r
101 * Returns XMLNamespace assigned to the YANG module.
\r
103 * @return XMLNamespace assigned to the YANG module.
\r
105 public URI getNamespace() {
\r
110 * Returns YANG schema identifier which were defined for this node in the
\r
113 * @return YANG schema identifier which were defined for this node in the
\r
116 public String getLocalName() {
\r
121 * Returns revision of the YANG module if the module has defined revision,
\r
122 * otherwise returns <code>null</code>
\r
124 * @return revision of the YANG module if the module has defined revision,
\r
125 * otherwise returns <code>null</code>
\r
127 public Date getRevision() {
\r
132 * Returns locally defined prefix assigned to local name
\r
134 * @return locally defined prefix assigned to local name
\r
136 public String getPrefix() {
\r
141 public int hashCode() {
\r
142 final int prime = 31;
\r
144 result = prime * result
\r
145 + ((localName == null) ? 0 : localName.hashCode());
\r
146 result = prime * result
\r
147 + ((namespace == null) ? 0 : namespace.hashCode());
\r
148 result = prime * result
\r
149 + ((revision == null) ? 0 : revision.hashCode());
\r
154 public boolean equals(Object obj) {
\r
159 if (getClass() != obj.getClass())
\r
161 QName other = (QName) obj;
\r
162 if (localName == null) {
\r
163 if (other.localName != null)
\r
165 } else if (!localName.equals(other.localName))
\r
167 if (namespace == null) {
\r
168 if (other.namespace != null)
\r
170 } else if (!namespace.equals(other.namespace))
\r
172 if (revision == null) {
\r
173 if (other.revision != null)
\r
175 } else if (!revision.equals(other.revision))
\r
181 public String toString() {
\r
182 StringBuilder sb = new StringBuilder();
\r
183 if (namespace != null) {
\r
184 sb.append("(" + namespace);
\r
186 if (revision != null) {
\r
187 sb.append("?revision=" + revisionFormat.format(revision));
\r
191 sb.append(localName);
\r
192 return sb.toString();
\r
196 * Returns a namespace in form defined by section 5.6.4. of {@link https
\r
197 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
\r
198 * the method will return <code>null</code> <br>
\r
199 * example "http://example.acme.com/system?revision=2008-04-01"
\r
201 * @return namespace in form defined by section 5.6.4. of {@link https
\r
202 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
\r
203 * defined, the method will return <code>null</code>
\r
206 URI getRevisionNamespace() {
\r
208 if (namespace == null)
\r
212 if (revision != null) {
\r
213 query = "revision=" + revisionFormat.format(revision);
\r
216 URI compositeURI = null;
\r
218 compositeURI = new URI(namespace.getScheme(),
\r
219 namespace.getUserInfo(), namespace.getHost(),
\r
220 namespace.getPort(), namespace.getPath(), query,
\r
221 namespace.getFragment());
\r
222 } catch (URISyntaxException e) {
\r
223 logger.error("",e);
\r
225 return compositeURI;
\r