Example usage for javax.xml.parsers SAXParserFactory newSAXParser

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

Introduction

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

Prototype


public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;

Source Link

Document

Creates a new instance of a SAXParser using the currently configured factory parameters.

Usage

From source file:fr.eyal.lib.data.parser.GenericParser.java

public void parseSheet(final Object content, final int parseType) throws ParseException {

    //Sax method used for XML
    if (parseType == PARSE_TYPE_SAX) {

        //we convert the content to String
        String xml = new String((byte[]) content);
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        try {//from  w  ww .  ja  va  2s  . c  om
            final SAXParser sp = factory.newSAXParser();
            final XMLReader xr = sp.getXMLReader();

            final InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));

            //we set the SAX DefaultHandler
            xr.setContentHandler((DefaultHandler) mHandler);

            Out.v(TAG, "start parsing SAX");

            xr.parse(is);

            Out.v(TAG, "end parsing SAX");

        } catch (final Exception e) {
            e.printStackTrace();
            throw new ParseException("Parsing error");
        }

    } else if (parseType == PARSE_TYPE_JSON) {

        mHandler.parse(content);

    } else if (parseType == PARSE_TYPE_IMAGE) {

        mHandler.parse(content);

    }

}

From source file:com.joshdrummond.webpasswordsafe.android.GetCurrentPassword.java

private String parseResponse(String responseSOAP) {
    String response = "";
    try {//from   ww w .  j a  v a2 s. c  o  m
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        GetCurrentPasswordHandler handler = new GetCurrentPasswordHandler();
        xr.setContentHandler(handler);
        xr.parse(new InputSource(new StringReader(responseSOAP)));
        response = handler.getParsedData();
    } catch (Exception e) {
        response = "ERROR parsing: " + Arrays.deepToString(e.getStackTrace());
    }
    return response;
}

From source file:com.bluecloud.ioc.parse.KernelXMLParser.java

public CompositeMetadata parse() throws KernelXMLParserException {
    KernelXMLParserHandler parserHandler = new KernelXMLParserHandler();
    if (validatedXML != null) {
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        try {// w w w. ja v  a  2  s. co  m
            SAXParser parser = parserFactory.newSAXParser();
            for (URL url : validatedXML) {
                InputStream is = url.openStream();
                parser.parse(is, parserHandler);
                is.close();
            }
        } catch (Exception e) {
            throw new KernelXMLParserException(e);
        }
    }
    return parserHandler.getCompositeMetadata();
}

From source file:it.openyoureyes.test.OpenCellId.java

private String parseImage(HttpEntity entity, DefaultHandler dd) throws IOException {
    String result = "";
    String line;//from   w  w  w.  ja  v a2s .  c  om

    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(dd);

        /* Parse the xml-data from our URL. */
        xr.parse(new InputSource(entity.getContent()));

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        entity.consumeContent();
    }
    return result;
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.xml.XmlReaderText.java

@Override
public void getNext(CAS aCAS) throws IOException, CollectionException {
    Resource res = nextFile();/*  w  ww  . jav  a  2s .  com*/
    initCas(aCAS, res);

    InputStream is = null;

    try {
        JCas jcas = aCAS.getJCas();

        is = res.getInputStream();

        // Create handler
        Handler handler = newSaxHandler();
        handler.setJCas(jcas);
        handler.setLogger(getLogger());

        // Parser XML
        SAXParserFactory pf = SAXParserFactory.newInstance();
        SAXParser parser = pf.newSAXParser();

        InputSource source = new InputSource(is);
        source.setPublicId(res.getLocation());
        source.setSystemId(res.getLocation());
        parser.parse(source, handler);

        // Set up language
        if (getConfigParameterValue(PARAM_LANGUAGE) != null) {
            aCAS.setDocumentLanguage((String) getConfigParameterValue(PARAM_LANGUAGE));
        }
    } catch (CASException e) {
        throw new CollectionException(e);
    } catch (ParserConfigurationException e) {
        throw new CollectionException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    } finally {
        closeQuietly(is);
    }
}

From source file:io.lightlink.excel.StreamingExcelTransformer.java

public void doExport(InputStream template, OutputStream out, ExcelStreamVisitor visitor) throws IOException {
    try {//w  w w .jav a  2 s  .c  om
        ZipInputStream zipIn = new ZipInputStream(template);
        ZipOutputStream zipOut = new ZipOutputStream(out);

        ZipEntry entry;

        Map<String, byte[]> sheets = new HashMap<String, byte[]>();

        while ((entry = zipIn.getNextEntry()) != null) {

            String name = entry.getName();

            if (name.startsWith("xl/sharedStrings.xml")) {

                byte[] bytes = IOUtils.toByteArray(zipIn);
                zipOut.putNextEntry(new ZipEntry(name));
                zipOut.write(bytes);

                sharedStrings = processSharedStrings(bytes);

            } else if (name.startsWith("xl/worksheets/sheet")) {
                byte[] bytes = IOUtils.toByteArray(zipIn);
                sheets.put(name, bytes);
            } else if (name.equals("xl/calcChain.xml")) {
                // skip this file, let excel recreate it
            } else if (name.equals("xl/workbook.xml")) {
                zipOut.putNextEntry(new ZipEntry(name));

                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                Writer writer = new OutputStreamWriter(zipOut, "UTF-8");

                byte[] bytes = IOUtils.toByteArray(zipIn);
                saxParser.parse(new ByteArrayInputStream(bytes), new WorkbookTemplateHandler(writer));

                writer.flush();
            } else {
                zipOut.putNextEntry(new ZipEntry(name));
                IOUtils.copy(zipIn, zipOut);
            }

        }

        for (Map.Entry<String, byte[]> sheetEntry : sheets.entrySet()) {
            String name = sheetEntry.getKey();

            byte[] bytes = sheetEntry.getValue();
            zipOut.putNextEntry(new ZipEntry(name));
            processSheet(bytes, zipOut, visitor);
        }

        zipIn.close();
        template.close();

        zipOut.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:eu.scape_project.planning.xml.TreeLoader.java

private MindMap loadFreeMindMap(InputStream in) {
    try {/*from w  w  w.  jav a  2 s .co m*/
        MindMap map = new MindMap();
        SAXParserFactory f = SAXParserFactory.newInstance();
        SAXParser parser = f.newSAXParser();

        //            SAXParser parser = validatingParserFactory.getValidatingParser();
        //            parser.setProperty(ValidatingParserFactory.JAXP_SCHEMA_SOURCE,
        //                "http://freemind.sourceforge.net/freemind.xsd");
        // load content into temporary structure
        Digester digester = new Digester(parser);
        //            digester.setEntityResolver(new SchemaResolver().addSchemaLocation(
        //                "http://freemind.sourceforge.net/freemind.xsd", "data/schemas/freemind.xsd"));
        //            digester.setErrorHandler(new StrictErrorHandler());

        digester.push(map);

        digester.addObjectCreate("*/node", "eu.scape_project.planning.xml.freemind.Node");
        digester.addSetProperties("*/node");
        digester.addCallMethod("*/node/hook/text", "setDESCRIPTION", 0);
        digester.addSetNext("*/node", "addChild");

        digester.setUseContextClassLoader(true);
        digester.parse(in);
        return map;
    } catch (IOException e) {
        log.error("Error loading Freemind file.", e);
    } catch (SAXException e) {
        log.error("Document is not a valid Freemind file.", e);
    } catch (ParserConfigurationException e) {
        log.error("Parser not properly configured.", e);
    }
    return null;
}

From source file:com.tomdoel.mpg2dcm.EndoscopicFileProcessor.java

private EndoscopicXmlParser parseEndoscopicXmlFile(final File xmlFile)
        throws IOException, SAXException, ParserConfigurationException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    final SAXParser saxParser = factory.newSAXParser();
    final EndoscopicXmlParser parser = new EndoscopicXmlParser();
    saxParser.parse(xmlFile, parser);/*from ww  w .ja  va  2 s . c  om*/
    return parser;
}

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

public static String deserialize(String serialized, String key) {
    javax.xml.parsers.SAXParserFactory fac = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
    javax.xml.parsers.SAXParser saxParser;
    SaxHandler p = new SaxHandler();
    try {//from  w  w  w  .  j a  va 2s . co m
        saxParser = fac.newSAXParser();
        saxParser.parse(new ByteArrayInputStream(serialized.getBytes()), p);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return p.getValueByKey(key);
}

From source file:org.solmix.runtime.support.spring.TunedDocumentLoader.java

@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler,
        int validationMode, boolean namespaceAware) throws Exception {
    if (validationMode == XmlBeanDefinitionReader.VALIDATION_NONE) {
        SAXParserFactory parserFactory = namespaceAware ? nsasaxParserFactory : saxParserFactory;
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(entityResolver);
        reader.setErrorHandler(errorHandler);
        //            SAXSource saxSource = new SAXSource(reader, inputSource);
        //            W3CDOMStreamWriter writer = new W3CDOMStreaXMLStreamWriterImplmWriter();
        //            StaxUtils.copy(saxSource, writer);
        //            return writer.getDocument();
        return null;
    } else {//from  w  w  w . j av  a2s  .  co m
        return super.loadDocument(inputSource, entityResolver, errorHandler, validationMode, namespaceAware);
    }
}