use of org.xml.sax.EntityResolver in project metagenomicsTools by afodor.
the class GenbankXMLParser method main.
/**
* Author: anthony.fodor@gmail.com
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version,
* provided that any use properly credits the author.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details at http://www.gnu.org * *
*/
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dbf.setValidating(false);
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) throws SAXException, java.io.IOException {
return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
}
});
Document document = db.parse(ConfigReader.getGenbankCacheDir() + File.separator + "D37875.xml");
Element element = document.getDocumentElement();
// printNodeAndChildren(element, 0);
Holder h = new Holder();
getFirstGBSeq_Definition(element, h);
System.out.println(h.returnVal);
}
use of org.xml.sax.EntityResolver in project Mycat_plus by coderczp.
the class ConfigUtil method getDocument.
public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(dtd);
}
});
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) {
}
@Override
public void error(SAXParseException e) throws SAXException {
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
return builder.parse(xml);
}
use of org.xml.sax.EntityResolver in project motech by motech.
the class Log4JBundleLoader method loadBundle.
public void loadBundle(Bundle bundle) throws BundleConfigurationLoadingException, IOException {
String symbolicName = bundle.getSymbolicName();
LOGGER.debug("Looking for log4j config in {}", symbolicName);
URL log4jUrl = bundle.getResource(log4JConf);
if (log4jUrl != null) {
LOGGER.debug("Log4j config found in {}, loading", symbolicName);
InputStream log4jStream = null;
try {
URLConnection conn = log4jUrl.openConnection();
log4jStream = conn.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
Document log4jDoc = db.parse(log4jStream);
if (loggers != null && checkLogXmlConfiguration(log4jDoc)) {
PropertyConfigurator.configure(loggerProperties);
} else {
DOMConfigurator.configure(log4jDoc.getDocumentElement());
}
logService.reconfigure();
LOGGER.debug("Added log4j configuration for [" + bundle.getLocation() + "]");
} catch (ParserConfigurationException | SAXException e) {
throw new BundleConfigurationLoadingException("Error while loading log4j configuration from " + bundle, e);
} finally {
IOUtils.closeQuietly(log4jStream);
}
}
}
use of org.xml.sax.EntityResolver in project validator by validator.
the class DmozDriver method main.
/**
*/
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
XMLReader parser = factory.newSAXParser().getXMLReader();
DmozHandler domzHandler = new DmozHandler();
parser.setContentHandler(domzHandler);
parser.setFeature("http://xml.org/sax/features/string-interning", true);
parser.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
InputSource is = new InputSource(new ByteArrayInputStream(new byte[0]));
is.setPublicId(publicId);
is.setSystemId(systemId);
return is;
}
});
parser.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
}
public void fatalError(SAXParseException exception) throws SAXException {
}
public void warning(SAXParseException exception) throws SAXException {
}
});
InputSource is = new InputSource(new GZIPInputStream(new FileInputStream(args[0])));
parser.parse(is);
Writer out = new OutputStreamWriter(new FileOutputStream(args[1]), "utf-8");
for (Map.Entry<String, String> entry : domzHandler.getTheMap().entrySet()) {
out.write(entry.getKey());
out.write('\t');
out.write(entry.getValue());
out.write('\n');
}
}
use of org.xml.sax.EntityResolver in project maven-plugins by apache.
the class XmlAppendingTransformer method processResource.
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
Document r;
try {
SAXBuilder builder = new SAXBuilder(false);
builder.setExpandEntities(false);
if (ignoreDtd) {
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
r = builder.build(is);
} catch (JDOMException e) {
throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
}
if (doc == null) {
doc = r;
} else {
Element root = r.getRootElement();
for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
Attribute a = itr.next();
itr.remove();
Element mergedEl = doc.getRootElement();
Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
if (mergedAtt == null) {
mergedEl.setAttribute(a);
}
}
for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
Content n = itr.next();
itr.remove();
doc.getRootElement().addContent(n);
}
}
}
Aggregations