com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterExtractor.java Source code

Java tutorial

Introduction

Here is the source code for com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterExtractor.java

Source

/*
Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License 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 com.c4om.autoconf.ulysses.extra.svrlinterpreter;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.*;

import static com.c4om.utils.xmlutils.constants.CommonXMLConstants.*;

/**
 * This class is intended to extract the information from the SVRL file, in order to be processed.
 * 
 * @author Pablo Alonso Rodriguez (Center for Open Middleware)
 */
public class SVRLInterpreterExtractor {

    /**
     * Token to be replaced by the metamodel directory where necessary
     */
    private static final String METAMODEL_DIR_REPLACE_TOKEN = "${METAMODEL_DIR}";

    /**
     * String that contains the XPath query to perform.
     */
    private static final String XPATH_STRING = "//svrl:failed-assert";

    /**
     * JDOM2 document with the input document
     */
    private Document document;

    /**
     * XPathFactory used to build XPath expressions
     */
    private XPathFactory xpathFactory;

    /**
     * Path to the metamodel folder, provided as argument.
     */
    private String metamodelPath;

    /**
     * Constructor
     * @param document the document where information should be extracted from
     * @param metamodelPathFile TODO
     */
    public SVRLInterpreterExtractor(Document document, String metamodelPath) {
        this.document = document;
        xpathFactory = XPathFactory.instance();
        this.metamodelPath = metamodelPath.replace('\\', '/');
    }

    /**
     * Method that actually extracts the info
     */
    public List<List<String>> extractInfo() {
        XPathExpression<Element> xpathExpression = xpathFactory.compile(XPATH_STRING, Filters.element(), null,
                NAMESPACE_SVRL);

        List<Element> xpathResults = xpathExpression.evaluate(document);
        List<List<String>> results = new ArrayList<>(xpathResults.size());
        for (Element element : xpathResults) {
            List<String> result = new ArrayList<>(2);
            result.add(0, element.getChildTextNormalize("text", NAMESPACE_SVRL).replace(METAMODEL_DIR_REPLACE_TOKEN,
                    metamodelPath));
            result.add(1, element.getChildTextNormalize("diagnostic-reference", NAMESPACE_SVRL));
            //         result.add(2,element.getAttributeValue("location"));
            if (!results.contains(result)) {
                results.add(result);
            }
        }
        return results;

    }
}