1 6 7 package org.netbeans.test.editor.app.util; 8 9 import org.netbeans.test.editor.app.core.*; 10 11 import java.util.Vector ; 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Element ; 14 import org.w3c.dom.Node ; 15 import org.w3c.dom.NodeList ; 16 17 21 public class ParsingUtils { 22 23 24 public ParsingUtils() { 25 } 26 27 private static class LoadedToCreated { 28 Class loaded; 29 Class created; 30 31 public LoadedToCreated(Class loaded, Class 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 saveSubNodes(Element node, Vector nodes) { 57 Document doc = node.getOwnerDocument(); 58 59 for (int cntr = 0; cntr < nodes.size(); cntr++) { 60 Object item = nodes.elementAt(cntr); 61 62 for (int index = 0; index < classNames.length; index++) { 63 64 if (item.getClass() == classNames[index].loaded) { 65 String name = classNames[index].loaded.getName(); 67 Element newNode = doc.createElement(name.substring(name.lastIndexOf('.')+1)); 69 node.appendChild(((TestNode)item).toXML(newNode)); 70 break; 71 } 72 } 73 } 74 return node; 75 } 76 77 public static final Object loadClass(Class clazz, Object from) { 78 Class [] types = new Class [] {Element .class }; 79 Object [] par = new Object [] {from}; 80 81 try { 82 return clazz.getConstructor(types).newInstance(par); 83 } catch (java.lang.NoSuchMethodException e) { 84 System.err.println("Fatal error - attempt to load unloadable class(1)."); 85 } catch (java.lang.IllegalAccessException e) { 86 System.err.println("Fatal error - attempt to load unloadable class(2)."); 87 } catch (java.lang.reflect.InvocationTargetException 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 e) { 93 System.err.println("Fatal error - attempt to load unloadable class(4)."); 94 }; 95 return null; 96 } 97 98 public static final Vector loadSubNodes(Element node) { 99 NodeList nodes = node.getChildNodes(); 100 Vector res = new Vector (10, 10); 101 String 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) { res.add(loadClass(classNames[index].created, nodes.item(cntr))); 115 break; 116 } 117 } 118 } 119 return res; 120 } 121 122 public static final Element saveString(Element node, String name, String what) { 123 Element 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 loadString(Element node, String name) { 131 NodeList mainNodes = node.getElementsByTagName(name); 132 133 if (mainNodes.getLength() == 0) 134 return null; 135 Element el = (Element )mainNodes.item(0); 136 StringBuffer sb = new StringBuffer (); 137 Node newEl = el; 138 139 sb.append(newEl.getNodeValue()); 140 NodeList nodes = newEl.getChildNodes(); 141 for (int cntr = 0; cntr < nodes.getLength(); cntr++) { 142 sb.append(nodes.item(cntr).getNodeValue()); 143 } 144 String 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 source; 155 private String image; 156 public Twin(String asource, String aimage) { 157 source = asource; 158 image = aimage; 159 } 160 161 public String toImage(String what) { 162 int index = 0; 163 164 while ((index = what.indexOf(source, index)) != (-1)) { 165 String prefix = what.substring(0, index); 166 String 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 fromImage(String what) { 175 int index = 0; 176 177 while ((index = what.indexOf(image, index)) != (-1)) { 178 String prefix = what.substring(0, index); 179 String 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 toSafeString(String 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 fromSafeString(String 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 what) { 242 return parseInt(what, 0); 243 } 244 245 public static int parseInt(String what, int prefered) { 246 try { 247 return Integer.parseInt(what); 248 } catch (java.lang.NumberFormatException e) { 249 return prefered; 250 } 251 } 252 253 public static boolean readBoolean(Element node, String name) { 254 String 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 node, String name) { 267 String 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 e) { 275 e.printStackTrace(System.err); 276 return 0; 277 } 278 } 279 } 280 | Popular Tags |