/** * Reads the Filter XML request into a geotools Feature object. * * <p>This uses the "old" filter parser and is around to maintain some backwards compatability * with cases in which the new parser chokes on a filter that hte old one could handle. * * @param rawRequest The plain POST text from the client. * @return The geotools filter constructed from rawRequest. * @throws WfsException For any problems reading the request. */ protected Filter parseXMLFilterWithOldParser(Reader rawRequest) throws ServiceException { // translate string into a proper SAX input source InputSource requestSource = new InputSource(rawRequest); // instantiante parsers and content handlers FilterHandlerImpl contentHandler = new FilterHandlerImpl(); contentHandler.setEntityResolver(entityResolverProvider.getEntityResolver()); FilterFilter filterParser = new FilterFilter(contentHandler, null); GMLFilterGeometry geometryFilter = new GMLFilterGeometry(filterParser); GMLFilterDocument documentFilter = new GMLFilterDocument(geometryFilter); // read in XML file and parse to content handler try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); ParserAdapter adapter = new ParserAdapter(parser.getParser()); adapter.setEntityResolver(entityResolverProvider.getEntityResolver()); adapter.setContentHandler(documentFilter); adapter.parse(requestSource); LOGGER.fine("just parsed: " + requestSource); } catch (SAXException e) { throw new ServiceException( e, "XML getFeature request SAX parsing error", XmlRequestReader.class.getName()); } catch (IOException e) { throw new ServiceException( e, "XML get feature request input error", XmlRequestReader.class.getName()); } catch (ParserConfigurationException e) { throw new ServiceException( e, "Some sort of issue creating parser", XmlRequestReader.class.getName()); } LOGGER.fine("passing filter: " + contentHandler.getFilter()); return contentHandler.getFilter(); }
static Parser getParser() throws SAXException, ParserConfigurationException { // // turn it into an in-memory object. // Parser parser; SAXParserFactory spf = SAXParserFactory.newInstance(); String validation = System.getProperty("javax.xml.parsers.validation", "false"); // NOT LOCALIZABLE if (validation.equalsIgnoreCase("true")) // NOT LOCALIZABLE spf.setValidating(true); SAXParser sp = spf.newSAXParser(); parser = sp.getParser(); return parser; }
public void parse() throws IOException, SAXException, ParserConfigurationException { // parser stuff figured out with great ease thanks to java examples in a nutshell By David // Flanagan // http://www.oreilly.com/catalog/jenut2/chapter/ch19.html // Create a JAXP "parser factory" for creating SAX parsers SAXParserFactory spf = SAXParserFactory.newInstance(); // Configure the parser factory for the type of parsers we require spf.setValidating(false); // No validation required // Now use the parser factory to create a SAXParser object // Note that SAXParser is a JAXP class, not a SAX class javax.xml.parsers.SAXParser sp = spf.newSAXParser(); // Create a SAX input source for the file argument org.xml.sax.InputSource input = new InputSource(new FileReader(this.xmlFileLocation)); // Give the InputSource an absolute URL for the file, so that // it can resolve relative URLs in a <!DOCTYPE> declaration, e.g. input.setSystemId("file://" + new File(this.xmlFileLocation).getAbsolutePath()); // Finally, tell the parser to parse the input and notify the handler // sp.parse(input, this); // Instead of using the SAXParser.parse() method, which is part of the // JAXP API, we could also use the SAX1 API directly. Note the // difference between the JAXP class javax.xml.parsers.SAXParser and // the SAX1 class org.xml.sax.Parser // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser if (this.resolveEntities) { parser.setEntityResolver(this); } else { parser.setEntityResolver(new SAXFakeEntityResolver()); } parser.setDocumentHandler(this); // Set main handler parser.setErrorHandler(errorHandler); // Set error handler parser.parse(input); // Parse! }