1 23 24 import java.util.*; 25 import java.io.*; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Attr ; 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.FactoryConfigurationError ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.dom.DOMSource ; 37 import javax.xml.transform.stream.StreamResult ; 38 39 40 public class LocalStringsHelper{ 41 public static void main(String [] args){ 42 try{ 43 checkArgs(args); 44 if(args[0].equals("-map")){ 45 map(args); 46 }else if(args[0].equals("-update")){ 47 update(args); 48 }else if(args[0].equals("-transform")){ 49 transform(args); 50 } 51 }catch(Exception e){ 52 e.printStackTrace(); 53 System.exit(1); 54 } 55 } 56 57 private static void checkArgs(String [] args){ 58 if(!(args.length==4 && 59 (args[0].equals("-map") || args[0].equals("-update") || args[0].equals("-transform")))){ 60 System.out.println ("java "+LocalStringsHelper.class.getName()+" -map <LocalStrings.properties> <starting pattern, e.g. com.sun.enterprise.tools.verifier.tests.ejb> <output XML file name>"); 61 System.out.println ("OR"); 62 System.out.println ("java "+LocalStringsHelper.class.getName()+" -update <input LocalStrings.properties> <output LocalStrings.properties> <input mapping XML file name>"); 63 System.out.println ("OR"); 64 System.out.println ("java "+LocalStringsHelper.class.getName()+" -transform <input XML> <input XSLT> <output file name>"); 65 System.exit(1); 66 } 67 } 68 69 private static void map(String [] args) throws Exception { 70 Properties prop=new Properties(); 71 prop.load(new FileInputStream(args[1])); 72 String bp= args[2]; map(prop, bp, new FileOutputStream(args[3])); 74 } 75 76 private static void update(String [] args) throws Exception { 77 BufferedReader in=new BufferedReader(new FileReader(args[1])); 78 PrintWriter out=new PrintWriter(new FileOutputStream(args[2]), true); 79 Properties mappings=new Properties(); 80 mappings.load(new FileInputStream(args[3])); 81 updateLocalStrings(in,out,mappings); 82 } 83 84 private static void transform(String [] args) throws Exception { 85 FileInputStream xmlIn=new FileInputStream(args[1]); 86 FileInputStream xsltIn=new FileInputStream(args[2]); 87 FileOutputStream out=new FileOutputStream(args[3]); 88 transform(xmlIn, xsltIn, out); 89 } 90 91 public static void map(Properties prop, String bp, OutputStream out) throws Exception { 93 Document doc=getTestToAssertionMapping(prop, bp); 94 TransformerFactory tf=TransformerFactory.newInstance(); 95 Transformer t=tf.newTransformer(); 96 t.transform(new DOMSource (doc), new StreamResult (new PrintWriter(out))); 97 } 98 99 public static List getTestNames(Properties prop, String bp) throws Exception { 102 List testNames=new ArrayList(); 103 String ep= ".assertion"; 104 for(Enumeration e=prop.propertyNames();e.hasMoreElements();){ 105 String cur=(String )e.nextElement(); 106 if(cur.startsWith(bp) && cur.endsWith(ep)){ 107 testNames.add(cur.substring(0, cur.indexOf(ep))); 108 } 109 } 110 Collections.sort(testNames); 111 return testNames; 112 } 113 114 public static Document getTestToAssertionMapping(Properties prop, String bp) throws Exception { 116 List testNames=getTestNames(prop, bp); 117 Document doc=createDOM(bp); 118 for(Iterator iter=testNames.iterator();iter.hasNext();){ 119 String name=(String )iter.next(); 120 String assertion=prop.getProperty(name+".assertion",""); 121 String mapping=prop.getProperty(name+".specMappingInfo",""); 122 addTestDetails(doc,name,assertion, mapping); 123 } 124 return doc; 125 } 126 127 private static Document createDOM(String bp) throws Exception { 128 DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); 129 DocumentBuilder builder = factory.newDocumentBuilder(); 130 Document doc= builder.newDocument(); 131 Element tests= doc.createElement("Tests"); 132 Attr bpattern= doc.createAttribute("BeginningPattern"); 133 bpattern.setValue(bp); 134 tests.setAttributeNode(bpattern); 135 doc.appendChild(tests); 136 tests.appendChild(doc.createTextNode("\n")); 137 return doc; 138 } 139 140 private static void addTestDetails(Document doc, String testName, String assertion, String specMappingInfo) throws Exception { 142 Element testNode=doc.createElement("Test"); 143 doc.getDocumentElement().appendChild(testNode); 144 testNode.appendChild(doc.createTextNode("\n")); 145 146 String bp=doc.getElementsByTagName("Tests").item(0).getAttributes().getNamedItem("BeginningPattern").getNodeValue(); 147 Node nameNode=doc.createElement("Name"); 148 nameNode.appendChild(doc.createTextNode(testName.trim().substring(bp.length()))); 149 testNode.appendChild(nameNode); 150 testNode.appendChild(doc.createTextNode("\n")); 151 152 Node assertionNode=doc.createElement("Assertion"); 153 assertionNode.appendChild(doc.createTextNode(assertion.trim())); 154 testNode.appendChild(assertionNode); 155 testNode.appendChild(doc.createTextNode("\n")); 156 157 Node specNode=doc.createElement("SpecMappingInfo"); 158 specMappingInfo=specMappingInfo.trim(); 159 specMappingInfo=specMappingInfo.length()==0? "Not yet mapped":specMappingInfo; 160 specNode.appendChild(doc.createTextNode(specMappingInfo)); 161 testNode.appendChild(specNode); 162 testNode.appendChild(doc.createTextNode("\n")); 163 164 testNode.getParentNode().appendChild(doc.createTextNode("\n")); 165 } 166 167 public static void transform(InputStream xmlIn, InputStream xsltIn, OutputStream out) throws Exception { 168 javax.xml.transform.Source xmlSource = 169 new javax.xml.transform.stream.StreamSource (xmlIn); 170 javax.xml.transform.Source xsltSource = 171 new javax.xml.transform.stream.StreamSource (xsltIn); 172 javax.xml.transform.Result result = 173 new javax.xml.transform.stream.StreamResult (out); 174 175 javax.xml.transform.TransformerFactory transFact = 177 javax.xml.transform.TransformerFactory.newInstance( ); 178 179 javax.xml.transform.Transformer trans = 180 transFact.newTransformer(xsltSource); 181 182 trans.transform(xmlSource, result); 183 } 184 185 public static void updateLocalStrings(BufferedReader src, PrintWriter dest, Properties mappings) throws Exception { 189 String bp= "com.sun.enterprise.tools.verifier.tests."; 190 String ep= ".assertion=\\"; 191 String prev, cur; 193 while((cur=src.readLine())!=null){ 194 dest.println(cur); 197 if(cur.startsWith(bp) && cur.endsWith(ep)){ 198 String test=cur.substring(0,cur.indexOf(ep)); 199 String mapping=mappings.getProperty(test); 200 if(mapping==null) mapping=""; 201 do { 202 String nextLine=src.readLine(); 203 if(nextLine == null){ 204 System.err.println(test +" has a broken assertion. Either there is no line following \\ or there is no assertion for this test."); 205 break; 206 } 207 dest.println(nextLine); 208 int length=nextLine.length(); 209 if(nextLine.charAt(length-1)!='\\'){ 210 if(mapping.length()==0){ 211 dest.println(test+".specMappingInfo="); 212 }else { 213 dest.println(test+".specMappingInfo=\\\n\tPlease refer to "+mapping+" for further information."); 214 } 215 break; 216 } 217 } while(true); 218 } } return; 221 } 222 } 223 | Popular Tags |