KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LocalStringsHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 import java.util.*;
25 import java.io.*;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Attr JavaDoc;
30 import javax.xml.parsers.DocumentBuilder JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34 import javax.xml.transform.TransformerFactory JavaDoc;
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.dom.DOMSource JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38
39
40 public class LocalStringsHelper{
41     public static void main(String JavaDoc[] 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 JavaDoc e){
52             e.printStackTrace();
53             System.exit(1);
54         }
55     }
56
57     private static void checkArgs(String JavaDoc[] 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 JavaDoc[] args) throws Exception JavaDoc{
70         Properties prop=new Properties();
71         prop.load(new FileInputStream(args[1]));
72         String JavaDoc bp= args[2];//"com.sun.enterprise.tools.verifier.tests";
73
map(prop, bp, new FileOutputStream(args[3]));
74     }
75     
76     private static void update(String JavaDoc[] args) throws Exception JavaDoc{
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 JavaDoc[] args) throws Exception JavaDoc{
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     //Writes the XML formatted details of tests that match the given starting pattern.
92
public static void map(Properties prop, String JavaDoc bp, OutputStream out) throws Exception JavaDoc{
93         Document JavaDoc doc=getTestToAssertionMapping(prop, bp);
94         TransformerFactory JavaDoc tf=TransformerFactory.newInstance();
95         Transformer JavaDoc t=tf.newTransformer();
96         t.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(new PrintWriter(out)));
97     }
98     
99     //returns lexicographically sorted list of test names
100
//from LocalStrings.properties that match the given starting pattern
101
public static List getTestNames(Properties prop, String JavaDoc bp) throws Exception JavaDoc{
102         List testNames=new ArrayList();
103         String JavaDoc ep= ".assertion";
104         for(Enumeration e=prop.propertyNames();e.hasMoreElements();){
105             String JavaDoc cur=(String JavaDoc)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     //Returns DOM that contains the details of tests that match the given starting pattern.
115
public static Document JavaDoc getTestToAssertionMapping(Properties prop, String JavaDoc bp) throws Exception JavaDoc{
116         List testNames=getTestNames(prop, bp);
117         Document JavaDoc doc=createDOM(bp);
118         for(Iterator iter=testNames.iterator();iter.hasNext();){
119             String JavaDoc name=(String JavaDoc)iter.next();
120             String JavaDoc assertion=prop.getProperty(name+".assertion","");
121             String JavaDoc mapping=prop.getProperty(name+".specMappingInfo","");
122             addTestDetails(doc,name,assertion, mapping);
123         }
124         return doc;
125     }
126     
127     private static Document JavaDoc createDOM(String JavaDoc bp) throws Exception JavaDoc{
128         DocumentBuilderFactory JavaDoc factory =DocumentBuilderFactory.newInstance();
129         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
130         Document JavaDoc doc= builder.newDocument();
131         Element JavaDoc tests= doc.createElement("Tests");
132         Attr JavaDoc 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     //Creates a test node and adds it to the document.
141
private static void addTestDetails(Document JavaDoc doc, String JavaDoc testName, String JavaDoc assertion, String JavaDoc specMappingInfo) throws Exception JavaDoc{
142         Element JavaDoc testNode=doc.createElement("Test");
143         doc.getDocumentElement().appendChild(testNode);
144         testNode.appendChild(doc.createTextNode("\n"));
145
146         String JavaDoc bp=doc.getElementsByTagName("Tests").item(0).getAttributes().getNamedItem("BeginningPattern").getNodeValue();
147         Node JavaDoc 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 JavaDoc assertionNode=doc.createElement("Assertion");
153         assertionNode.appendChild(doc.createTextNode(assertion.trim()));
154         testNode.appendChild(assertionNode);
155         testNode.appendChild(doc.createTextNode("\n"));
156
157         Node JavaDoc 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 JavaDoc{
168         javax.xml.transform.Source JavaDoc xmlSource =
169                 new javax.xml.transform.stream.StreamSource JavaDoc(xmlIn);
170         javax.xml.transform.Source JavaDoc xsltSource =
171                 new javax.xml.transform.stream.StreamSource JavaDoc(xsltIn);
172         javax.xml.transform.Result JavaDoc result =
173                 new javax.xml.transform.stream.StreamResult JavaDoc(out);
174  
175         // create an instance of TransformerFactory
176
javax.xml.transform.TransformerFactory JavaDoc transFact =
177                 javax.xml.transform.TransformerFactory.newInstance( );
178  
179         javax.xml.transform.Transformer JavaDoc trans =
180                 transFact.newTransformer(xsltSource);
181  
182         trans.transform(xmlSource, result);
183     }
184
185     //creates a new LocalStrings.properties from a given LocalStrings.properties and spec mapping information.
186
//This is a one time operation, which I had to do to import the mappings into the
187
//LocalStrings.properties for the first time.
188
public static void updateLocalStrings(BufferedReader src, PrintWriter dest, Properties mappings) throws Exception JavaDoc{
189         String JavaDoc bp= "com.sun.enterprise.tools.verifier.tests.";
190         String JavaDoc ep= ".assertion=\\";
191         //Pattern pattern=Pattern.compile(p,DOTALL);
192
String JavaDoc prev, cur;
193         while((cur=src.readLine())!=null){
194             //System.out.println("Read line "+cur);
195
//if(pattern.matcher(cur).matches()){
196
dest.println(cur);
197             if(cur.startsWith(bp) && cur.endsWith(ep)){
198                 String JavaDoc test=cur.substring(0,cur.indexOf(ep));
199                 String JavaDoc mapping=mappings.getProperty(test);
200                 if(mapping==null) mapping="";
201                 do {
202                     String JavaDoc 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             }//found a test name pattern
219
}//end of file
220
return;
221     }
222 }
223
Popular Tags