Package org.xml.sax

Examples of org.xml.sax.Parser


    public static void print(String parserName, String uri, boolean canonical) {

        try {
            HandlerBase handler = new SAXWriter(canonical);

            Parser parser = ParserFactory.makeParser(parserName);
            parser.setDocumentHandler(handler);
            parser.setErrorHandler(handler);
            parser.parse(uri);
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
View Full Code Here


    public static void print(String parserName, String uri, boolean validate) {

        try {
            SAXCount counter = new SAXCount();

            Parser parser = ParserFactory.makeParser(parserName);
            parser.setDocumentHandler(counter);
            parser.setErrorHandler(counter);
            try {
                if (validate && parser instanceof Configurable)
                    ((Configurable)parser).setFeature("http://xml.org/sax/features/validation", true);
            } catch (Exception ex) {}

            if (warmup) {
                if (parser instanceof Configurable)
                    ((Configurable)parser).setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
                parser.parse(uri);
                warmup = false;
            }
            long before = System.currentTimeMillis();
            parser.parse(uri);
            long after = System.currentTimeMillis();
            counter.printResults(uri, after - before);
        }
        catch (org.xml.sax.SAXParseException spe) {
            spe.printStackTrace(System.err);
View Full Code Here

                    try {
                        parser = XMLReaderFactory.createXMLReader(parserName);
                    }
                    catch (Exception e) {
                        try {
                            Parser sax1Parser = ParserFactory.makeParser(parserName);
                            parser = new ParserAdapter(sax1Parser);
                            System.err.println("warning: Features and properties not supported on SAX1 parsers.");
                        }
                        catch (Exception ex) {
                            parser = null;
View Full Code Here

     * @exception Exception if any processing exception occurs
     */
    public void parse(URL url) throws Exception {

        // Construct a parser for our use
        Parser parser =
            ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");
        parser.setDocumentHandler(this);
        parser.setErrorHandler(this);

        // Perform the requested parse
        long before = System.currentTimeMillis();
        parser.parse(url.toString());
        long after = System.currentTimeMillis();

        // Log the results
        StaticLogger.write("Parsing time = " + (after - before) +
                           " milliseconds");
View Full Code Here

        throws SAXException, IOException {
        if (is == null) {
            throw new IllegalArgumentException("InputSource cannot be null");
        }

        Parser parser = this.getParser();
        if (hb != null) {
            parser.setDocumentHandler(hb);
            parser.setEntityResolver(hb);
            parser.setErrorHandler(hb);
            parser.setDTDHandler(hb);
        }
        parser.parse(is);
    }
View Full Code Here

        throws SAXException, IOException {
        if (is == null) {
            throw new IllegalArgumentException("InputSource cannot be null");
        }

        Parser parser = this.getParser();
        if (hb != null) {
            parser.setDocumentHandler(hb);
            parser.setEntityResolver(hb);
            parser.setErrorHandler(hb);
            parser.setDTDHandler(hb);
        }
        parser.parse(is);
    }
View Full Code Here

     * @return {@link Schema} object instance.
     * @throws SAXException When a problem with XML data binding occurs.
     * @throws IOException When the XML schema file cannot be accessed.
     */
    private Schema unmarshalSchema(final String schemaName) throws SAXException, IOException {
        Parser parser = null;
        InternalContext internalContext = new BackwardCompatibilityContext();

        try {
            parser = internalContext.getParser();
        } catch (RuntimeException rte) {
            fail("Can't optain sax parser!");
        }

        if (parser == null) {
            fail("Unable to create SAX parser.");
        }

        SchemaContext schemaContext = new SchemaContextImpl();
        SchemaUnmarshaller schemaUnmarshaller = null;
        try {
            schemaUnmarshaller = new SchemaUnmarshaller(schemaContext);
        } catch (XMLException e) {
            fail(e.getMessage());
        }

        Sax2ComponentReader handler = new Sax2ComponentReader(
                schemaUnmarshaller);
        parser.setDocumentHandler(handler);
        parser.setErrorHandler(handler);

        parser.parse(new InputSource(getClass().getResource(schemaName).toExternalForm()));

        Schema schema = schemaUnmarshaller.getSchema();
        return schema;
    }
View Full Code Here

    /**
     * Signals to start producing events.
     */
    public void start() throws SAXException {
        Parser parser = Configuration.getDefaultParser();
        if (parser == null) {
            throw new SAXException("Unable to create parser");
        }

        DocumentHandler handler = new AttributeInterceptor();
        parser.setDocumentHandler(handler);
        try {
            parser.parse(new InputSource(_reader));
        } catch (IOException exception) {
            throw new SAXException(exception.getMessage(), exception);
        }
    }
View Full Code Here

        state.markAsProcessed(schemaLocation, importedSchema);

        if (alreadyLoaded) return;

        //-- Parser Schema
        Parser parser = null;
        try {
            parser = getSchemaContext().getParser();
        }
        catch(RuntimeException rte) {}
        if (parser == null) {
            throw new SchemaException("Error failed to create parser for import");
        }
    //-- Create Schema object and setup unmarshaller
    SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
          schemaUnmarshaller.setURIResolver(getURIResolver());
    schemaUnmarshaller.setSchema(importedSchema);
    Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
    parser.setDocumentHandler(handler);
    parser.setErrorHandler(handler);

        try {
            InputSource source = new InputSource(uri.getReader());
            source.setSystemId(uri.getAbsoluteURI());
            parser.parse(source);
        }
        catch(java.io.IOException ioe) {
            throw new SchemaException("Error reading import file '"+schemaLocation+"': "+ ioe);
        }
        catch(org.xml.sax.SAXException sx) {
View Full Code Here

    **/
    public Object unmarshal(InputSource source)
        throws MarshalException, ValidationException
    {
        XMLReader reader = null;
        Parser parser = null;
       
        //-- First try XMLReader
        try {
            reader = _internalContext.getXMLReader();
            if (entityResolver != null)
                reader.setEntityResolver(entityResolver);
        } catch (RuntimeException rx) {
          LOG.debug("Unable to create SAX XMLReader, attempting SAX Parser.");
        }
       
        if (reader == null) {
            parser = _internalContext.getParser();
            if (parser == null)
                throw new MarshalException("Unable to create SAX Parser.");
            if (entityResolver != null)
                parser.setEntityResolver(entityResolver);
        }


        UnmarshalHandler handler = createHandler();
       

        try {
            if (reader != null) {
                reader.setContentHandler(handler);
                reader.setErrorHandler(handler);
                reader.parse(source);
            }
            else {
                parser.setDocumentHandler(handler);
                parser.setErrorHandler(handler);
                parser.parse(source);
            }
        }
        catch (java.io.IOException ioe) {
            throw new MarshalException(ioe);
        }
View Full Code Here

TOP

Related Classes of org.xml.sax.Parser

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.