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:com.larvalabs.svgandroid.SVGParser.java

private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode)
        throws SVGParseException {
    // Util.debug("Parsing SVG...");
    SVGHandler svgHandler = null;//from  w w w .j av  a 2  s .  c o  m
    try {
        // long start = System.currentTimeMillis();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        final Picture picture = new Picture();
        svgHandler = new SVGHandler(picture);
        svgHandler.setColorSwap(searchColor, replaceColor);
        svgHandler.setWhiteMode(whiteMode);

        CopyInputStream cin = new CopyInputStream(in);

        IDHandler idHandler = new IDHandler();
        xr.setContentHandler(idHandler);
        xr.parse(new InputSource(cin.getCopy()));
        svgHandler.idXml = idHandler.idXml;

        xr.setContentHandler(svgHandler);
        xr.parse(new InputSource(cin.getCopy()));
        // Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis.");
        SVG result = new SVG(picture, svgHandler.bounds);
        // Skip bounds if it was an empty pic
        if (!Float.isInfinite(svgHandler.limits.top)) {
            result.setLimits(svgHandler.limits);
        }
        return result;
    } catch (Exception e) {
        //for (String s : handler.parsed.toString().replace(">", ">\n").split("\n"))
        //   Log.d(TAG, "Parsed: " + s);
        throw new SVGParseException(e);
    }
}

From source file:com.myjeeva.poi.ExcelReader.java

/**
 * Parses the content of one sheet using the specified styles and shared-strings tables.
 * //from ww w.ja  v a  2s .c  o m
 * @param styles a {@link StylesTable} object
 * @param sharedStringsTable a {@link ReadOnlySharedStringsTable} object
 * @param sheetInputStream a {@link InputStream} object
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private void readSheet(StylesTable styles, ReadOnlySharedStringsTable sharedStringsTable,
        InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException {

    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader();

    ContentHandler handler = new XSSFSheetXMLHandler(styles, sharedStringsTable, sheetContentsHandler, true);

    sheetParser.setContentHandler(handler);
    sheetParser.parse(new InputSource(sheetInputStream));
}

From source file:com.vinexs.tool.XML.java

/**
 * Parse standard XML to element structure, allow access with css selector.
 *
 * @param inputStream InputStream point to a XML document.
 * @return JSONObject/*from  w w w .j  a  v a  2 s.c  o m*/
 */
public static Element toElement(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser parser = saxParserFactory.newSAXParser();
    parseHandler handler = new parseHandler();
    parser.parse(inputStream, handler);
    return handler.datas;
}

From source file:de.unisb.cs.st.javalanche.rhino.coverage.CoberturaParser.java

public static void parseXmlFile(File file, DefaultHandler handler, boolean validating) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);/*from  w  w  w.  ja v a 2s. com*/

    try {
        factory.newSAXParser().parse(file, handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:dreamboxdataservice.DreamboxDataService.java

/**
 * @param service Service-ID/*w  w w . j  a  v a 2  s . c o  m*/
 * @return Data of specific service
 */
public TreeMap<String, String> getServiceData(String service) {
    try {
        URL url = new URL("http://" + mProperties.getProperty("ip", "") + "/web/getservices?sRef=" + service);
        URLConnection connection = url.openConnection();

        String userpassword = mProperties.getProperty("username", "") + ":" + IOUtilities
                .xorEncode(mProperties.getProperty("password", ""), DreamboxSettingsPanel.PASSWORDSEED);
        String encoded = new String(Base64.encodeBase64(userpassword.getBytes()));
        connection.setRequestProperty("Authorization", "Basic " + encoded);

        connection.setConnectTimeout(10);
        InputStream stream = connection.getInputStream();

        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();

        DreamboxHandler handler = new DreamboxHandler();

        saxParser.parse(stream, handler);

        return handler.getData();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.connectsdk.service.RokuService.java

@Override
public void getAppList(final AppListListener listener) {
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override//from  w ww  .  j  a va 2  s.c om
        public void onSuccess(Object response) {
            String msg = (String) response;

            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            InputStream stream;
            try {
                stream = new ByteArrayInputStream(msg.getBytes("UTF-8"));
                SAXParser saxParser = saxParserFactory.newSAXParser();

                RokuApplicationListParser parser = new RokuApplicationListParser();
                saxParser.parse(stream, parser);

                List<AppInfo> appList = parser.getApplicationList();

                Util.postSuccess(listener, appList);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };

    String action = "query";
    String param = "apps";

    String uri = requestURL(action, param);

    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri,
            null, responseListener);
    request.setHttpMethod(ServiceCommand.TYPE_GET);
    request.send();
}

From source file:com.dgwave.osrs.OsrsClient.java

private void initJaxb() throws OsrsException {
    if (jc != null && oj != null)
        return;/*from  www . j  av a2s  .  c  om*/
    try {
        this.jc = JAXBContext.newInstance("com.dgwave.osrs.jaxb");
        this.oj = new ObjectFactory();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        xmlReader = spf.newSAXParser().getXMLReader();
        xmlReader.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                logger.debug("Ignoring DTD");
                return new InputSource(new StringReader(""));
            }
        });
    } catch (Exception e) {
        throw new OsrsException("JAXB Error", e);
    }
}

From source file:com.penbase.dma.Dalyo.HTTPConnection.DmaHttpClient.java

/**
 * Gets the resources of an application// w ww. j av a 2s . c o  m
 */
public void getResource(String urlRequest) {
    if (mSendResource) {
        StringBuffer getResources = new StringBuffer("act=getresources");
        getResources.append(urlRequest);
        byte[] bytes = sendPost(getResources.toString());
        SAXParserFactory spFactory = SAXParserFactory.newInstance();
        SAXParser saxParser;
        try {
            saxParser = spFactory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            EventsHandler eventsHandler = new EventsHandler(urlRequest);
            xmlReader.setContentHandler(eventsHandler);
            xmlReader.parse(new InputSource(new ByteArrayInputStream(bytes)));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Common.streamToFile(bytes, mResources_XML, false);
    } else {
        SAXParserFactory spFactory = SAXParserFactory.newInstance();
        SAXParser saxParser;
        try {
            saxParser = spFactory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            EventsHandler eventsHandler = new EventsHandler(urlRequest);
            xmlReader.setContentHandler(eventsHandler);
            xmlReader.parse(new InputSource(new FileInputStream(new File(mResources_XML))));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.scripps.fl.pubchem.web.entrez.EUtilsWebSession.java

public Collection<Long> getIds(String query, String db, Collection<Long> ids, int chunkSize) throws Exception {
    int retStart = 0;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    ESearchHandler handler = new ESearchHandler(ids);
    InputStream is = getInputStream("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", "db", db,
            "term", query, "retstart", retStart, "retmax", chunkSize, "usehistory", "y", "version", "2.0")
                    .call();/*  w ww.  j a  va  2s. c  o m*/
    while (true) {
        saxParser.parse(is, handler);
        if ((handler.retStart + handler.retMax) < (handler.count - 1)) {
            retStart += handler.retMax;
            is = getInputStream("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", "db", db, "term",
                    query, "retstart", retStart, "retmax", "" + chunkSize, "usehistory", "y", "QueryKey",
                    handler.queryKey, "WebEnv", handler.webEnv, "version", "2.0").call();
        } else
            break;
    }
    return ids;
}

From source file:com.sshtools.common.configuration.SshToolsConnectionProfile.java

/**
 *
 *
 * @param in/* w w  w. java2 s  .c o m*/
 *
 * @throws InvalidProfileFileException
 */
public void open(InputStream in) throws InvalidProfileFileException {
    try {
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxFactory.newSAXParser();
        XMLHandler handler = new XMLHandler();
        saxParser.parse(in, handler);
        handler = null;

        //            in.close();
    } catch (IOException ioe) {
        throw new InvalidProfileFileException("IO error. " + ioe.getMessage());
    } catch (SAXException sax) {
        throw new InvalidProfileFileException("SAX Error: " + sax.getMessage());
    } catch (ParserConfigurationException pce) {
        throw new InvalidProfileFileException("SAX Parser Error: " + pce.getMessage());
    } finally {
    }
}