KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > xml > XmlTools


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk.
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.common.xml;
24
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 import org.continuent.sequoia.common.i18n.Translate;
34 import org.continuent.sequoia.common.log.Trace;
35
36 /**
37  * This class defines a XmlTools
38  *
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @version 1.0
41  */

42 public final class XmlTools
43 {
44
45   /** Logger instance. */
46   static Trace logger = Trace
47                                                .getLogger("org.continuent.sequoia.common.xml");
48
49   private static final String JavaDoc PRETTIFY_XSL = "org/continuent/sequoia/common/xml/prettify.xsl";
50   
51   /** XSL Transformation */
52   private static TransformerFactory JavaDoc tFactory;
53   private static Transformer JavaDoc infoTransformer;
54
55   /**
56    * Indent xml with xslt
57    *
58    * @param xml to indent
59    * @return indented xml
60    * @throws Exception if an error occurs
61    */

62   public static String JavaDoc prettyXml(String JavaDoc xml) throws Exception JavaDoc
63   {
64     return applyXsl(xml, PRETTIFY_XSL);
65   }
66
67   /**
68    * Apply xslt to xml
69    *
70    * @param xml to transform
71    * @param xsl transformation to apply
72    * @return xml formatted string or error message
73    */

74   private static String JavaDoc applyXsl(String JavaDoc xml, String JavaDoc xsl)
75   {
76     try
77     {
78       StringWriter JavaDoc result = new StringWriter JavaDoc();
79       if (tFactory == null)
80         tFactory = TransformerFactory.newInstance();
81       if (logger.isDebugEnabled())
82         logger.debug(Translate.get("controller.xml.use.xsl", xsl));
83       // if(infoTransformer==null)
84
infoTransformer = tFactory.newTransformer(new StreamSource JavaDoc(ClassLoader
85           .getSystemResourceAsStream(xsl)));
86       infoTransformer.transform(new StreamSource JavaDoc(new StringReader JavaDoc(xml)),
87           new StreamResult JavaDoc(result));
88       return result.toString();
89     }
90     catch (Exception JavaDoc e)
91     {
92       String JavaDoc msg = Translate.get("controller.xml.transformation.failed", e);
93
94       if (logger.isDebugEnabled())
95         logger.debug(msg, e);
96       logger.error(msg, e);
97       return msg;
98     }
99   }
100
101   /**
102    * Insert a doctype in a XML file. Ugly hack: the DOCTYPE is inserted this way
103    * since the DOCTYPE is stripped from the xml when applying the pretty xsl
104    * stylesheet and I could not find a way to access it from within the xsl. Any
105    * suggestion is welcome...
106    *
107    * @param xml XML content
108    * @param doctype the DTD Doctype to insert
109    * @return the xml where the DTD doctype has been inserted so that the xml can
110    * be validated against this DTD
111    */

112   public static String JavaDoc insertDoctype(String JavaDoc xml, String JavaDoc doctype)
113   {
114     int index = xml.indexOf("?>");
115     if (index < 0)
116     {
117       return xml;
118     }
119     String JavaDoc xmlWithDoctype = xml.substring(0, index + 2);
120     xmlWithDoctype += "\n";
121     xmlWithDoctype += doctype;
122     xmlWithDoctype += "\n";
123     xmlWithDoctype += xml.substring(index + 3, xml.length());
124     return xmlWithDoctype;
125   }
126 }
Popular Tags