Search in sources :

Example 11 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project languagetool by languagetool-org.

the class XMLValidator method validateInternal.

private void validateInternal(String xml, String dtdPath, String docType) throws SAXException, IOException, ParserConfigurationException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser saxParser = factory.newSAXParser();
    //used for removing existing DOCTYPE from grammar.xml files
    String cleanXml = xml.replaceAll("<!DOCTYPE.+>", "");
    String decl = "<?xml version=\"1.0\"";
    String endDecl = "?>";
    URL dtdUrl = this.getClass().getResource(dtdPath);
    if (dtdUrl == null) {
        throw new RuntimeException("DTD not found in classpath: " + dtdPath);
    }
    String dtd = "<!DOCTYPE " + docType + " PUBLIC \"-//W3C//DTD Rules 0.1//EN\" \"" + dtdUrl + "\">";
    int pos = cleanXml.indexOf(decl);
    int endPos = cleanXml.indexOf(endDecl);
    if (pos == -1) {
        throw new IOException("No XML declaration found in '" + cleanXml.substring(0, Math.min(100, cleanXml.length())) + "...'");
    }
    String newXML = cleanXml.substring(0, endPos + endDecl.length()) + "\r\n" + dtd + cleanXml.substring(endPos + endDecl.length());
    InputSource is = new InputSource(new StringReader(newXML));
    saxParser.parse(is, new ErrorHandler());
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) URL(java.net.URL) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 12 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project AndEngine by nicolasgramlich.

the class LevelLoader method loadLevelFromStream.

public void loadLevelFromStream(final InputStream pInputStream) throws IOException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        this.onBeforeLoadLevel();
        final LevelParser levelParser = new LevelParser(this.mDefaultEntityLoader, this.mEntityLoaders);
        xr.setContentHandler(levelParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        this.onAfterLoadLevel();
    } catch (final SAXException se) {
        Debug.e(se);
    /* Doesn't happen. */
    } catch (final ParserConfigurationException pe) {
        Debug.e(pe);
    /* Doesn't happen. */
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 13 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project AndEngine by nicolasgramlich.

the class TexturePackLoader method load.

public TexturePack load(final InputStream pInputStream, final String pAssetBasePath) throws TexturePackParseException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        final TexturePackParser texturePackParser = new TexturePackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
        xr.setContentHandler(texturePackParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        return texturePackParser.getTexturePack();
    } catch (final SAXException e) {
        throw new TexturePackParseException(e);
    } catch (final ParserConfigurationException pe) {
        /* Doesn't happen. */
        return null;
    } catch (final IOException e) {
        throw new TexturePackParseException(e);
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) TexturePackParseException(org.andengine.util.texturepack.exception.TexturePackParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 14 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project AndEngine by nicolasgramlich.

the class AnimationPackLoader method load.

public AnimationPack load(final InputStream pInputStream, final String pAssetBasePath) throws AnimationPackParseException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        final AnimationPackParser animationPackParser = new AnimationPackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
        xr.setContentHandler(animationPackParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        return animationPackParser.getAnimationPack();
    } catch (final SAXException e) {
        throw new AnimationPackParseException(e);
    } catch (final ParserConfigurationException pe) {
        /* Doesn't happen. */
        return null;
    } catch (final IOException e) {
        throw new AnimationPackParseException(e);
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) AnimationPackParseException(org.andengine.util.animationpack.exception.AnimationPackParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 15 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project robovm by robovm.

the class SimpleParserTest method testWorkingFile1.

public void testWorkingFile1() throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    SAXParser parser = factory.newSAXParser();
    parser.getXMLReader().setContentHandler(contentHandler);
    parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"), (DefaultHandler) null);
    assertEquals("The:quick,brown:fox", instructions.toString());
    assertEquals("stuff,nestedStuff,nestedStuff,nestedStuff", elements1.toString());
    assertEquals("Some text here,some more here...", text.toString());
    assertEquals("eins", attributes1.get("one"));
    assertEquals("zwei", attributes1.get("two"));
    assertEquals("drei", attributes1.get("three"));
    assertEquals("http://www.foobar.org", namespaces1.get("stuff"));
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9