Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

XmlTools.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.xml;
00026 
00027 import java.io.BufferedReader;
00028 import java.io.BufferedWriter;
00029 import java.io.File;
00030 import java.io.FileReader;
00031 import java.io.FileWriter;
00032 import java.io.StringReader;
00033 import java.io.StringWriter;
00034 import java.net.URLDecoder;
00035 import java.util.Locale;
00036 import java.util.ResourceBundle;
00037 
00038 import javax.xml.transform.Transformer;
00039 import javax.xml.transform.TransformerFactory;
00040 import javax.xml.transform.stream.StreamResult;
00041 import javax.xml.transform.stream.StreamSource;
00042 
00043 import org.objectweb.cjdbc.common.i18n.Translate;
00044 import org.objectweb.cjdbc.common.log.Trace;
00045 
00046 /**
00047  * This class defines a XmlTools
00048  * 
00049  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00050  * @version 1.0
00051  */
00052 public final class XmlTools
00053 {
00054 
00055   /** Logger instance. */
00056   static Trace                      logger = Trace.getLogger(XmlTools.class
00057                                                .getName());
00058 
00059   /** XSL Transformation */
00060   private static TransformerFactory tFactory;
00061   private static Transformer        infoTransformer;
00062 
00063   /**
00064    * Indent xml with xslt
00065    * 
00066    * @param xml to indent
00067    * @return indented xml
00068    * @throws Exception if an error occurs
00069    */
00070   public static String prettyXml(String xml) throws Exception
00071   {
00072     return applyXsl(xml, "c-jdbc-pretty.xsl");
00073   }
00074 
00075   /**
00076    * Apply xslt to xml
00077    * 
00078    * @param xml to transform
00079    * @param xsl transformation to apply
00080    * @return xml formatted string or error message
00081    */
00082   public static String applyXsl(String xml, String xsl)
00083   {
00084     try
00085     {
00086       StringWriter result = new StringWriter();
00087       if (tFactory == null)
00088         tFactory = TransformerFactory.newInstance();
00089       File infoXsl = internationalizeXsl(new File(URLDecoder
00090           .decode(XmlTools.class.getResource("/" + xsl).getFile())));
00091       if (logger.isDebugEnabled())
00092         logger.debug(Translate.get("controller.xml.use.xsl", infoXsl));
00093       //if(infoTransformer==null)
00094       infoTransformer = tFactory.newTransformer(new StreamSource(infoXsl));
00095       infoTransformer.transform(new StreamSource(new StringReader(xml)),
00096           new StreamResult(result));
00097       return result.toString();
00098     }
00099     catch (Exception e)
00100     {
00101       String msg = Translate.get("controller.xml.transformation.failed", e);
00102 
00103       if (logger.isDebugEnabled())
00104         logger.debug(msg, e);
00105       logger.error(msg);
00106       return msg;
00107     }
00108   }
00109 
00110   /**
00111    * Transform the xsl file so that it is internationalized.
00112    * 
00113    * @param xsl xsl file
00114    * @return internationalized file
00115    * @throws Exception if an error occurs
00116    */
00117   public static File internationalizeXsl(File xsl) throws Exception
00118   {
00119     int point = xsl.getAbsolutePath().lastIndexOf('.');
00120     String xslPath = xsl.getAbsolutePath();
00121     xslPath = xslPath.substring(0, point) + "_" + Locale.getDefault()
00122         + xslPath.substring(point);
00123     File i18nXsl = new File(xslPath);
00124     if (i18nXsl.exists() == false)
00125     {
00126       ResourceBundle rb = ResourceBundle.getBundle("c-jdbc-xsl");
00127       BufferedReader br = new BufferedReader(new FileReader(xsl));
00128       String xml = "";
00129       String oi18n = "<i18n>";
00130       String ci18n = "</i18n>";
00131       int oi18nl = oi18n.length();
00132       int ci18nl = oi18nl + 1;
00133       StringBuffer buffer = new StringBuffer();
00134       String i18n = "";
00135       while ((xml = br.readLine()) != null)
00136       {
00137         int indexOpen = 0, indexClose = 0;
00138         while ((indexOpen = xml.indexOf(oi18n)) != -1)
00139         {
00140           indexClose = xml.indexOf(ci18n);
00141           i18n = xml.substring(indexOpen + oi18nl, indexClose).trim();
00142           try
00143           {
00144             i18n = rb.getString(i18n);
00145           }
00146           catch (Exception ignore)
00147           // if the key has no match return the key itself
00148           {
00149           }
00150           xml = xml.substring(0, indexOpen) + i18n
00151               + xml.substring(indexClose + ci18nl);
00152         }
00153         buffer.append(xml + System.getProperty("line.separator"));
00154       }
00155       BufferedWriter bw = new BufferedWriter(new FileWriter(i18nXsl));
00156       bw.write(buffer.toString());
00157       bw.flush();
00158       bw.close();
00159     }
00160     return i18nXsl;
00161   }
00162 
00163 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1