List of usage examples for org.xml.sax XMLReader setFeature
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException;
From source file:org.finra.jtaf.ewd.widget.element.Element.java
/** * Get the list of nodes which satisfy the xpath expression passed in * /* www . ja va 2 s . c o m*/ * @param xpath * the input xpath expression * @return the nodeset of matching elements * @throws Exception */ private NodeList getNodeListUsingJavaXPath(String xpath) throws Exception { XPathFactory xpathFac = XPathFactory.newInstance(); XPath theXpath = xpathFac.newXPath(); String html = getGUIDriver().getHtmlSource(); html = html.replaceAll(">\\s+<", "><"); InputStream input = new ByteArrayInputStream(html.getBytes()); XMLReader reader = new Parser(); reader.setFeature(Parser.namespacesFeature, false); Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMResult result = new DOMResult(); transformer.transform(new SAXSource(reader, new InputSource(input)), result); Node htmlNode = result.getNode(); // This code gets a Node from the // result. NodeList nodes = (NodeList) theXpath.evaluate(xpath, htmlNode, XPathConstants.NODESET); return nodes; }
From source file:org.getobjects.foundation.NSXMLPropertyListParser.java
public Object parse(InputStream _in) { if (_in == null) return null; final SAXParserFactory factory = SAXParserFactory.newInstance(); try {/* ww w. java 2s . com*/ final NSXMLPlistHandler handler = new NSXMLPlistHandler(); final XMLReader reader = factory.newSAXParser().getXMLReader(); final InputSource input = new InputSource(_in); reader.setContentHandler(handler); reader.setEntityResolver(handler); try { reader.setFeature("http://xml.org/sax/features/validation", false); reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (Exception e) { log.error("Couldn't turn validation off: " + e); } reader.parse(input); return handler.rootObject(); } catch (ParserConfigurationException e) { log.error("error during parser instantiation: " + e); } catch (SAXException e) { log.error("error during parsing: " + e); } catch (IOException e) { log.error("error during parsing: " + e); } return null; }
From source file:org.jbpm.jpdl.internal.convert.Jpdl3ConverterParser.java
public static XMLReader createXmlReader() throws Exception { SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); try {/*from w w w.j av a2 s. c o m*/ saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); } catch (SAXException e) { log.warn("couldn't set schema language property", e); } try { saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemaSource()); } catch (SAXException e) { log.warn("couldn't set schema source property", e); } try { xmlReader.setFeature("http://apache.org/xml/features/validation/dynamic", true); } catch (SAXException e) { log.warn("couldn't set dynamic validation feature", e); } return xmlReader; }
From source file:org.jbpm.jpdl.xml.JpdlParser.java
public static XMLReader createXmlReader() throws Exception { SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); try {/*ww w . j ava2s.c om*/ saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); } catch (SAXException e) { log.warn( "couldn't set xml parser property 'http://java.sun.com/xml/jaxp/properties/schemaLanguage' to 'http://www.w3.org/2001/XMLSchema'", e); } try { saxParser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://jbpm.org/3/jpdl http://jbpm.org/jpdl-3.0.xsd " + "urn:jbpm.org:jpdl-3.0 http://jbpm.org/jpdl-3.0.xsd " + "urn:jbpm.org:jpdl-3.1 http://jbpm.org/jpdl-3.1.xsd " + "urn:jbpm.org:jpdl-3.2 http://jbpm.org/jpdl-3.2.xsd"); } catch (SAXException e) { log.warn( "couldn't set xml parser property 'http://apache.org/xml/properties/schema/external-schemaLocation'", e); } try { xmlReader.setFeature("http://apache.org/xml/features/validation/dynamic", true); } catch (SAXException e) { log.warn("couldn't set xml parser feature 'http://apache.org/xml/features/validation/dynamic'", e); } return xmlReader; }
From source file:org.jlibrary.core.search.extraction.XMLExtractor.java
public XMLExtractor() throws ExtractionException { try {// w w w . java2 s.c om SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(false); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/validation", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); parser = new XMLParser(xmlReader); } catch (Exception e) { throw new ExtractionException(e); } }
From source file:org.kalypso.ogc.gml.serialize.GmlSerializer.java
/** * Most general way to serialize the workspace. All other serialize methods should eventually cal this method. */// w ww .j a va 2s.co m public static void serializeWorkspace(final GMLWorkspaceInputSource inputSource, final StreamResult result, final String charsetEncoding) throws GmlSerializeException { try { // TODO: error handling final XMLReader reader = new GMLWorkspaceReader(); // TODO: add an error handler that logs everything into a status // reader.setErrorHandler( null ); // TODO: write gml to a temporary location and replace the target file later, // in order to preserve the old version if anything goes wrong reader.setFeature("http://xml.org/sax/features/namespaces", true); //$NON-NLS-1$ reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); //$NON-NLS-1$ final Source source = new SAXSource(reader, inputSource); final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, charsetEncoding); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ // TODO: maybe also use OutputKeys.CDATA_SECTION_ELEMENTS ? See the marshallMethod of the XSDBaseTypeHandlerString // TODO put new QName( NS.OM, "result" ) here instead inside the GMLSaxFactory // Problem: must now know the prefix of NS.OM transformer.transform(source, result); } catch (final Exception e) { throw new GmlSerializeException(Messages.getString("org.kalypso.ogc.gml.serialize.GmlSerializer.4"), e); //$NON-NLS-1$ } }
From source file:org.kalypso.ogc.gml.serialize.GmlSerializer.java
private static XMLReader createXMLReader() throws ParserConfigurationException, SAXException { final SAXParserFactory saxFac = SAXParserFactory.newInstance(); saxFac.setNamespaceAware(true);/* w ww .ja va2s . c o m*/ final SAXParser saxParser = saxFac.newSAXParser(); // make namespace-prefixes visible to content handler // used to allow necessary schemas from gml document final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE); //$NON-NLS-1$ return xmlReader; }
From source file:org.kalypsodeegree_impl.io.sax.test.TriangulatedSurfaceContentHandlerTest.java
private GM_TriangulatedSurface readTriangles(final InputSource is) throws IOException, ParserConfigurationException, SAXException { final SAXParserFactory saxFac = SAXParserFactory.newInstance(); saxFac.setNamespaceAware(true);/*from w w w .j a v a 2s. c om*/ final SAXParser saxParser = saxFac.newSAXParser(); // make namespace-prefixes visible to content handler // used to allow necessary schemas from gml document final XMLReader reader = saxParser.getXMLReader(); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE); //$NON-NLS-1$ final GM_TriangulatedSurface[] result = new GM_TriangulatedSurface[1]; final UnmarshallResultEater resultEater = new UnmarshallResultEater() { @Override public void unmarshallSuccesful(final Object value) { assertTrue(value instanceof GM_TriangulatedSurface); result[0] = (GM_TriangulatedSurface) value; } }; final TriangulatedSurfaceContentHandler contentHandler = new TriangulatedSurfaceContentHandler(reader, resultEater); reader.setContentHandler(contentHandler); reader.parse(is); return result[0]; }
From source file:org.lnicholls.galleon.apps.iTunes.PlaylistParser.java
public PlaylistParser(String path) { try {/*w ww .j a v a 2s.co m*/ //path = "D:/galleon/iTunes Music Library.xml"; ArrayList currentPlaylists = new ArrayList(); // Read all tracks XMLReader trackReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); TrackParser trackParser = new TrackParser(); trackReader.setContentHandler(trackParser); trackReader.setErrorHandler(trackParser); trackReader.setFeature("http://xml.org/sax/features/validation", false); File file = new File(path); if (file.exists()) { InputStream inputStream = Tools.getInputStream(file); trackReader.parse(new InputSource(inputStream)); inputStream.close(); } // Read all playlists XMLReader listReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); ListParser listParser = new ListParser(currentPlaylists); listReader.setContentHandler(listParser); listReader.setErrorHandler(listParser); listReader.setFeature("http://xml.org/sax/features/validation", false); file = new File(path); if (file.exists()) { InputStream inputStream = Tools.getInputStream(file); listReader.parse(new InputSource(inputStream)); inputStream.close(); } // Remove old playlists List list = PlaylistsManager.listAll(); if (list != null && list.size() > 0) { Iterator playlistIterator = list.iterator(); while (playlistIterator.hasNext()) { Playlists playlist = (Playlists) playlistIterator.next(); boolean found = false; Iterator iterator = currentPlaylists.iterator(); while (iterator.hasNext()) { String externalId = (String) iterator.next(); if (externalId.equals(playlist.getExternalId())) { found = true; break; } } if (!found) { PlaylistsManager.deletePlaylistsTracks(playlist); PlaylistsManager.deletePlaylists(playlist); log.debug("Removed playlist: " + playlist.getTitle()); } } list.clear(); } currentPlaylists.clear(); } catch (IOException ex) { Tools.logException(PlaylistParser.class, ex); } catch (SAXException ex) { Tools.logException(PlaylistParser.class, ex); } catch (Exception ex) { Tools.logException(PlaylistParser.class, ex); } }
From source file:org.opencms.xml.CmsXmlUtils.java
/** * Validates the structure of a XML document contained in a byte array * with the DTD or XML schema used by the document.<p> * /*ww w. j a v a 2 s . c om*/ * @param xmlStream a source providing a XML document that should be validated * @param resolver the XML entity resolver to use * * @throws CmsXmlException if the validation fails */ public static void validateXmlStructure(InputStream xmlStream, EntityResolver resolver) throws CmsXmlException { XMLReader reader; try { reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); } catch (SAXException e) { // xerces parser not available - no schema validation possible if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_VALIDATION_INIT_XERXES_SAX_READER_FAILED_0), e); } // no validation of the content is possible return; } // turn on validation try { reader.setFeature("http://xml.org/sax/features/validation", true); // turn on schema validation reader.setFeature("http://apache.org/xml/features/validation/schema", true); // configure namespace support reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); } catch (SAXNotRecognizedException e) { // should not happen as Xerces 2 support this feature if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_RECOGNIZED_0), e); } // no validation of the content is possible return; } catch (SAXNotSupportedException e) { // should not happen as Xerces 2 support this feature if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_SUPPORTED_0), e); } // no validation of the content is possible return; } // add an error handler which turns any errors into XML CmsXmlValidationErrorHandler errorHandler = new CmsXmlValidationErrorHandler(); reader.setErrorHandler(errorHandler); if (resolver != null) { // set the resolver for the "opencms://" URIs reader.setEntityResolver(resolver); } try { reader.parse(new InputSource(xmlStream)); } catch (IOException e) { // should not happen since we read form a byte array if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_XML_FROM_BYTE_ARR_FAILED_0), e); } return; } catch (SAXException e) { // should not happen since all errors are handled in the XML error handler if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_PARSE_SAX_EXC_0), e); } return; } if (errorHandler.getErrors().elements().size() > 0) { // there was at last one validation error, so throw an exception StringWriter out = new StringWriter(256); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, format); try { writer.write(errorHandler.getErrors()); writer.write(errorHandler.getWarnings()); writer.close(); } catch (IOException e) { // should not happen since we write to a StringWriter if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_STRINGWRITER_IO_EXC_0), e); } } // generate String from XML for display of document in error message throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_VALIDATION_1, out.toString())); } }