package org.opendaylight.controller.config.util.xml;
import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
private final Element element;
private static final Logger LOG = LoggerFactory.getLogger(XmlElement.class);
- private XmlElement(Element element) {
+ private XmlElement(final Element element) {
this.element = element;
}
- public static XmlElement fromDomElement(Element e) {
+ public static XmlElement fromDomElement(final Element e) {
return new XmlElement(e);
}
- public static XmlElement fromDomDocument(Document xml) {
+ public static XmlElement fromDomDocument(final Document xml) {
return new XmlElement(xml.getDocumentElement());
}
- public static XmlElement fromString(String s) throws DocumentedException {
+ public static XmlElement fromString(final String s) throws DocumentedException {
try {
return new XmlElement(XmlUtil.readXmlToElement(s));
} catch (IOException | SAXException e) {
}
}
- public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws DocumentedException {
+ public static XmlElement fromDomElementWithExpected(final Element element, final 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 DocumentedException {
+ public static XmlElement fromDomElementWithExpected(final Element element, final String expectedName, final String expectedNamespace) throws DocumentedException {
XmlElement xmlElement = XmlElement.fromDomElementWithExpected(element, expectedName);
xmlElement.checkNamespace(expectedNamespace);
return xmlElement;
} else {
if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")){
throw new DocumentedException("Attribute doesn't start with :",
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.INVALID_VALUE,
+ DocumentedException.ErrorSeverity.ERROR);
}
prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
}
return namespaces;
}
- public void checkName(String expectedName) throws UnexpectedElementException {
+ public void checkName(final String expectedName) throws UnexpectedElementException {
if (!getName().equals(expectedName)){
throw new UnexpectedElementException(String.format("Expected %s xml element but was %s", expectedName,
getName()),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.operation_failed,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.OPERATION_FAILED,
+ DocumentedException.ErrorSeverity.ERROR);
}
}
- public void checkNamespaceAttribute(String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
+ public void checkNamespaceAttribute(final String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
if (!getNamespaceAttribute().equals(expectedNamespace))
{
throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
getNamespaceAttribute(),
expectedNamespace),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.operation_failed,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.OPERATION_FAILED,
+ DocumentedException.ErrorSeverity.ERROR);
}
}
- public void checkNamespace(String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
+ public void checkNamespace(final String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
if (!getNamespace().equals(expectedNamespace))
{
throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
getNamespace(),
expectedNamespace),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.operation_failed,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.OPERATION_FAILED,
+ DocumentedException.ErrorSeverity.ERROR);
}
}
return element.getTagName();
}
- public String getAttribute(String attributeName) {
+ public String getAttribute(final String attributeName) {
return element.getAttribute(attributeName);
}
- public String getAttribute(String attributeName, String namespace) {
+ public String getAttribute(final String attributeName, final String namespace) {
return element.getAttributeNS(namespace, attributeName);
}
- public NodeList getElementsByTagName(String name) {
+ public NodeList getElementsByTagName(final String name) {
return element.getElementsByTagName(name);
}
- public void appendChild(Element element) {
+ public void appendChild(final Element element) {
this.element.appendChild(element);
}
/**
* Non recursive
*/
- private List<XmlElement> getChildElementsInternal(ElementFilteringStrategy strat) {
+ private List<XmlElement> getChildElementsInternal(final ElementFilteringStrategy strat) {
NodeList childNodes = element.getChildNodes();
final List<XmlElement> result = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
}
public List<XmlElement> getChildElements() {
- return getChildElementsInternal(new ElementFilteringStrategy() {
- @Override
- public boolean accept(Element e) {
- return true;
- }
- });
+ return getChildElementsInternal(e -> true);
}
- public List<XmlElement> getChildElementsWithinNamespace(final String childName, String namespace) {
+ public List<XmlElement> getChildElementsWithinNamespace(final String childName, final String namespace) {
return Lists.newArrayList(Collections2.filter(getChildElementsWithinNamespace(namespace),
- new Predicate<XmlElement>() {
- @Override
- public boolean apply(XmlElement xmlElement) {
- return xmlElement.getName().equals(childName);
- }
- }));
+ xmlElement -> xmlElement.getName().equals(childName)));
}
public List<XmlElement> getChildElementsWithinNamespace(final String namespace) {
- return getChildElementsInternal(new ElementFilteringStrategy() {
- @Override
- public boolean accept(Element e) {
- try {
- return XmlElement.fromDomElement(e).getNamespace().equals(namespace);
- } catch (MissingNameSpaceException e1) {
- return false;
- }
+ return getChildElementsInternal(e -> {
+ try {
+ return XmlElement.fromDomElement(e).getNamespace().equals(namespace);
+ } catch (final MissingNameSpaceException e1) {
+ return false;
}
-
});
}
* @return List of child elements
*/
public List<XmlElement> getChildElements(final String tagName) {
- return getChildElementsInternal(new ElementFilteringStrategy() {
- @Override
- public boolean accept(Element e) {
- // localName returns pure localName without prefix
- return e.getLocalName().equals(tagName);
- }
+ return getChildElementsInternal(e -> {
+ // localName returns pure localName without prefix
+ return e.getLocalName().equals(tagName);
});
}
- public XmlElement getOnlyChildElement(String childName) throws DocumentedException {
+ public XmlElement getOnlyChildElement(final String childName) throws DocumentedException {
List<XmlElement> nameElements = getChildElements(childName);
if (nameElements.size() != 1){
throw new DocumentedException("One element " + childName + " expected in " + toString(),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.INVALID_VALUE,
+ DocumentedException.ErrorSeverity.ERROR);
}
return nameElements.get(0);
}
- public Optional<XmlElement> getOnlyChildElementOptionally(String childName) {
+ public Optional<XmlElement> getOnlyChildElementOptionally(final String childName) {
List<XmlElement> nameElements = getChildElements(childName);
if (nameElements.size() != 1) {
return Optional.absent();
public Optional<XmlElement> getOnlyChildElementOptionally(final String childName, final String namespace) {
List<XmlElement> children = getChildElementsWithinNamespace(namespace);
- children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
- @Override
- public boolean apply(XmlElement xmlElement) {
- return xmlElement.getName().equals(childName);
- }
- }));
+ children = Lists.newArrayList(Collections2.filter(children,
+ xmlElement -> xmlElement.getName().equals(childName)));
if (children.size() != 1){
return Optional.absent();
}
return Optional.of(children.get(0));
}
- public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws DocumentedException {
+ public XmlElement getOnlyChildElementWithSameNamespace(final String childName) throws DocumentedException {
return getOnlyChildElement(childName, getNamespace());
}
Optional<String> namespace = getNamespaceOptionally();
if (namespace.isPresent()) {
List<XmlElement> children = getChildElementsWithinNamespace(namespace.get());
- children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
- @Override
- public boolean apply(XmlElement xmlElement) {
- return xmlElement.getName().equals(childName);
- }
- }));
+ children = Lists.newArrayList(Collections2.filter(children,
+ xmlElement -> xmlElement.getName().equals(childName)));
if (children.size() != 1){
return Optional.absent();
}
return Optional.absent();
}
- public XmlElement getOnlyChildElement(final String childName, String namespace) throws DocumentedException {
+ public XmlElement getOnlyChildElement(final String childName, final String namespace) throws DocumentedException {
List<XmlElement> children = getChildElementsWithinNamespace(namespace);
- children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
- @Override
- public boolean apply(XmlElement xmlElement) {
- return xmlElement.getName().equals(childName);
- }
- }));
+ children = Lists.newArrayList(Collections2.filter(children,
+ xmlElement -> xmlElement.getName().equals(childName)));
if (children.size() != 1){
throw new DocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
childName, toString(), children.size()),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.INVALID_VALUE,
+ DocumentedException.ErrorSeverity.ERROR);
}
return children.get(0);
if (children.size() != 1){
throw new DocumentedException(String.format( "One element expected in %s but was %s", toString(),
children.size()),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.INVALID_VALUE,
+ DocumentedException.ErrorSeverity.ERROR);
}
return children.get(0);
}
}
}
throw new DocumentedException(getName() + " should contain text.",
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error
+ 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()),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.operation_failed,
- DocumentedException.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),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.operation_failed,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.OPERATION_FAILED,
+ DocumentedException.ErrorSeverity.ERROR);
}
return namespaceURI.get();
}
if (element.getNamespaceURI() != null) {
try {
sb.append(", namespace='").append(getNamespace()).append('\'');
- } catch (MissingNameSpaceException e) {
+ } catch (final MissingNameSpaceException e) {
LOG.trace("Missing namespace for element.");
}
}
public List<XmlElement> getChildElementsWithSameNamespace(final String childName) throws MissingNameSpaceException {
List<XmlElement> children = getChildElementsWithinNamespace(getNamespace());
- return Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
- @Override
- public boolean apply(XmlElement xmlElement) {
- return xmlElement.getName().equals(childName);
- }
- }));
+ return Lists.newArrayList(Collections2.filter(children, xmlElement -> xmlElement.getName().equals(childName)));
}
- public void checkUnrecognisedElements(List<XmlElement> recognisedElements,
- XmlElement... additionalRecognisedElements) throws DocumentedException {
+ public void checkUnrecognisedElements(final List<XmlElement> recognisedElements,
+ final XmlElement... additionalRecognisedElements) throws DocumentedException {
List<XmlElement> childElements = getChildElements();
childElements.removeAll(recognisedElements);
for (XmlElement additionalRecognisedElement : additionalRecognisedElements) {
}
if (!childElements.isEmpty()){
throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
- DocumentedException.ErrorType.application,
- DocumentedException.ErrorTag.invalid_value,
- DocumentedException.ErrorSeverity.error);
+ DocumentedException.ErrorType.APPLICATION,
+ DocumentedException.ErrorTag.INVALID_VALUE,
+ DocumentedException.ErrorSeverity.ERROR);
}
}
- public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws DocumentedException {
+ public void checkUnrecognisedElements(final XmlElement... additionalRecognisedElements) throws DocumentedException {
checkUnrecognisedElements(Collections.<XmlElement>emptyList(), additionalRecognisedElements);
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(final Object o) {
if (this == o) {
return true;
}
}
public boolean hasNamespace() {
- if (!getNamespaceAttributeOptionally().isPresent()) {
- if (!getNamespaceOptionally().isPresent()) {
- return false;
- }
- }
- return true;
+ return getNamespaceAttributeOptionally().isPresent() || getNamespaceOptionally().isPresent();
}
private interface ElementFilteringStrategy {