1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.opti; 59 60 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 61 import com.sun.org.apache.xerces.internal.xni.QName; 62 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 63 import com.sun.org.apache.xerces.internal.xni.XMLString; 64 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 65 import org.w3c.dom.Attr ; 66 import org.w3c.dom.Element ; 67 import org.w3c.dom.NamedNodeMap ; 68 import org.w3c.dom.Node ; 69 70 import java.util.Vector ; 71 import java.util.Enumeration ; 72 73 79 public class SchemaDOM extends DefaultDocument { 80 81 static final int relationsRowResizeFactor = 15; 82 static final int relationsColResizeFactor = 10; 83 84 NodeImpl[][] relations; 85 ElementImpl parent; 87 int currLoc; 88 int nextFreeLoc; 89 boolean hidden; 90 91 StringBuffer fAnnotationBuffer = null; 93 94 public SchemaDOM() { 95 reset(); 96 } 97 98 99 public void startElement(QName element, XMLAttributes attributes, 100 int line, int column) { 101 ElementImpl node = new ElementImpl(line, column); 102 processElement(element, attributes, node); 103 parent = node; 105 } 106 107 108 public void emptyElement(QName element, XMLAttributes attributes, 109 int line, int column) { 110 ElementImpl node = new ElementImpl(line, column); 111 processElement(element, attributes, node); 112 } 113 114 115 private void processElement(QName element, XMLAttributes attributes, ElementImpl node) { 116 117 node.prefix = element.prefix; 119 node.localpart = element.localpart; 120 node.rawname = element.rawname; 121 node.uri = element.uri; 122 node.schemaDOM = this; 123 124 Attr [] attrs = new Attr [attributes.getLength()]; 126 for (int i=0; i<attributes.getLength(); i++) { 127 attrs[i] = new AttrImpl(null, 128 attributes.getPrefix(i), 129 attributes.getLocalName(i), 130 attributes.getQName(i), 131 attributes.getURI(i), 132 attributes.getValue(i)); 133 } 134 node.attrs = attrs; 135 136 if (nextFreeLoc == relations.length) { 138 resizeRelations(); 139 } 140 141 if (relations[currLoc][0] != parent) { 144 relations[nextFreeLoc][0] = parent; 145 currLoc = nextFreeLoc++; 146 } 147 148 boolean foundPlace = false; 150 int i = 1; 151 for (i = 1; i<relations[currLoc].length; i++) { 152 if (relations[currLoc][i] == null) { 153 foundPlace = true; 154 break; 155 } 156 } 157 158 if (!foundPlace) { 159 resizeRelations(currLoc); 160 } 161 relations[currLoc][i] = node; 162 163 parent.parentRow = currLoc; 164 node.row = currLoc; 165 node.col = i; 166 } 167 168 169 public void endElement() { 170 currLoc = parent.row; 173 parent = (ElementImpl)relations[currLoc][0]; 174 } 175 176 void comment(XMLString text) { 178 fAnnotationBuffer.append("<!--").append(text.toString()).append("-->"); 179 } 180 181 void processingInstruction(String target, String data) { 183 fAnnotationBuffer.append("<?").append(target).append(" ").append(data).append("?>"); 184 } 185 186 void characters(XMLString text ) { 188 for(int i=text.offset; i<text.offset+text.length; i++ ) { 190 if(text.ch[i] == '&') { 191 fAnnotationBuffer.append("&"); 192 } else if (text.ch[i] == '<') { 193 fAnnotationBuffer.append("<"); 194 } else { 195 fAnnotationBuffer.append(text.ch[i]); 196 } 197 } 198 } 199 200 void endAnnotationElement(QName elemName, boolean complete) { 201 if(complete) { 202 fAnnotationBuffer.append("\n</").append(elemName.rawname).append(">"); 203 ElementImpl child = (ElementImpl)relations[currLoc][1]; 208 209 if (nextFreeLoc == relations.length) { 211 resizeRelations(); 212 } 213 int newRow = child.parentRow = nextFreeLoc++; 214 215 boolean foundPlace = false; 217 int i = 1; 218 for (; i<relations[newRow].length; i++) { 219 if (relations[newRow][i] == null) { 220 foundPlace = true; 221 break; 222 } 223 } 224 225 if (!foundPlace) { 226 resizeRelations(newRow); 227 } 228 relations[newRow][i] = new TextImpl(fAnnotationBuffer, this, newRow, i); 229 fAnnotationBuffer = null; 232 } else fAnnotationBuffer.append("</").append(elemName.rawname).append(">"); 234 } 235 236 void startAnnotationCDATA() { 237 fAnnotationBuffer.append("<![CDATA["); 238 } 239 240 void endAnnotationCDATA() { 241 fAnnotationBuffer.append("]]>"); 242 } 243 244 private void resizeRelations() { 245 NodeImpl[][] temp = new NodeImpl[relations.length+relationsRowResizeFactor][]; 246 System.arraycopy(relations, 0, temp, 0, relations.length); 247 for (int i = relations.length ; i < temp.length ; i++) { 248 temp[i] = new NodeImpl[relationsColResizeFactor]; 249 } 250 relations = temp; 251 } 252 253 private void resizeRelations(int i) { 254 NodeImpl[] temp = new NodeImpl[relations[i].length+relationsColResizeFactor]; 255 System.arraycopy(relations[i], 0, temp, 0, relations[i].length); 256 relations[i] = temp; 257 } 258 259 260 public void reset() { 261 262 if(relations != null) 264 for(int i=0; i<relations.length; i++) 265 for(int j=0; j<relations[i].length; j++) 266 relations[i][j] = null; 267 relations = new NodeImpl[relationsRowResizeFactor][]; 268 parent = new ElementImpl(0, 0); 269 parent.rawname = "DOCUMENT_NODE"; 270 currLoc = 0; 271 nextFreeLoc = 1; 272 for (int i=0; i<relationsRowResizeFactor; i++) { 273 relations[i] = new NodeImpl[relationsColResizeFactor]; 274 } 275 relations[currLoc][0] = parent; 276 } 277 278 279 public void printDOM() { 280 292 } 294 295 296 298 public static void traverse(Node node, int depth) { 299 indent(depth); 300 System.out.print("<"+node.getNodeName()); 301 302 if (node.hasAttributes()) { 303 NamedNodeMap attrs = node.getAttributes(); 304 for (int i=0; i<attrs.getLength(); i++) { 305 System.out.print(" "+((Attr )attrs.item(i)).getName()+"=\""+((Attr )attrs.item(i)).getValue()+"\""); 306 } 307 } 308 309 if (node.hasChildNodes()) { 310 System.out.println(">"); 311 depth+=4; 312 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 313 traverse(child, depth); 314 } 315 depth-=4; 316 indent(depth); 317 System.out.println("</"+node.getNodeName()+">"); 318 } 319 else { 320 System.out.println("/>"); 321 } 322 } 323 324 public static void indent(int amount) { 325 for (int i = 0; i < amount; i++) { 326 System.out.print(' '); 327 } 328 } 329 330 public Element getDocumentElement() { 332 return (ElementImpl)relations[0][1]; 334 } 335 336 void startAnnotation(QName elemName, XMLAttributes attributes, 338 NamespaceContext namespaceContext) { 339 if(fAnnotationBuffer == null) fAnnotationBuffer = new StringBuffer (256); 340 fAnnotationBuffer.append("<").append(elemName.rawname).append(" "); 341 342 Vector namespaces = new Vector (); 348 for(int i=0; i<attributes.getLength(); i++) { 349 String aValue = attributes.getValue(i); 350 String aPrefix = attributes.getPrefix(i); 351 namespaces.addElement(aValue); 353 fAnnotationBuffer.append(attributes.getQName(i)).append("=\"").append(aValue).append("\" "); 354 } 355 Enumeration currPrefixes = namespaceContext.getAllPrefixes(); 358 while(currPrefixes.hasMoreElements()) { 359 String prefix = (String )currPrefixes.nextElement(); 360 String uri = namespaceContext.getURI(prefix); 361 if(!namespaces.contains(uri)) { 362 if(prefix == XMLSymbols.EMPTY_STRING) 364 fAnnotationBuffer.append("xmlns").append("=\"").append(uri).append("\" "); 365 else 366 fAnnotationBuffer.append("xmlns:").append(prefix).append("=\"").append(uri).append("\" "); 367 } 368 } 369 fAnnotationBuffer.append(">\n"); 370 } 371 void startAnnotationElement(QName elemName, XMLAttributes attributes) { 372 fAnnotationBuffer.append("<").append(elemName.rawname).append(" "); 373 for(int i=0; i<attributes.getLength(); i++) { 374 String aValue = attributes.getValue(i); 375 fAnnotationBuffer.append(" ").append(attributes.getQName(i)).append("=\"").append(processAttValue(aValue)).append("\" "); 376 } 377 fAnnotationBuffer.append(">"); 378 } 379 380 private static String processAttValue(String original) { 381 StringBuffer newVal = new StringBuffer (original.length()); 383 for(int i=0; i<original.length(); i++) { 384 char currChar = original.charAt(i); 385 if(currChar == '"') { 386 newVal.append("""); 387 } else if (currChar == '>') { 388 newVal.append(">"); 389 } else if (currChar == '&') { 390 newVal.append("&"); 391 } else { 392 newVal.append(currChar); 393 } 394 } 395 return newVal.toString(); 396 } 397 } 398 | Popular Tags |