1 22 23 package org.xquark.util; 24 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.ext.LexicalHandler ; 31 import org.xml.sax.helpers.DefaultHandler ; 32 33 51 public class BasicDOMBuilder extends DefaultHandler 52 implements ContentHandler , LexicalHandler 53 { 54 private static final String RCSRevision = "$Revision: 1.1 $"; 55 private static final String RCSName = "$Name: $"; 56 57 61 64 private Document doc; 65 66 69 private Node nodeactu; 70 71 74 private boolean cs; 75 76 80 87 public BasicDOMBuilder(Document doc) throws NullPointerException 88 { 89 setDocument(doc); 90 } 91 92 99 public BasicDOMBuilder(Element element) throws NullPointerException 100 { 101 setElement(element); 102 } 103 104 108 public void characters(char[] ch, int start, int length) 109 { 110 if (nodeactu != doc) { 112 if (cs) 113 nodeactu.appendChild(doc.createCDATASection(new String (ch,start,length))); 114 else 115 nodeactu.appendChild(doc.createTextNode(new String (ch,start,length))); 116 } 117 } 118 119 public void endElement(String namespaceUI, String localName, String qName) 120 { 121 nodeactu=nodeactu.getParentNode(); 122 } 123 124 public void ignorableWhitespace(char[] ch, int start, int length) 125 { 126 if (nodeactu != doc) nodeactu.appendChild(doc.createTextNode(new String (ch,start,length))); 128 } 129 130 public void processingInstruction(String target, String data) 131 { 132 nodeactu.appendChild(doc.createProcessingInstruction(target,data)); 133 } 134 135 public void skippedEntity(String name) 136 { 137 nodeactu.appendChild(doc.createEntityReference(name)); 138 } 139 140 public void startElement 141 (String namespaceURI, 142 String localName, 143 String qName, 144 Attributes atts) 145 { 146 if (namespaceURI.equals("")) 147 namespaceURI = null; 148 149 Element element=doc.createElementNS(namespaceURI,qName); 150 int nbattr=atts.getLength(); 151 for (int i=0; i<nbattr; i++) 152 { 153 namespaceURI=atts.getURI(i); 154 if (namespaceURI.equals("")) 155 namespaceURI = null; 156 element.setAttributeNS(namespaceURI,atts.getQName(i),atts.getValue(i)); 157 } 158 nodeactu.appendChild(element); 159 nodeactu=element; 160 } 161 162 166 public void comment(char[] ch, int start, int length) 167 { 168 nodeactu.appendChild 169 (doc.createComment 170 (new String (ch,start,length))); 171 } 172 173 public void endCDATA() 174 { 175 cs=false; 176 } 177 178 public void endDTD() 179 { 180 } 181 182 public void endEntity(String name) 183 { 184 } 185 186 public void startCDATA() 187 { 188 cs=true; 189 } 190 191 public void startDTD(String name, String publicId, String systemId) 192 { 193 } 194 195 public void startEntity(String name) 196 { 197 } 198 199 203 public void setDocument(Document doc) 204 { 205 if (doc==null) 206 throw new NullPointerException ("Couldn't create such an object from a null reference"); 207 this.doc=doc; 208 nodeactu=doc; 209 cs=false; 210 } 211 212 public void setElement(Element root) 213 { 214 if (root==null) 215 throw new NullPointerException ("Couldn't create such an object from a null reference"); 216 doc=root.getOwnerDocument(); 217 nodeactu=root; 218 cs=false; 219 } 220 221 } 222 | Popular Tags |