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.ParseException;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
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,Serializable {
43 private static final long serialVersionUID = 5398411242927766414L;
45 protected static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
47 private static final SimpleDateFormat REVISION_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
49 private final URI namespace;
50 private final String localName;
51 private final String prefix;
52 private final String formattedRevision;
53 private final Date revision;
59 * the namespace assigned to the YANG module
61 * the revision of the YANG module
63 * locally defined prefix assigned to local name
65 * YANG schema identifier
67 public QName(URI namespace, Date revision, String prefix, String localName) {
68 this.namespace = namespace;
69 this.localName = localName;
70 this.revision = revision;
72 if(revision != null) {
73 this.formattedRevision = REVISION_FORMAT.format(revision);
75 this.formattedRevision = null;
83 * the namespace assigned to the YANG module
85 * YANG schema identifier
87 public QName(URI namespace, String localName) {
88 this(namespace, null, "", localName);
95 * the namespace assigned to the YANG module
97 * the revision of the YANG module
99 * YANG schema identifier
101 public QName(URI namespace, Date revision, String localName) {
102 this(namespace, revision, null, localName);
105 public QName(QName base, String localName) {
106 this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
110 * Returns XMLNamespace assigned to the YANG module.
112 * @return XMLNamespace assigned to the YANG module.
114 public URI getNamespace() {
119 * Returns YANG schema identifier which were defined for this node in the
122 * @return YANG schema identifier which were defined for this node in the
125 public String getLocalName() {
130 * Returns revision of the YANG module if the module has defined revision,
131 * otherwise returns <code>null</code>
133 * @return revision of the YANG module if the module has defined revision,
134 * otherwise returns <code>null</code>
136 public Date getRevision() {
141 * Returns locally defined prefix assigned to local name
143 * @return locally defined prefix assigned to local name
145 public String getPrefix() {
150 public int hashCode() {
151 final int prime = 31;
153 result = prime * result + ((localName == null) ? 0 : localName.hashCode());
154 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
155 result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
160 public boolean equals(Object obj) {
167 if (getClass() != obj.getClass()) {
170 QName other = (QName) obj;
171 if (localName == null) {
172 if (other.localName != null) {
175 } else if (!localName.equals(other.localName)) {
178 if (namespace == null) {
179 if (other.namespace != null) {
182 } else if (!namespace.equals(other.namespace)) {
185 if (formattedRevision == null) {
186 if (other.formattedRevision != null) {
189 } else if (!revision.equals(other.revision)) {
196 public static QName create(QName base, String localName){
197 return new QName(base, localName);
200 public static QName create(URI namespace, Date revision, String localName){
201 return new QName(namespace, revision, localName);
205 public static QName create(String namespace, String revision, String localName) throws IllegalArgumentException{
207 URI namespaceUri = new URI(namespace);
208 Date revisionDate = REVISION_FORMAT.parse(revision);
209 return create(namespaceUri, revisionDate, localName);
210 } catch (ParseException pe) {
211 throw new IllegalArgumentException("Revision is not in supported format", pe);
212 } catch (URISyntaxException ue) {
213 throw new IllegalArgumentException("Namespace is is not valid URI", ue);
218 public String toString() {
219 StringBuilder sb = new StringBuilder();
220 if (namespace != null) {
221 sb.append("(" + namespace);
223 if (revision != null) {
224 sb.append("?revision=" + REVISION_FORMAT.format(revision));
228 sb.append(localName);
229 return sb.toString();
233 * Returns a namespace in form defined by section 5.6.4. of {@link https
234 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
235 * the method will return <code>null</code> <br>
236 * example "http://example.acme.com/system?revision=2008-04-01"
238 * @return namespace in form defined by section 5.6.4. of {@link https
239 * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
240 * defined, the method will return <code>null</code>
243 URI getRevisionNamespace() {
245 if (namespace == null) {
250 if (revision != null) {
251 query = "revision=" + formattedRevision;
254 URI compositeURI = null;
256 compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
257 namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
258 } catch (URISyntaxException e) {
264 public String getFormattedRevision() {
265 return formattedRevision;