Example usage for org.dom4j.io SAXReader SAXReader

List of usage examples for org.dom4j.io SAXReader SAXReader

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader SAXReader.

Prototype

public SAXReader() 

Source Link

Usage

From source file:HelloWorldServlet.java

License:Apache License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();

    Document doc = null;/*from   ww  w .  jav a  2  s  .  c o m*/
    SAXReader reader = new SAXReader();
    InputStream in = req.getInputStream();
    try {

        doc = reader.read(in);
        Metadata question = new Metadata(doc);
        String answer = MessageProcessor.process(question);
        log.info("==============" + answer);
        out.printf(RESPONSE_TXT, question.getFromUserName(), question.getToUserName(),
                System.currentTimeMillis(), "text", answer);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    in.close();
    in = null;
    out.close();
    out = null;
}

From source file:InitializeDB.java

License:Open Source License

public static void setupDB(int year) {

    try {/*from w  ww. j ava  2  s.  co m*/

        //String filename = "nvdcve-2008.xml";

        Connection con = getConnection();

        Statement sql = con.createStatement();

        sql.execute("drop table if exists nvd"); //,primary key(id)

        sql.execute(
                "create table nvd(id varchar(20) not null,soft varchar(160) not null default 'ndefined',rng varchar(100) not null default 'undefined',lose_types varchar(100) not null default 'undefind',severity varchar(20) not null default 'unefined',access varchar(20) not null default 'unefined');");

        SAXReader saxReader = new SAXReader();

        for (int ct = 2002; ct <= year; ct++) {

            //String fname="/transient/mulval/oval/nvd/nvdcve-"+Integer.toString(ct)+".xml";
            String fname = "nvd_old/nvdcve-" + Integer.toString(ct) + ".xml";

            Document document = saxReader.read(fname);

            List entry = document.selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']");

            Iterator ent = entry.iterator();

            int act = 0;

            while (ent.hasNext()) { // varchar(20) not null default 'name',

                Element id = (Element) ent.next();

                String cveid = id.attributeValue("name");

                String cvss = "";

                String access = "";

                //   System.out.println(cveid + access);

                String sev = "";

                String host = "localhost";

                String sftw = "";

                String rge = "";

                String rge_tmp = "";

                String lose_tmp = "";

                String lose_types = "";

                ArrayList<String> subele = new ArrayList<String>();

                ArrayList<String> attr = new ArrayList<String>();

                Iterator ei = id.elementIterator();

                while (ei.hasNext()) { // put all of the subelements'

                    // names(subelement of entry) to the

                    // array list

                    Element sube = (Element) ei.next();

                    subele.add(sube.getName());

                }

                //System.out.println(id.getText());

                Iterator i = id.attributeIterator();

                while (i.hasNext()) { // put the attributes of the entries to

                    // the arraylist

                    Attribute att = (Attribute) i.next();

                    attr.add(att.getName());

                }

                if (subele.contains("vuln_soft")) {

                    Element vs = (Element) id.element("vuln_soft");

                    Iterator itr = vs.elementIterator("prod");

                    while (itr.hasNext()) { // record all of the softwares

                        Element n = (Element) itr.next();

                        //sftw = sftw + n.attributeValue("name") + ",";

                        sftw = n.attributeValue("name");

                        if (sftw.contains("'")) {

                            sftw = sftw.replace("'", "''");

                        }

                        break;

                    }

                    //int lsf = sftw.length();

                    //sftw = sftw.substring(0, lsf - 1);// delete the last comma

                }

                if (attr.contains("severity")) {

                    sev = id.attributeValue("severity");

                }

                if (attr.contains("CVSS_vector")) {

                    cvss = id.attributeValue("CVSS_vector");
                    char ac = cvss.charAt(9);
                    if (ac == 'L')
                        access = "l";
                    else if (ac == 'M')
                        access = "m";
                    else if (ac == 'H')
                        access = "h";
                    else
                        ;

                }

                if (subele.contains("range")) { // to get the range as a array

                    Element vs = (Element) id.element("range");

                    Iterator rgi = vs.elementIterator();

                    while (rgi.hasNext()) { // record all of the softwares

                        Element rg = (Element) rgi.next();

                        if (rg.getName().equals("user_init"))

                            rge_tmp = "user_action_req";

                        else if (rg.getName().equals("local_network"))

                            rge_tmp = "lan";

                        else if (rg.getName().equals("network"))

                            rge_tmp = "remoteExploit";

                        else if (rg.getName().equals("local"))

                            rge_tmp = "local";

                        else

                            rge_tmp = "other";

                        rge = rge + "''" + rge_tmp + "'',";

                    }

                    int lr = rge.length();

                    rge = rge.substring(0, lr - 1);// delete the last comma

                }

                if (subele.contains("loss_types")) {

                    Element lt = (Element) id.element("loss_types");

                    Iterator lti = lt.elementIterator();

                    while (lti.hasNext()) {

                        ArrayList<String> isecat = new ArrayList<String>();

                        Element ls = (Element) lti.next();

                        if (ls.getName().equals("avail"))

                            lose_tmp = "availability_loss";

                        else if (ls.getName().equals("conf"))

                            lose_tmp = "data_loss";

                        else if (ls.getName().equals("int"))

                            lose_tmp = "data_modification";

                        else

                            lose_tmp = "other";

                        lose_types = lose_types + "''" + lose_tmp + "'',";

                    }

                    int ltp = lose_types.length();

                    lose_types = lose_types.substring(0, ltp - 1);// delete the

                    // last

                    // comma

                }

                //System.out.println(cveid + lose_types + rge + sftw + sev + access);

                String insert = "insert nvd values('" + cveid + "','"

                        + sftw + "','" + rge + "','" + lose_types + "','" + sev

                        + "','" + access + "')";

                sql.execute(insert);

            }

        }

        sql.close();

        con.close();

    } catch (java.lang.ClassNotFoundException e) {

        System.err.println("ClassNotFoundException:" + e.getMessage());

    } catch (SQLException ex) {

        System.err.println("SQLException:" + ex.getMessage());

    } catch (DocumentException e) {

        e.printStackTrace();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}

From source file:InitializeDB.java

License:Open Source License

public static void clearEntryWithVulsoft(String filename) {

    try {/*from  w  w  w  .j a  v  a 2s .c  om*/

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(filename);

        List soft = document

                .selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']/*[local-name(.)='vuln_soft']");

        Iterator sft = soft.iterator();

        Element nvd = (Element) document

                .selectSingleNode("/*[local-name(.)='nvd']");

        while (sft.hasNext()) {

            Element vsft = (Element) sft.next();

            nvd.remove(vsft.getParent());

            XMLWriter output = new XMLWriter(new FileWriter(filename));//

            output.write(document);

            output.flush();

            output.close();

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

From source file:NessusXMLParser.java

License:Open Source License

public static void parseNessus(String nessusReport) {

    try {/*from w w  w . j  a  v a2  s .  c  o m*/

        SAXReader saxReader = new SAXReader();

        FileWriter fr = new FileWriter("vulInfo.txt");

        Document document = saxReader.read(nessusReport);

        // each entry is indexed by one cve_id
        List reportHost = document.selectNodes(
                "/*[local-name(.)='NessusClientData_v2']/*[local-name(.)='Report']/*[local-name(.)='ReportHost']");
        Iterator reportHostItrt = reportHost.iterator();

        while (reportHostItrt.hasNext()) {

            Element host = (Element) reportHostItrt.next();

            //   System.out.println("host name is: "+host.attribute(0).getText());

            // element iterator of each entry
            Iterator ei = host.elementIterator();

            // put all of the subelements' names(subelement of entry) to
            // an array list(subele)
            while (ei.hasNext()) {

                Element sube = (Element) ei.next();
                //   System.out.println("attribute count is: "+sube.attributeCount());
                if (!sube.getName().equals("ReportItem"))
                    continue;

                // a list of elements for each entry
                ArrayList<String> subele = new ArrayList<String>();

                Iterator reportItemItrt = sube.elementIterator();
                while (reportItemItrt.hasNext()) {

                    Element reportItemElement = (Element) reportItemItrt.next();
                    //      System.out.println(reportItemElement.getName());
                    subele.add(reportItemElement.getName());
                }

                if (subele.size() == 0 || (!subele.contains("cve")))
                    continue;

                Iterator itr = sube.elementIterator("cve");
                while (itr.hasNext()) {

                    System.out.println("host name is: " + host.attribute(0).getText());

                    fr.write(host.attribute(0).getText() + "\n");

                    Element cve = (Element) itr.next();

                    System.out.println(cve.getText());

                    fr.write(cve.getText() + "\n");

                    System.out.println("port number is: " + sube.attribute(0).getText());

                    fr.write(sube.attribute(0).getText() + "\n");

                    System.out.println("protocol is: " + sube.attribute(2).getText());

                    fr.write(sube.attribute(2).getText() + "\n");

                    System.out.println();

                    //   fr.write("\n");

                }

            }
        } // end of each entry's processing

        fr.close();

        // print out the stack trace for each exception(either documentation
        // exception or IO exception).
    } catch (DocumentException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:GetCVEID.java

License:Open Source License

public static String getHostname(String filename) {

    String hname = "";
    try {/*from   w w  w  .ja v  a  2s  . com*/

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(filename);

        Element hostname = (Element) document.selectSingleNode(
                "/*[local-name(.)='oval_results']/*[local-name(.)='results']/*[local-name(.)='system']/*[local-name(.)='oval_system_characteristics']/*[local-name(.)='system_info']/*[local-name(.)='primary_host_name']");

        String hname1 = hostname.getText();

        //   System.out.println(hname1);

        int in = hname1.indexOf('.'); //only keep the string before the first dot

        //if no dot, then ignore it
        if (in == -1)
            hname = hname1;
        //else take the machine domain name 
        else
            hname = hname1.substring(0, in);

        System.out.println("host name is: " + hname);
    }

    catch (Exception e) {

        e.printStackTrace();
    }

    return hname;

}

From source file:GetCVEID.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void getCVEs(String filename) {

    Hashtable ovalIDs = makehashTable(filename);

    String hname = getHostname(filename);

    try {/*from w  w w .j  a v a 2  s.c  o  m*/

        FileWriter fr = new FileWriter("CVE.txt");

        fr.write(hname + "\n");

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(new File(filename));

        Element definitions = (Element) document.selectSingleNode(
                "/*[local-name(.)='oval_results']/*[local-name(.)='oval_definitions']/*[local-name(.)='definitions']");

        List definition = document.selectNodes(
                "/*[local-name(.)='oval_results']/*[local-name(.)='oval_definitions']/*[local-name(.)='definitions']/*[local-name(.)='definition']");

        Iterator def = definition.iterator();

        while (def.hasNext()) {

            Element deft = (Element) def.next();

            //skip if the element is inventory

            String type = deft.attributeValue("class");

            String id = deft.attributeValue("id");

            if (type.contains("inventory"))

                continue;

            if (ovalIDs.containsKey(id)) {

                String cve = deft.element("metadata").element("reference").attributeValue("ref_id");

                fr.write(cve + "\n");

            }
        }

        fr.close();

    } catch (Exception e) {

        e.printStackTrace();
    }

}

From source file:GetCVEID.java

License:Open Source License

@SuppressWarnings("unchecked")
public static Hashtable<String, Boolean> makehashTable(String filename) {

    Hashtable<String, Boolean> hs = new Hashtable<String, Boolean>();

    try {/*from   w w  w.j  a  v a 2s.com*/
        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(new File(filename));

        List<String> ldid = document.selectNodes(
                "/*[local-name(.)='oval_results']/*[local-name(.)='results']/*[local-name(.)='system']/*[local-name(.)='definitions']/*[local-name(.)='definition']/@definition_id");

        Iterator itdid = ldid.iterator();

        List<String> rst = document.selectNodes(
                "/*[local-name(.)='oval_results']/*[local-name(.)='results']/*[local-name(.)='system']/*[local-name(.)='definitions']/*[local-name(.)='definition']/@result");

        Iterator result = rst.iterator();

        while (itdid.hasNext()) {

            Attribute defid = (Attribute) itdid.next();

            Attribute rs = (Attribute) result.next();

            if (rs.getText().contains("true")) {

                hs.put(defid.getText(), true);

            }

        }

        //   System.out.println(hs.size());

    } catch (Exception e) {

        e.printStackTrace();

    }

    return hs;

}

From source file:StreamFlusher.java

License:Apache License

private static Document parseXML(String str) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(str);
    return document;
}

From source file:StreamFlusher.java

License:Apache License

private Fst xml2fst(String filepath) throws Exception {

    // read an XML file representing a network, return the network

    final Fst fst = lib.EmptyLanguageFst();

    final HashSet<Integer> sigma = fst.getSigma();

    SAXReader reader = new SAXReader(); // SAXReader from dom4j

    // each SAXReader handler must define onStart() and onEnd() methods

    // when the kleeneFst element is first found
    reader.addHandler("/kleeneFst", new ElementHandler() {
        public void onStart(ElementPath path) {
            Element current = path.getCurrent();

            // semiring is an attribute on the kleeneFst node
            String semiring = current.attribute("semiring").getValue();
        }//from  ww w .  j ava  2  s .co  m

        public void onEnd(ElementPath path) {
        }
    });

    reader.addHandler("/kleeneFst/sigma", new ElementHandler() {
        public void onStart(ElementPath path) {
            if (path.getCurrent().attribute("containsOther").getValue().equals("true")) {
                fst.setContainsOther(true);
            } else {
                fst.setContainsOther(false);
            }
            ;
        }

        public void onEnd(ElementPath path) {
        }
    });

    reader.addHandler("/kleeneFst/sigma/sym", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element sym = path.getCurrent();
            sigma.add(symmap.putsym(sym.getText()));

            sym.detach();
        }
    });

    // when the arcs element is first found
    reader.addHandler("/kleeneFst/arcs", new ElementHandler() {
        public void onStart(ElementPath path) {
            // grab the two attrs and convert to int
            int startState = Integer.parseInt(path.getCurrent().attribute("start").getValue());
            int numStates = Integer.parseInt(path.getCurrent().attribute("numStates").getValue());
            lib.AddStates(fst, numStates);
            // native function, add this many  
            // states to the new Fst

            lib.SetStart(fst, startState); // set the start state
        }

        public void onEnd(ElementPath path) {
        }
    });

    // handle each whole arc element
    reader.addHandler("/kleeneFst/arcs/arc", new ElementHandler() {

        // in an ElementHandler, need to supply .onStart(),
        // called when the start tag is found, and
        // .onEnd(), which is called when the end tag is found.

        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {

            // retrieve the entire arc element
            Element arc = path.getCurrent();

            // these two are always present
            int src_id = Integer.parseInt(arc.attribute("s").getValue());
            int dest_id = Integer.parseInt(arc.attribute("d").getValue());

            // there will be either one io attr xor separate i and o attrs
            // (keep the io option to facilitate hand-written XML files)

            String input;
            String output;
            Attribute io = arc.attribute("io");
            if (io != null) {
                input = io.getValue();
                output = io.getValue();
            } else {
                input = arc.attribute("i").getValue();
                output = arc.attribute("o").getValue();
            }

            if (!symmap.containsKey(input)) {
                // symbol name in XML file not in the 
                //     current internal symtab
                symmap.putsym(input);
            }
            if (!symmap.containsKey(output)) {
                symmap.putsym(output);
            }

            // the w attr is optional in the arc elmt
            Attribute w = arc.attribute("w");
            if (w != null) {
                // call AddArc to add an arc to the fst 
                //      being built from the XML file description
                // semiring generalization point
                lib.AddArc(fst, src_id, symmap.getint(input), symmap.getint(output),
                        Float.parseFloat(w.getValue()), dest_id);

            } else {
                // semiring generalization point
                lib.AddArcNeutralWeight(fst, src_id, symmap.getint(input), symmap.getint(output), dest_id);
            }

            arc.detach();
        }
    });

    // for each full final element
    reader.addHandler("/kleeneFst/arcs/final", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element arc = path.getCurrent();

            // s attr is always present
            int src_id = Integer.parseInt(arc.attribute("s").getValue());

            // the w attr is optional
            Attribute w = arc.attribute("w");
            if (w != null) {
                lib.SetFinal(fst, src_id, Float.parseFloat(w.getValue()));
            } else {
                lib.SetFinalNeutralWeight(fst, src_id);
            }

            arc.detach();
        }
    });

    Document document = null;

    // the actual XML reading/parsing is done here
    try {
        // XmlReader detects the encoding of the XML document and
        // handles BOMs, including the UTF-8 BOMs that Java usually
        // chokes on
        document = reader.read(new XmlReader(new FileInputStream(filepath)));

        // Old, pre-XmlReader code
        //if (encoding.equals("UTF-8")) {
        //   // then need to work around SUN's irresponsible decision not to
        //   //  handle the optional UTF-8 BOM correctly
        //   document = reader.read(new InputStreamReader(
        //            new UTF8BOMStripperInputStream(new FileInputStream(filepath)),
        //            "UTF-8")
        //                 ) ;
        //} else {
        //   document = reader.read(new InputStreamReader(
        //                           new FileInputStream(filepath),
        //                           encoding)
        //                 ) ;
        //}
    } catch (DocumentException de) {
        // dom4j DocumentException extends Exception
        de.printStackTrace();
        throw de;
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
        throw fnfe;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

    correctSigmaOther(fst);

    return fst;
}

From source file:StreamFlusher.java

License:Apache License

private Document parseXMLPrefs(String filepath) throws Exception {
    SAXReader reader = new SAXReader();
    Document document = null;//from ww w . ja v a 2  s.  c  o m
    // Document document = reader.read(filepath) ;

    try {
        // the encoding should be UTF-8
        // then need to work around SUN's irresponsible decision not to
        //  handle the optional UTF-8 BOM correctly
        document = reader.read(
                new InputStreamReader(new UTF8BOMStripperInputStream(new FileInputStream(filepath)), "UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

    return document;
}