1 24 25 package org.objectweb.cjdbc.common.xml; 26 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.File ; 30 import java.io.FileReader ; 31 import java.io.FileWriter ; 32 import java.io.StringReader ; 33 import java.io.StringWriter ; 34 import java.net.URLDecoder ; 35 import java.util.Locale ; 36 import java.util.ResourceBundle ; 37 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.stream.StreamResult ; 41 import javax.xml.transform.stream.StreamSource ; 42 43 import org.objectweb.cjdbc.common.i18n.Translate; 44 import org.objectweb.cjdbc.common.log.Trace; 45 46 52 public final class XmlTools 53 { 54 55 56 static Trace logger = Trace.getLogger(XmlTools.class 57 .getName()); 58 59 60 private static TransformerFactory tFactory; 61 private static Transformer infoTransformer; 62 63 70 public static String prettyXml(String xml) throws Exception 71 { 72 return applyXsl(xml, "c-jdbc-pretty.xsl"); 73 } 74 75 82 public static String applyXsl(String xml, String xsl) 83 { 84 try 85 { 86 StringWriter result = new StringWriter (); 87 if (tFactory == null) 88 tFactory = TransformerFactory.newInstance(); 89 File infoXsl = internationalizeXsl(new File (URLDecoder 90 .decode(XmlTools.class.getResource("/" + xsl).getFile()))); 91 if (logger.isDebugEnabled()) 92 logger.debug(Translate.get("controller.xml.use.xsl", infoXsl)); 93 infoTransformer = tFactory.newTransformer(new StreamSource (infoXsl)); 95 infoTransformer.transform(new StreamSource (new StringReader (xml)), 96 new StreamResult (result)); 97 return result.toString(); 98 } 99 catch (Exception e) 100 { 101 String 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 117 public static File internationalizeXsl(File xsl) throws Exception 118 { 119 int point = xsl.getAbsolutePath().lastIndexOf('.'); 120 String xslPath = xsl.getAbsolutePath(); 121 xslPath = xslPath.substring(0, point) + "_" + Locale.getDefault() 122 + xslPath.substring(point); 123 File i18nXsl = new File (xslPath); 124 if (i18nXsl.exists() == false) 125 { 126 ResourceBundle rb = ResourceBundle.getBundle("c-jdbc-xsl"); 127 BufferedReader br = new BufferedReader (new FileReader (xsl)); 128 String xml = ""; 129 String oi18n = "<i18n>"; 130 String ci18n = "</i18n>"; 131 int oi18nl = oi18n.length(); 132 int ci18nl = oi18nl + 1; 133 StringBuffer buffer = new StringBuffer (); 134 String 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 ignore) 147 { 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 bw = new BufferedWriter (new FileWriter (i18nXsl)); 156 bw.write(buffer.toString()); 157 bw.flush(); 158 bw.close(); 159 } 160 return i18nXsl; 161 } 162 163 180 public static String insertCjdbcDoctype(String xml) 181 { 182 int index = xml.indexOf("?>"); 183 if (index < 0) 184 { 185 return xml; 186 } 187 String 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 212 public static String insertCjdbcControllerDoctype(String xml) 213 { 214 int index = xml.indexOf("?>"); 215 if (index < 0) 216 { 217 return xml; 218 } 219 String 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 |