01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19:
20: package de.schlund.pfixcore.webservice.utils;
21:
22: import java.io.StringReader;
23: import java.io.StringWriter;
24:
25: import javax.xml.transform.Templates;
26: import javax.xml.transform.Transformer;
27: import javax.xml.transform.TransformerFactory;
28: import javax.xml.transform.stream.StreamResult;
29: import javax.xml.transform.stream.StreamSource;
30:
31: /**
32: * @author mleidig@schlund.de
33: */
34: public class XMLFormatter {
35:
36: final static String XSL = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
37: + " <xsl:output method=\"xml\" indent=\"yes\"/>"
38: + " <xsl:strip-space elements=\"*\"/>"
39: + " <xsl:template match=\"/\">"
40: + " <xsl:copy-of select=\".\"/>"
41: + " </xsl:template>"
42: + "</xsl:stylesheet>";
43:
44: static Templates templates;
45:
46: static synchronized Templates getTemplates() throws Exception {
47: if (templates == null) {
48: TransformerFactory tf = TransformerFactory.newInstance();
49: templates = tf.newTemplates(new StreamSource(
50: new StringReader(XSL)));
51: }
52: return templates;
53: }
54:
55: public static String format(String message) {
56: try {
57: StringWriter sw = new StringWriter();
58: Transformer t = getTemplates().newTransformer();
59: t.transform(new StreamSource(new StringReader(message)),
60: new StreamResult(sw));
61: return sw.toString();
62: } catch (Exception x) {
63: //return unmodified message if formatting fails
64: return message;
65: }
66: }
67:
68: }
|