List of usage examples for org.xml.sax XMLReader setEntityResolver
public void setEntityResolver(EntityResolver resolver);
From source file:net.lightbody.bmp.proxy.jetty.xml.XmlParser.java
public synchronized Node parse(InputSource source) throws IOException, SAXException { Handler handler = new Handler(); XMLReader reader = _parser.getXMLReader(); reader.setContentHandler(handler);//from ww w . j a va 2 s .c om reader.setErrorHandler(handler); reader.setEntityResolver(handler); if (log.isDebugEnabled()) log.debug("parsing: sid=" + source.getSystemId() + ",pid=" + source.getPublicId()); _parser.parse(source, handler); if (handler._error != null) throw handler._error; Node doc = (Node) handler._top.get(0); handler.clear(); return doc; }
From source file:net.sf.firemox.xml.XmlParser.java
/** * @param source// w ww . j a v a2s . c om * the document source. * @return the node of document. * @throws IOException * error while opening the stream. * @throws SAXException * error while parsing. */ public synchronized Node parse(InputSource source) throws IOException, SAXException { Handler handler = new Handler(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); parser.parse(source, handler); if (handler.error != null) { throw handler.error; } Node doc = (Node) handler.top.get(0); handler.clear(); return doc; }
From source file:net.sf.firemox.xml.XmlParser.java
/** * Parse InputStream./*w w w .ja v a 2 s . c o m*/ * * @param in * the document source. * @return the node of document. * @throws IOException * error while opening the stream. * @throws SAXException * error while parsing. */ public synchronized Node parse(InputStream in) throws IOException, SAXException { Handler handler = new Handler(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); parser.parse(new InputSource(in), handler); if (handler.error != null) { throw handler.error; } Node doc = (Node) handler.top.get(0); handler.clear(); return doc; }
From source file:org.esco.grouper.parsing.SGSParsingUtil.java
/** * Starts the parsing process./*from w w w . jav a 2 s . c o m*/ * @throws SAXException If there is an error during the parsing (invalid group defintion for instance). * @throws IOException If there is an IO error. */ public void parse() throws IOException, SAXException { XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); saxReader.setFeature("http://xml.org/sax/features/validation", true); saxReader.setContentHandler(this); saxReader.setErrorHandler(this); saxReader.setEntityResolver(this); if (LOGGER.isInfoEnabled()) { LOGGER.info("---------------------------------"); LOGGER.info("Parsing definition file: "); LOGGER.info(definitionsFileURI); LOGGER.info("---------------------------------"); } InputStream iStream = getClass().getClassLoader().getResourceAsStream(definitionsFileURI); if (iStream == null) { LOGGER.fatal("Unable to load (from classpath) file: " + definitionsFileURI + "."); } //final InputSource is = saxReader.parse(new InputSource(iStream)); if (LOGGER.isInfoEnabled()) { LOGGER.info("---------------------------------"); LOGGER.info("Definition file parsed."); LOGGER.info("---------------------------------"); } }
From source file:com.cloudhopper.sxmp.SxmpParser.java
/** public Node parse(InputSource source) throws IOException, SAXException { // _dtd=null;/*from www.j a va 2s. c o m*/ Handler handler = new Handler(); XMLReader reader = _parser.getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); if (logger.isDebugEnabled()) logger.debug("parsing: sid=" + source.getSystemId() + ",pid=" + source.getPublicId()); _parser.parse(source, handler); if (handler.error != null) throw handler.error; Node root = (Node)handler.root; handler.reset(); return root; } public synchronized Node parse(String xml) throws IOException, SAXException { ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); return parse(is); } public synchronized Node parse(File file) throws IOException, SAXException { return parse(new InputSource(file.toURI().toURL().toString())); } public synchronized Node parse(InputStream in) throws IOException, SAXException { //_dtd=null; Handler handler = new Handler(); XMLReader reader = _parser.getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); _parser.parse(new InputSource(in), handler); if (handler.error != null) throw handler.error; Node root = (Node)handler.root; handler.reset(); return root; } */ public Operation parse(InputStream in) throws SxmpParsingException, IOException, SAXException, ParserConfigurationException { // create a new SAX parser SAXParserFactory factory = SAXParserFactory.newInstance(); //factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); SAXParser parser = factory.newSAXParser(); //_parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", validating); parser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true); parser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", true); parser.getXMLReader().setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); //_dtd=null; Handler handler = new Handler(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); // try parsing (may throw an SxmpParsingException in the handler) try { parser.parse(new InputSource(in), handler); } catch (com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException e) { throw new SxmpParsingException(SxmpErrorCode.INVALID_XML, "XML encoding mismatch", null); } // check if there was an error if (handler.error != null) { throw handler.error; } // check to see if an operation was actually parsed if (handler.getOperation() == null) { throw new SxmpParsingException(SxmpErrorCode.MISSING_REQUIRED_ELEMENT, "The operation type [" + handler.operationType.getValue() + "] requires a request element", new PartialOperation(handler.operationType)); } // if we got here, an operation was parsed -- now we need to validate it // to make sure that it has all required elements try { handler.getOperation().validate(); } catch (SxmpErrorException e) { throw new SxmpParsingException(e.getErrorCode(), e.getErrorMessage(), handler.getOperation()); } return handler.getOperation(); }
From source file:com.agilejava.docbkx.maven.AbstractTransformerMojo.java
/** * Builds the actual output document.//w w w . j a va2 s . co m */ public void execute() throws MojoExecutionException, MojoFailureException { if (isSkip()) { getLog().info("Skipping plugin execution"); return; } // userland (ant tasks) pre process preProcess(); final File targetDirectory = getTargetDirectory(); final File sourceDirectory = getSourceDirectory(); if (!sourceDirectory.exists()) { return; // No sources, so there is nothing to render. } if (!targetDirectory.exists()) { org.codehaus.plexus.util.FileUtils.mkdir(targetDirectory.getAbsolutePath()); } final String[] included = scanIncludedFiles(); // configure a resolver for catalog files final CatalogManager catalogManager = createCatalogManager(); final CatalogResolver catalogResolver = new CatalogResolver(catalogManager); // configure a resolver for urn:dockbx:stylesheet final URIResolver uriResolver = createStyleSheetResolver(catalogResolver); // configure a resolver for xml entities final InjectingEntityResolver injectingResolver = createEntityResolver(catalogResolver); EntityResolver resolver = catalogResolver; if (injectingResolver != null) { resolver = injectingResolver; } // configure the builder for XSL Transforms final TransformerBuilder builder = createTransformerBuilder(uriResolver); // configure the XML parser SAXParserFactory factory = createParserFactory(); // iterate over included source files for (int i = included.length - 1; i >= 0; i--) { try { if (injectingResolver != null) { injectingResolver.forceInjection(); } final String inputFilename = included[i]; // targetFilename is inputFilename - ".xml" + targetFile extension String baseTargetFile = inputFilename.substring(0, inputFilename.length() - 4); final String targetFilename = baseTargetFile + "." + getTargetFileExtension(); final File sourceFile = new File(sourceDirectory, inputFilename); getLog().debug("SourceFile: " + sourceFile.toString()); // creating targetFile File targetFile = null; if (isUseStandardOutput()) { targetFile = new File(targetDirectory, targetFilename); getLog().debug("TargetFile: " + targetFile.toString()); } else { String name = new File(baseTargetFile).getName(); String dir = new File(baseTargetFile).getParent(); if (dir == null) { // file is located on root of targetDirectory targetFile = targetDirectory; } else { // else append the relative directory to targetDirectory targetFile = new File(targetDirectory, dir); } targetFile = new File(targetFile, name + "." + getTargetFileExtension()); getLog().debug("TargetDirectory: " + targetDirectory.getAbsolutePath()); } if (!targetFile.exists() || (targetFile.exists() && FileUtils.isFileNewer(sourceFile, targetFile)) || (targetFile.exists() && getXIncludeSupported())) { getLog().info("Processing input file: " + inputFilename); final XMLReader reader = factory.newSAXParser().getXMLReader(); // configure XML reader reader.setEntityResolver(resolver); // eval PI final PreprocessingFilter filter = createPIHandler(resolver, reader); // configure SAXSource for XInclude final Source xmlSource = createSource(inputFilename, sourceFile, filter); configureXref(targetFile); // XSL Transformation setup final Transformer transformer = builder.build(); adjustTransformer(transformer, sourceFile.getAbsolutePath(), targetFile); // configure the output file Result result = null; if (!shouldProcessResult()) { // if the output is not the main result of the transformation, ie xref database if (getLog().isDebugEnabled()) { result = new StreamResult(System.out); } else { result = new StreamResult(new NullOutputStream()); } } else if (isUseStandardOutput()) { // if the output of the main result is the standard output result = new StreamResult(targetFile.getAbsolutePath()); } else { // if the output of the main result is not the standard output if (getLog().isDebugEnabled()) { result = new StreamResult(System.out); } else { result = new StreamResult(new NullOutputStream()); } } transformer.transform(xmlSource, result); if (shouldProcessResult()) { // if the transformation has produce the expected main results, we can continue // the chain of processing in the output mojos which can override postProcessResult postProcessResult(targetFile); if (isUseStandardOutput()) { getLog().info(targetFile + " has been generated."); } else { getLog().info("See " + targetFile.getParentFile().getAbsolutePath() + " for generated file(s)"); } } else { // if the output is not the main result getLog().info("See " + targetFile.getParentFile().getAbsolutePath() + " for generated secondary file(s)"); } } else { getLog().info(targetFile + " is up to date."); } } catch (SAXException saxe) { throw new MojoExecutionException("Failed to parse " + included[i] + ".", saxe); } catch (TransformerException te) { throw new MojoExecutionException("Failed to transform " + included[i] + ".", te); } catch (ParserConfigurationException pce) { throw new MojoExecutionException("Failed to construct parser.", pce); } } // userland (ant tasks) post process postProcess(); }
From source file:net.sf.joost.trax.TrAXFilter.java
/** * Parses the <code>InputSource</code> * @param input A <code>InputSource</code> object. * @throws SAXException//from w w w .j a v a 2 s. c o m * @throws IOException */ public void parse(InputSource input) throws SAXException, IOException { Transformer transformer = null; if (DEBUG) { if (log.isDebugEnabled()) log.debug("parsing InputSource " + input.getSystemId()); } try { // get a new Transformer transformer = this.templates.newTransformer(); if (transformer instanceof TransformerImpl) { this.processor = ((TransformerImpl) transformer).getStxProcessor(); } else { String msg = "An error is occured, because the given transformer is " + "not an instance of TransformerImpl"; if (log != null) log.fatal(msg); else System.err.println("Fatal error - " + msg); } XMLReader parent = this.getParent(); if (parent == null) { parent = XMLReaderFactory.createXMLReader(); setParent(parent); } ContentHandler handler = this.getContentHandler(); if (handler == null) { handler = parent.getContentHandler(); } if (handler == null) { throw new SAXException("no ContentHandler registered"); } //init StxEmitter StxEmitter out = null; //SAX specific Implementation out = new SAXEmitter(handler); if (this.processor != null) { this.processor.setContentHandler(out); this.processor.setLexicalHandler(out); } else { throw new SAXException("Joost-Processor is not correct configured."); } if (parent == null) { throw new SAXException("No parent for filter"); } parent.setContentHandler(this.processor); parent.setProperty("http://xml.org/sax/properties/lexical-handler", this.processor); parent.setEntityResolver(this); parent.setDTDHandler(this); parent.setErrorHandler(this); parent.parse(input); } catch (TransformerConfigurationException tE) { try { configErrListener.fatalError(tE); } catch (TransformerConfigurationException innerE) { throw new SAXException(innerE.getMessage(), innerE); } } catch (SAXException sE) { try { configErrListener.fatalError(new TransformerConfigurationException(sE.getMessage(), sE)); } catch (TransformerConfigurationException innerE) { throw new SAXException(innerE.getMessage(), innerE); } } catch (IOException iE) { try { configErrListener.fatalError(new TransformerConfigurationException(iE.getMessage(), iE)); } catch (TransformerConfigurationException innerE) { throw new IOException(innerE.getMessage()); } } }
From source file:nl.b3p.wms.capabilities.WMSCapabilitiesReader.java
public ServiceProvider getProvider(ByteArrayInputStream in) { //Nu kan het service provider object gemaakt en gevuld worden serviceProvider = new ServiceProvider(); XMLReader reader = null; try {// w ww. ja va2 s . co m reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); } catch (SAXException ex) { } IgnoreEntityResolver r = new IgnoreEntityResolver(); reader.setEntityResolver(r); reader.setContentHandler(s); InputSource is = new InputSource(in); is.setEncoding(KBConfiguration.CHARSET); try { reader.parse(is); } catch (Exception ex) { } return serviceProvider; }
From source file:nl.b3p.wms.capabilities.WMSCapabilitiesReader.java
public ServiceProvider getProvider(String location, B3PCredentials credentials, String remoteAddr) throws IOException, SAXException, Exception { ByteArrayOutputStream getCap = getCapabilities(location, credentials, remoteAddr); //String xml = getCapabilities(location, username, password); ByteArrayInputStream in = new ByteArrayInputStream(getCap.toByteArray()); //Nu kan het service provider object gemaakt en gevuld worden serviceProvider = new ServiceProvider(); XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); // niet zinvol met IgnoreEntityResolver hierna // reader.setFeature(VALIDATION_FEATURE, true); // reader.setFeature(SCHEMA_FEATURE, true); IgnoreEntityResolver r = new IgnoreEntityResolver(); reader.setEntityResolver(r); reader.setContentHandler(s);/*ww w . jav a 2 s .c o m*/ InputSource is = new InputSource(in); is.setEncoding(KBConfiguration.CHARSET); reader.parse(is); if (serviceProvider == null) { log.error("Host: " + location + " error: No service provider object could be created, unkown reason!"); throw new Exception( "Host: " + location + " error: No service provider object could be created, unkown reason!"); } return serviceProvider; }
From source file:nl.b3p.ogc.utils.OgcWfsClient.java
public static AnyNode xmlStringToAnyNode(String xml) throws Exception { AnyNode anyNode = null;//from w w w . java 2 s . c o m try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader reader = saxParser.getXMLReader(); org.exolab.castor.xml.util.SAX2ANY handler = new org.exolab.castor.xml.util.SAX2ANY(); IgnoreEntityResolver r = new IgnoreEntityResolver(); reader.setEntityResolver(r); reader.setContentHandler(handler); reader.setErrorHandler(handler); InputSource source = new InputSource(new StringReader(xml)); reader.parse(source); anyNode = handler.getStartingNode(); } catch (Exception e) { log.error("error", e); } return anyNode; }