+++ /dev/null
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.controller.yang.common;\r
-\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-\r
-import java.text.SimpleDateFormat;\r
-import java.util.Date;\r
-\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * The QName from XML consists of local name of element and XML namespace, but\r
- * for our use, we added module revision to it.\r
- * \r
- * In YANG context QName is full name of defined node, type, procedure or\r
- * notification. QName consists of XML namespace, YANG model revision and local\r
- * name of defined type. It is used to prevent name clashes between nodes with\r
- * same local name, but from different schemas.\r
- * \r
- * <ul>\r
- * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which\r
- * defined element, type, procedure or notification.</li>\r
- * <li><b>Revision</b> - the revision of the YANG module which describes the\r
- * element</li>\r
- * <li><b>LocalName</b> - the YANG schema identifier which were defined for this\r
- * node in the YANG module</li>\r
- * </ul>\r
- * \r
- * \r
- */\r
-public final class QName {\r
- protected static final Logger logger = LoggerFactory\r
- .getLogger(QName.class);\r
-\r
- private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");\r
-\r
- final URI namespace;\r
- final String localName;\r
- final String prefix;\r
- final Date revision;\r
-\r
- /**\r
- * QName Constructor.\r
- * \r
- * @param namespace\r
- * the namespace assigned to the YANG module\r
- * @param revision\r
- * the revision of the YANG module\r
- * @param prefix\r
- * locally defined prefix assigned to local name\r
- * @param localName\r
- * YANG schema identifier\r
- */\r
- public QName(URI namespace, Date revision, String prefix, String localName) {\r
- this.namespace = namespace;\r
- this.localName = localName;\r
- this.revision = revision;\r
- this.prefix = prefix;\r
- }\r
-\r
- /**\r
- * QName Constructor.\r
- * \r
- * @param namespace\r
- * the namespace assigned to the YANG module\r
- * @param localName\r
- * YANG schema identifier\r
- */\r
- public QName(URI namespace, String localName) {\r
- this(namespace, null, "", localName);\r
- }\r
-\r
- /**\r
- * QName Constructor.\r
- * \r
- * @param namespace\r
- * the namespace assigned to the YANG module\r
- * @param revision\r
- * the revision of the YANG module\r
- * @param localName\r
- * YANG schema identifier\r
- */\r
- public QName(URI namespace, Date revision, String localName) {\r
- this(namespace, revision, null, localName);\r
- }\r
-\r
- public QName(QName base, String localName) {\r
- this(base.getNamespace(), base.getRevision(), base.getPrefix(),\r
- localName);\r
- }\r
-\r
- /**\r
- * Returns XMLNamespace assigned to the YANG module.\r
- * \r
- * @return XMLNamespace assigned to the YANG module.\r
- */\r
- public URI getNamespace() {\r
- return namespace;\r
- }\r
-\r
- /**\r
- * Returns YANG schema identifier which were defined for this node in the\r
- * YANG module\r
- * \r
- * @return YANG schema identifier which were defined for this node in the\r
- * YANG module\r
- */\r
- public String getLocalName() {\r
- return localName;\r
- }\r
-\r
- /**\r
- * Returns revision of the YANG module if the module has defined revision,\r
- * otherwise returns <code>null</code>\r
- * \r
- * @return revision of the YANG module if the module has defined revision,\r
- * otherwise returns <code>null</code>\r
- */\r
- public Date getRevision() {\r
- return revision;\r
- }\r
-\r
- /**\r
- * Returns locally defined prefix assigned to local name\r
- * \r
- * @return locally defined prefix assigned to local name\r
- */\r
- public String getPrefix() {\r
- return prefix;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result\r
- + ((localName == null) ? 0 : localName.hashCode());\r
- result = prime * result\r
- + ((namespace == null) ? 0 : namespace.hashCode());\r
- result = prime * result\r
- + ((revision == null) ? 0 : revision.hashCode());\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- QName other = (QName) obj;\r
- if (localName == null) {\r
- if (other.localName != null)\r
- return false;\r
- } else if (!localName.equals(other.localName))\r
- return false;\r
- if (namespace == null) {\r
- if (other.namespace != null)\r
- return false;\r
- } else if (!namespace.equals(other.namespace))\r
- return false;\r
- if (revision == null) {\r
- if (other.revision != null)\r
- return false;\r
- } else if (!revision.equals(other.revision))\r
- return false;\r
- return true;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- StringBuilder sb = new StringBuilder();\r
- if (namespace != null) {\r
- sb.append("(" + namespace);\r
-\r
- if (revision != null) {\r
- sb.append("?revision=" + revisionFormat.format(revision));\r
- }\r
- sb.append(")");\r
- }\r
- sb.append(localName);\r
- return sb.toString();\r
- }\r
-\r
- /**\r
- * Returns a namespace in form defined by section 5.6.4. of {@link https\r
- * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,\r
- * the method will return <code>null</code> <br>\r
- * example "http://example.acme.com/system?revision=2008-04-01"\r
- * \r
- * @return namespace in form defined by section 5.6.4. of {@link https\r
- * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly\r
- * defined, the method will return <code>null</code>\r
- * \r
- */\r
- URI getRevisionNamespace() {\r
-\r
- if (namespace == null)\r
- return null;\r
-\r
- String query = "";\r
- if (revision != null) {\r
- query = "revision=" + revisionFormat.format(revision);\r
- }\r
-\r
- URI compositeURI = null;\r
- try {\r
- compositeURI = new URI(namespace.getScheme(),\r
- namespace.getUserInfo(), namespace.getHost(),\r
- namespace.getPort(), namespace.getPath(), query,\r
- namespace.getFragment());\r
- } catch (URISyntaxException e) {\r
- logger.error("",e);\r
- }\r
- return compositeURI;\r
- }\r
-}\r