use of org.xml.sax.EntityResolver in project XobotOS by xamarin.
the class Properties method loadFromXML.
/**
* Loads the properties from an {@code InputStream} containing the
* properties in XML form. The XML document must begin with (and conform to)
* following DOCTYPE:
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* Also the content of the XML data must satisfy the DTD but the xml is not
* validated against it. The DTD is not loaded from the SYSTEM ID. After
* this method returns the InputStream is not closed.
*
* @param in the InputStream containing the XML document.
* @throws IOException in case an error occurs during a read operation.
* @throws InvalidPropertiesFormatException if the XML data is not a valid
* properties file.
*/
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
if (in == null) {
throw new NullPointerException();
}
if (builder == null) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new Error(e);
}
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
throw e;
}
public void error(SAXParseException e) throws SAXException {
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (systemId.equals(PROP_DTD_NAME)) {
InputSource result = new InputSource(new StringReader(PROP_DTD));
result.setSystemId(PROP_DTD_NAME);
return result;
}
throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
}
});
}
try {
Document doc = builder.parse(in);
NodeList entries = doc.getElementsByTagName("entry");
if (entries == null) {
return;
}
int entriesListLength = entries.getLength();
for (int i = 0; i < entriesListLength; i++) {
Element entry = (Element) entries.item(i);
String key = entry.getAttribute("key");
String value = entry.getTextContent();
/*
* key != null & value != null but key or(and) value can be
* empty String
*/
put(key, value);
}
} catch (IOException e) {
throw e;
} catch (SAXException e) {
throw new InvalidPropertiesFormatException(e);
}
}
use of org.xml.sax.EntityResolver in project Openfire by igniterealtime.
the class UserSchemaValidator method validateAndParse.
/**
* Perform a validate and a parse of the specified source document.
* Validating is done with the specified schemafiles.
*
* @return A Document when the validation s successful.
*/
Document validateAndParse() {
ValidatorErrorHandler handler = new ValidatorErrorHandler();
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setXIncludeAware(true);
documentBuilderFactory.setValidating(false);
// We don't want xml:base and xml:lang attributes in the output.
documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-language", false);
if (schemaSources.length > 0) {
Log.info("Checking Schema's");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setErrorHandler(handler);
Schema schema = schemaFactory.newSchema(schemaSources);
documentBuilderFactory.setSchema(schema);
Log.info("Start validating document");
} else {
Log.info("Loading document");
}
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
handler.reset();
// Communicate some info about the Xincludes in the imported file. These imports should be resolvable by the server.
documentBuilder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
Log.info(String.format("resolved Entity:%s %s", (publicId != null ? publicId : ""), systemId));
return null;
}
});
documentBuilder.setErrorHandler(handler);
Document result = documentBuilder.parse(source);
if (result != null && handler.isValid()) {
return result;
} else {
Log.warn(String.format("document is invalid. %1$d errors found.", handler.getNrOfErrors()));
return null;
}
} catch (Exception e) {
Log.warn(String.format("document validation failed. %1$d errors found.", handler.getNrOfErrors()));
Log.debug("", e);
return null;
}
}
use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.
the class JDOMUtil method getSaxBuilder.
private static SAXBuilder getSaxBuilder() {
SoftReference<SAXBuilder> reference = ourSaxBuilder.get();
SAXBuilder saxBuilder = com.intellij.reference.SoftReference.dereference(reference);
if (saxBuilder == null) {
saxBuilder = new SAXBuilder() {
@Override
protected void configureParser(XMLReader parser, SAXHandler contentHandler) throws JDOMException {
super.configureParser(parser, contentHandler);
try {
parser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (Exception ignore) {
}
}
};
saxBuilder.setEntityResolver(new EntityResolver() {
@Override
@NotNull
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new CharArrayReader(ArrayUtil.EMPTY_CHAR_ARRAY));
}
});
ourSaxBuilder.set(new SoftReference<SAXBuilder>(saxBuilder));
}
return saxBuilder;
}
use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.
the class ModelGen method loadXml.
public static Element loadXml(File configXml) throws Exception {
SAXBuilder saxBuilder = new SAXBuilder();
saxBuilder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new CharArrayReader(new char[0]));
}
});
final Document document = saxBuilder.build(configXml);
return document.getRootElement();
}
use of org.xml.sax.EntityResolver in project robovm by robovm.
the class Properties method loadFromXML.
/**
* Loads the properties from an {@code InputStream} containing the
* properties in XML form. The XML document must begin with (and conform to)
* following DOCTYPE:
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* Also the content of the XML data must satisfy the DTD but the xml is not
* validated against it. The DTD is not loaded from the SYSTEM ID. After
* this method returns the InputStream is not closed.
*
* @param in the InputStream containing the XML document.
* @throws IOException in case an error occurs during a read operation.
* @throws InvalidPropertiesFormatException if the XML data is not a valid
* properties file.
*/
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
if (in == null) {
throw new NullPointerException("in == null");
}
if (builder == null) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new Error(e);
}
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
throw e;
}
public void error(SAXParseException e) throws SAXException {
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (systemId.equals(PROP_DTD_NAME)) {
InputSource result = new InputSource(new StringReader(PROP_DTD));
result.setSystemId(PROP_DTD_NAME);
return result;
}
throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
}
});
}
try {
Document doc = builder.parse(in);
NodeList entries = doc.getElementsByTagName("entry");
if (entries == null) {
return;
}
int entriesListLength = entries.getLength();
for (int i = 0; i < entriesListLength; i++) {
Element entry = (Element) entries.item(i);
String key = entry.getAttribute("key");
String value = entry.getTextContent();
/*
* key != null & value != null but key or(and) value can be
* empty String
*/
put(key, value);
}
} catch (IOException e) {
throw e;
} catch (SAXException e) {
throw new InvalidPropertiesFormatException(e);
}
}
Aggregations