Package javax.xml.parsers

Examples of javax.xml.parsers.SAXParser


            }

            documentsMRU.clear();

            // Parse the input
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(file, handler);

            GanttLookAndFeelInfo info = GanttLookAndFeels
                    .getGanttLookAndFeels().getInfoByClass(styleClass);
            if (null == info)
                info = GanttLookAndFeels.getGanttLookAndFeels().getInfoByName(


    public RSSReader(int maxsize, final InputStream stream, Type type) throws IOException {
        this(maxsize);
        this.type = type;
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            final SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(stream, this);
        } catch (SAXException e) {
            throw new IOException (e.getMessage());
        } catch (ParserConfigurationException e) {
            throw new IOException (e.getMessage());
        }

        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            parserFactory.setValidating(false);
            parserFactory.setNamespaceAware(false);

            SAXParser parser = parserFactory.newSAXParser();

            // call parsing
            parser.parse(in, this);

            this.exception = null;
            this.lineNumber = 0;
            this.columnNumber = 0;

                               URL baseUrl) throws IOException {
    try {
      SceneBase scene = new SceneBase();
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(false);
      SAXParser saxParser = factory.newSAXParser();
      saxParser.parse(in, new DAEHandler(scene, baseUrl));
      return scene;
    } catch (ParserConfigurationException ex) {
      IOException ex2 = new IOException("Can't parse XML stream");
      ex2.initCause(ex);
      throw ex2;

        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            parserFactory.setValidating(false);
            parserFactory.setNamespaceAware(false);

            SAXParser parser = parserFactory.newSAXParser();

            // call parsing
            parser.parse(in, handler);

            log.info("XML parsed in " + (System.currentTimeMillis() - startTime) + "ms.");
        } catch (Exception e) {
            throw new ParserException(e.getMessage(), e);
        }

            +"<root />"
            ;

        SAXParserFactory spf = new WstxSAXParserFactory();
        spf.setNamespaceAware(true);
        SAXParser sp = spf.newSAXParser();
        DefaultHandler h = new DefaultHandler();

        /* First: let's verify that we get an exception for
         * unresolved reference...
         */
        try {
            sp.parse(new InputSource(new StringReader(XML)), h);
        } catch (SAXException e) {
            verifyException(e, "No such file or directory");
        }

        // And then with dummy resolver; should work ok now
        sp = spf.newSAXParser();
        sp.getXMLReader().setEntityResolver(new MyResolver("   "));
        h = new DefaultHandler();
        try {
            sp.parse(new InputSource(new StringReader(XML)), h);
        } catch (SAXException e) {
            fail("Should not have failed with entity resolver, got ("+e.getClass()+"): "+e.getMessage());
        }
    }

    /**
     * Test for [WSTX_227]
     */
    public void testCData() throws Exception
    {
        SAXParser parser = new WstxSAXParser();
        StringBuffer buffer = new StringBuffer("<root><![CDATA[");
        for (int i=0; i<100000; i++) {
            buffer.append('a');
        }
        buffer.append("]]></root>");
        CDATASectionCounter handler = new CDATASectionCounter();
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        parser.parse(new InputSource(new StringReader(buffer.toString())), handler);
        // Should get as many cdata sections as text segments
        int cdatas = handler.getCDATASectionCount();
        int segments = handler.getSegmentCount();

        assertEquals("Should only get a single CDATA segments, got "+cdatas+" (for "+segments+" text segments)", 1, cdatas);

        throws Exception
    {
        // no need to test JAXP introspection...
        SAXParserFactory spf = new WstxSAXParserFactory();
        spf.setNamespaceAware(ns);
        SAXParser sp = spf.newSAXParser();
        MyHandler h = new MyHandler();

        InputSource src;

        if (useReader) {
            src = new InputSource(new StringReader(XML));
        } else {
            src = new InputSource(new ByteArrayInputStream(XML.getBytes("UTF-8")));
        }

        sp.parse(src, h);
        assertEquals(2, h._elems);
        assertEquals(1, h._attrs);
        assertEquals(11, h._charCount);
    }

        throws Exception
    {
        //SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParserFactory spf = new WstxSAXParserFactory();
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        SAXParser sp = spf.newSAXParser();
        MyContentHandler h = new MyContentHandler();
        sp.setProperty(SAXProperty.LEXICAL_HANDLER.toString(), /*DeclHandler*/h);
        // This shouldn't be needed -- nor seems to work for Xerces?
        //sp.getXMLReader().setEntityResolver(h);
        InputStream in = new FileInputStream(file);
        InputSource src = new InputSource(in);
        src.setSystemId(file.toURL().toExternalForm());
        sp.parse(src, h);
    }

     * @return The rule-set.
     */
    public RuleSet read(InputSource in) throws SAXException,
                                       IOException
    {
        SAXParser localParser = null;
        if ( this.parser == null )
        {
            SAXParserFactory factory = SAXParserFactory.newInstance( );
            factory.setNamespaceAware( true );

            String isValidatingString = System.getProperty( "drools.schema.validating" );
            if ( System.getProperty( "drools.schema.validating" ) != null )
            {
                this.isValidating = Boolean.getBoolean( "drools.schema.validating" );
            }

            if ( this.isValidating == true )
            {
                factory.setValidating( true );
                try
                {
                    localParser = factory.newSAXParser( );
                }
                catch ( ParserConfigurationException e )
                {
                    throw new RuntimeException( e.getMessage( ) );
                }

                try
                {
                    localParser.setProperty( JAXP_SCHEMA_LANGUAGE,
                                             W3C_XML_SCHEMA );
                }
                catch ( SAXNotRecognizedException e )
                {
                    boolean hideWarnings = Boolean.getBoolean( "drools.schema.hidewarnings" );
                    if ( !hideWarnings )
                    {
                        System.err.println( "Your SAX parser is not JAXP 1.2 compliant - turning off validation." );
                    }
                    localParser = null;
                }
            }

            if ( localParser == null )
            {
                // not jaxp1.2 compliant so turn off validation
                try
                {
                    this.isValidating = false;
                    factory.setValidating( this.isValidating );
                    localParser = factory.newSAXParser( );
                }
                catch ( ParserConfigurationException e )
                {
                    throw new RuntimeException( e.getMessage( ) );
                }
            }
        }
        else
        {
            localParser = this.parser;
        }

        if ( !localParser.isNamespaceAware( ) )
        {
            throw new RuntimeException( "parser must be namespace-aware" );
        }

        if ( this.repo == null )
        {
            try
            {
                this.repo = DefaultSemanticsRepository.getInstance( );
            }
            catch ( Exception e )
            {
                throw new SAXException( "Unable to reference a Semantics Repository:\n" + e.getMessage() );
            }
        }
        localParser.parse( in,
                           this );

        return this.ruleSet;
    }

TOP

Related Classes of javax.xml.parsers.SAXParser

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.