Example usage for javax.xml.parsers SAXParserFactory newInstance

List of usage examples for javax.xml.parsers SAXParserFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory newInstance.

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

From source file:ca.uqac.info.tag.Counter.TagCounter.java

public static void main(final String[] args) {
    // Parse command line arguments
    Options options = setupOptions();// ww w.  j a va2  s  . co m
    CommandLine c_line = setupCommandLine(args, options);

    String redirectionFile = "";
    String tagAnalyse = "";
    String inputFile = "";

    if (c_line.hasOption("h")) {
        showUsage(options);
        System.exit(ERR_OK);
    }

    //Contains a redirection file for the output
    if (c_line.hasOption("redirection")) {
        try {
            redirectionFile = c_line.getOptionValue("redirection");
            PrintStream ps;
            ps = new PrintStream(redirectionFile);
            System.setOut(ps);
        } catch (FileNotFoundException e) {
            System.out.println("Redirection error !!!");
            e.printStackTrace();
        }
    }

    //Contains a tag
    if (c_line.hasOption("Tag")) {
        tagAnalyse = c_line.getOptionValue("t");
    } else {
        System.err.println("No Tag in Arguments");
        System.exit(ERR_ARGUMENTS);
    }

    //Contains a InputFile 
    if (c_line.hasOption("InputFile")) {
        inputFile = c_line.getOptionValue("i");
    } else {
        System.err.println("No Input File in Arguments");
        System.exit(ERR_ARGUMENTS);
    }

    //Start of the program
    System.out.println("-----------------------------------------------");
    System.out.println("The count of the Tag is start !!!");

    // Throw the Sax parsing for the file 
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser;
        parser = factory.newSAXParser();

        File Tagfile = new File(inputFile);
        DefaultHandler manager = new SaxTagHandlers(tagAnalyse);
        parser.parse(Tagfile, manager);
    } catch (ParserConfigurationException e) {
        System.out.println("Parser Configuration Exception for Sax !!!");
        e.printStackTrace();

    } catch (SAXException e) {
        System.out.println("Sax Exception during the parsing !!!");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Input/Ouput Exception during the Sax parsing !!!");
        e.printStackTrace();
    }
}

From source file:net.semanticmetadata.lire.solr.SearchImages.java

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    // http://localhost:8080/solr/lire/query?q=hashes%3A1152++hashes%3A605++hashes%3A96++hashes%3A275++&wt=xml
    String hashes = "1152  605  96  275  2057  3579  3950  2831  2367  3169  3292  974  2465  1573  2933  3125  314  2158  3532  974  2198  2315  3013  3302  3316  1467  2213  818  3  1083  18  2604  327  1370  593  3677  464  79  256  984  2496  1124  855  2091  780  1941  1887  1145  1396  4016  2406  2227  1532  2598  215  1375  171  2516  1698  368  2350  3799  223  1471  2083  1051  3015  3789  3374  1442  3991  3575  1452  751  428  3103  1182  2241  474  275  3678  3970  559  3394  2662  2361  2048  1083  181  1483  3903  3331  2363  756  558  2838  3984  1878  2667  3333  1473  2136  3499  3873  1437  3091  1287  948  46  3660  3003  1572  1185  2231  2622  257  3538  3632  3989  1180  3928  3144  1492  3941  3253  3498  2721  1036  22  1020  725  1431  3821  2248  2542  3659  2849  524  2967  1  2493  3620  2951  3584  1641  3873  2087  1506  1489  3064";
    String[] split = hashes.split(" ");
    String query = "";
    for (int i = 0; i < split.length; i++) {
        String s = split[i];// w  w  w. jav  a2 s. com
        if (s.trim().length() > 0)
            query += " cl_ha:" + s.trim();
    }
    URL u = new URL(baseURL + "?q=" + URLEncoder.encode(query, "utf-8") + "&wt=xml&rows=500");
    InputStream in = u.openStream();
    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    SolrResponseHandler dh = new SolrResponseHandler();
    saxParser.parse(in, dh);
    ArrayList<ResultItem> results = dh.getResults();
    // re-rank:

}

From source file:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;//from ww w .jav  a  2 s .com
    try {
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyContentHandler());

    try {
        InputSource is = new InputSource("test.xml");
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;/* ww w  .ja  v a  2 s  .c om*/
    try {
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyContentHandler());

    try {
        InputSource is = new InputSource(new StringReader(getXMLData()));
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:Main.java

public static SAXParserFactory newSAXParserFactory() {
    return SAXParserFactory.newInstance();
}

From source file:Main.java

public static SAXParserFactory getSAXFactory() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);/*  w  w  w.j  a v a 2s . co  m*/

    return factory;
}

From source file:Main.java

public static SAXParser getSAXParser() throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    return saxParser;
}

From source file:Main.java

public static SAXParser createSAXParser() throws SAXException, ParserConfigurationException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    return factory.newSAXParser();
}

From source file:Main.java

public static synchronized SAXParserFactory getSAXFactory() {
    if (SAX_FACTORY == null) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        SAX_FACTORY = factory;/*from   w w w  .  j  av a  2 s  . c  o m*/
    }
    return SAX_FACTORY;
}

From source file:Main.java

public static SAXParser getParser() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//from  ww w.ja v a2 s. c om
    factory.setValidating(false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return factory.newSAXParser();
}