Redirecting Caught and Uncaught Exceptions to OSGI Console and Log File
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-common / src / main / java / org / opendaylight / controller / yang / common / QName.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.controller.yang.common;\r
9 \r
10 import java.net.URI;\r
11 import java.net.URISyntaxException;\r
12 \r
13 import java.text.SimpleDateFormat;\r
14 import java.util.Date;\r
15 \r
16 import org.slf4j.Logger;\r
17 import org.slf4j.LoggerFactory;\r
18 \r
19 /**\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
22  * \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
27  * \r
28  * <ul>\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
32  * element</li>\r
33  * <li><b>LocalName</b> - the YANG schema identifier which were defined for this\r
34  * node in the YANG module</li>\r
35  * </ul>\r
36  * \r
37  * \r
38  */\r
39 public class QName {\r
40     protected static final Logger logger = LoggerFactory\r
41         .getLogger(QName.class);\r
42 \r
43     private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");\r
44 \r
45     final URI namespace;\r
46     final String localName;\r
47     final String prefix;\r
48     final Date revision;\r
49 \r
50     /**\r
51      * QName Constructor.\r
52      * \r
53      * @param namespace\r
54      *            the namespace assigned to the YANG module\r
55      * @param revision\r
56      *            the revision of the YANG module\r
57      * @param prefix\r
58      *            locally defined prefix assigned to local name\r
59      * @param localName\r
60      *            YANG schema identifier\r
61      */\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
67     }\r
68 \r
69     /**\r
70      * QName Constructor.\r
71      * \r
72      * @param namespace\r
73      *            the namespace assigned to the YANG module\r
74      * @param localName\r
75      *            YANG schema identifier\r
76      */\r
77     public QName(URI namespace, String localName) {\r
78         this(namespace, null, "", localName);\r
79     }\r
80 \r
81     /**\r
82      * QName Constructor.\r
83      * \r
84      * @param namespace\r
85      *            the namespace assigned to the YANG module\r
86      * @param revision\r
87      *            the revision of the YANG module\r
88      * @param localName\r
89      *            YANG schema identifier\r
90      */\r
91     public QName(URI namespace, Date revision, String localName) {\r
92         this(namespace, revision, null, localName);\r
93     }\r
94 \r
95     public QName(QName base, String localName) {\r
96         this(base.getNamespace(), base.getRevision(), base.getPrefix(),\r
97                 localName);\r
98     }\r
99 \r
100     /**\r
101      * Returns XMLNamespace assigned to the YANG module.\r
102      * \r
103      * @return XMLNamespace assigned to the YANG module.\r
104      */\r
105     public URI getNamespace() {\r
106         return namespace;\r
107     }\r
108 \r
109     /**\r
110      * Returns YANG schema identifier which were defined for this node in the\r
111      * YANG module\r
112      * \r
113      * @return YANG schema identifier which were defined for this node in the\r
114      *         YANG module\r
115      */\r
116     public String getLocalName() {\r
117         return localName;\r
118     }\r
119 \r
120     /**\r
121      * Returns revision of the YANG module if the module has defined revision,\r
122      * otherwise returns <code>null</code>\r
123      * \r
124      * @return revision of the YANG module if the module has defined revision,\r
125      *         otherwise returns <code>null</code>\r
126      */\r
127     public Date getRevision() {\r
128         return revision;\r
129     }\r
130 \r
131     /**\r
132      * Returns locally defined prefix assigned to local name\r
133      * \r
134      * @return locally defined prefix assigned to local name\r
135      */\r
136     public String getPrefix() {\r
137         return prefix;\r
138     }\r
139 \r
140     @Override\r
141     public int hashCode() {\r
142         final int prime = 31;\r
143         int result = 1;\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
150         return result;\r
151     }\r
152 \r
153     @Override\r
154     public boolean equals(Object obj) {\r
155         if (this == obj)\r
156             return true;\r
157         if (obj == null)\r
158             return false;\r
159         if (getClass() != obj.getClass())\r
160             return false;\r
161         QName other = (QName) obj;\r
162         if (localName == null) {\r
163             if (other.localName != null)\r
164                 return false;\r
165         } else if (!localName.equals(other.localName))\r
166             return false;\r
167         if (namespace == null) {\r
168             if (other.namespace != null)\r
169                 return false;\r
170         } else if (!namespace.equals(other.namespace))\r
171             return false;\r
172         if (revision == null) {\r
173             if (other.revision != null)\r
174                 return false;\r
175         } else if (!revision.equals(other.revision))\r
176             return false;\r
177         return true;\r
178     }\r
179 \r
180     @Override\r
181     public String toString() {\r
182         StringBuilder sb = new StringBuilder();\r
183         if (namespace != null) {\r
184             sb.append("(" + namespace);\r
185 \r
186             if (revision != null) {\r
187                 sb.append("?revision=" + revisionFormat.format(revision));\r
188             }\r
189             sb.append(")");\r
190         }\r
191         sb.append(localName);\r
192         return sb.toString();\r
193     }\r
194 \r
195     /**\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
200      * \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
204      * \r
205      */\r
206     URI getRevisionNamespace() {\r
207 \r
208         if (namespace == null)\r
209             return null;\r
210 \r
211         String query = "";\r
212         if (revision != null) {\r
213             query = "revision=" + revisionFormat.format(revision);\r
214         }\r
215 \r
216         URI compositeURI = null;\r
217         try {\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
224         }\r
225         return compositeURI;\r
226     }\r
227 }\r