KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > util > ParsingUtils


1 /*
2  * ParsingUtils.java
3  *
4  * Created on November 18, 2002, 10:57 AM
5  */

6
7 package org.netbeans.test.editor.app.util;
8
9 import org.netbeans.test.editor.app.core.*;
10
11 import java.util.Vector JavaDoc;
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Element JavaDoc;
14 import org.w3c.dom.Node JavaDoc;
15 import org.w3c.dom.NodeList JavaDoc;
16
17 /**
18  *
19  * @author eh103527
20  */

21 public class ParsingUtils {
22     
23     /** Creates a new instance of ParsingUtils */
24     public ParsingUtils() {
25     }
26     
27     private static class LoadedToCreated {
28         Class JavaDoc loaded;
29         Class JavaDoc created;
30         
31         public LoadedToCreated(Class JavaDoc loaded, Class JavaDoc created) {
32             this.loaded = loaded;
33             this.created = created;
34         }
35     }
36     
37     private static LoadedToCreated[] classNames = new LoadedToCreated[] {
38         new LoadedToCreated(Test.class, Test.class),
39         new LoadedToCreated(TestAction.class, TestAction.class),
40         new LoadedToCreated(TestCallAction.class, TestCallAction.class),
41         new LoadedToCreated(TestGroup.class, TestGroup.class),
42         new LoadedToCreated(TestLogAction.class, TestLogAction.class),
43         new LoadedToCreated(TestStringAction.class, TestStringAction.class),
44         new LoadedToCreated(TestCompletionAction.class, TestCompletionAction.class),
45         new LoadedToCreated(TestAddAbbreviationAction.class, TestAddAbbreviationAction.class),
46         new LoadedToCreated(TestNode.class, TestNode.class),
47         new LoadedToCreated(TestSetAction.class, TestSetKitAction.class),
48         new LoadedToCreated(TestSetIEAction.class, TestSetIEAction.class),
49         new LoadedToCreated(TestSetJavaIEAction.class, TestSetJavaIEAction.class),
50         new LoadedToCreated(TestSetKitAction.class, TestSetKitAction.class),
51         new LoadedToCreated(TestSetCompletionAction.class, TestSetCompletionAction.class),
52         new LoadedToCreated(TestStep.class, TestStep.class),
53         new LoadedToCreated(TestSubTest.class, TestSubTest.class)
54     };
55     
56     public static final Element JavaDoc saveSubNodes(Element JavaDoc node, Vector JavaDoc nodes) {
57         Document JavaDoc doc = node.getOwnerDocument();
58         
59         for (int cntr = 0; cntr < nodes.size(); cntr++) {
60             Object JavaDoc item = nodes.elementAt(cntr);
61             
62             for (int index = 0; index < classNames.length; index++) {
63                 
64                 if (item.getClass() == classNames[index].loaded) {
65                     // Element newNode = doc.createElement(classNames[index].loaded.getName());//packages version
66
String JavaDoc name = classNames[index].loaded.getName();
67                     Element JavaDoc newNode = doc.createElement(name.substring(name.lastIndexOf('.')+1)); //no packages names
68

69                     node.appendChild(((TestNode)item).toXML(newNode));
70                     break;
71                 }
72             }
73         }
74         return node;
75     }
76     
77     public static final Object JavaDoc loadClass(Class JavaDoc clazz, Object JavaDoc from) {
78         Class JavaDoc[] types = new Class JavaDoc[] {Element JavaDoc.class };
79         Object JavaDoc[] par = new Object JavaDoc[] {from};
80         
81         try {
82             return clazz.getConstructor(types).newInstance(par);
83         } catch (java.lang.NoSuchMethodException JavaDoc e) {
84             System.err.println("Fatal error - attempt to load unloadable class(1).");
85         } catch (java.lang.IllegalAccessException JavaDoc e) {
86             System.err.println("Fatal error - attempt to load unloadable class(2).");
87         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
88             System.err.println("Fatal error - attempt to load unloadable class(3).");
89             System.err.println("Class name: " + clazz);
90             System.err.println("Exception: ");
91             e.getTargetException().printStackTrace(System.out);
92         } catch (java.lang.InstantiationException JavaDoc e) {
93             System.err.println("Fatal error - attempt to load unloadable class(4).");
94         };
95         return null;
96     }
97     
98     public static final Vector JavaDoc loadSubNodes(Element JavaDoc node) {
99         NodeList JavaDoc nodes = node.getChildNodes();
100         Vector JavaDoc res = new Vector JavaDoc(10, 10);
101         String JavaDoc nn,cn;
102         
103         for (int cntr = 0; cntr < nodes.getLength(); cntr++) {
104             nn=nodes.item(cntr).getNodeName();
105             if (nn.indexOf('#') == 0) {
106                 continue;
107             }
108             if (nn.indexOf('.') > -1)
109                 nn=nn.substring(nn.lastIndexOf('.')+1);
110             for (int index = 0; index < classNames.length; index++) {
111                 cn=classNames[index].loaded.getName();
112                 if (cn.indexOf(nn) > -1) { //without packages name version
113
//if (nodes.item(cntr).getNodeName().equals(classNames[index].loaded.getName())) { //with packages names version
114
res.add(loadClass(classNames[index].created, nodes.item(cntr)));
115                     break;
116                 }
117             }
118         }
119         return res;
120     }
121     
122     public static final Element JavaDoc saveString(Element JavaDoc node, String JavaDoc name, String JavaDoc what) {
123         Element JavaDoc newNode = node.getOwnerDocument().createElement(name);
124         
125         newNode.appendChild(node.getOwnerDocument().createCDATASection("\"" + what + "\""));
126         node.appendChild(newNode);
127         return node;
128     }
129     
130     public static final String JavaDoc loadString(Element JavaDoc node, String JavaDoc name) {
131         NodeList JavaDoc mainNodes = node.getElementsByTagName(name);
132         
133         if (mainNodes.getLength() == 0)
134             return null;
135         Element JavaDoc el = (Element JavaDoc)mainNodes.item(0);
136         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
137         Node JavaDoc newEl = el;
138         
139         sb.append(newEl.getNodeValue());
140         NodeList JavaDoc nodes = newEl.getChildNodes();
141         for (int cntr = 0; cntr < nodes.getLength(); cntr++) {
142             sb.append(nodes.item(cntr).getNodeValue());
143         }
144         String JavaDoc raw = sb.toString();
145         int first = raw.indexOf('\"');
146         int last = raw.lastIndexOf('\"');
147         if (first != (-1) && last != (-1))
148             return raw.substring(first + 1, last);
149         else
150             return raw;
151     }
152     
153     private static class Twin {
154         private String JavaDoc source;
155         private String JavaDoc image;
156         public Twin(String JavaDoc asource, String JavaDoc aimage) {
157             source = asource;
158             image = aimage;
159         }
160         
161         public String JavaDoc toImage(String JavaDoc what) {
162             int index = 0;
163             
164             while ((index = what.indexOf(source, index)) != (-1)) {
165                 String JavaDoc prefix = what.substring(0, index);
166                 String JavaDoc postfix = what.substring(index + source.length());
167                 
168                 what = prefix + image + postfix;
169                 index += image.length();
170             };
171             return what;
172         }
173         
174         public String JavaDoc fromImage(String JavaDoc what) {
175             int index = 0;
176             
177             while ((index = what.indexOf(image, index)) != (-1)) {
178                 String JavaDoc prefix = what.substring(0, index);
179                 String JavaDoc postfix = what.substring(index + image.length());
180                 
181                 what = prefix + source + postfix;
182                 index += source.length();
183             };
184             return what;
185         }
186     }
187     
188     private static Twin[] arr;
189     
190     static {
191         arr = new Twin[] {
192             new Twin("\000", "\\00"),
193             new Twin("\001", "\\01"),
194             new Twin("\002", "\\02"),
195             new Twin("\003", "\\03"),
196             new Twin("\004", "\\04"),
197             new Twin("\005", "\\05"),
198             new Twin("\006", "\\06"),
199             new Twin("\007", "\\07"),
200             new Twin("\008", "\\08"),
201             new Twin("\009", "\\09"),
202             new Twin("\010", "\\0A"),
203             new Twin("\011", "\\0B"),
204             new Twin("\012", "\\0C"),
205             new Twin("\013", "\\0D"),
206             new Twin("\014", "\\0E"),
207             new Twin("\015", "\\0F"),
208             new Twin("\016", "\\10"),
209             new Twin("\017", "\\11"),
210             new Twin("\018", "\\12"),
211             new Twin("\019", "\\13"),
212             new Twin("\020", "\\14"),
213             new Twin("\021", "\\15"),
214             new Twin("\022", "\\16"),
215             new Twin("\023", "\\17"),
216             new Twin("\024", "\\18"),
217             new Twin("\025", "\\19"),
218             new Twin("\026", "\\1A"),
219             new Twin("\027", "\\1B"),
220             new Twin("\028", "\\1C"),
221             new Twin("\029", "\\1D"),
222             new Twin("\030", "\\1E"),
223             new Twin("\031", "\\1F")
224         };
225     }
226     
227     public static String JavaDoc toSafeString(String JavaDoc what) {
228         for (int cntr = 0; cntr < arr.length; cntr++) {
229             what = arr[cntr].toImage(what);
230         };
231         return what;
232     }
233     
234     public static String JavaDoc fromSafeString(String JavaDoc what) {
235         for (int cntr = 0; cntr < arr.length; cntr++) {
236             what = arr[cntr].fromImage(what);
237         };
238         return what;
239     }
240     
241     public static int parseInt(String JavaDoc what) {
242         return parseInt(what, 0);
243     }
244     
245     public static int parseInt(String JavaDoc what, int prefered) {
246         try {
247             return Integer.parseInt(what);
248         } catch (java.lang.NumberFormatException JavaDoc e) {
249             return prefered;
250         }
251     }
252     
253     public static boolean readBoolean(Element JavaDoc node, String JavaDoc name) {
254         String JavaDoc attribute = node.getAttribute(name);
255         
256         if (attribute == null) {
257             return false;
258         }
259         if ("true".equalsIgnoreCase(attribute)) {
260             return true;
261         } else {
262             return false;
263         }
264     }
265     
266     public static int readInt(Element JavaDoc node, String JavaDoc name) {
267         String JavaDoc attribute = node.getAttribute(name);
268         
269         if (attribute == null || attribute.length() == 0) {
270             return 0;
271         }
272         try {
273             return Integer.parseInt(attribute);
274         } catch (NumberFormatException JavaDoc e) {
275             e.printStackTrace(System.err);
276             return 0;
277         }
278     }
279 }
280
Popular Tags