/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 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.controller.netconf.util.xml;
+package org.opendaylight.controller.config.util.xml;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Attr;
return new XmlElement(xml.getDocumentElement());
}
- public static XmlElement fromString(String s) throws NetconfDocumentedException {
+ public static XmlElement fromString(String s) throws DocumentedException {
try {
return new XmlElement(XmlUtil.readXmlToElement(s));
} catch (IOException | SAXException e) {
- throw NetconfDocumentedException.wrap(e);
+ throw DocumentedException.wrap(e);
}
}
- public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws NetconfDocumentedException {
+ public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws DocumentedException {
XmlElement xmlElement = XmlElement.fromDomElement(element);
xmlElement.checkName(expectedName);
return xmlElement;
}
- public static XmlElement fromDomElementWithExpected(Element element, String expectedName, String expectedNamespace) throws NetconfDocumentedException {
+ public static XmlElement fromDomElementWithExpected(Element element, String expectedName, String expectedNamespace) throws DocumentedException {
XmlElement xmlElement = XmlElement.fromDomElementWithExpected(element, expectedName);
xmlElement.checkNamespace(expectedNamespace);
return xmlElement;
}
- private Map<String, String> extractNamespaces() throws NetconfDocumentedException {
+ private Map<String, String> extractNamespaces() throws DocumentedException {
Map<String, String> namespaces = new HashMap<>();
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
prefix = DEFAULT_NAMESPACE_PREFIX;
} else {
if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")){
- throw new NetconfDocumentedException("Attribute doesn't start with :",
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error);
+ throw new DocumentedException("Attribute doesn't start with :",
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error);
}
prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
}
if (!getName().equals(expectedName)){
throw new UnexpectedElementException(String.format("Expected %s xml element but was %s", expectedName,
getName()),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
}
}
throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
getNamespaceAttribute(),
expectedNamespace),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
}
}
throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
getNamespace(),
expectedNamespace),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
}
}
});
}
- public XmlElement getOnlyChildElement(String childName) throws NetconfDocumentedException {
+ public XmlElement getOnlyChildElement(String childName) throws DocumentedException {
List<XmlElement> nameElements = getChildElements(childName);
if (nameElements.size() != 1){
- throw new NetconfDocumentedException("One element " + childName + " expected in " + toString(),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error);
+ throw new DocumentedException("One element " + childName + " expected in " + toString(),
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error);
}
return nameElements.get(0);
}
return Optional.of(children.get(0));
}
- public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws NetconfDocumentedException {
+ public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws DocumentedException {
return getOnlyChildElement(childName, getNamespace());
}
return Optional.absent();
}
- public XmlElement getOnlyChildElementWithSameNamespace() throws NetconfDocumentedException {
+ public XmlElement getOnlyChildElementWithSameNamespace() throws DocumentedException {
XmlElement childElement = getOnlyChildElement();
childElement.checkNamespace(getNamespace());
return childElement;
return Optional.absent();
}
- public XmlElement getOnlyChildElement(final String childName, String namespace) throws NetconfDocumentedException {
+ public XmlElement getOnlyChildElement(final String childName, String namespace) throws DocumentedException {
List<XmlElement> children = getChildElementsWithinNamespace(namespace);
children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
@Override
}
}));
if (children.size() != 1){
- throw new NetconfDocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
+ throw new DocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
childName, toString(), children.size()),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error);
}
return children.get(0);
}
- public XmlElement getOnlyChildElement() throws NetconfDocumentedException {
+ public XmlElement getOnlyChildElement() throws DocumentedException {
List<XmlElement> children = getChildElements();
if (children.size() != 1){
- throw new NetconfDocumentedException(String.format( "One element expected in %s but was %s", toString(),
+ throw new DocumentedException(String.format( "One element expected in %s but was %s", toString(),
children.size()),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error);
}
return children.get(0);
}
return Optional.of(children.get(0));
}
- public String getTextContent() throws NetconfDocumentedException {
+ public String getTextContent() throws DocumentedException {
NodeList childNodes = element.getChildNodes();
if (childNodes.getLength() == 0) {
return DEFAULT_NAMESPACE_PREFIX;
return content.trim();
}
}
- throw new NetconfDocumentedException(getName() + " should contain text.",
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error
+ throw new DocumentedException(getName() + " should contain text.",
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error
);
}
if (attribute == null || attribute.equals(DEFAULT_NAMESPACE_PREFIX)){
throw new MissingNameSpaceException(String.format("Element %s must specify namespace",
toString()),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
}
return attribute;
}
Optional<String> namespaceURI = getNamespaceOptionally();
if (!namespaceURI.isPresent()){
throw new MissingNameSpaceException(String.format("No namespace defined for %s", this),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
}
return namespaceURI.get();
}
* namespace is returned with empty string as key. If no default namespace
* is found value will be null.
*/
- public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() throws NetconfDocumentedException {
+ public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() throws DocumentedException {
Map<String, String> namespaces = extractNamespaces();
String textContent = getTextContent();
int indexOfColon = textContent.indexOf(':');
}
public void checkUnrecognisedElements(List<XmlElement> recognisedElements,
- XmlElement... additionalRecognisedElements) throws NetconfDocumentedException {
+ XmlElement... additionalRecognisedElements) throws DocumentedException {
List<XmlElement> childElements = getChildElements();
childElements.removeAll(recognisedElements);
for (XmlElement additionalRecognisedElement : additionalRecognisedElements) {
childElements.remove(additionalRecognisedElement);
}
if (!childElements.isEmpty()){
- throw new NetconfDocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
- NetconfDocumentedException.ErrorType.application,
- NetconfDocumentedException.ErrorTag.invalid_value,
- NetconfDocumentedException.ErrorSeverity.error);
+ throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.invalid_value,
+ DocumentedException.ErrorSeverity.error);
}
}
- public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws NetconfDocumentedException {
+ public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws DocumentedException {
checkUnrecognisedElements(Collections.<XmlElement>emptyList(), additionalRecognisedElements);
}