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

io.cloudslang.content.xml.services.InsertBeforeService Maven / Gradle / Ivy

The newest version!
/*
 * (c) Copyright 2019 EntIT Software LLC, a Micro Focus company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package io.cloudslang.content.xml.services;

import io.cloudslang.content.xml.entities.inputs.CommonInputs;
import io.cloudslang.content.xml.entities.inputs.CustomInputs;
import io.cloudslang.content.xml.utils.Constants;
import io.cloudslang.content.xml.utils.ResultUtils;
import io.cloudslang.content.xml.utils.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.namespace.NamespaceContext;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by markowis on 03/03/2016.
 */
public class InsertBeforeService {
    public Map execute(CommonInputs commonInputs, CustomInputs customInputs) {
        Map result = new HashMap<>();

        try {
            Document doc = XmlUtils.getDocument(commonInputs);
            NamespaceContext context = XmlUtils.getNamespaceContext(commonInputs, doc);

            Document beforeDoc = XmlUtils.parseXmlStringSecurely(customInputs.getXmlElement(), commonInputs.getSecureProcessing());
            Node beforeNode = doc.importNode(beforeDoc.getDocumentElement(), true);

            NodeList nodeList = XmlUtils.evaluateXPathQuery(doc, context, commonInputs.getXPathQuery());

            XmlUtils.validateNodeList(nodeList);

            insertBeforeToNodeList(nodeList, beforeNode);
            ResultUtils.populateSuccessResult(result, Constants.SuccessMessages.INSERT_BEFORE_SUCCESS,
                    XmlUtils.nodeToString(doc));

        } catch (XPathExpressionException e) {
            ResultUtils.populateFailureResult(result, Constants.ErrorMessages.XPATH_PARSING_ERROR + e.getMessage());
        } catch (TransformerException te) {
            ResultUtils.populateFailureResult(result, Constants.ErrorMessages.TRANSFORMER_ERROR + te.getMessage());
        } catch (Exception e) {
            ResultUtils.populateFailureResult(result, Constants.ErrorMessages.PARSING_ERROR + e.getMessage());
        }

        return result;
    }

    private static void insertBeforeToNodeList(NodeList nodeList, Node beforeNode) throws Exception {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            if (node.getNodeType() != Node.ELEMENT_NODE) {
                throw new Exception(Constants.ErrorMessages.INSERT_BEFORE_FAILURE +
                        Constants.ErrorMessages.NEED_ELEMENT_TYPE);
            }
            node.getParentNode().insertBefore(beforeNode.cloneNode(true), node);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy