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());
}
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);
}
}
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);
}
}
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);
}
}
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"));
}
Aggregations