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;
}
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;
}
}
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;
}
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();
}
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())));
}
Aggregations