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;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * The QName from XML consists of local name of element and XML namespace, but
21 * for our use, we added module revision to it.
23 * In YANG context QName is full name of defined node, type, procedure or
24 * notification. QName consists of XML namespace, YANG model revision and local
25 * name of defined type. It is used to prevent name clashes between nodes with
26 * same local name, but from different schemas.
29 * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
30 * defined element, type, procedure or notification.</li>
31 * <li><b>Revision</b> - the revision of the YANG module which describes the
33 * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
34 * node in the YANG module</li>
39 public final class QName {
40 protected static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
42 private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
44 private final URI namespace;
45 private final String localName;
46 private final String prefix;
47 private final Date revision;
53 * the namespace assigned to the YANG module
55 * the revision of the YANG module
57 * locally defined prefix assigned to local name
59 * YANG schema identifier
61 public QName(URI namespace, Date revision, String prefix, String localName) {
62 this.namespace = namespace;
63 this.localName = localName;
64 this.revision = revision;
72 * the namespace assigned to the YANG module
74 * YANG schema identifier
76 public QName(URI namespace, String localName) {
77 this(namespace, null, "", localName);
84 * the namespace assigned to the YANG module
86 * the revision of the YANG module
88 * YANG schema identifier
90 public QName(URI namespace, Date revision, String localName) {
91 this(namespace, revision, null, localName);
94 public QName(QName base, String localName) {
95 this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
99 * Returns XMLNamespace assigned to the YANG module.
101 * @return XMLNamespace assigned to the YANG module.
103 public URI getNamespace() {
108 * Returns YANG schema identifier which were defined for this node in the
111 * @return YANG schema identifier which were defined for this node in the
114 public String getLocalName() {
119 * Returns revision of the YANG module if the module has defined revision,
120 * otherwise returns <code>null</code>
122 * @return revision of the YANG module if the module has defined revision,
123 * otherwise returns <code>null</code>
125 public Date getRevision() {
130 * Returns locally defined prefix assigned to local name
132 * @return locally defined prefix assigned to local name
134 public String getPrefix() {
139 public int hashCode() {
140 final int prime = 31;
142 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
143 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
144 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
149 public boolean equals(Object obj) {
156 if (getClass() != obj.getClass()) {
159 QName other = (QName) obj;
160 if (localName == null) {
161 if (other.localName != null) {
164 } else if (!localName.equals(other.localName)) {
167 if (namespace == null) {
168 if (other.namespace != null) {
171 } else if (!namespace.equals(other.namespace)) {
174 if (revision == null) {
175 if (other.revision != null) {
178 } else if (!revision.equals(other.revision)) {
185 public String toString() {
186 StringBuilder sb = new StringBuilder();
187 if (namespace != null) {
188 sb.append("(" + namespace);
190 if (revision != null) {
191 sb.append("?revision=" + revisionFormat.format(revision));
195 sb.append(localName);
196 return sb.toString();
200 * Returns a namespace in form defined by section 5.6.4. of {@link https
201 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
202 * the method will return <code>null</code> <br>
203 * example "http://example.acme.com/system?revision=2008-04-01"
205 * @return namespace in form defined by section 5.6.4. of {@link https
206 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
207 * defined, the method will return <code>null</code>
210 URI getRevisionNamespace() {
212 if (namespace == null) {
217 if (revision != null) {
218 query = "revision=" + revisionFormat.format(revision);
221 URI compositeURI = null;
223 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
224 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
225 } catch (URISyntaxException e) {