package net.pms.xmlwise;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.IOException;
import java.io.StringReader;
import java.io.File;
/**
* Xmlwise convenience methods for loading xml documents and render them into
* XmlElement trees.
*
* @author Christoffer Lerno
*/
public class Xmlwise {
private Xmlwise() {
}
/**
* Loads an XML document ignoring DTD-validation.
*
* @param file the file to read from.
* @return an XML document.
* @throws IOException if we fail to load the file.
* @throws XmlParseException if there is a problem parsing the xml in the file.
*/
public static Document loadDocument(File file) throws IOException, XmlParseException {
return loadDocument(file, false, false);
}
/**
* Loads an XML document.
*
* @param file the file to read from.
* @param validate if we should validate the document or not.
* @param loadExternalDTD true to allow loading of external dtds.
* @return an XML document.
* @throws IOException if we fail to load the file.
* @throws XmlParseException if there is a problem parsing the xml in the file.
*/
public static Document loadDocument(File file, boolean validate, boolean loadExternalDTD) throws IOException, XmlParseException {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
loadExternalDTD);
documentBuilderFactory.setValidating(validate);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
return builder.parse(file);
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new XmlParseException(e);
}
}
/**
* Creates a DOM Document from the specified XML string, ignoring DTD-validation.
*
* @param xml a valid XML document, ie the String can't be null or empty
* @param validate if we should validate the document or not.
* @param loadExternalDTD true to allow loading of external dtds.
* @return the Document
object for the specified string.
* @throws XmlParseException if we fail to parse the XML.
*/
public static Document createDocument(String xml, boolean validate, boolean loadExternalDTD) throws XmlParseException {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
loadExternalDTD);
documentBuilderFactory.setValidating(validate);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xml)));
} catch (Exception e) {
throw new XmlParseException(e);
}
}
/**
* Creates a DOM Document from the specified XML string, ignoring DTD-validation.
*
* @param xml a valid XML document, ie the String can't be null or empty
* @return the Document
object for the specified string.
* @throws XmlParseException if we fail to parse the XML.
*/
public static Document createDocument(String xml) throws XmlParseException {
return createDocument(xml, false, false);
}
/**
* Escapes a string to be used in an xml document.
*
* The following replacements are made: *
*
& | & |
< | < |
> | > |
" | " |
' | ' |