1 23 24 package org.enhydra.xml.dom; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 29 import org.enhydra.xml.lazydom.LazyDocument; 30 import org.enhydra.xml.xmlc.XMLObject; 31 import org.w3c.dom.DOMException ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Entity ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.html.HTMLElement; 36 37 40 public final class DOMOps { 41 42 43 private static final Class [] ARGTYPES_STRING = { String .class }; 44 45 46 private static final Class [] ARGTYPES_PRIM_BOOLEAN = { Boolean.TYPE }; 47 48 49 private static final Class [] ARGTYPES_NODE = { Node .class }; 50 51 52 private static final Class [] ARGTYPES_HTMLELEMENT = { HTMLElement.class }; 53 54 57 private DOMOps() { 58 } 59 60 65 public static Document getDocument(Node node) { 66 if (node instanceof XMLObject) { 67 return ((XMLObject)node).getDocument(); 69 } else if (node instanceof Document) { 70 return (Document)node; 71 } else { 72 return node.getOwnerDocument(); 73 } 74 } 75 76 81 public static Node getActualNode(Node node) { 82 if (node instanceof XMLObject) { 83 return ((XMLObject)node).getDocument(); 84 } else { 85 return node; 86 } 87 } 88 89 95 public static Node replaceNode(Node srcNode, Node destNode) { 96 Node node = destNode.getOwnerDocument().importNode(srcNode, true); 97 Node parent = destNode.getParentNode(); 98 if (parent != null){ 99 parent.replaceChild(node, destNode); 100 } 101 return node; 102 } 103 104 107 public static int countChildren(Node node) { 108 int cnt = 0; 109 for (Node child = node.getFirstChild(); child != null; 110 child = child.getNextSibling()) { 111 cnt++; 112 } 113 return cnt; 114 } 115 116 121 public static boolean isLazyDOMInstance(Document doc) { 122 return ((doc instanceof LazyDocument) && !((LazyDocument)doc).isTemplateNode()); 123 } 124 125 141 private static Object callByReflection(Object obj, String name, 142 Class [] argtypes, Object [] args) 143 throws UnsupportedOperationException , 144 InvocationTargetException { 145 Class cl = obj.getClass(); 146 try { 147 Method m = cl.getMethod(name, argtypes); 148 return m.invoke(obj, args); 149 } catch (NoSuchMethodException e) { 150 throw new UnsupportedOperationException (e.toString()); 151 } catch (SecurityException e) { 152 throw new UnsupportedOperationException (e.toString()); 153 } catch (IllegalAccessException e) { 154 throw new UnsupportedOperationException (e.toString()); 155 } catch (IllegalArgumentException e) { 156 throw new UnsupportedOperationException (e.toString()); 157 } catch (InvocationTargetException e) { 158 Throwable cause = e.getTargetException(); 159 if (cause instanceof Error ) { 160 throw (Error )cause; 161 } else if (cause instanceof RuntimeException ) { 162 throw (RuntimeException )cause; 163 } else { 164 throw e; 165 } 166 } 167 } 168 169 182 private static Object callByReflection(Object obj, String name, 183 boolean arg) 184 throws UnsupportedOperationException , 185 InvocationTargetException { 186 187 return callByReflection(obj, name, ARGTYPES_PRIM_BOOLEAN, 188 new Object [] { (arg?Boolean.TRUE:Boolean.FALSE) }); 189 } 190 191 204 private static Object callByReflection(Object obj, String name, String arg) 205 throws UnsupportedOperationException , 206 InvocationTargetException { 207 208 return callByReflection(obj, name, 209 ARGTYPES_STRING, new Object [] { arg }); 210 } 211 212 224 public static boolean getStandalone(Document doc) { 225 try { 226 Boolean result = 227 (Boolean )callByReflection(doc, "getStandalone", null, null); 228 return result.booleanValue(); 229 } catch (InvocationTargetException e) { 230 throw new Error ("Unexpected exception: " + e.getTargetException()); 233 } 234 } 235 236 247 public static void setStandalone(Document doc, boolean val) { 248 try { 249 callByReflection(doc, "setStandalone", val); 250 } catch (InvocationTargetException e) { 251 throw new Error ("Unexpected exception: " + e.getTargetException()); 254 } 255 } 256 257 258 269 public static String getEncoding(Document doc) { 270 try { 271 return (String )callByReflection(doc, "getEncoding", null, null); 272 } catch (InvocationTargetException e) { 273 throw new Error ("Unexpected exception: " + e.getTargetException()); 276 } 277 } 278 279 291 public static void setEncoding(Document doc, String enc) { 292 try { 293 callByReflection(doc, "setEncoding", enc); 294 } catch (InvocationTargetException e) { 295 throw new Error ("Unexpected exception: " + e.getTargetException()); 298 } 299 } 300 301 313 public static boolean getStrictErrorChecking(Document doc) { 314 try { 315 Boolean result = 316 (Boolean )callByReflection(doc, "getStrictErrorChecking", 317 null, null); 318 return result.booleanValue(); 319 } catch (InvocationTargetException e) { 320 throw new Error ("Unexpected exception: " + e.getTargetException()); 323 } 324 } 325 326 337 public static void setStrictErrorChecking(Document doc, boolean val) { 338 try { 339 callByReflection(doc, "setStrictErrorChecking", val); 340 } catch (InvocationTargetException e) { 341 throw new Error ("Unexpected exception: " + e.getTargetException()); 344 } 345 } 346 347 358 public static String getVersion(Document doc) { 359 try { 360 return (String )callByReflection(doc, "getVersion", null, null); 361 } catch (InvocationTargetException e) { 362 throw new Error ("Unexpected exception: " + e.getTargetException()); 365 } 366 } 367 368 380 public static void setVersion(Document doc, String version) { 381 try { 382 callByReflection(doc, "setVersion", version); 383 } catch (InvocationTargetException e) { 384 throw new Error ("Unexpected exception: " + e.getTargetException()); 387 } 388 } 389 390 408 public static Node adoptNode(Document doc, Node node) throws DOMException { 409 try { 410 return (Node )callByReflection(doc, "adoptNode", 411 ARGTYPES_NODE, 412 new Object [] { node }); 413 } catch (InvocationTargetException e) { 414 Throwable cause = e.getTargetException(); 415 if (cause instanceof DOMException ) { 416 throw (DOMException ) cause; 417 } else { 418 throw new Error ("Unexpected exception: " + cause); 421 } 422 } 423 } 424 425 426 427 438 public static String getEncoding(Entity entity) { 439 try { 440 return (String )callByReflection(entity, "getEncoding", null, null); 441 } catch (InvocationTargetException e) { 442 throw new Error ("Unexpected exception: " + e.getTargetException()); 445 } 446 } 447 448 460 public static void setEncoding(Entity entity, String enc) { 461 try { 462 callByReflection(entity, "setEncoding", enc); 463 } catch (InvocationTargetException e) { 464 throw new Error ("Unexpected exception: " + e.getTargetException()); 467 } 468 } 469 470 483 public static Document getContentDocument(HTMLElement elem) { 484 try { 485 return (Document)callByReflection(elem, "getContentDocument", 486 ARGTYPES_HTMLELEMENT, 487 new Object [] { elem }); 488 } catch (InvocationTargetException e) { 489 Throwable cause = e.getTargetException(); 490 if (cause instanceof DOMException ) { 491 throw (DOMException ) cause; 492 } else { 493 throw new Error ("Unexpected exception: " + cause); 496 } 497 } 498 } 499 } 500 | Popular Tags |