1 16 19 package org.apache.xml.utils; 20 21 import java.io.IOException ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.TransformerException ; 27 28 import org.w3c.dom.Attr ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 33 import org.xml.sax.InputSource ; 34 35 40 public class DOM2Helper extends DOMHelper 41 { 42 43 46 public DOM2Helper(){} 47 48 61 public void checkNode(Node node) throws TransformerException 62 { 63 64 } 68 69 75 public boolean supportsSAX() 76 { 77 return true; 78 } 79 80 85 private Document m_doc; 86 87 93 public void setDocument(Document doc) 94 { 95 m_doc = doc; 96 } 97 98 104 public Document getDocument() 105 { 106 return m_doc; 107 } 108 109 131 public void parse(InputSource source) throws TransformerException 132 { 133 134 try 135 { 136 137 DocumentBuilderFactory builderFactory = 141 DocumentBuilderFactory.newInstance(); 142 143 builderFactory.setNamespaceAware(true); 144 builderFactory.setValidating(true); 145 146 DocumentBuilder parser = builderFactory.newDocumentBuilder(); 147 148 162 163 parser.setErrorHandler( 164 new org.apache.xml.utils.DefaultErrorHandler()); 165 166 setDocument(parser.parse(source)); 172 } 173 catch (org.xml.sax.SAXException se) 174 { 175 throw new TransformerException (se); 176 } 177 catch (ParserConfigurationException pce) 178 { 179 throw new TransformerException (pce); 180 } 181 catch (IOException ioe) 182 { 183 throw new TransformerException (ioe); 184 } 185 186 } 188 189 204 public Element getElementByID(String id, Document doc) 205 { 206 return doc.getElementById(id); 207 } 208 209 227 public static boolean isNodeAfter(Node node1, Node node2) 228 { 229 230 if(node1 instanceof DOMOrder && node2 instanceof DOMOrder) 233 { 234 int index1 = ((DOMOrder) node1).getUid(); 235 int index2 = ((DOMOrder) node2).getUid(); 236 237 return index1 <= index2; 238 } 239 else 240 { 241 242 return DOMHelper.isNodeAfter(node1, node2); 245 } 246 } 247 248 260 public static Node getParentOfNode(Node node) 261 { 262 Node parent=node.getParentNode(); 263 if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) ) 264 parent=((Attr ) node).getOwnerElement(); 265 return parent; 266 } 267 268 279 public String getLocalNameOfNode(Node n) 280 { 281 282 String name = n.getLocalName(); 283 284 return (null == name) ? super.getLocalNameOfNode(n) : name; 285 } 286 287 302 public String getNamespaceOfNode(Node n) 303 { 304 return n.getNamespaceURI(); 305 } 306 307 311 } 313 | Popular Tags |