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

Java tutorial

Introduction

Here is the source code for com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterMain.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 static com.c4om.utils.xmlutils.JDOMUtils.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import net.sf.saxon.s9api.SaxonApiException;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * Main class.
 * First, it reads the svrl input (as XML). 
 * Then, it gets all the useful information by means of {@link SVRLInterpreterExtractor}.
 * Then, it processes it by means of {@link SVRLInterpreterProcessor}.
 * Last, it returns the results. If the tool was launched from the command line, those results are printed 
 * to stdout.
 * 
 * @author Pablo Alonso Rodriguez (Center for Open Middleware)
 */
public class SVRLInterpreterMain {

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

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

    /**
     * Constructor
     * @param file {@link SVRLInterpreterMain#file}
     * @param metamodelPath {@link SVRLInterpreterMain#metamodelPath}
     */
    public SVRLInterpreterMain(String file, String metamodelPath) throws IOException, JDOMException {
        this.metamodelPath = metamodelPath;
        SAXBuilder builder = new SAXBuilder();
        InputStream is;
        if (file.equals("--")) {
            is = System.in;
        } else {
            File fileFile = new File(file);
            if (!fileFile.exists()) {
                throw new FileNotFoundException("File \"" + file + "\" does not exist.");
            }
            is = new FileInputStream(fileFile);
        }
        this.document = builder.build(is);
    }

    /**
     * Constructor
     * @param file {@link SVRLInterpreterMain#file}
     * @param metamodelPath {@link SVRLInterpreterMain#metamodelPath}
     */
    public SVRLInterpreterMain(Document document, String metamodelPath) {
        this.metamodelPath = metamodelPath;
        this.document = document;
    }

    /**
     * Method that actually performs the interpretation and converts it to a {@link String}.
     * @return the report generated by the interpreter, including all the additional information.
     * @throws IOException Input/Output problems
     * @throws JDOMException Problems at XML parsing
     * @throws SaxonApiException Problems at XQuery queries
     */
    public String interpret() throws SaxonApiException, JDOMException, IOException {
        Document resultingReportDoc = interpretAsDoc();
        String result = convertJDOMDocumentToString(resultingReportDoc);
        return result;
    }

    /**
     * Method that actually performs the interpretation
     * @return the report generated by the interpreter, including all the additional information.
     * @throws IOException Input/Output problems
     * @throws JDOMException Problems at XML parsing
     * @throws SaxonApiException Problems at XQuery queries
     */
    public Document interpretAsDoc() throws SaxonApiException, JDOMException, IOException {
        SVRLInterpreterExtractor extractor = new SVRLInterpreterExtractor(document, metamodelPath);
        List<List<String>> extractedInfo = extractor.extractInfo();
        SVRLInterpreterProcessor processor = new SVRLInterpreterProcessor();
        Document resultingReportDoc = processor.process(extractedInfo);
        return resultingReportDoc;
    }

    /**
     * Method called when the program is executed
     * @param args it must contain one argument. If its value is "--", input will be read from stdin. Otherwise, the argument will be interpreted as the path to the input file.
     */
    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.err.println(
                    "Not enough arguments. Please, provide a valid path to the input file (or \"--\" to get the input from stdin) and the path to the metamodel folder.");
        }
        SVRLInterpreterMain interpeter = new SVRLInterpreterMain(args[0], args[1]);
        String result = interpeter.interpret();
        System.out.println(result);
    }
}