KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > xml > XmlTools


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.xml;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.net.URLDecoder JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 import javax.xml.transform.Transformer JavaDoc;
39 import javax.xml.transform.TransformerFactory JavaDoc;
40 import javax.xml.transform.stream.StreamResult JavaDoc;
41 import javax.xml.transform.stream.StreamSource JavaDoc;
42
43 import org.objectweb.cjdbc.common.i18n.Translate;
44 import org.objectweb.cjdbc.common.log.Trace;
45
46 /**
47  * This class defines a XmlTools
48  *
49  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
50  * @version 1.0
51  */

52 public final class XmlTools
53 {
54
55   /** Logger instance. */
56   static Trace logger = Trace.getLogger(XmlTools.class
57                                                .getName());
58
59   /** XSL Transformation */
60   private static TransformerFactory JavaDoc tFactory;
61   private static Transformer JavaDoc infoTransformer;
62
63   /**
64    * Indent xml with xslt
65    *
66    * @param xml to indent
67    * @return indented xml
68    * @throws Exception if an error occurs
69    */

70   public static String JavaDoc prettyXml(String JavaDoc xml) throws Exception JavaDoc
71   {
72     return applyXsl(xml, "c-jdbc-pretty.xsl");
73   }
74
75   /**
76    * Apply xslt to xml
77    *
78    * @param xml to transform
79    * @param xsl transformation to apply
80    * @return xml formatted string or error message
81    */

82   public static String JavaDoc applyXsl(String JavaDoc xml, String JavaDoc xsl)
83   {
84     try
85     {
86       StringWriter JavaDoc result = new StringWriter JavaDoc();
87       if (tFactory == null)
88         tFactory = TransformerFactory.newInstance();
89       File JavaDoc infoXsl = internationalizeXsl(new File JavaDoc(URLDecoder
90           .decode(XmlTools.class.getResource("/" + xsl).getFile())));
91       if (logger.isDebugEnabled())
92         logger.debug(Translate.get("controller.xml.use.xsl", infoXsl));
93       //if(infoTransformer==null)
94
infoTransformer = tFactory.newTransformer(new StreamSource JavaDoc(infoXsl));
95       infoTransformer.transform(new StreamSource JavaDoc(new StringReader JavaDoc(xml)),
96           new StreamResult JavaDoc(result));
97       return result.toString();
98     }
99     catch (Exception JavaDoc e)
100     {
101       String JavaDoc msg = Translate.get("controller.xml.transformation.failed", e);
102
103       if (logger.isDebugEnabled())
104         logger.debug(msg, e);
105       logger.error(msg);
106       return msg;
107     }
108   }
109
110   /**
111    * Transform the xsl file so that it is internationalized.
112    *
113    * @param xsl xsl file
114    * @return internationalized file
115    * @throws Exception if an error occurs
116    */

117   public static File JavaDoc internationalizeXsl(File JavaDoc xsl) throws Exception JavaDoc
118   {
119     int point = xsl.getAbsolutePath().lastIndexOf('.');
120     String JavaDoc xslPath = xsl.getAbsolutePath();
121     xslPath = xslPath.substring(0, point) + "_" + Locale.getDefault()
122         + xslPath.substring(point);
123     File JavaDoc i18nXsl = new File JavaDoc(xslPath);
124     if (i18nXsl.exists() == false)
125     {
126       ResourceBundle JavaDoc rb = ResourceBundle.getBundle("c-jdbc-xsl");
127       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(xsl));
128       String JavaDoc xml = "";
129       String JavaDoc oi18n = "<i18n>";
130       String JavaDoc ci18n = "</i18n>";
131       int oi18nl = oi18n.length();
132       int ci18nl = oi18nl + 1;
133       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
134       String JavaDoc i18n = "";
135       while ((xml = br.readLine()) != null)
136       {
137         int indexOpen = 0, indexClose = 0;
138         while ((indexOpen = xml.indexOf(oi18n)) != -1)
139         {
140           indexClose = xml.indexOf(ci18n);
141           i18n = xml.substring(indexOpen + oi18nl, indexClose).trim();
142           try
143           {
144             i18n = rb.getString(i18n);
145           }
146           catch (Exception JavaDoc ignore)
147           // if the key has no match return the key itself
148
{
149           }
150           xml = xml.substring(0, indexOpen) + i18n
151               + xml.substring(indexClose + ci18nl);
152         }
153         buffer.append(xml + System.getProperty("line.separator"));
154       }
155       BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new FileWriter JavaDoc(i18nXsl));
156       bw.write(buffer.toString());
157       bw.flush();
158       bw.close();
159     }
160     return i18nXsl;
161   }
162
163   /**
164    * Insert C-JDBC DOCTYPE in a XML file.
165    * Ugly hack: the DOCTYPE is inserted this way since the DOCTYPE
166    * is stripped from the xml when applying the pretty xsl stylesheet
167    * and I could not find a way to access it from within the xsl.
168    * Any suggestion is welcome...
169    *
170    * Insert C-JDBC DOCTYPE after the &lt;?xml ... ?&gt; and before the
171    * rest of the content.
172    *
173    * @param xml XML content
174    *
175    * @return the xml where the C-JDBC DOCTYPE has been inserted so that the xml
176    * can be validated against this DTD
177    *
178    * @see XmlComponent.DOCTYPE_DB
179    */

180   public static String JavaDoc insertCjdbcDoctype(String JavaDoc xml)
181   {
182     int index = xml.indexOf("?>");
183     if (index < 0)
184     {
185       return xml;
186     }
187     String JavaDoc xmlWithDoctype = xml.substring(0, index+2);
188     xmlWithDoctype += "\n";
189     xmlWithDoctype += XmlComponent.DOCTYPE_DB;
190     xmlWithDoctype += "\n";
191     xmlWithDoctype += xml.substring(index+3, xml.length());
192     return xmlWithDoctype;
193   }
194   
195   /**
196    * Insert C-JDBC-CONTROLLER DOCTYPE in a XML file.
197    * Ugly hack: the DOCTYPE is inserted this way since the DOCTYPE
198    * is stripped from the xml when applying the pretty xsl stylesheet
199    * and I could not find a way to access it from within the xsl.
200    * Any suggestion is welcome...
201    *
202    * Insert C-JDBC-CONTROLLER DOCTYPE after the &lt;?xml ... ?&gt; and before the
203    * rest of the content.
204    *
205    * @param xml XML content
206    *
207    * @return the xml where the C-JDBC-CONTROLLER DOCTYPE has been inserted so that the xml
208    * can be validated against this DTD
209    *
210    * @see XmlComponent.DOCTYPE_CONTROLLER
211    */

212   public static String JavaDoc insertCjdbcControllerDoctype(String JavaDoc xml)
213   {
214     int index = xml.indexOf("?>");
215     if (index < 0)
216     {
217       return xml;
218     }
219     String JavaDoc xmlWithDoctype = xml.substring(0, index+2);
220     xmlWithDoctype += "\n";
221     xmlWithDoctype += XmlComponent.DOCTYPE_CONTROLLER;
222     xmlWithDoctype += "\n";
223     xmlWithDoctype += xml.substring(index+3, xml.length());
224     return xmlWithDoctype;
225   }
226 }
Popular Tags