Example usage for org.jdom2.input SAXBuilder build

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

Introduction

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

Prototype

@Override
public Document build(final String systemId) throws JDOMException, IOException 

Source Link

Document

This builds a document from the supplied URI.

Usage

From source file:edu.utep.cs.jasg.specificationGenerator.XMLParser.java

License:Open Source License

/** Print XML file using XMLOutputter. */
public void printXMLFile(File file) {

    try {// ww w . j a  v  a  2 s  . com
        // Build the document with SAX and Xerces, no validation
        SAXBuilder builder = new SAXBuilder();
        // Create the document
        Document doc = builder.build(file);

        // Output the document, use standard formatter
        XMLOutputter fmt = new XMLOutputter();
        fmt.output(doc, System.out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.wisc.ssec.adapter.NetCDFFile.java

License:Open Source License

public static NetCDFFile makeUnion(String filename, String other) throws Exception {
    Object obj = new Object();
    URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml");
    SAXBuilder builder = new SAXBuilder(false);
    Document doc = null;//  www  .ja  va 2s .com

    try {
        doc = builder.build(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Element root = doc.getRootElement();

    List list = root.getChildren();

    list = ((Element) list.get(1)).getChildren();

    org.jdom2.Attribute attr1 = (org.jdom2.Attribute) (((Element) list.get(0)).getAttributes()).get(0);
    attr1.setValue(filename);

    org.jdom2.Attribute attr2 = (org.jdom2.Attribute) (((Element) list.get(1)).getAttributes()).get(0);
    attr2.setValue(other);

    XMLOutputter xmlOut = new XMLOutputter();
    String newStr = xmlOut.outputString(doc);
    ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes());
    return new NetCDFFile(is);
}

From source file:edu.wisc.ssec.mcidasv.chooser.TDSRadarChooser.java

License:Open Source License

/**
 * Get  the radar collections for  the given server URL
 *
 * @param radarServerURL  server URL/*from w ww  .j  a  v  a2  s .c o m*/
 *
 * @return  a map of the collection names to URL
 */
private List getRadarCollections(String radarServerURL) {
    SAXBuilder builder;
    Document doc = null;
    XMLEntityResolver jaxp = new XMLEntityResolver(true);
    builder = jaxp.getSAXBuilder();
    List collections = new ArrayList();

    try {
        doc = builder.build(radarServerURL);
    } catch (JDOMException e) {
        userMessage("Invalid catalog");
    } catch (IOException e) {
        userMessage("Unable to open catalog");
    }

    org.jdom2.Element rootElem = doc.getRootElement();
    org.jdom2.Element serviceElem = readElements(rootElem, "service");
    String uriBase = serviceElem.getAttributeValue("base");
    org.jdom2.Element dsElem = readElements(rootElem, "dataset");
    String naming = "catalogRef";
    Namespace nss = rootElem.getNamespace("xlink");
    List children = dsElem.getChildren();
    for (int j = 0; j < children.size(); j++) {
        org.jdom2.Element child = (org.jdom2.Element) children.get(j);
        String childName = child.getName();
        if (childName.equals(naming)) {
            //String id   = child.getAttributeValue("ID");
            String desc = child.getAttributeValue("title", nss);
            String urlpath = child.getAttributeValue("href", nss);
            String[] c = radarServerURL.split(uriBase); //.replaceFirst("catalog.xml", "");
            String ul = c[0] + uriBase + urlpath;
            TwoFacedObject twoObj = new TwoFacedObject(desc, ul);
            collections.add(twoObj);
            //collections.put(desc, ul);
        }

    }

    return collections;
}

From source file:edu.wisc.ssec.mcidasv.data.hydra.NetCDFFile.java

License:Open Source License

public static NetCDFFile makeUnion(String filename, String other) throws Exception {
    Object obj = new Object();
    URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml");
    SAXBuilder builder = new SAXBuilder(false);
    Document doc = null;/*from   w w  w.j a  v  a2  s  .co m*/

    try {
        doc = builder.build(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Element root = doc.getRootElement();

    List list = root.getChildren();

    list = ((Element) list.get(1)).getChildren();

    org.jdom2.Attribute attr1 = (((Element) list.get(0)).getAttributes()).get(0);
    attr1.setValue(filename);

    org.jdom2.Attribute attr2 = (((Element) list.get(1)).getAttributes()).get(0);
    attr2.setValue(other);

    XMLOutputter xmlOut = new XMLOutputter();
    String newStr = xmlOut.outputString(doc);
    logger.trace("union string:\n{}", newStr);
    ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes());
    return new NetCDFFile(is);
}

From source file:elh.eus.absa.CorpusReader.java

License:Open Source License

private void extractOpinionsAbsaSemEval2014(InputStream fileName) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {//from   w ww. j  ava  2 s.  c o m
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        Integer sId = 0; //sentence id
        Integer oId = 0; //opinion id         
        for (Element sent : sentences) {
            sId++;
            StringBuilder sb = new StringBuilder();
            String sentString = sent.getChildText("text");
            sb = sb.append(sentString);
            Element aspectTerms = sent.getChild("aspectTerms");
            if (aspectTerms != null) {
                List<Element> aspectTermList = aspectTerms.getChildren();
                for (Element aspectElem : aspectTermList) {
                    oId++;
                    String trgt = aspectElem.getAttributeValue("target");
                    Integer offsetFrom = Integer.parseInt(aspectElem.getAttributeValue("from"));
                    Integer offsetTo = Integer.parseInt(aspectElem.getAttributeValue("to"));
                    String polarity = aspectElem.getAttributeValue("polarity");
                    //String cat = aspectElem.getAttributeValue("category");

                    //create and add opinion to the structure
                    Opinion op = new Opinion("o" + oId, trgt, offsetFrom, offsetTo, polarity, null, "s" + sId);
                    this.addOpinion(op);
                }

                //System.out.println(sb.toString());
            }
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}

From source file:elh.eus.absa.CorpusReader.java

License:Open Source License

/**
 * Read semeval-absa 2015 shared task formatted corpus and extract opinions.
 * //w w w .j a  v a 2  s  .  c om
 * @param InputStream fileName: corpus 
 * @param boolean nullSentOps: whether null opinions should be created for sentence with no opinion
 *                              (only used for semeval-absa 2015 formatted corpora)
 * 
 */
private void extractOpinionsAbsaSemEval2015(InputStream fileName, boolean nullSentenceOps) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        String rId = "";
        String sId = ""; //sentence id
        Integer oId = 0; //opinion id
        for (Element sent : sentences) {
            sId = sent.getAttributeValue("id");
            rId = sId.replaceAll(":[0-9]+$", "");

            if (rId.equalsIgnoreCase(sId)) {
                rId = sId.replaceAll("#[0-9]+$", "");
            }

            //store the sentence and the corresponding review
            addRevSent(rId, sId);
            StringBuilder sb = new StringBuilder();
            String sentString = sent.getChildText("text");
            //add sentence to the reader object
            this.addSentence(sId, sentString);

            sb = sb.append(sentString);
            Element opinions = sent.getChild("Opinions");
            if (opinions != null) {
                List<Element> opinionList = opinions.getChildren();
                //there is no opinions
                if (opinionList.isEmpty()) {
                    //System.err.println("kkkkk");
                    //create sentence at list, even if it has no opinion elements
                    sId = sent.getAttributeValue("id");
                    addRevSent(rId, sId);
                    String sentStr = sent.getChildText("text");
                    //add sentence to the reader object
                    this.addSentence(sId, sentStr);
                    if (nullSentenceOps) {
                        oId++;
                        //create and add opinion to the structure
                        Opinion op = new Opinion("o" + oId, "NULL", 0, 0, "NULL", "NULL", sId);
                        this.addOpinion(op);
                    }
                }

                for (Element opElem : opinionList) {
                    oId++;
                    String trgt = opElem.getAttributeValue("target");
                    Integer offsetFrom = 0;
                    Integer offsetTo = 0;
                    try {
                        offsetFrom = Integer.parseInt(opElem.getAttributeValue("from"));
                        offsetTo = Integer.parseInt(opElem.getAttributeValue("to"));

                    } catch (NumberFormatException ne) {
                    }
                    String polarity = opElem.getAttributeValue("polarity");
                    String cat = opElem.getAttributeValue("category");

                    //create and add opinion to the structure
                    Opinion op = new Opinion("o" + oId, trgt, offsetFrom, offsetTo, polarity, cat, sId);
                    this.addOpinion(op);

                    //debugging
                    sb.append("\n\t> " + "o" + oId + " " + trgt + " " + offsetFrom + "-" + offsetTo + " "
                            + polarity + " " + cat);
                }
                //System.out.println(sb.toString());
            } else {
                //System.err.println("kkkkk");
                //create sentence at list, even if it has no opinion elements
                sId = sent.getAttributeValue("id");
                addRevSent(rId, sId);
                String sentStr = sent.getChildText("text");
                //add sentence to the reader object
                this.addSentence(sId, sentStr);
                if (nullSentenceOps) {
                    oId++;
                    //create and add opinion to the structure
                    Opinion op = new Opinion("o" + oId, "NULL", 0, 0, "NULL", "NULL", sId);
                    this.addOpinion(op);
                }
            }
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}

From source file:elh.eus.absa.CorpusReader.java

License:Open Source License

/**
 *    Extract sentence texts from tabulated format. The function assumes the text is PoS tagged in
 *  Conll tabulated format./*from   w  w  w .  ja va2 s. c o  m*/
 * 
 * @param fileName string: input corpus file path
 */
private void extractOpinionsTabText(InputStream fileName) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        String rId = "";
        String sId = ""; //sentence id
        Integer oId = 0; //opinion id
        for (Element sent : sentences) {
            sId = sent.getAttributeValue("id");
            rId = sId;
            oId++;

            /*store the sentence and the corresponding review
             * (in this case this info is redundant, because a whole review is represented by a sentence)  
             */
            addRevSent(rId, sId);
            //StringBuilder sb = new StringBuilder();
            String sentString = sent.getChildText("text");
            //add sentence to the reader object
            this.addSentence(sId, sentString);

            String polarity = sent.getAttributeValue("polarity");
            if (polarity == null) {
                System.err.println("no polarity annotation for review " + rId + "."
                        + " Review won't be taken into account");
                continue;
            }

            String trgt = "";
            String cat = "global";
            Integer offsetFrom = 0;
            Integer offsetTo = 0;

            //create and add opinion to the structure
            Opinion op = new Opinion("o" + oId, trgt, offsetFrom, offsetTo, polarity, cat, sId);
            this.addOpinion(op);

            //debugging
            //sb.append("\n\t> "+"o"+oId+" "+trgt+" "+offsetFrom+"-"+offsetTo+" "+polarity+" "+cat);
        }
        //System.out.println(sentString);         
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}

From source file:engine.Engine.java

License:Open Source License

public void loadLevel(String filePath) throws JDOMException, IOException {
    SAXBuilder sax = new SAXBuilder();
    Document doc;// ww w.  j  a  va  2s .c o m

    doc = sax.build(new File(filePath));
    Element root = doc.getRootElement();
    List<Element> listElem = root.getChildren();

    for (Element elem : listElem) {
        Entity entity = null;
        switch (elem.getName()) {
        case "BreakableBlock":
            entity = new BreakableBlock(this);
            break;
        case "SolidBlock":
            entity = new SolidBlock(this);
            break;
        case "Bomb":
            entity = new Bomb(this);
            break;
        case "Fire":
            entity = new Fire(this);
            break;
        case "Bomberman":
            entity = new Bomberman(this);
            break;
        case "Bonus":
            Bonus bonus = new Bonus(this);
            bonus.setBomb(elem.getAttribute("bomb").getIntValue());
            bonus.setPower(elem.getAttribute("power").getIntValue());
            bonus.setSpeed(elem.getAttribute("speed").getIntValue());

            entity = bonus;
            break;
        case "SpeedBonus":
            entity = new SpeedBonus(this);
            break;
        case "PowerBonus":
            entity = new PowerBonus(this);
            break;
        case "BombBonus":
            entity = new BombBonus(this);
            break;
        case "KickBonus":
            entity = new KickBonus(this);
            break;
        case "FutureBonus":
            futureBonus(elem);
            continue;
        default:
            this.log.warn("loadLevel: unknown type object -> " + elem.getName());
            continue;
        }

        Vector2f position = new Vector2f();
        position.x = elem.getAttribute("x").getIntValue() * 1000;
        position.y = elem.getAttribute("y").getIntValue() * 1000;
        entity.setPosition(position);

        entity.setDirection(elem.getAttribute("dir").getIntValue());

        addEntity(entity);
    }

    this.collisionManager.addHandler(new BombermanBlockCH(this.collisionManager));
    this.collisionManager.addHandler(new BombermanBonusCH());
    this.collisionManager.addHandler(new BombermanFireCH());
    this.collisionManager.addHandler(new BombFireCH());
    this.collisionManager.addHandler(new BonusFireCH());
    this.collisionManager.addHandler(new BlockFireCH());
    this.collisionManager.addHandler(new BombBlockCH());
    BombermanBombCH han = new BombermanBombCH(this.collisionManager);
    addListener(han);
    this.collisionManager.addHandler(han);

    this.loaded = true;
}

From source file:Enrichissement.Jaccard.java

private static void ouvrirDoc() {
    SAXBuilder sxb = new SAXBuilder();
    try {/*from   w w  w.jav a2s  . c om*/
        // On cre un nouveau document JDOM avec en argument le fichier XML
        // Le parsing est termin ;)
        document = sxb.build(new File("rdf.xml"));
        System.out.println("Fichier ouvert");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // On initialise un nouvel lment racine avec l'lment racine du
    // document.
    racine = document.getRootElement();
}

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

License:Apache License

public void absaSemEvalToNER(String fileName) {
    SAXBuilder sax = new SAXBuilder();
    XPathFactory xFactory = XPathFactory.instance();
    try {/*from  ww  w  .j  ava2s  . c  o  m*/
        Document doc = sax.build(fileName);
        XPathExpression<Element> expr = xFactory.compile("//sentence", Filters.element());
        List<Element> sentences = expr.evaluate(doc);
        for (Element sent : sentences) {

            StringBuilder sb = new StringBuilder();
            String sentString = sent.getChildText("text");
            sb = sb.append(sentString);
            Element aspectTerms = sent.getChild("aspectTerms");
            if (aspectTerms != null) {
                List<List<Integer>> offsetList = new ArrayList<List<Integer>>();
                List<Integer> offsets = new ArrayList<Integer>();
                List<Element> aspectTermList = aspectTerms.getChildren();
                if (!aspectTermList.isEmpty()) {
                    for (Element aspectElem : aspectTermList) {
                        Integer offsetFrom = Integer.parseInt(aspectElem.getAttributeValue("from"));
                        Integer offsetTo = Integer.parseInt(aspectElem.getAttributeValue("to"));
                        offsets.add(offsetFrom);
                        offsets.add(offsetTo);
                    }
                }
                Collections.sort(offsets);
                for (int i = 0; i < offsets.size(); i++) {
                    List<Integer> offsetArray = new ArrayList<Integer>();
                    offsetArray.add(offsets.get(i++));
                    if (offsets.size() > i) {
                        offsetArray.add(offsets.get(i));
                    }
                    offsetList.add(offsetArray);
                }
                int counter = 0;
                for (List<Integer> offsetSent : offsetList) {
                    Integer offsetFrom = offsetSent.get(0);
                    Integer offsetTo = offsetSent.get(1);
                    String aspectString = sentString.substring(offsetFrom, offsetTo);
                    sb.replace(offsetFrom + counter, offsetTo + counter,
                            "<START:term> " + aspectString + " <END>");
                    counter += 19;
                }
            }
            System.out.println(sb.toString());
        }
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
}