Package org.xml.sax

Examples of org.xml.sax.XMLReader


  private Grammar getGrammar(String in) throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    GrammarFactory grammarfactory = new GrammarFactory();
    parser.setContentHandler(grammarfactory);
    parser.parse(new InputSource(getClass().getClassLoader().getResourceAsStream(in)));

    //System.out.println("====================================\nGrammar:\n"+grammarfactory.getGrammar()+"\n");
    return grammarfactory.getGrammar();
  }
View Full Code Here


  private Document getDocument(String in) throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    SAXTransformerFactory transformerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler handler = transformerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    handler.setResult(result);

    parser.setContentHandler(new WhitespaceFilter(handler));
    parser.parse(new InputSource(getClass().getClassLoader().getResourceAsStream(in)));

    return (Document)result.getNode();
  }
View Full Code Here

    throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    SAXTransformerFactory serializerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler handler = serializerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    handler.setResult(result);

    parser.setContentHandler(new WhitespaceFilter(handler));

    InputSource inputsource = new InputSource(in);
    inputsource.setSystemId(filename);
    parser.parse(inputsource);

    return (Document)result.getNode();
  }
View Full Code Here

    throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    SAXTransformerFactory serializerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler handler = serializerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    handler.setResult(result);

    parser.setContentHandler(new WhitespaceFilter(handler));

    InputSource inputsource = new InputSource(in);
    inputsource.setSystemId(filename);
    parser.parse(inputsource);

    return (Document)result.getNode();
  }
View Full Code Here

   * @throws XMLException
   *             if an appropriate parser cannot be found or created.
   * @return a new Builder
   */
  public Builder createDTDBuilder(EntityResolver resolver) {
    XMLReader parser = createParser(false);
    //parser.setFeature("http://xml.org/sax/features/validation", true); // will be set by XOM anyway
    parser.setEntityResolver(resolver);
    return newBuilder(parser, true);
  }
View Full Code Here

   * @throws XMLException
   *             if an appropriate parser cannot be found or created.
   * @return a new Builder
   */
  public Builder createW3CBuilder(Map schemaLocations) {
    XMLReader parser = createParser(true);
    try {
      setupW3CParser(parser, schemaLocations);     
    } catch (SAXException e) {
      throw new XMLException(
        "Can't find or create W3C schema validating parser (i.e. Xerces)" +
        " - check your classpath",
        e);
    }
   
    try { // improve performance; see http://www-106.ibm.com/developerworks/xml/library/x-perfap3.html
      parser.setFeature("http://apache.org/xml/features/validation/schema/augment-psvi", false);
    } catch (SAXException e) {
      ; // we can live with that
    }
   
    return newBuilder(parser, true);
View Full Code Here

      "At least one of the parameters 'schema' and 'systemId' must not be null");
    InputSource source = new InputSource();
    if (schema != null) source.setByteStream(schema);
    if (systemID != null) source.setSystemId(systemID.toASCIIString());
   
    XMLReader parser;
    try {
      // somewhat akward work-around to avoid making MSV a compile-time dependency
      SAXParserFactory factory = (SAXParserFactory) ClassLoaderUtil.newInstance(
          "com.sun.msv.verifier.jaxp.SAXParserFactoryImpl");
      factory.setNamespaceAware(true);
      SAXParser saxParser = factory.newSAXParser();
      // would be nice to set the property on the XMLReader (instead of the SAXParser), but that wouldn't work
      saxParser.setProperty("http://www.sun.com/xml/msv/schema", source);
      parser = saxParser.getXMLReader();
     
      // find root filter, if any
      XMLReader filter = parser;
      while (filter instanceof XMLFilter && ((XMLFilter) filter).getParent() instanceof XMLFilter) {
        if (DEBUG) System.err.println("currFilter=" + filter);
        filter = ((XMLFilter) filter).getParent();
      }
      // replace default parser with our own parser, if possible
View Full Code Here

  protected Builder newBuilder(XMLReader parser, boolean validate) {
    return new Builder(parser, validate, null);    
  }
 
  private XMLReader createParser(boolean w3cSchemaParser) {
    XMLReader parser;
    for (int i = 0; i < PARSERS.length; i++) {
      try {
        parser = XMLReaderFactory.createXMLReader(PARSERS[i]);
        if (w3cSchemaParser && ENABLE_PARSER_GRAMMAR_POOLS) {
          String clazz = (String) PARSER_GRAMMAR_POOLS.get(PARSERS[i]);
          if (clazz != null) {
            // This improves performance and, more importantly, prevents xerces
            // bugs/exceptions when the Builder is used more than once (at least for
            // xerces-2.6.2). But it would lead to xerces bugs in non-validating mode with DTDs,
            // at least for xerces-2.7.1, so in this case we're not enabling it.
            // See http://xml.apache.org/xerces2-j/faq-grammars.html
            // See http://www-106.ibm.com/developerworks/xml/library/x-perfap3.html
             parser.setProperty("http://apache.org/xml/properties/internal/grammar-pool",
              ClassLoaderUtil.newInstance(clazz));
          }
        }
        setupFeaturesAndProps(parser);       
        //parser.setProperty("http://apache.org/xml/properties/input-buffer-size", new Integer(8192));
        if (DEBUG) System.err.println("using XMLReader=" + parser.getClass().getName());
        return parser;
      } catch (SAXException ex) {
        // keep on trying
      } catch (NoClassDefFoundError err) {
        // keep on trying
      } catch (Exception err) {
        // keep on trying
      }
    }

    try { // SAX default
      parser = XMLReaderFactory.createXMLReader();
      setupFeaturesAndProps(parser);
      if (DEBUG) System.err.println("using default SAX XMLReader=" + parser.getClass().getName());
      return parser;
    } catch (Exception ex) {
      // keep on trying
    }

    try { // JAXP default
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setNamespaceAware(true);
      parser = factory.newSAXParser().getXMLReader();
      setupFeaturesAndProps(parser);
      if (DEBUG) System.err.println("using default JAXP XMLReader=" + parser.getClass().getName());
      return parser;
    } catch (Exception ex) {
      throw new XMLException(
          "Could not find or create a suitable SAX2 parser" +
          " - check your classpath",
View Full Code Here

      XMLStreamReader fistaxReader = null;
      Method fistaxMethod = null;
      if (mode.startsWith("fi")) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        XMLReader parser = (XMLReader) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentParser").newInstance();
        fiBuilder = new Builder(parser, false, factory);
       
        if (mode.indexOf("stax") >= 0) {
          fiSerializer = (XMLStreamWriter) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentSerializer").newInstance();       
        } else {
View Full Code Here

            prop = System.getProperty("org.xml.sax.driver", defaultReader);
        } catch (SecurityException se) {
            // This can happen if we are running as an applet
            prop = defaultReader;
        }
        XMLReader xmlReader = XMLReaderFactory.createXMLReader(prop);

        //
        // Namespace related features needed for XmlDocumentBuilder
        //
        String namespaces = "http://xml.org/sax/features/namespaces";
        xmlReader.setFeature(namespaces, true);

        String nsPrefixes = "http://xml.org/sax/features/namespace-prefixes";
        xmlReader.setFeature(nsPrefixes, true);

        // Create XmlDocumentBuilder instance
        XmlDocumentBuilder builder = new XmlDocumentBuilder();

        // Use as the ContentHandler
        xmlReader.setContentHandler(builder);
         
        // org.xml.sax.ext.LexicalHandler
        String lexHandler = "http://xml.org/sax/properties/lexical-handler";
        xmlReader.setProperty(lexHandler, builder);

        // org.xml.sax.ext.DeclHandler
        String declHandler
            = "http://xml.org/sax/properties/declaration-handler";
        xmlReader.setProperty(declHandler, builder);

        // DTDHandler
        xmlReader.setDTDHandler(builder);

        // Validation
        String validation = "http://xml.org/sax/features/validation";
        xmlReader.setFeature(validation, doValidate);

        // If validating, use an error handler that throws an exception for
        // validation errors.
        if (doValidate) {
            xmlReader.setErrorHandler(new DefaultHandler() {
                public void error(SAXParseException e) throws SAXException {
                    throw e;
                }
            });
        }

        builder.setDisableNamespaces(true);

        // Parse the input
        xmlReader.parse(in);
        return builder.getDocument();
    }
View Full Code Here

TOP

Related Classes of org.xml.sax.XMLReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.