BUG-865: prune deprecated constructors
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QName.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat;
11
12 import java.io.Serializable;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.text.ParseException;
16 import java.util.Date;
17 import java.util.Objects;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.opendaylight.yangtools.concepts.Immutable;
22
23 /**
24  * The QName from XML consists of local name of element and XML namespace, but
25  * for our use, we added module revision to it.
26  *
27  * In YANG context QName is full name of defined node, type, procedure or
28  * notification. QName consists of XML namespace, YANG model revision and local
29  * name of defined type. It is used to prevent name clashes between nodes with
30  * same local name, but from different schemas.
31  *
32  * <ul>
33  * <li><b>XMLNamespace</b> - {@link #getNamespace()} - the namespace assigned to the YANG module which
34  * defined element, type, procedure or notification.</li>
35  * <li><b>Revision</b> - {@link #getRevision()} - the revision of the YANG module which describes the
36  * element</li>
37  * <li><b>LocalName</b> - {@link #getLocalName()} - the YANG schema identifier which were defined for this
38  * node in the YANG module</li>
39  * </ul>
40  *
41  * QName may also have <code>prefix</code> assigned, but prefix does not
42  * affect equality and identity of two QNames and carry only information
43  * which may be useful for serializers / deserializers.
44  *
45  *
46  */
47 public final class QName implements Immutable, Serializable, Comparable<QName> {
48     private static final long serialVersionUID = 5398411242927766414L;
49
50     static final String QNAME_REVISION_DELIMITER = "?revision=";
51     static final String QNAME_LEFT_PARENTHESIS = "(";
52     static final String QNAME_RIGHT_PARENTHESIS = ")";
53
54     private static final Pattern QNAME_PATTERN_FULL = Pattern.compile("^\\((.+)\\" + QNAME_REVISION_DELIMITER
55             + "(.+)\\)(.+)$");
56     private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
57     private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
58
59     private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
60
61     // Mandatory
62     private final QNameModule module;
63     // Mandatory
64     private final String localName;
65     // Nullable
66     private final String prefix;
67
68     private QName(final QNameModule module, final String prefix, final String localName) {
69         this.localName = checkLocalName(localName);
70         this.prefix = prefix;
71         this.module = module;
72     }
73
74     /**
75      * QName Constructor.
76      *
77      * @param namespace
78      *            the namespace assigned to the YANG module
79      * @param revision
80      *            the revision of the YANG module
81      * @param prefix
82      *            locally defined prefix assigned to local name
83      * @param localName
84      *            YANG schema identifier
85      *
86      */
87     public QName(final URI namespace, final Date revision, final String prefix, final String localName) {
88         this(QNameModule.create(namespace, revision), prefix, localName);
89     }
90
91     /**
92      * QName Constructor.
93      *
94      * @param namespace
95      *            the namespace assigned to the YANG module
96      * @param localName
97      *            YANG schema identifier
98      */
99     public QName(final URI namespace, final String localName) {
100         this(namespace, null, "", localName);
101     }
102
103     private static String checkLocalName(final String localName) {
104         if (localName == null) {
105             throw new IllegalArgumentException("Parameter 'localName' may not be null.");
106         }
107         if (localName.length() == 0) {
108             throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
109         }
110
111         for (char c : ILLEGAL_CHARACTERS) {
112             if (localName.indexOf(c) != -1) {
113                 throw new IllegalArgumentException(String.format(
114                         "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
115             }
116         }
117         return localName;
118     }
119
120     public static QName create(final String input) {
121         Matcher matcher = QNAME_PATTERN_FULL.matcher(input);
122         if (matcher.matches()) {
123             String namespace = matcher.group(1);
124             String revision = matcher.group(2);
125             String localName = matcher.group(3);
126             return create(namespace, revision, localName);
127         }
128         matcher = QNAME_PATTERN_NO_REVISION.matcher(input);
129         if (matcher.matches()) {
130             URI namespace = URI.create(matcher.group(1));
131             String localName = matcher.group(2);
132             return new QName(namespace, localName);
133         }
134         matcher = QNAME_PATTERN_NO_NAMESPACE_NO_REVISION.matcher(input);
135         if (matcher.matches()) {
136             String localName = matcher.group(1);
137             return new QName((URI) null, localName);
138         }
139         throw new IllegalArgumentException("Invalid input:" + input);
140     }
141
142     /**
143      * Get the module component of the QName.
144      *
145      * @return Module component
146      */
147     public QNameModule getModule() {
148         return module;
149     }
150
151     /**
152      * Returns XMLNamespace assigned to the YANG module.
153      *
154      * @return XMLNamespace assigned to the YANG module.
155      */
156     public URI getNamespace() {
157         return module.getNamespace();
158     }
159
160     /**
161      * Returns YANG schema identifier which were defined for this node in the
162      * YANG module
163      *
164      * @return YANG schema identifier which were defined for this node in the
165      *         YANG module
166      */
167     public String getLocalName() {
168         return localName;
169     }
170
171     /**
172      * Returns revision of the YANG module if the module has defined revision,
173      * otherwise returns <code>null</code>
174      *
175      * @return revision of the YANG module if the module has defined revision,
176      *         otherwise returns <code>null</code>
177      */
178     public Date getRevision() {
179         return module.getRevision();
180     }
181
182     /**
183      * Returns locally defined prefix assigned to local name
184      *
185      * @return locally defined prefix assigned to local name
186      */
187     public String getPrefix() {
188         return prefix;
189     }
190
191     @Override
192     public int hashCode() {
193         final int prime = 31;
194         int result = 1;
195         result = prime * result + ((localName == null) ? 0 : localName.hashCode());
196         result = prime * result + module.hashCode();
197         return result;
198     }
199
200     /**
201      *
202      * Compares the specified object with this list for equality.  Returns
203      * <tt>true</tt> if and only if the specified object is also instance of
204      * {@link QName} and its {@link #getLocalName()}, {@link #getNamespace()} and
205      * {@link #getRevision()} are equals to same properties of this instance.
206      *
207      * @param o the object to be compared for equality with this QName
208      * @return <tt>true</tt> if the specified object is equal to this QName
209      *
210      */
211     @Override
212     public boolean equals(final Object obj) {
213         if (this == obj) {
214             return true;
215         }
216         if (!(obj instanceof QName)) {
217             return false;
218         }
219         final QName other = (QName) obj;
220         if (localName == null) {
221             if (other.localName != null) {
222                 return false;
223             }
224         } else if (!localName.equals(other.localName)) {
225             return false;
226         }
227         return module.equals(other.module);
228     }
229
230     public static QName create(final QName base, final String localName) {
231         return new QName(base.getModule(), base.getPrefix(), localName);
232     }
233
234     /**
235      * Creates new QName.
236      *
237      * @param qnameModule
238      *            Namespace and revision enclosed as a QNameModule
239      * @param prefix
240      *            Namespace prefix
241      * @param localName
242      *            Local name part of QName. MUST NOT BE null.
243      * @return Instance of QName
244      */
245     public static QName create(final QNameModule module, final String prefix, final String localName) {
246         if (module == null) {
247             throw new NullPointerException("module may not be null");
248         }
249         return new QName(module, prefix, localName);
250     }
251
252     /**
253      * Creates new QName.
254      *
255      * @param qnameModule
256      *            Namespace and revision enclosed as a QNameModule
257      * @param localName
258      *            Local name part of QName. MUST NOT BE null.
259      * @return Instance of QName
260      */
261     public static QName create(final QNameModule qnameModule, final String localName) {
262         return new QName(qnameModule, null, localName);
263     }
264
265     /**
266      * Creates new QName.
267      *
268      * @param namespace
269      *            Namespace of QName or null if namespace is undefined.
270      * @param revision
271      *            Revision of namespace or null if revision is unspecified.
272      * @param localName
273      *            Local name part of QName. MUST NOT BE null.
274      * @return Instance of QName
275      */
276     public static QName create(final URI namespace, final Date revision, final String localName) {
277         return new QName(QNameModule.create(namespace, revision), null, localName);
278     }
279
280     /**
281      *
282      * Creates new QName.
283      *
284      * @param namespace
285      *            Namespace of QName, MUST NOT BE Null.
286      * @param revision
287      *            Revision of namespace / YANG module. MUST NOT BE null, MUST BE
288      *            in format <code>YYYY-mm-dd</code>.
289      * @param localName
290      *            Local name part of QName. MUST NOT BE null.
291      * @return
292      * @throws NullPointerException
293      *             If any of paramaters is null.
294      * @throws IllegalArgumentException
295      *             If <code>namespace</code> is not valid URI or
296      *             <code>revision</code> is not according to format
297      *             <code>YYYY-mm-dd</code>.
298      */
299     public static QName create(final String namespace, final String revision, final String localName)
300             throws IllegalArgumentException {
301         final URI namespaceUri;
302         try {
303             namespaceUri = new URI(namespace);
304         } catch (URISyntaxException ue) {
305             throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
306         }
307
308         Date revisionDate = parseRevision(revision);
309         return create(namespaceUri, revisionDate, localName);
310     }
311
312     @Override
313     public String toString() {
314         StringBuilder sb = new StringBuilder();
315         if (getNamespace() != null) {
316             sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
317
318             if (getFormattedRevision() != null) {
319                 sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
320             }
321             sb.append(QNAME_RIGHT_PARENTHESIS);
322         }
323         sb.append(localName);
324         return sb.toString();
325     }
326
327     /**
328      * Return string representation of revision in format
329      * <code>YYYY-mm-dd</code>
330      *
331      * YANG Specification defines format for <code>revision</code> as
332      * YYYY-mm-dd. This format for revision is reused accross multiple places
333      * such as capabilities URI, YANG modules, etc.
334      *
335      * @return String representation of revision or null, if revision is not
336      *         set.
337      */
338     public String getFormattedRevision() {
339         return module.getFormattedRevision();
340     }
341
342     /**
343      * Creates copy of this with revision and prefix unset.
344      *
345      * @return copy of this QName with revision and prefix unset.
346      */
347     public QName withoutRevision() {
348         return QName.create(getNamespace(), null, localName);
349     }
350
351     public static Date parseRevision(final String formatedDate) {
352         try {
353             return getRevisionFormat().parse(formatedDate);
354         } catch (ParseException | RuntimeException e) {
355             throw new IllegalArgumentException(
356                     String.format("Revision '%s'is not in a supported format", formatedDate), e);
357         }
358     }
359
360     /**
361      * Formats {@link Date} representing revision to format
362      * <code>YYYY-mm-dd</code>
363      *
364      * YANG Specification defines format for <code>revision</code> as
365      * YYYY-mm-dd. This format for revision is reused accross multiple places
366      * such as capabilities URI, YANG modules, etc.
367      *
368      * @param revision
369      *            Date object to format or null
370      * @return String representation or null if the input was null.
371      */
372     public static String formattedRevision(final Date revision) {
373         if (revision == null) {
374             return null;
375         }
376         return getRevisionFormat().format(revision);
377     }
378
379     /**
380      *
381      * Compares this QName to other, without comparing revision.
382      *
383      * Compares instance of this to other instance of QName and returns true if
384      * both instances have equal <code>localName</code> ({@link #getLocalName()}
385      * ) and <code>namespace</code> ({@link #getNamespace()}).
386      *
387      * @param other
388      *            Other QName. Must not be null.
389      * @return true if this instance and other have equals localName and
390      *         namespace.
391      * @throws NullPointerException
392      *             if <code>other</code> is null.
393      */
394     public boolean isEqualWithoutRevision(final QName other) {
395         return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
396     }
397
398     @Override
399     public int compareTo(final QName other) {
400         // compare mandatory localName parameter
401         int result = localName.compareTo(other.localName);
402         if (result != 0) {
403             return result;
404         }
405
406         // compare nullable namespace parameter
407         if (getNamespace() == null) {
408             if (other.getNamespace() != null) {
409                 return -1;
410             }
411         } else {
412             if (other.getNamespace() == null) {
413                 return 1;
414             }
415             result = getNamespace().compareTo(other.getNamespace());
416             if (result != 0) {
417                 return result;
418             }
419         }
420
421         // compare nullable revision parameter
422         if (getRevision() == null) {
423             if (other.getRevision() != null) {
424                 return -1;
425             }
426         } else {
427             if (other.getRevision() == null) {
428                 return 1;
429             }
430             result = getRevision().compareTo(other.getRevision());
431             if (result != 0) {
432                 return result;
433             }
434         }
435
436         return result;
437     }
438
439 }