001: /*
002: * Created on 16.05.2005
003: *
004: */
005: package org.ontoware.aifbcommons.xml;
006:
007: import java.io.ByteArrayOutputStream;
008: import java.io.File;
009: import java.io.FileNotFoundException;
010: import java.io.FileOutputStream;
011: import java.io.FileReader;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.OutputStream;
015: import java.io.OutputStreamWriter;
016: import java.io.Reader;
017: import java.io.StringReader;
018: import java.io.Writer;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import javax.xml.transform.OutputKeys;
023: import javax.xml.transform.Transformer;
024: import javax.xml.transform.TransformerException;
025: import javax.xml.transform.TransformerFactory;
026: import javax.xml.transform.stream.StreamResult;
027: import javax.xml.transform.stream.StreamSource;
028:
029: import org.dom4j.Document;
030: import org.dom4j.DocumentException;
031: import org.dom4j.io.DOMReader;
032: import org.dom4j.io.DocumentResult;
033: import org.dom4j.io.DocumentSource;
034: import org.dom4j.io.OutputFormat;
035: import org.dom4j.io.SAXReader;
036: import org.dom4j.io.XMLWriter;
037: import org.dom4j.io.XPP3Reader;
038: import org.slf4j.Logger;
039: import org.slf4j.LoggerFactory;
040: import org.xmlpull.v1.XmlPullParserException;
041:
042: /**
043: * Helper class for XMl handling
044: *
045: * @author mvo
046: *
047: */
048: public class XMLUtils {
049:
050: private static Logger log = LoggerFactory.getLogger(XMLUtils.class);
051:
052: public static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
053:
054: /** loads from string as URL */
055: public static Document getAsXML(String url) {
056: assert url != null;
057: log.debug("loading xml from URL(?) '" + url + "'");
058: try {
059: return getAsXML(new URL(url));
060: } catch (MalformedURLException e) {
061: throw new RuntimeException(e);
062: }
063: }
064:
065: public static Document getAsXML(URL url) {
066: SAXReader reader = new SAXReader();
067: try {
068: return reader.read(url);
069: } catch (DocumentException e) {
070: log.info("Troublesome URL: " + url);
071: throw new RuntimeException(e);
072: }
073: }
074:
075: public static Document getAsXML(File file) {
076: assert file.exists() : file.getAbsoluteFile() + " not found";
077: try {
078: SAXReader xmlReader = new SAXReader();
079: return xmlReader.read(file);
080: } catch (DocumentException e) {
081: throw new RuntimeException(file.getAbsoluteFile()
082: + " threw " + e);
083: }
084: }
085:
086: public static Document getAsXML(InputStream is)
087: throws DocumentException, IOException,
088: XmlPullParserException {
089: XPP3Reader xr = new XPP3Reader();
090: // SAXReader sr = new SAXReader();
091: // sr.setEncoding("ISO-8859-1");
092: return xr.read(is);
093: }
094:
095: public static Document getAsXML(Reader r) throws DocumentException,
096: IOException, XmlPullParserException {
097: XPP3Reader xr = new XPP3Reader();
098: // SAXReader sr = new SAXReader();
099: // sr.setEncoding("ISO-8859-1");
100: return xr.read(r);
101: }
102:
103: public static Document loadXML(String xmlAsString) {
104: SAXReader reader = new SAXReader();
105: StringReader sr = new StringReader(xmlAsString);
106: try {
107: return reader.read(sr);
108: } catch (DocumentException e) {
109: log.warn("Could not parse: " + xmlAsString);
110: throw new RuntimeException(e);
111: }
112: }
113:
114: public static Document loadXMLFile(String filename) {
115: return loadXMLFile(new File(filename));
116: }
117:
118: public static Document loadXMLFile(File file) {
119: SAXReader reader = new SAXReader();
120: try {
121: FileReader fr = new FileReader(file);
122: return reader.read(fr);
123: } catch (DocumentException e) {
124: throw new RuntimeException(e);
125: } catch (FileNotFoundException e) {
126: throw new RuntimeException(e);
127: }
128: }
129:
130: /**
131: * procudes correct UTF8
132: *
133: * @param d
134: * @param f
135: * @throws IOException
136: */
137: public static void write(Document d, File f) throws IOException {
138: FileOutputStream fos = new FileOutputStream(f);
139: OutputStreamWriter osw = new OutputStreamWriter(fos, "utf8");
140: write(d, osw);
141: }
142:
143: public static void write(Document d, Writer w) throws IOException {
144: // TODO: check correct utf8 output
145: // prepare xml writer
146: OutputFormat fo = OutputFormat.createPrettyPrint();
147: fo.setEncoding("utf-8"); // XML encoding name
148: fo.setIndent(true);
149: fo.setIndent(" ");
150: fo.setTrimText(false);
151: fo.setXHTML(true);
152: XMLWriter xw = new XMLWriter(fo);
153: xw.setWriter(w);
154: xw.write(d);
155: xw.flush();
156: }
157:
158: public static void write(Document d, OutputStream out,
159: String outputCharset) throws IOException {
160: OutputStreamWriter outw = new OutputStreamWriter(out,
161: outputCharset);
162: write(d, outw);
163: }
164:
165: public static String xmldecode(String in) {
166: String result = in;
167: result = result.replace("&", "&");
168: result = result.replace("<", "<");
169: result = result.replace(">", ">");
170: result = result.replace("'", "'");
171: result = result.replace(""", "\"");
172: return result;
173:
174: }
175:
176: public static String xmlencode(String in) {
177: String result = in;
178: result = result.replace("&", "&");
179: result = result.replace("<", "<");
180: result = result.replace(">", ">");
181: result = result.replace("'", "'");
182: result = result.replace("\"", """);
183: return result;
184: }
185:
186: /**
187: * Streaming transformation
188: * @param in
189: * @param xslt
190: * @param out
191: * @throws TransformerException
192: */
193: public static void transform(Reader in, Document xslt, Writer out)
194: throws TransformerException {
195:
196: // create transformer
197: TransformerFactory factory = TransformerFactory.newInstance();
198: DocumentSource styleSource = new DocumentSource(xslt);
199: Transformer transformer = factory.newTransformer(styleSource);
200:
201: // perform transformation
202: StreamSource source = new StreamSource(in);
203: StreamResult result = new StreamResult(out);
204:
205: transformer.transform(source, result);
206: }
207:
208: /**
209: *
210: * @param in
211: * @param xslt
212: * @return 'in' styled with 'xslt'
213: * @throws TransformerException
214: * @throws IOException
215: */
216: public static Document transform(Document in, Document xslt)
217: throws TransformerException, IOException {
218: // create transformer
219: TransformerFactory factory = TransformerFactory.newInstance();
220: DocumentSource styleSource = new DocumentSource(xslt);
221: Transformer transformer = factory.newTransformer(styleSource);
222: // perform transformation
223: DocumentSource source = new DocumentSource(in);
224: DocumentResult result = new DocumentResult();
225: transformer.transform(source, result);
226: return result.getDocument();
227: }
228:
229: public static String transformToString(Document in, Document xslt)
230: throws TransformerException, IOException {
231: // create transformer
232: TransformerFactory factory = TransformerFactory.newInstance();
233: DocumentSource styleSource = new DocumentSource(xslt);
234: Transformer transformer = factory.newTransformer(styleSource);
235: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
236: // perform transformation
237: DocumentSource source = new DocumentSource(in);
238:
239: // StringWriter sw = new StringWriter();
240: // StreamResult result = new StreamResult(sw);
241:
242: ByteArrayOutputStream out = new ByteArrayOutputStream();
243: OutputStreamWriter outw = new OutputStreamWriter(out, "UTF-8");
244: StreamResult result = new StreamResult(outw);
245: transformer.transform(source, result);
246:
247: // return sw.getBuffer().toString();
248: return out.toString("UTF-8");
249:
250: }
251:
252: public static Document toDom4jDocument(
253: org.w3c.dom.Document w3cDomDocument) {
254: DOMReader xmlReader = new DOMReader();
255: return xmlReader.read(w3cDomDocument);
256: }
257:
258: }
|