001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk.
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.common.xml;
022:
023: import java.io.StringReader;
024: import java.io.StringWriter;
025:
026: import javax.xml.transform.Transformer;
027: import javax.xml.transform.TransformerFactory;
028: import javax.xml.transform.stream.StreamResult;
029: import javax.xml.transform.stream.StreamSource;
030:
031: import org.continuent.sequoia.common.i18n.Translate;
032: import org.continuent.sequoia.common.log.Trace;
033:
034: /**
035: * This class defines a XmlTools
036: *
037: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
038: * @version 1.0
039: */
040: public final class XmlTools {
041:
042: /** Logger instance. */
043: static Trace logger = Trace
044: .getLogger("org.continuent.sequoia.common.xml");
045:
046: private static final String PRETTIFY_XSL = "org/continuent/sequoia/common/xml/prettify.xsl";
047:
048: /** XSL Transformation */
049: private static TransformerFactory tFactory;
050: private static Transformer infoTransformer;
051:
052: /**
053: * Indent xml with xslt
054: *
055: * @param xml to indent
056: * @return indented xml
057: * @throws Exception if an error occurs
058: */
059: public static String prettyXml(String xml) throws Exception {
060: return applyXsl(xml, PRETTIFY_XSL);
061: }
062:
063: /**
064: * Apply xslt to xml
065: *
066: * @param xml to transform
067: * @param xsl transformation to apply
068: * @return xml formatted string or error message
069: */
070: private static String applyXsl(String xml, String xsl) {
071: try {
072: StringWriter result = new StringWriter();
073: if (tFactory == null)
074: tFactory = TransformerFactory.newInstance();
075: if (logger.isDebugEnabled())
076: logger.debug(Translate.get("controller.xml.use.xsl",
077: xsl));
078: // if(infoTransformer==null)
079: infoTransformer = tFactory.newTransformer(new StreamSource(
080: ClassLoader.getSystemResourceAsStream(xsl)));
081: infoTransformer.transform(new StreamSource(
082: new StringReader(xml)), new StreamResult(result));
083: return result.toString();
084: } catch (Exception e) {
085: String msg = Translate.get(
086: "controller.xml.transformation.failed", e);
087:
088: if (logger.isErrorEnabled())
089: logger.error(msg, e);
090: return msg;
091: }
092: }
093:
094: /**
095: * Insert a doctype in a XML file. Ugly hack: the DOCTYPE is inserted this way
096: * since the DOCTYPE is stripped from the xml when applying the pretty xsl
097: * stylesheet and I could not find a way to access it from within the xsl. Any
098: * suggestion is welcome...
099: *
100: * @param xml XML content
101: * @param doctype the DTD Doctype to insert
102: * @return the xml where the DTD doctype has been inserted so that the xml can
103: * be validated against this DTD
104: */
105: public static String insertDoctype(String xml, String doctype) {
106: int index = xml.indexOf("?>");
107: if (index < 0) {
108: return xml;
109: }
110: String xmlWithDoctype = xml.substring(0, index + 2);
111: xmlWithDoctype += "\n";
112: xmlWithDoctype += doctype;
113: xmlWithDoctype += "\n";
114: xmlWithDoctype += xml.substring(index + 3, xml.length());
115: return xmlWithDoctype;
116: }
117: }
|