javax.xml.parsers.SAXParserFactory Java Examples
The following examples show how to use
javax.xml.parsers.SAXParserFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlUtil.java From openjdk-jdk9 with GNU General Public License v2.0 | 9 votes |
public static SAXParserFactory newSAXParserFactory(boolean disableSecurity) { SAXParserFactory factory = SAXParserFactory.newInstance(); String featureToSet = XMLConstants.FEATURE_SECURE_PROCESSING; try { boolean securityOn = !xmlSecurityDisabled(disableSecurity); factory.setFeature(featureToSet, securityOn); factory.setNamespaceAware(true); if (securityOn) { featureToSet = DISALLOW_DOCTYPE_DECL; factory.setFeature(featureToSet, true); featureToSet = EXTERNAL_GE; factory.setFeature(featureToSet, false); featureToSet = EXTERNAL_PE; factory.setFeature(featureToSet, false); featureToSet = LOAD_EXTERNAL_DTD; factory.setFeature(featureToSet, false); } } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support "+featureToSet+" feature!", new Object[]{factory.getClass().getName()}); } return factory; }
Example #2
Source File: StartupListener.java From portals-pluto with Apache License 2.0 | 6 votes |
@Override public void contextInitialized(ServletContextEvent servletContextEvent) { servletContext = servletContextEvent.getServletContext(); ResourceReader resourceReader = new ResourceReader(servletContext); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); boolean validating = false; saxParserFactory.setValidating(validating); saxParserFactory.setNamespaceAware(true); SAXParser saxParser; try { saxParser = saxParserFactory.newSAXParser(); boolean resolveEntities = false; boolean scanWebFragments = true; WebConfigScanner webConfigScanner = new WebConfigScanner(getClass().getClassLoader(), resourceReader, saxParser, resolveEntities, scanWebFragments); WebConfig webConfig = webConfigScanner.scan(); configuredContextParams = webConfig.getConfiguredContextParams(); displayName = webConfig.getDisplayName(); } catch (Exception e) { e.printStackTrace(); } }
Example #3
Source File: IntentReceiver.java From aptoide-client with GNU General Public License v2.0 | 6 votes |
private void parseXmlString(String file) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); XmlAppHandler handler = new XmlAppHandler(); xr.setContentHandler(handler); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(file)); xr.parse(is); server = handler.getServers(); app = handler.getApp(); } catch (IOException | SAXException | ParserConfigurationException e) { Logger.printException(e); } }
Example #4
Source File: SVGParser.java From UltimateAndroid with Apache License 2.0 | 6 votes |
private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode, int targetWidth, int targetHeight) throws SVGParseException { // Util.debug("Parsing SVG..."); try { long start = System.currentTimeMillis(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); final Picture picture = new Picture(); SVGHandler handler = new SVGHandler(picture, targetWidth, targetHeight); handler.setColorSwap(searchColor, replaceColor); handler.setWhiteMode(whiteMode); xr.setContentHandler(handler); xr.parse(new InputSource(in)); // Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis."); SVG result = new SVG(picture, handler.bounds); // Skip bounds if it was an empty pic if (!Float.isInfinite(handler.limits.top)) { result.setLimits(handler.limits); } return result; } catch (Exception e) { throw new SVGParseException(e); } }
Example #5
Source File: EsoReader.java From EventCoreference with Apache License 2.0 | 6 votes |
public void parseFile(String filePath) { String myerror = ""; try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); SAXParser parser = factory.newSAXParser(); InputSource inp = new InputSource (new FileReader(filePath)); parser.parse(inp, this); } catch (SAXParseException err) { myerror = "\n** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId(); myerror += "\n" + err.getMessage(); System.out.println("myerror = " + myerror); } catch (SAXException e) { Exception x = e; if (e.getException() != null) x = e.getException(); myerror += "\nSAXException --" + x.getMessage(); System.out.println("myerror = " + myerror); } catch (Exception eee) { eee.printStackTrace(); myerror += "\nException --" + eee.getMessage(); System.out.println("myerror = " + myerror); } }
Example #6
Source File: XmlUtil.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public static SAXParserFactory newSAXParserFactory(boolean disableSecurity) { SAXParserFactory factory = SAXParserFactory.newInstance(); String featureToSet = XMLConstants.FEATURE_SECURE_PROCESSING; try { boolean securityOn = !isXMLSecurityDisabled(disableSecurity); factory.setFeature(featureToSet, securityOn); factory.setNamespaceAware(true); if (securityOn) { featureToSet = DISALLOW_DOCTYPE_DECL; factory.setFeature(featureToSet, true); featureToSet = EXTERNAL_GE; factory.setFeature(featureToSet, false); featureToSet = EXTERNAL_PE; factory.setFeature(featureToSet, false); featureToSet = LOAD_EXTERNAL_DTD; factory.setFeature(featureToSet, false); } } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support "+featureToSet+" feature!", new Object[]{factory.getClass().getName()}); } return factory; }
Example #7
Source File: AuctionItemRepository.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Testing set MaxOccursLimit to 10000 in the secure processing enabled for * SAXParserFactory. * * @throws Exception If any errors occur. */ @Test public void testMaxOccurLimitPos() throws Exception { String schema_file = XML_DIR + "toys.xsd"; String xml_file = XML_DIR + "toys.xml"; SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setFeature(FEATURE_SECURE_PROCESSING, true); setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000)); SAXParser parser = factory.newSAXParser(); parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file)); try (InputStream is = new FileInputStream(xml_file)) { MyErrorHandler eh = new MyErrorHandler(); parser.parse(is, eh); assertFalse(eh.isAnyError()); } }
Example #8
Source File: EHFatalTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Error Handler to capture all error events to output file. Verifies the * output file is same as golden file. * * @throws Exception If any errors occur. */ @Test public void testEHFatal() throws Exception { String outputFile = USER_DIR + "EHFatal.out"; String goldFile = GOLDEN_DIR + "EHFatalGF.out"; String xmlFile = XML_DIR + "invalid.xml"; try(MyErrorHandler eHandler = new MyErrorHandler(outputFile); FileInputStream instream = new FileInputStream(xmlFile)) { SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setErrorHandler(eHandler); InputSource is = new InputSource(instream); xmlReader.parse(is); fail("Parse should throw SAXException"); } catch (SAXException expected) { // This is expected. } // Need close the output file before we compare it with golden file. assertTrue(compareWithGold(goldFile, outputFile)); }
Example #9
Source File: XSLTTransform.java From sakai with Educational Community License v2.0 | 6 votes |
/** * Set the xslt resource. * * @param xsltresource * an Input Source to the XSLT * @throws Exception */ public void setXslt(InputSource xsltresource) throws Exception { if ( saxParserFactory == null) { saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); } TemplatesHandler th = factory.newTemplatesHandler(); String systemId = xsltresource.getSystemId(); th.setSystemId(systemId); SAXParser parser = saxParserFactory.newSAXParser(); XMLReader xr = parser.getXMLReader(); xr.setContentHandler(th); xr.parse(xsltresource); templates = th.getTemplates(); }
Example #10
Source File: OscarDataParser.java From Darcula with Apache License 2.0 | 6 votes |
public void parseDocument(URL oscarURL) { //get a factory SAXParserFactory spf = SAXParserFactory.newInstance(); try { //get a new instance of parser SAXParser sp = spf.newSAXParser(); //parse the file and also register this class for call backs InputStream is = new BufferedInputStream(oscarURL.openStream()); sp.parse(is, this); System.out.println("done parsing count="+count); is.close(); } catch (SAXException se) { se.printStackTrace(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } }
Example #11
Source File: SAXDataParser.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 6 votes |
public void init() throws ComponentNotReadyException { //create mapping DOM Document mappingDOM; try { mappingDOM = createMappingDOM(rawMapping); createMapping(mappingDOM); } catch (XMLConfigurationException e) { throw new ComponentNotReadyException(e); } // create new sax factory SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validate); initXmlFeatures(factory); try { // create new sax parser parser = factory.newSAXParser(); } catch (Exception ex) { throw new ComponentNotReadyException(ex); } }
Example #12
Source File: XercesParser.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Create a <code>SAXParser</code> based on the underlying * <code>Xerces</code> version. * @param properties parser specific properties/features * @return an XML Schema/DTD enabled <code>SAXParser</code> */ public static SAXParser newSAXParser(Properties properties) throws ParserConfigurationException, SAXException, SAXNotSupportedException { SAXParserFactory factory = (SAXParserFactory)properties.get("SAXParserFactory"); if (versionNumber == null){ versionNumber = getXercesVersion(); version = new Float( versionNumber ).floatValue(); } // Note: 2.2 is completely broken (with XML Schema). if (version > 2.1) { configureXerces(factory); return factory.newSAXParser(); } else { SAXParser parser = factory.newSAXParser(); configureOldXerces(parser,properties); return parser; } }
Example #13
Source File: Catalog.java From JDKSourceCode1.8 with MIT License | 6 votes |
/** * Setup readers. */ public void setupReaders() { SAXParserFactory spf = JdkXmlUtils.getSAXFactory(catalogManager.overrideDefaultParser()); spf.setValidating(false); SAXCatalogReader saxReader = new SAXCatalogReader(spf); saxReader.setCatalogParser(null, "XMLCatalog", "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader"); saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog", "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader"); addReader("application/xml", saxReader); TR9401CatalogReader textReader = new TR9401CatalogReader(); addReader("text/plain", textReader); }
Example #14
Source File: XmlUtils.java From juddi with Apache License 2.0 | 6 votes |
public static Object unmarshal(Reader reader, String packageName) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/external-general-entities", false); spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setNamespaceAware(true); Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader)); JAXBContext jc = JAXBContext.newInstance(packageName); Unmarshaller um = jc.createUnmarshaller(); return ((javax.xml.bind.JAXBElement)um.unmarshal(xmlSource)).getValue(); } catch (Exception ex) { log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 3" + ex.getMessage()); log.debug(ex.getMessage(), ex); } return null; }
Example #15
Source File: LevelLoader.java From 30-android-libraries-in-30-days with Apache License 2.0 | 6 votes |
public void loadLevelFromStream(final InputStream pInputStream) throws IOException { try{ final SAXParserFactory spf = SAXParserFactory.newInstance(); final SAXParser sp = spf.newSAXParser(); final XMLReader xr = sp.getXMLReader(); this.onBeforeLoadLevel(); final LevelParser levelParser = new LevelParser(this.mDefaultEntityLoader, this.mEntityLoaders); xr.setContentHandler(levelParser); xr.parse(new InputSource(new BufferedInputStream(pInputStream))); this.onAfterLoadLevel(); } catch (final SAXException se) { Debug.e(se); /* Doesn't happen. */ } catch (final ParserConfigurationException pe) { Debug.e(pe); /* Doesn't happen. */ } finally { StreamUtils.close(pInputStream); } }
Example #16
Source File: MinimalPomParser.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public static PomModel parse(String path, InputStream is) { MinimalPomParser handler = new MinimalPomParser(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating( false ); factory.setNamespaceAware( false ); SAXParser parser = factory.newSAXParser(); parser.parse( is, handler ); } catch (Exception e) { throw new RuntimeException("Unable to parse File '" + path + "'", e); } return handler.getPomModel(); }
Example #17
Source File: XMLCheck.java From pentaho-kettle with Apache License 2.0 | 6 votes |
/** * Checks an xml string is well formed. * * @param is * inputstream * @return true if the xml is well formed. */ public static boolean isXMLWellFormed( InputStream is ) throws KettleException { boolean retval = false; try { SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory(); XMLTreeHandler handler = new XMLTreeHandler(); // Parse the input. SAXParser saxParser = factory.newSAXParser(); saxParser.parse( is, handler ); retval = true; } catch ( Exception e ) { throw new KettleException( e ); } return retval; }
Example #18
Source File: Bug6889654Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void parse() throws SAXException { String xml = "<data>\n<broken/>\u0000</data>"; try { InputSource is = new InputSource(new StringReader(xml)); is.setSystemId("file:///path/to/some.xml"); // notice that exception thrown here doesn't include the line number // information when reported by JVM -- CR6889654 SAXParserFactory.newInstance().newSAXParser().parse(is, new DefaultHandler()); } catch (SAXException e) { // notice that this message isn't getting displayed -- CR6889649 throw new SAXException(MSG, e); } catch (ParserConfigurationException pce) { } catch (IOException ioe) { } }
Example #19
Source File: Parser.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private XMLReader createReader() throws SAXException { try { SAXParserFactory pfactory = SAXParserFactory.newInstance(); pfactory.setValidating(true); pfactory.setNamespaceAware(true); // Enable schema validation SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd"); pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)})); return pfactory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException(ex); } }
Example #20
Source File: SAXParserFactTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Test the functionality of setFeature and getFeature methods * for namespace-prefixes property. * @throws Exception If any errors occur. */ @Test public void testFeature03() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature(NAMESPACE_PREFIXES, true); assertTrue(spf.getFeature(NAMESPACE_PREFIXES)); spf.setFeature(NAMESPACE_PREFIXES, false); assertFalse(spf.getFeature(NAMESPACE_PREFIXES)); }
Example #21
Source File: MainActivity.java From AndroidDemo with MIT License | 5 votes |
private void parseXMLWithSAX(String xmlData) { try {//流程参考课本 P186 :SAX解析思路 SAXParserFactory factory = SAXParserFactory.newInstance();//1.创建对象 XMLReader xmlReader = factory.newSAXParser().getXMLReader();//2.3.获取SAXParser解析器,并赋值给事件源对象XMLReader ContentHandler handler = new ContentHandler();//4.实例化DefaultHandler对象 xmlReader.setContentHandler(handler);// 5.将ContentHandler的实例设置到XMLReader中 xmlReader.parse(new InputSource(new StringReader(xmlData)));// 6.使用XMLReader的parse方法,从输入源获取xml数据 } catch (Exception e) { e.printStackTrace(); } }
Example #22
Source File: XMLCatalogResolver.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Attaches the reader to the catalog. */ private void attachReaderToCatalog (Catalog catalog) { SAXParserFactory spf = new SAXParserFactoryImpl(); spf.setNamespaceAware(true); spf.setValidating(false); SAXCatalogReader saxReader = new SAXCatalogReader(spf); saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog", "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader"); catalog.addReader("application/xml", saxReader); }
Example #23
Source File: FI_SAX_Or_XML_SAX_DOM_SAX_SAXEvent.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private SAXParser getParser() { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); try { return saxParserFactory.newSAXParser(); } catch (Exception e) { return null; } }
Example #24
Source File: ConfigReader.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Parses an xml config file and returns a Config object. * * @param xmlFile * The xml config file which is passed by the user to annotation processing * @return * A non null Config object */ private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException { XMLReader reader; try { SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing); reader = factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { // in practice this will never happen throw new Error(e); } NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler); // set up validator ValidatorHandler validator = configSchema.newValidator(); validator.setErrorHandler(errorHandler); // the validator will receive events first, then the parser. reader.setContentHandler(new ForkContentHandler(validator,runtime)); reader.setErrorHandler(errorHandler); Config config = new Config(runtime); runtime.setRootHandler(config); reader.parse(new InputSource(xmlFile.toURL().toExternalForm())); runtime.reset(); return config; }
Example #25
Source File: CloverLogParser.java From netbeans with Apache License 2.0 | 5 votes |
private static XMLReader createXmlReader() throws SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(false); try { return factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException("Cannot create SAX parser", ex); } }
Example #26
Source File: Xxe_sax.java From openrasp-testcases with MIT License | 5 votes |
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String data = req.getParameter("data"); if (data != null) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(new MyHandler()); reader.parse(new InputSource(new StringReader(data))); } catch (Exception e) { resp.getWriter().println(e); } } }
Example #27
Source File: DefaultClassLoaderFactory.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
public FilteringClassLoader createFilteringClassLoader(ClassLoader parent) { // See the comment for {@link #createIsolatedClassLoader} above FilteringClassLoader classLoader = new FilteringClassLoader(parent); if (needJaxpImpl()) { ServiceLocator locator = new ServiceLocator(ClassLoader.getSystemClassLoader()); makeServiceVisible(locator, classLoader, SAXParserFactory.class); makeServiceVisible(locator, classLoader, DocumentBuilderFactory.class); makeServiceVisible(locator, classLoader, DatatypeFactory.class); } return classLoader; }
Example #28
Source File: GenerateJfrFiles.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
Metadata(File metadataXml, File metadataSchema) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setSchema(schemaFactory.newSchema(metadataSchema)); SAXParser sp = factory.newSAXParser(); sp.parse(metadataXml, new MetadataHandler(this)); }
Example #29
Source File: XMLFilterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * By default false is expected get namespaces-prefix feature. * * @throws Exception If any errors occur. */ @Test public void getFeature02() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlFilter.setParent(spf.newSAXParser().getXMLReader()); assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); }
Example #30
Source File: DroidDrawHandler.java From DroidUIBuilder with Apache License 2.0 | 5 votes |
public static void parseInternal(DroidDrawHandler ddh, InputSource in) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(in, ddh); }