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:eu.faircode.netguard.ActivitySettings.java

private void xmlImport(InputStream in) throws IOException, SAXException, ParserConfigurationException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);
    prefs.edit().putBoolean("enabled", false).apply();
    ServiceSinkhole.stop("import", this, false);

    XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    XmlImportHandler handler = new XmlImportHandler(this);
    reader.setContentHandler(handler);/*from   ww  w.  j av a2s. c om*/
    reader.parse(new InputSource(in));

    xmlImport(handler.application, prefs);
    xmlImport(handler.wifi, getSharedPreferences("wifi", Context.MODE_PRIVATE));
    xmlImport(handler.mobile, getSharedPreferences("other", Context.MODE_PRIVATE));
    xmlImport(handler.screen_wifi, getSharedPreferences("screen_wifi", Context.MODE_PRIVATE));
    xmlImport(handler.screen_other, getSharedPreferences("screen_other", Context.MODE_PRIVATE));
    xmlImport(handler.roaming, getSharedPreferences("roaming", Context.MODE_PRIVATE));
    xmlImport(handler.lockdown, getSharedPreferences("lockdown", Context.MODE_PRIVATE));
    xmlImport(handler.apply, getSharedPreferences("apply", Context.MODE_PRIVATE));
    xmlImport(handler.notify, getSharedPreferences("notify", Context.MODE_PRIVATE));

    // Upgrade imported settings
    ReceiverAutostart.upgrade(true, this);

    DatabaseHelper.clearCache();

    // Refresh UI
    prefs.edit().putBoolean("imported", true).apply();
    prefs.registerOnSharedPreferenceChangeListener(this);
}

From source file:gov.whitehouse.services.LiveService.java

private boolean updateEvents() {
    try {/*from ww w  .  j  a v  a 2s .  c o  m*/
        HttpURLConnection conn = (HttpURLConnection) new URL(mFeedUrl).openConnection();
        InputStream in;
        int status;

        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestProperty("User-Agent", getString(R.string.user_agent_string));

        in = conn.getInputStream();
        status = conn.getResponseCode();

        if (status < 400 && status != 304) {
            /* We should be good to go */
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            FeedHandler handler = new FeedHandler();
            parser.parse(in, handler);

            /*
             * Cycle through the received events and make sure they're all valid.
             */
            ArrayList<FeedItem> validLiveEvents = new ArrayList<FeedItem>();
            for (FeedItem item : handler.getFeedItems()) {
                if (item != null && item.getPubDate() != null) {
                    validLiveEvents.add(item);
                }
            }

            mLiveEvents = validLiveEvents;
        }

        conn.disconnect();

        return status < 400;
    } catch (SAXException e) {
        Log.d(TAG, "failed to parse XML");
        Log.d(TAG, Log.getStackTraceString(e));
    } catch (IOException e) {
        Log.d(TAG, "error reading feed");
        Log.d(TAG, Log.getStackTraceString(e));
    } catch (IllegalStateException e) {
        Log.d(TAG, "this should not happen");
        Log.d(TAG, Log.getStackTraceString(e));
    } catch (ParserConfigurationException e) {
        Log.d(TAG, "this should not happen");
        Log.d(TAG, Log.getStackTraceString(e));
    }

    return false;
}

From source file:net.sourceforge.pmd.AbstractRuleSetFactoryTest.java

/**
 * Setups the XML parser with validation.
 * //from w w w . j  av  a  2s  .c o  m
 * @throws Exception
 *             any error
 */
@BeforeClass
public static void init() throws Exception {
    saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);

    // Hope we're using Xerces, or this may not work!
    // Note: Features are listed here
    // http://xerces.apache.org/xerces2-j/features.html
    saxParserFactory.setFeature("http://xml.org/sax/features/validation", true);
    saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
    saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

    validateDefaultHandler = new ValidateDefaultHandler();

    saxParser = saxParserFactory.newSAXParser();
}

From source file:net.sourceforge.seqware.common.util.workflowtools.WorkflowTools.java

private HashMap<String, HashMap<String, String>> parseLog(File failedJobLogFile) {

    int tries = 20;

    while (tries >= 0) {

        tries--;/*from   w w w .  jav a  2 s . c om*/

        if (failedJobLogFile != null && failedJobLogFile.exists() && failedJobLogFile.canRead()) {
            try {
                if (failedJobLogFile.length() <= 0) {
                    Logger.getLogger(WorkflowTools.class.getName()).log(Level.SEVERE,
                            "empty file: " + failedJobLogFile.getAbsolutePath());
                    return new HashMap<String, HashMap<String, String>>();
                }
                Log.info("  + Parsing file: " + failedJobLogFile.getAbsolutePath());

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

                // inner class to handle parsing
                LogDefaultHandler handler = new LogDefaultHandler();

                saxParser.parse(failedJobLogFile.getAbsoluteFile(), handler);

                return (handler.getData());

            } catch (IOException ex) {
                ex.printStackTrace();
                Logger.getLogger(WorkflowTools.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                ex.printStackTrace();
                Logger.getLogger(WorkflowTools.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                ex.printStackTrace();
                Logger.getLogger(WorkflowTools.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        // sleep
        try {
            Thread.sleep(6000);
        } catch (InterruptedException ex) {
            Log.error("Threw interrupt exception during parseLog: " + ex.getMessage());
        }

    }

    return (null);

}

From source file:net.stuxcrystal.simpledev.configuration.parser.generators.xml.XMLParser.java

/**
 * Creates a new SAX-Parser./*from w w w.j av  a2 s . c om*/
 * @return The new sax parser.
 * @throws ParserConfigurationException If we failed to create anew SAX-Parser.
 * @throws SAXException                 If we failed to create anew SAX-Parser.
 */
private static SAXParser getParser() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    return factory.newSAXParser();
}

From source file:net.timewalker.ffmq4.utils.xml.XMLDescriptorReader.java

/**
 * Read and parse an XML descriptor file
 *///from w  ww.  j  ava 2  s .  co  m
public AbstractDescriptor read(File descriptorFile, Class<? extends AbstractXMLDescriptorHandler> handlerClass)
        throws JMSException {
    if (!descriptorFile.canRead())
        throw new FFMQException("Can't read descriptor file : " + descriptorFile.getAbsolutePath(), "FS_ERROR");

    log.debug("Parsing descriptor : " + descriptorFile.getAbsolutePath());

    AbstractXMLDescriptorHandler handler;
    try {
        // Create an handler instance
        handler = handlerClass.newInstance();

        // Parse the descriptor file
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        FileInputStream in = new FileInputStream(descriptorFile);
        parser.parse(in, handler);
        in.close();
    } catch (Exception e) {
        throw new FFMQException("Cannot parse descriptor file : " + descriptorFile.getAbsolutePath(),
                "PARSE_ERROR", e);
    }

    AbstractDescriptor descriptor = handler.getDescriptor();
    descriptor.setDescriptorFile(descriptorFile);

    return descriptor;
}

From source file:net.unicon.warlock.fac.AbstractWarlockFactory.java

private static XMLReader getXMLReader() throws SAXException, javax.xml.parsers.ParserConfigurationException {
    XMLReader rslt = null;/* w  w  w .  j a v a  2 s.  c om*/

    synchronized (saxParserLock) {
        if (saxParserFactory == null) {
            saxParserFactory = SAXParserFactory.newInstance();
            saxParserFactory.setValidating(false);
            saxParserFactory.setNamespaceAware(false);
        }

        rslt = saxParserFactory.newSAXParser().getXMLReader();
    }

    return rslt;
}

From source file:net.yacy.cora.document.feed.RSSReader.java

private static SAXParser getParser() throws SAXException {
    SAXParser parser = tlSax.get();
    if (parser == null) {
        try {//from   w  ww .j  av  a 2  s.  c o  m
            parser = SAXParserFactory.newInstance().newSAXParser();
        } catch (final ParserConfigurationException e) {
            throw new SAXException(e.getMessage(), e);
        }
        tlSax.set(parser);
    }
    return parser;
}

From source file:net.yacy.document.parser.GenericXMLParser.java

/**
 * @return a SAXParser instance for the current thread
 * @throws SAXException when an error prevented parser creation
 *///from w ww . ja v  a2s.c om
private static SAXParser getParser() throws SAXException {
    SAXParser parser = tlSax.get();
    if (parser == null) {
        try {
            parser = SAXParserFactory.newInstance().newSAXParser();
        } catch (final ParserConfigurationException e) {
            throw new SAXException(e.getMessage(), e);
        }
        tlSax.set(parser);
    }
    return parser;
}

From source file:nl.b3p.ogc.utils.OgcWfsClient.java

public static AnyNode xmlStringToAnyNode(String xml) throws Exception {
    AnyNode anyNode = null;/*from  w  ww.  ja  v a2  s.  c  om*/
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XMLReader reader = saxParser.getXMLReader();

        org.exolab.castor.xml.util.SAX2ANY handler = new org.exolab.castor.xml.util.SAX2ANY();

        IgnoreEntityResolver r = new IgnoreEntityResolver();
        reader.setEntityResolver(r);

        reader.setContentHandler(handler);
        reader.setErrorHandler(handler);
        InputSource source = new InputSource(new StringReader(xml));
        reader.parse(source);

        anyNode = handler.getStartingNode();
    } catch (Exception e) {
        log.error("error", e);
    }
    return anyNode;

}