org.ow2.petals.binding.rest.utils.DOMUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of petals-bc-rest Show documentation
Show all versions of petals-bc-rest Show documentation
petals-bc-rest Binding Component description
/**
* Copyright (c) 2018-2020 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.binding.rest.utils;
import javax.xml.namespace.QName;
import org.ow2.petals.binding.rest.config.RESTConfiguration;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMUtils {
private DOMUtils() {
// Utility class --> No constructor
}
private static final QName getTextContentAsQName(final Node node) throws PEtALSCDKException {
final String textContent = node.getTextContent();
return getStringAsQName(node, textContent);
}
public static final QName getAttributeAsQName(final Element element, final String attributeName)
throws PEtALSCDKException {
return getStringAsQName(element, element.getAttribute(attributeName));
}
public static final String getSubElementTextContent(final Element elt, final String subEltName, boolean mandatory)
throws PEtALSCDKException {
final Node subElement = getSubElement(elt, subEltName, mandatory);
return subElement != null ? subElement.getTextContent() : null;
}
public static final Node getSubElement(final Element elt, final String subEltName, final boolean mandatory)
throws PEtALSCDKException {
Node subElement = null;
final String namespaceURI = elt.getNamespaceURI();
final NodeList subElements = elt.getElementsByTagNameNS(namespaceURI, subEltName);
final int subElementsNb = subElements.getLength();
if (subElementsNb == 1) {
subElement = subElements.item(0);
} else if (subElementsNb == 0 && mandatory) {
// TODO: Replace by throwing the right exception, so 'subElement' can be removed
RESTConfiguration.handleMissingMandatoryParameterError(subEltName);
} else if (subElementsNb > 1) {
// TODO: Replace by throwing the right exception, so 'subElement' can be removed
RESTConfiguration.handleNotUniqueParameterError(subEltName);
}
return subElement;
}
public static final NodeList getSubElements(final Element elt, final String subEltName) {
String namespaceURI = elt.getNamespaceURI();
return elt.getElementsByTagNameNS(namespaceURI, subEltName);
}
private static final QName getStringAsQName(final Node container, final String text) throws PEtALSCDKException {
return getStringAsQName(container, text, container.getNamespaceURI());
}
public static final QName getStringAsQName(final Node container, final String text, final String defaultNs)
throws PEtALSCDKException {
final String[] texts = text.split(":");
final int size = texts.length;
final String localName;
final String namespaceURI;
if (size == 0) {
throw new PEtALSCDKException("Empty QName string");
} else if (size == 1) {
localName = texts[0];
namespaceURI = defaultNs;
} else if (size == 2) {
namespaceURI = container.lookupNamespaceURI(texts[0]);
localName = texts[1];
} else {
throw new PEtALSCDKException("Invalid QName string '" + text + "'");
}
return new QName(namespaceURI, localName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy