Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:es.ehu.si.ixa.pipe.convert.Convert.java

License:Apache License

public void absaSemEvalToMultiClassNER2015(String fileName) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {//w w w . j a  v a 2s  .  co m
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        for (Element sent : sentences) {

            String sentString = sent.getChildText("text");
            StringBuilder sb = new StringBuilder();
            sb = sb.append(sentString);
            Element opinionsElement = sent.getChild("Opinions");
            if (opinionsElement != null) {
                List<List<Integer>> offsetList = new ArrayList<List<Integer>>();
                HashSet<String> targetClassSet = new LinkedHashSet<String>();
                List<Integer> offsets = new ArrayList<Integer>();
                List<Element> opinionList = opinionsElement.getChildren();
                for (Element opinion : opinionList) {
                    if (!opinion.getAttributeValue("target").equals("NULL")) {
                        String className = opinion.getAttributeValue("category");
                        String targetString = opinion.getAttributeValue("target");
                        Integer offsetFrom = Integer.parseInt(opinion.getAttributeValue("from"));
                        Integer offsetTo = Integer.parseInt(opinion.getAttributeValue("to"));
                        offsets.add(offsetFrom);
                        offsets.add(offsetTo);
                        targetClassSet.add(targetString + "JAR!" + className + opinion.getAttributeValue("from")
                                + opinion.getAttributeValue("to"));
                    }
                }
                List<Integer> offsetsWithoutDuplicates = new ArrayList<Integer>(new HashSet<Integer>(offsets));
                Collections.sort(offsetsWithoutDuplicates);
                List<String> targetClassList = new ArrayList<String>(targetClassSet);

                for (int i = 0; i < offsetsWithoutDuplicates.size(); i++) {
                    List<Integer> offsetArray = new ArrayList<Integer>();
                    offsetArray.add(offsetsWithoutDuplicates.get(i++));
                    if (offsetsWithoutDuplicates.size() > i) {
                        offsetArray.add(offsetsWithoutDuplicates.get(i));
                    }
                    offsetList.add(offsetArray);
                }
                int counter = 0;
                for (int i = 0; i < offsetList.size(); i++) {
                    Integer offsetFrom = offsetList.get(i).get(0);
                    Integer offsetTo = offsetList.get(i).get(1);
                    String className = targetClassList.get(i);
                    String aspectString = sentString.substring(offsetFrom, offsetTo);
                    sb.replace(offsetFrom + counter, offsetTo + counter, "<START:"
                            + className.split("JAR!")[1].substring(0, 3) + "> " + aspectString + " <END>");
                    counter += 18;
                }
                System.out.println(sb.toString());
            }
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}

From source file:es.ehu.si.ixa.pipe.convert.Convert.java

License:Apache License

public void absaSemEvalText(Reader reader) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {/* ww  w. j  av  a 2  s  .  c  o m*/
        Document doc = sax.build(reader);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        for (Element sent : sentences) {
            String sentString = sent.getChildText("text");
            System.out.println(sentString);
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}

From source file:es.ehu.si.ixa.pipe.convert.Convert.java

License:Apache License

public String absa15testToNAF(String fileName) {
    KAFDocument kaf = new KAFDocument("en", "v1.naf");
    Segmenter segmenter = new Segmenter();
    TokenFactory tokenFactory = new TokenFactory();
    Properties properties = setAnnotateProperties();
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {/*from   w ww. ja  v  a  2s.co m*/
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);

        int counter = 1;
        for (Element sent : sentences) {
            String sentId = sent.getAttributeValue("id");
            String sentString = sent.getChildText("text");
            StringReader stringReader = new StringReader(sentString);
            BufferedReader breader = new BufferedReader(stringReader);
            IxaPipeTokenizer<Token> tokenizer = new IxaPipeTokenizer<Token>(breader, tokenFactory, properties);
            List<Token> tokens = tokenizer.tokenize();
            List<List<Token>> segmentedSentences = segmenter.segment(tokens);
            for (List<Token> sentence : segmentedSentences) {
                for (Token token : sentence) {
                    WF wf = kaf.newWF(token.value(), token.startOffset(), counter);
                    wf.setXpath(sentId);
                }
            }
            counter++;
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
    return kaf.toString();
}

From source file:es.ucm.fdi.ac.Analysis.java

License:Open Source License

/**
 * Reads an analysis from a file//  w w  w . j a va2  s  .c  o  m
 *
 * @param f the file to read from
 * @throws IOException on any error (may wrap invalid internal XML errors)
 */
public void loadFromFile(File f) throws IOException {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(f);
        loadFromXML(doc.getRootElement());
    } catch (Exception e) {
        throw new IOException("Error loading from '" + f.getAbsolutePath() + "' xml save file", e);
    }
}

From source file:es.ucm.fdi.ac.gui.MainGui.java

License:Open Source License

/** 
 * Load a test help file, adhering to the following structure:
 * <helppairs>//from w  w w.  j  av  a 2s.  c o  m
 *  <testhelp>
 *   <testname>...</testname>
 *   <helpcontent>...</helpcontent>
 *  </testhelp>
 *  ...
 * </helppairs>
 * Notice that 'helpcontent' should be XHTML: old-fashioned HTML may fail to
 * validate.
 */
public HashMap<String, String> loadTestHelpFile(String fileName) throws IOException {
    HashMap<String, String> m = new HashMap<String, String>();
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    URL url = getClass().getClassLoader().getResource(fileName);
    try {
        Document doc = (new SAXBuilder()).build(url);
        for (Element th : doc.getRootElement().getChildren()) {
            m.put(th.getChildTextTrim("testname"), outputter.outputString(th.getChild("helpcontent")));
        }
    } catch (JDOMException | NullPointerException e) {
        throw new IOException("Impossible to read XML file for " + url, e);
    }
    return m;
}

From source file:es.ucm.fdi.clover.gui.CloverSave.java

License:Open Source License

/**
 * Restores the CloverSave to a different XML file; totally changes the
 * 'views' (it is basically recreated) output array;
 *//*from  w  w  w . ja va2 s .c  o m*/
public static ArrayList<ClusterView> restore(BaseGraph base, ClusterViewFactory vf, File f) throws IOException {

    if (vf == null) {
        vf = new DefaultClusterViewFactory();
    }

    ArrayList<ClusterView> views = new ArrayList<ClusterView>();
    HashMap<String, Filter> filters = new HashMap<String, Filter>();
    HashMap<String, ClusterHierarchy> hierarchies = new HashMap<String, ClusterHierarchy>();

    SAXBuilder builder = new SAXBuilder();
    ClassLoader loader = base.getClass().getClassLoader();

    try {
        Document doc = builder.build(f.getAbsolutePath());
        Element root = doc.getRootElement();

        Element sharedElems = (Element) root.getChildren().get(0);

        for (Element e : (List<Element>) sharedElems.getChildren()) {
            if (e.getName().equals("filter")) {
                String className = e.getAttributeValue("filterClass");
                Filter filter = (Filter) loader.loadClass(className).newInstance();
                filter.restore(e);
                filters.put(e.getAttributeValue("id"), filter);
            } else if (e.getName().equals("hierarchy")) {
                Element cluster = (Element) e.getChildren().get(0);

                Element clusterer = (Element) e.getChildren().get(1);
                String className = clusterer.getAttributeValue("engineClass");
                ClusteringEngine engine = (ClusteringEngine) loader.loadClass(className).newInstance();
                engine.restore(clusterer);

                BaseGraph hb = base;
                if (e.getAttribute("filterId") != null) {
                    Filter filter = filters.get(e.getAttributeValue("filterId"));
                    hb = new FilteredGraph(base, filter);
                }

                ClusterHierarchy h = new ClusterHierarchy(e, hb, engine);
                hierarchies.put(e.getAttributeValue("id"), h);
            }
        }

        for (Element e : (List<Element>) root.getChildren()) {
            if (!e.getName().equals("view"))
                continue;

            // build view
            String hid = e.getAttributeValue("hierarchyId");
            ClusterView view = vf.createClusterView(hierarchies.get(hid), e);
            view.restore(e);

            Element layoutCache = (Element) e.getChildren().get(0);
            view.getAnimator().getLayoutCache().restore(layoutCache, view.getBase());

            Element animatorProps = (Element) e.getChildren().get(1);
            view.getAnimator().restore(animatorProps);

            views.add(view);
        }
    }

    // indicates a well-formedness error
    catch (JDOMException jde) {
        log.warn("Document is not well-formed XML");
        jde.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe);
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }

    return views;
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

License:Apache License

/**
 * Method that performs the conversion between JSON and XML
 * @param jsonRoot the root of the JSON document. It must be either a {@link JSONObject} or a {@link JSONArray}
 * @param namespaceRoot the namespace of the resulting root element
 * @return a JDOM2 {@link Document} representing the obtained XML.
 * @throws JDOMException//  w w w .ja va2s  .co  m
 * @throws IOException
 */
protected Document convertJSONToXML(Object jsonRoot, Namespace namespaceRoot)
        throws JDOMException, IOException {
    if (!(jsonRoot instanceof JSONArray || jsonRoot instanceof JSONObject)) {
        throw new IllegalArgumentException("'jsonRoot' must be either a JSONObject or a JSONArray");
    }
    quoteStringsAtJSON(jsonRoot);
    addPrefixToJSONKeysEndingsRecursive(jsonRoot, XSDInferenceConfiguration.ARRAY_ELEMENT_NAME,
            XSDInferenceConfiguration.ARRAY_ELEMENT_ESCAPING_PREFIX);
    encapsulateArraysAtJSONRecursive(jsonRoot);
    String xmlString = "<" + XSDInferenceConfiguration.XML_ROOT_NAME + ">" + XML.toString(jsonRoot) + "</"
            + XSDInferenceConfiguration.XML_ROOT_NAME + ">";
    SAXBuilder saxBuilder = new SAXBuilder();
    Document xmlDocument = saxBuilder.build(new StringReader(xmlString));
    setNamespaceToElementsByName(XSDInferenceConfiguration.ARRAY_ELEMENT_NAME, xmlDocument.getRootElement(),
            XSDInferenceConfiguration.NAMESPACE_ARRAY_ELEMENT);
    removePrefixToElementNameEndings(xmlDocument.getRootElement(), XSDInferenceConfiguration.ARRAY_ELEMENT_NAME,
            XSDInferenceConfiguration.ARRAY_ELEMENT_ESCAPING_PREFIX);
    xmlDocument.getRootElement().setNamespace(namespaceRoot);
    sortElementsRecursive(xmlDocument.getRootElement());
    return xmlDocument;
}

From source file:es.upm.dit.xsdinferencer.XSDInferencer.java

License:Apache License

/**
 * Method that, given an args array, does the whole inference process by calling the appropriate submodules.
 * @param args the args array, as provided by {@link XSDInferencer#main(String[])}
 * @return a {@link Results} object with the inference results (both statistics and XSDs)
 * @throws XSDConfigurationException if there is a problem with the configuration
 * @throws IOException if there is an I/O problem while reading the input XML files or writing the output files
 * @throws JDOMException if there is any problem while parsing the input XML files 
 *//*from ww  w .  j a  v a2 s.  c o m*/
public Results inferSchema(String[] args) throws XSDInferencerException {
    try {
        XSDInferenceConfiguration configuration = new XSDInferenceConfiguration(args);
        FilenameFilter filenameFilter;
        if (configuration.getWorkingFormat().equals("xml")) {
            filenameFilter = FILE_NAME_FILTER_XML_EXTENSION;
            List<File> xmlFiles = getInstanceFileNames(args, filenameFilter);
            List<Document> xmlDocuments = new ArrayList<>(xmlFiles.size());
            SAXBuilder saxBuilder = new SAXBuilder();
            for (int i = 0; i < xmlFiles.size(); i++) {
                File xmlFile = xmlFiles.get(i);
                System.out.print("Reading input file " + xmlFile.getName() + "...");
                FileInputStream fis = new FileInputStream(xmlFile);
                //BufferedReader reader = new BufferedReader(new InputStreamReader(fis, Charsets.UTF_8));
                Document xmlDocument = saxBuilder.build(fis);
                xmlDocuments.add(xmlDocument);
                System.out.println("OK");
            }
            return inferSchema(xmlDocuments, configuration);
        } else if (configuration.getWorkingFormat().equals("json")) {
            filenameFilter = FILE_NAME_FILTER_JSON_EXTENSION;
            List<File> jsonFiles = getInstanceFileNames(args, filenameFilter);
            List<JSONObject> jsonDocumentWithRootObjects = new ArrayList<>(jsonFiles.size());
            List<JSONArray> jsonDocumentWithRootArrays = new ArrayList<>(jsonFiles.size());
            for (int i = 0; i < jsonFiles.size(); i++) {
                File jsonFile = jsonFiles.get(i);
                String jsonString = Joiner.on(System.lineSeparator())
                        .join(Files.readAllLines(jsonFile.toPath()));
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(jsonString);
                } catch (JSONException e) {
                }

                if (jsonObject != null) {
                    jsonDocumentWithRootObjects.add(jsonObject);
                } else {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(jsonString);
                    } catch (JSONException e) {
                    }
                    if (jsonArray != null) {
                        jsonDocumentWithRootArrays.add(jsonArray);
                    } else {
                        throw new JSONException("Invalid JSON Document " + jsonFile);
                    }

                }
            }
            return inferSchema(jsonDocumentWithRootObjects, jsonDocumentWithRootArrays, configuration);
        } else {
            throw new InvalidXSDConfigurationParameterException(
                    "Unknown working format. Impossible to load files");
        }
    } catch (IOException | JDOMException | RuntimeException e) {
        throw new XSDInferencerException(e);
    }

}

From source file:es.uvigo.ei.sing.rubioseq.gui.view.models.configuration.ConfigProgramPathsChipSeq.java

License:Open Source License

public void loadDataFromFile(File editFile) {

    loadDefaultParameters(editFile);/*from  w  w w .ja v a 2 s  . com*/

    SAXBuilder saxBuilder = new SAXBuilder();
    try {
        Document document = saxBuilder.build(editFile);
        Element configData = document.getRootElement();

        Element bwaPath = configData.getChild(BWA_PATH);
        if (validElement(bwaPath)) {
            this.setBwaPath(Utils.getRUbioSeqFile(bwaPath.getValue()));
        }

        Element samtoolsPath = configData.getChild(SAMTOOLS_PATH);
        if (validElement(samtoolsPath)) {
            this.setSamtoolsPath(Utils.getRUbioSeqFile(samtoolsPath.getValue()));
        }

        Element picardPath = configData.getChild(PICARD_PATH);
        if (validElement(picardPath)) {
            this.setPicardPath(Utils.getRUbioSeqFile(picardPath.getValue()));
        }

        Element MACSPath = configData.getChild(MACS_PATH);
        if (validElement(MACSPath)) {
            this.setMacsPath(Utils.getRUbioSeqFile(MACSPath.getValue()));
        }

        Element PythonPath = configData.getChild(PYTHON_PATH);
        if (validElement(PythonPath)) {
            this.setPythonPath(Utils.getRUbioSeqFile(PythonPath.getValue()));
        }

        Element fastqcPath = configData.getChild(FASTQC_PATH);
        if (validElement(fastqcPath)) {
            this.setFastqcPath(Utils.getRUbioSeqFile(fastqcPath.getValue()));
        }

        Element BEDToolsPath = configData.getChild(BED_TOOLS_PATH);
        if (validElement(BEDToolsPath)) {
            this.setBedtoolsPath(Utils.getRUbioSeqFile(BEDToolsPath.getValue()));
        }

        Element BedgraphtoWigPath = configData.getChild(BGTW_PATH);
        if (validElement(BedgraphtoWigPath)) {
            this.setBedgraphtobigwigPath(Utils.getRUbioSeqFile(BedgraphtoWigPath.getValue()));
        }

        Element IDRPath = configData.getChild(IDR_PATH);
        if (validElement(IDRPath)) {
            this.setIdrPath(Utils.getRUbioSeqFile(IDRPath.getValue()));
        }

        Element PeakAnnotatorPath = configData.getChild(PEAKANNOTATOR_PATH);
        if (validElement(PeakAnnotatorPath)) {
            this.setPeakannotatorPath(Utils.getRUbioSeqFile(PeakAnnotatorPath.getValue()));
        }

        Element CCATPath = configData.getChild(CCAT_PATH);
        if (validElement(CCATPath)) {
            this.setCcatPath(Utils.getRUbioSeqFile(CCATPath.getValue()));
        }

        Element nthr = configData.getChild(NTHR);
        if (validElement(nthr)) {
            try {
                this.setNthr(Integer.valueOf(nthr.getValue()));
            } catch (Exception e) {
            }
        }

        Element javaRam = configData.getChild(JAVA_RAM);
        if (validElement(javaRam)) {
            this.setJavaRam(javaRam.getValue());
        }

        Element queueSystem = configData.getChild(QUEUE_SYSTEM);
        System.err.println("\n\n\t QUEUE_SYSTEM = " + queueSystem + "\n");
        if (validElement(queueSystem)) {
            String queueSystemValue = queueSystem.getValue().toUpperCase();
            if (queueSystemValue.equals("SGE")) {
                this.setQueueSystem(QueueSystem.SGE);
            } else if (queueSystemValue.equals("PBS")) {
                this.setQueueSystem(QueueSystem.PBS);
            } else if (queueSystemValue.equals("NONE")) {
                this.setQueueSystem(QueueSystem.NONE);
            }
        }

        Element queueName = configData.getChild(QUEUE_NAME);
        if (validElement(queueName)) {
            this.setQueueName(queueName.getValue());
        }

        Element multicoreName = configData.getChild(MULTICORE_NAME);
        if (validElement(multicoreName)) {
            this.setMulticoreName(multicoreName.getValue());
        }

        Element multicoreNumber = configData.getChild(MULTICORE_NUMBER);
        if (validElement(multicoreNumber)) {
            try {
                this.setMulticoreNumber(Integer.valueOf(multicoreNumber.getValue()));
            } catch (Exception e) {
            }
        }

    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:es.uvigo.ei.sing.rubioseq.gui.view.models.configuration.ConfigProgramPathsCNV.java

License:Open Source License

public void loadDataFromFile(File editFile) {

    loadDefaultParameters(editFile);//from ww  w.  ja  v  a2  s  .com

    SAXBuilder saxBuilder = new SAXBuilder();
    try {
        Document document = saxBuilder.build(editFile);
        Element configData = document.getRootElement();

        Element bwaPath = configData.getChild(BWA_PATH);
        if (validElement(bwaPath)) {
            this.setBwaPath(Utils.getRUbioSeqFile(bwaPath.getValue()));
        }

        Element samtoolsPath = configData.getChild(SAMTOOLS_PATH);
        if (validElement(samtoolsPath)) {
            this.setSamtoolsPath(Utils.getRUbioSeqFile(samtoolsPath.getValue()));
        }

        Element gatkpath = configData.getChild(GATK_PATH);
        if (validElement(gatkpath)) {
            this.setGatkpath(Utils.getRUbioSeqFile(gatkpath.getValue()));
        }

        Element picardPath = configData.getChild(PICARD_PATH);
        if (validElement(picardPath)) {
            this.setPicardPath(Utils.getRUbioSeqFile(picardPath.getValue()));
        }

        Element BFASTPath = configData.getChild(BFAST_PATH);
        if (validElement(BFASTPath)) {
            this.setBfastPath(Utils.getRUbioSeqFile(BFASTPath.getValue()));
        }

        Element fastqcPath = configData.getChild(FASTQC_PATH);
        if (validElement(fastqcPath)) {
            this.setFastqcPath(Utils.getRUbioSeqFile(fastqcPath.getValue()));
        }

        Element BEDToolsPath = configData.getChild(BED_TOOLS_PATH);
        if (validElement(BEDToolsPath)) {
            this.setBedtoolsPath(Utils.getRUbioSeqFile(BEDToolsPath.getValue()));
        }

        Element CONTRAPath = configData.getChild(CONTRA_PATH);
        if (validElement(CONTRAPath)) {
            this.setContraPath(Utils.getRUbioSeqFile(CONTRAPath.getValue()));
        }

        Element nthr = configData.getChild(NTHR);
        if (validElement(nthr)) {
            try {
                this.setNthr(Integer.valueOf(nthr.getValue()));
            } catch (Exception e) {
            }
        }

        Element javaRam = configData.getChild(JAVA_RAM);
        if (validElement(javaRam)) {
            this.setJavaRam(javaRam.getValue());
        }

        Element queueSystem = configData.getChild(QUEUE_SYSTEM);
        if (validElement(queueSystem)) {
            String queueSystemValue = queueSystem.getValue().toUpperCase();
            if (queueSystemValue.equals("SGE")) {
                this.setQueueSystem(QueueSystem.SGE);
            } else if (queueSystemValue.equals("PBS")) {
                this.setQueueSystem(QueueSystem.PBS);
            } else if (queueSystemValue.equals("NONE")) {
                this.setQueueSystem(QueueSystem.NONE);
            }
        }

        Element queueName = configData.getChild(QUEUE_NAME);
        if (validElement(queueName)) {
            this.setQueueName(queueName.getValue());
        }

        Element multicoreName = configData.getChild(MULTICORE_NAME);
        if (validElement(multicoreName)) {
            this.setMulticoreName(multicoreName.getValue());
        }

        Element multicoreNumber = configData.getChild(MULTICORE_NUMBER);
        if (validElement(multicoreNumber)) {
            try {
                this.setMulticoreNumber(Integer.valueOf(multicoreNumber.getValue()));
            } catch (Exception e) {
            }
        }

    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}