/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.common; import org.opendaylight.yangtools.concepts.Immutable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Serializable; import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; import java.util.Date; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat; /** * The QName from XML consists of local name of element and XML namespace, but * for our use, we added module revision to it. * * In YANG context QName is full name of defined node, type, procedure or * notification. QName consists of XML namespace, YANG model revision and local * name of defined type. It is used to prevent name clashes between nodes with * same local name, but from different schemas. * *
null
*
* @return revision of the YANG module if the module has defined revision,
* otherwise returns null
*/
public Date getRevision() {
return revision;
}
/**
* Returns locally defined prefix assigned to local name
*
* @return locally defined prefix assigned to local name
*/
public String getPrefix() {
return prefix;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((localName == null) ? 0 : localName.hashCode());
result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
QName other = (QName) obj;
if (localName == null) {
if (other.localName != null) {
return false;
}
} else if (!localName.equals(other.localName)) {
return false;
}
if (namespace == null) {
if (other.namespace != null) {
return false;
}
} else if (!namespace.equals(other.namespace)) {
return false;
}
if (formattedRevision == null) {
if (other.formattedRevision != null) {
return false;
}
} else if (!revision.equals(other.revision)) {
return false;
}
return true;
}
public static QName create(QName base, String localName){
return new QName(base, localName);
}
public static QName create(URI namespace, Date revision, String localName){
return new QName(namespace, revision, localName);
}
public static QName create(String namespace, String revision, String localName) throws IllegalArgumentException{
try {
URI namespaceUri = new URI(namespace);
Date revisionDate = parseRevision(revision);
return create(namespaceUri, revisionDate, localName);
} catch (URISyntaxException ue) {
throw new IllegalArgumentException("Namespace is is not valid URI", ue);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (namespace != null) {
sb.append(QNAME_LEFT_PARENTHESIS + namespace);
if (revision != null) {
sb.append(QNAME_REVISION_DELIMITER + getRevisionFormat().format(revision));
}
sb.append(QNAME_RIGHT_PARENTHESIS);
}
sb.append(localName);
return sb.toString();
}
/**
* Returns a namespace in form defined by section 5.6.4. of {@link https
* ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
* the method will return null
null
*
*/
URI getRevisionNamespace() {
if (namespace == null) {
return null;
}
String query = "";
if (revision != null) {
query = "revision=" + formattedRevision;
}
URI compositeURI = null;
try {
compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
} catch (URISyntaxException e) {
LOGGER.error("", e);
}
return compositeURI;
}
public String getFormattedRevision() {
return formattedRevision;
}
public QName withoutRevision() {
return QName.create(namespace, null, localName);
}
public static Date parseRevision(String formatedDate) {
try {
return getRevisionFormat().parse(formatedDate);
} catch (ParseException| RuntimeException e) {
throw new IllegalArgumentException("Revision is not in supported format:" + formatedDate,e);
}
}
public static String formattedRevision(Date revision) {
if(revision == null) {
return null;
}
return getRevisionFormat().format(revision);
}
public boolean isEqualWithoutRevision(QName other) {
return localName.equals(other.getLocalName()) && Objects.equals(namespace, other.getNamespace());
}
}