List of usage examples for javax.xml.parsers SAXParserFactory newInstance
public static SAXParserFactory newInstance()
From source file:net.sourceforge.fenixedu.utilTests.ParseMetadata.java
public Vector<Element> parseMetadata(String metadataFile) throws ParseException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(true);//from w w w .java 2 s . c o m try { SAXParser saxParser = spf.newSAXParser(); XMLReader reader = saxParser.getXMLReader(); reader.setContentHandler(this); reader.setErrorHandler(this); StringReader sr = new StringReader(metadataFile); InputSource input = new InputSource(sr); MetadataResolver resolver = new MetadataResolver(); reader.setEntityResolver(resolver); reader.parse(input); } catch (Exception e) { throw new ParseException(); } setMembers(vector); return vector; }
From source file:com.rany.albeg.wein.rssr.RssReader.java
private void init(RssReaderListener readCompleteListener) { mReadyToRead = true;//from ww w . ja v a 2s . c o m mRssReaderListener = readCompleteListener; mRssReaderXmlHandler = new RssReaderXMLHandler(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); mXMLReader = parser.getXMLReader(); mXMLReader.setContentHandler(mRssReaderXmlHandler); } catch (ParserConfigurationException e) { Log.e(TAG, "ParserConfigurationException"); } catch (SAXException e) { Log.e(TAG, "SAXException init()"); } }
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 {// w ww .ja v a 2s .co m 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.qspin.qtaste.recorder.SpyInstaller.java
public SpyInstaller(String pXmlFilterDefinitionPath) { super();/* w ww . j av a 2s .co m*/ mFilter = new ArrayList<RecorderFilter>(); if (pXmlFilterDefinitionPath != null) { FilterXmlHandler gestionnaire = new FilterXmlHandler(); try { SAXParserFactory fabrique = SAXParserFactory.newInstance(); SAXParser parseur = fabrique.newSAXParser(); parseur.parse(pXmlFilterDefinitionPath, gestionnaire); for (Filter f : gestionnaire.getDecodedFilters()) { mFilter.add(new RecorderFilter(f)); } } catch (IOException pExc) { LOGGER.error(pExc); } catch (SAXException pExc) { LOGGER.error(pExc); } catch (ParserConfigurationException pExc) { LOGGER.error(pExc); } } }
From source file:ValidateXMLInput.java
void validate() throws Exception { // Since we're going to use a SAX feature, the transformer must support // input in the form of a SAXSource. TransformerFactory tfactory = TransformerFactory.newInstance(); if (tfactory.getFeature(SAXSource.FEATURE)) { // Standard way of creating an XMLReader in JAXP 1.1. SAXParserFactory pfactory = SAXParserFactory.newInstance(); pfactory.setNamespaceAware(true); // Very important! // Turn on validation. pfactory.setValidating(true);/*from w ww. j a v a 2 s .com*/ // Get an XMLReader. XMLReader reader = pfactory.newSAXParser().getXMLReader(); // Instantiate an error handler (see the Handler inner class below) that will report any // errors or warnings that occur as the XMLReader is parsing the XML input. Handler handler = new Handler(); reader.setErrorHandler(handler); // Standard way of creating a transformer from a URL. Transformer t = tfactory.newTransformer(new StreamSource("birds.xsl")); // Specify a SAXSource that takes both an XMLReader and a URL. SAXSource source = new SAXSource(reader, new InputSource("birds.xml")); // Transform to a file. try { t.transform(source, new StreamResult("birds.out")); } catch (TransformerException te) { // The TransformerException wraps someting other than a SAXParseException // warning or error, either of which should be "caught" by the Handler. System.out.println("Not a SAXParseException warning or error: " + te.getMessage()); } System.out.println("=====Done====="); } else System.out.println("tfactory does not support SAX features!"); }
From source file:eu.annocultor.utils.XmlUtils.java
/** * //from www . ja va 2s .com * @param fileStream * @param handler * @param validating * @return <code>true</code> on success. * @throws Exception */ public static int parseXmlFileSAX(File sourceFile, ConverterHandler handler, boolean validating) throws Exception { // Create a builder factory SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validating); factory.setNamespaceAware(true); InputStream fileStream = new BufferedInputStream(new FileInputStream(sourceFile), 1024 * 1024); if (fileStream == null) { throw new Exception("Null input XML file stream"); } if (handler == null) { throw new Exception("Null XML handler"); } try { // Create the builder and parse the file SAXParser newSAXParser = factory.newSAXParser(); if (newSAXParser == null) { throw new Exception("null SAX parser"); } newSAXParser.parse(fileStream, handler); } catch (Exception e) { System.err.println("\n" + "*****************************************************\n" + "EXCEPTION OCCURRED in file " + sourceFile.getCanonicalPath() + "\n" + "at line " + handler.getDocumentLocator().getLineNumber() + ", column " + handler.getDocumentLocator().getColumnNumber()); e.printStackTrace(); System.err.println("\n" + "TRYING TO CLOSE FILES GRACEFULLY \n" + "*****************************************************\n"); return -1; } return 0; }
From source file:eu.scape_project.planning.xml.TreeLoader.java
private MindMap loadFreeMindMap(InputStream in) { try {/*w w w.ja v a 2 s . c o 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:org.energyos.espi.datacustodian.integration.utils.ATOMContentHandlerTests.java
@Test @Ignore//from w ww.j a v a2s. c o m public void processEnty() throws Exception { JAXBContext context = marshaller.getJaxbContext(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); XMLReader reader = factory.newSAXParser().getXMLReader(); // EntryProcessorServiceImpl procssor = // mock(EntryProcessorServiceImpl.class); ATOMContentHandler atomContentHandler = new ATOMContentHandler(context, entryProcessorService); reader.setContentHandler(atomContentHandler); reader.parse(new InputSource(FixtureFactory.newUsagePointInputStream(UUID.randomUUID()))); // verify(procssor).process(any(EntryType.class)); }
From source file:com.opengamma.financial.convention.calendar.XMLCalendarLoader.java
/** * Populate the specified working day calendar from the XML file. * @param calendar the calendar to populate, not null *//*from w w w. ja v a2 s . c om*/ public void populateCalendar(final ExceptionCalendar calendar) { final SAXParserFactory factory = SAXParserFactory.newInstance(); try { final SAXParser parser = factory.newSAXParser(); parser.parse(getSourceDataURI(), new DefaultHandler() { private ParserState _state = ParserState.OTHER; private String _innerText; @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) { switch (_state) { case OTHER: if (qName.equalsIgnoreCase(TAG_WORKING_DAYS)) { _state = ParserState.WORKING_DAYS; } else if (qName.equalsIgnoreCase(TAG_NON_WORKING_DAYS)) { _state = ParserState.NON_WORKING_DAYS; } break; } } @Override public void characters(final char[] ch, final int start, final int length) { _innerText = new String(ch, start, length); } @Override public void endElement(final String uri, final String localName, final String qName) { switch (_state) { case WORKING_DAYS: if (qName.equalsIgnoreCase(TAG_DATE)) { calendar.addWorkingDay(LocalDate.parse(_innerText)); } else if (qName.equalsIgnoreCase(TAG_WORKING_DAYS)) { _state = ParserState.OTHER; } break; case NON_WORKING_DAYS: if (qName.equalsIgnoreCase(TAG_DATE)) { calendar.addNonWorkingDay(LocalDate.parse(_innerText)); } else if (qName.equalsIgnoreCase(TAG_NON_WORKING_DAYS)) { _state = ParserState.OTHER; } break; } } }); } catch (final ParserConfigurationException ex) { throw wrap(ex); } catch (final SAXException ex) { throw wrap(ex); } catch (final IOException ex) { throw wrap(ex); } }
From source file:gov.nasa.ensemble.core.jscience.xml.XMLProfileParser.java
@Override public int numberParsable(URI uri, InputStream inputStream) { BufferedInputStream bis = null; XMLProfileHandler xmlProfileHandler = new XMLProfileHandler(); try {/* w w w . j ava2 s . co m*/ bis = new BufferedInputStream(inputStream); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); xmlProfileHandler.setTestOnly(true); parser.parse(inputStream, xmlProfileHandler); } catch (Exception ex) { return ProfilesParser.UNPARSABLE; } finally { IOUtils.closeQuietly(bis); } return xmlProfileHandler.getProfileCount(); }