Search in sources :

Example 46 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project newsrob by marianokamp.

the class Feed method restoreFeedsIfNeccesary.

public static final boolean restoreFeedsIfNeccesary(Context context) {
    // initial startup?
    final EntryManager em = EntryManager.getInstance(context);
    if (em.getFeedCount() != 0)
        return false;
    SdCardStorageAdapter storageAdapter = new SdCardStorageAdapter(context.getApplicationContext(), false);
    final String fileName = storageAdapter.getAbsolutePathForAsset(FEED_SETTINGS_FILE_NAME);
    if (!new File(fileName).exists()) {
        Log.w("Feed", "No " + fileName + " existing. Not trying to restore feeds.");
        return false;
    }
    Log.i("Feed", "Trying to restore feeds.");
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            @Override
            public final void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                if (!"feed".equals(localName))
                    return;
                Feed f = new Feed();
                f.setAtomId(URLDecoder.decode(attributes.getValue("atomId")));
                f.setTitle(URLDecoder.decode(attributes.getValue("title")));
                f.setDownloadPref(Integer.parseInt(attributes.getValue("downloadPref")));
                f.setDisplayPref(Integer.parseInt(attributes.getValue("displayPref")));
                f.setWebScale(Float.parseFloat(attributes.getValue("webScale")));
                f.setFeedScale(Float.parseFloat(attributes.getValue("feedScale")));
                f.setJavaScriptEnabled(Boolean.parseBoolean(attributes.getValue("javaScriptEnabled")));
                try {
                    f.setFitToWidthEnabled(Boolean.parseBoolean(attributes.getValue("fitToWidthEnabled")));
                } catch (RuntimeException rte) {
                // skip as it may be missing. Default is true then.
                }
                f.setNotificationEnabled(Boolean.parseBoolean(attributes.getValue("notificationEnabled")));
                f.setAlternateUrl(attributes.getValue("altUrl"));
                em.insert(f);
            }

            @Override
            public void receivedString(String localTagName, String fullyQualifiedLocalName, String value) {
            }
        };
        parser.parse(new File(fileName), handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Log.i("Feed", "Restored feeds. Now " + em.getFeedCount() + " feeds in database.");
    return true;
}
Also used : SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 47 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project adempiere by adempiere.

the class PackIn method importXML.

// prepare
/**
	 * Uses PackInHandler to update AD.
	 * 
	 * @param fileName
	 *            xml file to read
	 * @return status message
	 */
public String importXML(String fileName, Properties ctx, String trxName) throws Exception {
    log.info("importXML:" + fileName);
    File in = new File(fileName);
    if (!in.exists()) {
        String msg = "File does not exist: " + fileName;
        log.info("importXML:" + msg);
        return msg;
    }
    try {
        log.info("starting");
        System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
        PackInHandler handler = new PackInHandler();
        handler.set_TrxName(trxName);
        handler.setCtx(ctx);
        handler.setProcess(this);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        String msg = "Start Parser";
        log.info(msg);
        parser.parse(in, handler);
        msg = "End Parser";
        log.info(msg);
        return "OK.";
    } catch (Exception e) {
        log.log(Level.SEVERE, "importXML:", e);
        throw e;
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 48 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project adempiere by adempiere.

the class OFXBankStatementHandler method init.

/**
	 * 	Initialize the loader
	 * 	 * @param controller Reference to the BankStatementLoaderController
	@return Initialized succesfully
	 */
protected boolean init(MBankStatementLoader controller) {
    boolean result = false;
    if (controller == null) {
        m_errorMessage = "ErrorInitializingParser";
        m_errorDescription = "ImportController is a null reference";
        return result;
    }
    this.m_controller = controller;
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        m_parser = factory.newSAXParser();
        result = true;
    } catch (ParserConfigurationException e) {
        m_errorMessage = "ErrorInitializingParser";
        m_errorDescription = "Unable to configure SAX parser: " + e.getMessage();
    } catch (SAXException e) {
        m_errorMessage = "ErrorInitializingParser";
        m_errorDescription = "Unable to initialize SAX parser: " + e.getMessage();
    }
    return result;
}
Also used : ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 49 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project aries by apache.

the class AbstractModelBuilder method parse.

private BeansModel parse(List<URL> osgiBeansDescriptorURLs, List<URL> beanDescriptorURLs) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    if (osgiBeansDescriptorURLs.isEmpty()) {
        throw new IllegalArgumentException("Missing osgi-beans descriptors");
    }
    SAXParser parser;
    try {
        parser = factory.newSAXParser();
    } catch (ParserConfigurationException | SAXException e) {
        return Throw.exception(e);
    }
    OSGiBeansHandler handler = getHandler(beanDescriptorURLs);
    for (URL osgiBeansDescriptorURL : osgiBeansDescriptorURLs) {
        try (InputStream inputStream = osgiBeansDescriptorURL.openStream()) {
            InputSource source = new InputSource(inputStream);
            if (source.getByteStream().available() == 0) {
                throw new IllegalArgumentException("Specified osgi-beans descriptor is empty: " + osgiBeansDescriptorURL);
            }
            try {
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", loadXsds());
            } catch (IllegalArgumentException | SAXNotRecognizedException | SAXNotSupportedException e) {
            // No op, we just don't validate the XML
            }
            parser.parse(source, handler);
        } catch (IOException | SAXException e) {
            return Throw.exception(e);
        }
    }
    return handler.createBeansModel();
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 50 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project asterixdb by apache.

the class TestSuiteParser method parse.

public org.apache.asterix.testframework.xml.TestSuite parse(File testSuiteCatalog) throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setXIncludeAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file");
    JAXBContext ctx = JAXBContext.newInstance(org.apache.asterix.testframework.xml.TestSuite.class);
    Unmarshaller um = ctx.createUnmarshaller();
    return (org.apache.asterix.testframework.xml.TestSuite) um.unmarshal(new SAXSource(saxParser.getXMLReader(), new InputSource(testSuiteCatalog.toURI().toString())));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9