All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.binding.rest.config.RESTSUConfiguration Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
/**
 * Copyright (c) 2007-2012 EBM WebSourcing, 2012-2016 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.config;

import static org.ow2.petals.binding.rest.RESTConstants.SUParameter.MAPPING;
import static org.ow2.petals.binding.rest.RESTConstants.SUParameter.OPERATION;

import java.util.List;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
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 abstract class RESTSUConfiguration extends RESTConfiguration {

    public RESTSUConfiguration(final Logger logger, final SuConfigurationParameters extensions)
            throws PEtALSCDKException {
        super(logger, extensions);
    }

    protected static final NodeList getSUOperationMappingNodes(final List elements)
            throws PEtALSCDKException {
        NodeList operationNodes = null;

        int elementsNb = elements.size();
        int eltIndex = 0;
        Element mappingElt = null;
        while (eltIndex < elementsNb && mappingElt == null) {
            Element elt = elements.get(eltIndex);
            if (elt != null && elt.getLocalName().equals(MAPPING)) {
                mappingElt = elt;
            }
            eltIndex++;
        }

        if (mappingElt != null) {
            String namespaceURI = mappingElt.getNamespaceURI();
            operationNodes = mappingElt.getElementsByTagNameNS(namespaceURI, OPERATION);
        } else {
            handleMissingMandatoryParameterError(MAPPING);
        }

        return operationNodes;
    }

    protected static final QName getTextContentAsQName(final Node node) throws PEtALSCDKException {
        final String textContent = node.getTextContent();
        return getStringAsQName(node, textContent);
    }
    
    protected static final QName getAttributeAsQName(final Element element, final String attributeName)
            throws PEtALSCDKException {
        return getStringAsQName(element, element.getAttribute(attributeName));
    }

    protected static final String getSubElementTextContent(Element elt, String subEltName,
            boolean mandatory) throws PEtALSCDKException {
        final Node subElement = getSubElement(elt, subEltName, mandatory);
        return subElement != null ? subElement.getTextContent() : null;
    }

    protected static final Node getSubElement(Element elt, String subEltName, boolean mandatory)
            throws PEtALSCDKException {
        Node subElement = null;

        String namespaceURI = elt.getNamespaceURI();
        NodeList subElements = elt.getElementsByTagNameNS(namespaceURI, subEltName);
        int subElementsNb = subElements.getLength();
        if (subElementsNb == 1) {
            subElement = subElements.item(0);
        } else if (subElementsNb == 0 && mandatory) {
            handleMissingMandatoryParameterError(subEltName);
        } else if (subElementsNb > 1) {
            handleNotUniqueParameterError(subEltName);
        }

        return subElement;
    }

    protected static final NodeList getSubElements(Element elt, String subEltName)
            throws PEtALSCDKException {
        String namespaceURI = elt.getNamespaceURI();
        NodeList subElements = elt.getElementsByTagNameNS(namespaceURI, subEltName);

        return subElements;
    }

    private static final QName getStringAsQName(final Node container, final String text) 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 = container.getNamespaceURI();
        } 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 - 2024 Weber Informatics LLC | Privacy Policy