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.controller.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
17 * The QName from XML consists of local name of element and XML namespace, but
\r
18 * for our use, we added module revision to it.
\r
20 * In YANG context QName is full name of defined node, type, procedure or
\r
21 * notification. QName consists of XML namespace, YANG model revision and local
\r
22 * name of defined type. It is used to prevent name clashes between nodes with
\r
23 * same local name, but from different schemas.
\r
26 * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
\r
27 * defined element, type, procedure or notification.</li>
\r
28 * <li><b>Revision</b> - the revision of the YANG module which describes the
\r
30 * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
\r
31 * node in the YANG module</li>
\r
36 public class QName {
\r
38 private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
\r
40 final URI namespace;
\r
41 final String localName;
\r
42 final String prefix;
\r
43 final Date revision;
\r
46 * QName Constructor.
\r
49 * the namespace assigned to the YANG module
\r
51 * the revision of the YANG module
\r
53 * locally defined prefix assigned to local name
\r
55 * YANG schema identifier
\r
57 public QName(URI namespace, Date revision, String prefix, String localName) {
\r
58 this.namespace = namespace;
\r
59 this.localName = localName;
\r
60 this.revision = revision;
\r
61 this.prefix = prefix;
\r
65 * QName Constructor.
\r
68 * the namespace assigned to the YANG module
\r
70 * YANG schema identifier
\r
72 public QName(URI namespace, String localName) {
\r
73 this(namespace, null, "", localName);
\r
77 * QName Constructor.
\r
80 * the namespace assigned to the YANG module
\r
82 * the revision of the YANG module
\r
84 * YANG schema identifier
\r
86 public QName(URI namespace, Date revision, String localName) {
\r
87 this(namespace, revision, null, localName);
\r
90 public QName(QName base, String localName) {
\r
91 this(base.getNamespace(), base.getRevision(), base.getPrefix(),
\r
96 * Returns XMLNamespace assigned to the YANG module.
\r
98 * @return XMLNamespace assigned to the YANG module.
\r
100 public URI getNamespace() {
\r
105 * Returns YANG schema identifier which were defined for this node in the
\r
108 * @return YANG schema identifier which were defined for this node in the
\r
111 public String getLocalName() {
\r
116 * Returns revision of the YANG module if the module has defined revision,
\r
117 * otherwise returns <code>null</code>
\r
119 * @return revision of the YANG module if the module has defined revision,
\r
120 * otherwise returns <code>null</code>
\r
122 public Date getRevision() {
\r
127 * Returns locally defined prefix assigned to local name
\r
129 * @return locally defined prefix assigned to local name
\r
131 public String getPrefix() {
\r
136 public int hashCode() {
\r
137 final int prime = 31;
\r
139 result = prime * result
\r
140 + ((localName == null) ? 0 : localName.hashCode());
\r
141 result = prime * result
\r
142 + ((namespace == null) ? 0 : namespace.hashCode());
\r
143 result = prime * result
\r
144 + ((revision == null) ? 0 : revision.hashCode());
\r
149 public boolean equals(Object obj) {
\r
154 if (getClass() != obj.getClass())
\r
156 QName other = (QName) obj;
\r
157 if (localName == null) {
\r
158 if (other.localName != null)
\r
160 } else if (!localName.equals(other.localName))
\r
162 if (namespace == null) {
\r
163 if (other.namespace != null)
\r
165 } else if (!namespace.equals(other.namespace))
\r
167 if (revision == null) {
\r
168 if (other.revision != null)
\r
170 } else if (!revision.equals(other.revision))
\r
176 public String toString() {
\r
177 StringBuilder sb = new StringBuilder();
\r
178 if (namespace != null) {
\r
179 sb.append("(" + namespace);
\r
181 if (revision != null) {
\r
182 sb.append("?revision=" + revisionFormat.format(revision));
\r
186 sb.append(localName);
\r
187 return sb.toString();
\r
191 * Returns a namespace in form defined by section 5.6.4. of {@link https
\r
192 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
\r
193 * the method will return <code>null</code> <br>
\r
194 * example "http://example.acme.com/system?revision=2008-04-01"
\r
196 * @return namespace in form defined by section 5.6.4. of {@link https
\r
197 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
\r
198 * defined, the method will return <code>null</code>
\r
201 URI getRevisionNamespace() {
\r
203 if (namespace == null)
\r
207 if (revision != null) {
\r
208 query = "revision=" + revisionFormat.format(revision);
\r
211 URI compositeURI = null;
\r
213 compositeURI = new URI(namespace.getScheme(),
\r
214 namespace.getUserInfo(), namespace.getHost(),
\r
215 namespace.getPort(), namespace.getPath(), query,
\r
216 namespace.getFragment());
\r
217 } catch (URISyntaxException e) {
\r
218 e.printStackTrace();
\r
220 return compositeURI;
\r