1 23 24 package org.enhydra.xml.xmlc; 25 26 import java.io.OutputStream ; 27 import java.io.PrintWriter ; 28 29 import org.enhydra.xml.dom.DOMInfo; 30 import org.enhydra.xml.dom.DOMOps; 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.Text ; 37 38 40 43 public abstract class XMLCUtil { 44 53 public static Attr getAttributeByName(Node node, String name) { 54 NamedNodeMap nodeMap = node.getAttributes(); 55 if (nodeMap == null) { 56 return null; 57 } else { 58 return (Attr )nodeMap.getNamedItem(name); 59 } 60 } 61 62 70 public static Text findFirstText(Node node) { 71 if (node instanceof Text ) { 72 return (Text )node; 73 } 74 for (Node child = node.getFirstChild(); child != null; 75 child = child.getNextSibling()) { 76 Text text = findFirstText(child); 77 if (text != null) { 78 return text; 79 } 80 } 81 return null; 82 } 83 84 94 public static Text getFirstText(Node node) { 95 Text text = findFirstText(node); 96 if (text == null) { 97 String msg = "No child text mode found for element"; 98 Attr id = getAttributeByName(node, "id"); 99 if (id != null) { 100 msg += "; id=\"" + id.getValue() + "\""; 101 } 102 throw new XMLCError(msg); 103 } 104 return text; 105 } 106 107 119 public static Element getElementById(String id, Node node) { 120 if (node instanceof Element ) { 122 Element elem = (Element )node; 123 Attr nodeId = getAttributeByName(elem, "id"); 124 if ((nodeId != null) && nodeId.getValue().equalsIgnoreCase(id)) { 125 return (Element )node; 126 } 127 } 128 129 for (Node child = node.getFirstChild(); child != null; 131 child = child.getNextSibling()) { 132 Element childElem = getElementById(id, child); 133 if (childElem != null) { 134 return childElem; 135 } 136 } 137 return null; 138 } 139 140 149 public static Element getRequiredElementById(String id, Node node) { 150 Element elem = getElementById(id, node); 151 if (elem == null) { 152 String msg = "No element found for id \"" + id + "\""; 154 Attr nodeId = getAttributeByName(node, "id"); 155 if (nodeId != null) { 156 msg += "; starting at id=\"" + nodeId.getValue() + "\""; 157 } 158 throw new XMLCError(msg); 159 } else { 160 return elem; 161 } 162 } 163 164 174 public static Node copyNode(Node srcNode, Document destDocument) { 175 return destDocument.importNode(srcNode, true); 176 } 177 178 186 public static Node replaceNode(Node srcNode, Node destNode) { 187 return DOMOps.replaceNode(srcNode, destNode); 188 } 189 190 192 196 public static final int PRINT_COMMENT = 0x00; 197 198 202 public static final int PRINT_TEXT = 0x00; 203 204 209 public static final int PRINT_CDATA = 0x00; 210 211 216 public static final int PRINT_DOCUMENTTYPE = 0x00; 217 218 223 public static final int PRINT_ALL = DOMInfo.PRINT_ALL; 224 225 230 public static final int PRINT_DEFAULT = DOMInfo.PRINT_DEFAULT; 231 232 236 public static void printNode(String msg, Node node, 237 int options, 238 PrintWriter out) { 239 DOMInfo.printTree(msg, node, options, out); 240 } 241 242 246 public static void printNode(String msg, Node node, 247 PrintWriter out) { 248 DOMInfo.printTree(msg, node, out); 249 } 250 251 255 public static void printNode(String msg, Node node, 256 OutputStream out) { 257 DOMInfo.printTree(msg, node, out); 258 } 259 } 260 | Popular Tags |