1 16 17 package org.apache.xerces.impl.xs.opti; 18 19 import org.apache.xerces.xni.NamespaceContext; 20 import org.apache.xerces.xni.QName; 21 import org.apache.xerces.xni.XMLAttributes; 22 import org.apache.xerces.xni.XMLString; 23 import org.apache.xerces.util.XMLSymbols; 24 import org.w3c.dom.Attr ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 29 import java.util.Vector ; 30 import java.util.Enumeration ; 31 32 40 public class SchemaDOM extends DefaultDocument { 41 42 static final int relationsRowResizeFactor = 15; 43 static final int relationsColResizeFactor = 10; 44 45 NodeImpl[][] relations; 46 ElementImpl parent; 48 int currLoc; 49 int nextFreeLoc; 50 boolean hidden; 51 boolean inCDATA; 52 53 StringBuffer fAnnotationBuffer = null; 55 56 public SchemaDOM() { 57 reset(); 58 } 59 60 61 public void startElement(QName element, XMLAttributes attributes, 62 int line, int column, int offset) { 63 ElementImpl node = new ElementImpl(line, column, offset); 64 processElement(element, attributes, node); 65 parent = node; 67 } 68 69 70 public void emptyElement(QName element, XMLAttributes attributes, 71 int line, int column, int offset) { 72 ElementImpl node = new ElementImpl(line, column, offset); 73 processElement(element, attributes, node); 74 } 75 76 public void startElement(QName element, XMLAttributes attributes, 77 int line, int column) { 78 startElement(element, attributes, line, column, -1); 79 } 80 81 82 public void emptyElement(QName element, XMLAttributes attributes, 83 int line, int column) { 84 emptyElement(element, attributes, line, column, -1); 85 } 86 87 88 private void processElement(QName element, XMLAttributes attributes, ElementImpl node) { 89 90 node.prefix = element.prefix; 92 node.localpart = element.localpart; 93 node.rawname = element.rawname; 94 node.uri = element.uri; 95 node.schemaDOM = this; 96 97 Attr [] attrs = new Attr [attributes.getLength()]; 99 for (int i=0; i<attributes.getLength(); i++) { 100 attrs[i] = new AttrImpl(null, 101 attributes.getPrefix(i), 102 attributes.getLocalName(i), 103 attributes.getQName(i), 104 attributes.getURI(i), 105 attributes.getValue(i)); 106 } 107 node.attrs = attrs; 108 109 if (nextFreeLoc == relations.length) { 111 resizeRelations(); 112 } 113 114 if (relations[currLoc][0] != parent) { 117 relations[nextFreeLoc][0] = parent; 118 currLoc = nextFreeLoc++; 119 } 120 121 boolean foundPlace = false; 123 int i = 1; 124 for (i = 1; i<relations[currLoc].length; i++) { 125 if (relations[currLoc][i] == null) { 126 foundPlace = true; 127 break; 128 } 129 } 130 131 if (!foundPlace) { 132 resizeRelations(currLoc); 133 } 134 relations[currLoc][i] = node; 135 136 parent.parentRow = currLoc; 137 node.row = currLoc; 138 node.col = i; 139 } 140 141 142 public void endElement() { 143 currLoc = parent.row; 146 parent = (ElementImpl)relations[currLoc][0]; 147 } 148 149 void comment(XMLString text) { 151 fAnnotationBuffer.append("<!--").append(text.toString()).append("-->"); 152 } 153 154 void processingInstruction(String target, String data) { 156 fAnnotationBuffer.append("<?").append(target).append(" ").append(data).append("?>"); 157 } 158 159 void characters(XMLString text ) { 161 162 if (!inCDATA) { 164 for (int i = text.offset; i < text.offset+text.length; ++i ) { 165 char ch = text.ch[i]; 166 if (ch == '&') { 167 fAnnotationBuffer.append("&"); 168 } 169 else if (ch == '<') { 170 fAnnotationBuffer.append("<"); 171 } 172 else if (ch == '>') { 175 fAnnotationBuffer.append(">"); 176 } 177 else if (ch == '\r') { 182 fAnnotationBuffer.append("
"); 183 } 184 else { 185 fAnnotationBuffer.append(ch); 186 } 187 } 188 } 189 else { 190 fAnnotationBuffer.append(text.ch, text.offset, text.length); 191 } 192 } 193 194 void endAnnotationElement(QName elemName, boolean complete) { 195 if(complete) { 196 fAnnotationBuffer.append("\n</").append(elemName.rawname).append(">"); 197 ElementImpl child = (ElementImpl)relations[currLoc][1]; 202 203 if (nextFreeLoc == relations.length) { 205 resizeRelations(); 206 } 207 int newRow = child.parentRow = nextFreeLoc++; 208 209 boolean foundPlace = false; 211 int i = 1; 212 for (; i<relations[newRow].length; i++) { 213 if (relations[newRow][i] == null) { 214 foundPlace = true; 215 break; 216 } 217 } 218 219 if (!foundPlace) { 220 resizeRelations(newRow); 221 } 222 relations[newRow][i] = new TextImpl(fAnnotationBuffer, this, newRow, i); 223 fAnnotationBuffer = null; 226 } else fAnnotationBuffer.append("</").append(elemName.rawname).append(">"); 228 } 229 230 void endSyntheticAnnotationElement(QName elemName, boolean complete) { 231 if(complete) { 232 fAnnotationBuffer.append("\n</").append(elemName.rawname).append(">"); 233 parent.fSyntheticAnnotation = fAnnotationBuffer.toString(); 238 239 fAnnotationBuffer = null; 242 } else fAnnotationBuffer.append("</").append(elemName.rawname).append(">"); 244 } 245 246 void startAnnotationCDATA() { 247 inCDATA = true; 248 fAnnotationBuffer.append("<![CDATA["); 249 } 250 251 void endAnnotationCDATA() { 252 fAnnotationBuffer.append("]]>"); 253 inCDATA = false; 254 } 255 256 private void resizeRelations() { 257 NodeImpl[][] temp = new NodeImpl[relations.length+relationsRowResizeFactor][]; 258 System.arraycopy(relations, 0, temp, 0, relations.length); 259 for (int i = relations.length ; i < temp.length ; i++) { 260 temp[i] = new NodeImpl[relationsColResizeFactor]; 261 } 262 relations = temp; 263 } 264 265 private void resizeRelations(int i) { 266 NodeImpl[] temp = new NodeImpl[relations[i].length+relationsColResizeFactor]; 267 System.arraycopy(relations[i], 0, temp, 0, relations[i].length); 268 relations[i] = temp; 269 } 270 271 272 public void reset() { 273 274 if(relations != null) 276 for(int i=0; i<relations.length; i++) 277 for(int j=0; j<relations[i].length; j++) 278 relations[i][j] = null; 279 relations = new NodeImpl[relationsRowResizeFactor][]; 280 parent = new ElementImpl(0, 0, 0); 281 parent.rawname = "DOCUMENT_NODE"; 282 currLoc = 0; 283 nextFreeLoc = 1; 284 inCDATA = false; 285 for (int i=0; i<relationsRowResizeFactor; i++) { 286 relations[i] = new NodeImpl[relationsColResizeFactor]; 287 } 288 relations[currLoc][0] = parent; 289 } 290 291 292 public void printDOM() { 293 305 } 307 308 309 311 public static void traverse(Node node, int depth) { 312 indent(depth); 313 System.out.print("<"+node.getNodeName()); 314 315 if (node.hasAttributes()) { 316 NamedNodeMap attrs = node.getAttributes(); 317 for (int i=0; i<attrs.getLength(); i++) { 318 System.out.print(" "+((Attr )attrs.item(i)).getName()+"=\""+((Attr )attrs.item(i)).getValue()+"\""); 319 } 320 } 321 322 if (node.hasChildNodes()) { 323 System.out.println(">"); 324 depth+=4; 325 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 326 traverse(child, depth); 327 } 328 depth-=4; 329 indent(depth); 330 System.out.println("</"+node.getNodeName()+">"); 331 } 332 else { 333 System.out.println("/>"); 334 } 335 } 336 337 public static void indent(int amount) { 338 for (int i = 0; i < amount; i++) { 339 System.out.print(' '); 340 } 341 } 342 343 public Element getDocumentElement() { 345 return (ElementImpl)relations[0][1]; 347 } 348 349 void startAnnotation(QName elemName, XMLAttributes attributes, 351 NamespaceContext namespaceContext) { 352 if(fAnnotationBuffer == null) fAnnotationBuffer = new StringBuffer (256); 353 fAnnotationBuffer.append("<").append(elemName.rawname).append(" "); 354 355 Vector namespaces = new Vector (); 361 for (int i = 0; i < attributes.getLength(); ++i) { 362 String aValue = attributes.getValue(i); 363 String aPrefix = attributes.getPrefix(i); 364 String aQName = attributes.getQName(i); 365 if (aPrefix == XMLSymbols.PREFIX_XMLNS || aQName == XMLSymbols.PREFIX_XMLNS) { 367 namespaces.addElement(aPrefix == XMLSymbols.PREFIX_XMLNS ? 368 attributes.getLocalName(i) : XMLSymbols.EMPTY_STRING); 369 } 370 fAnnotationBuffer.append(aQName).append("=\"").append(processAttValue(aValue)).append("\" "); 371 } 372 Enumeration currPrefixes = namespaceContext.getAllPrefixes(); 375 while(currPrefixes.hasMoreElements()) { 376 String prefix = (String )currPrefixes.nextElement(); 377 String uri = namespaceContext.getURI(prefix); 378 if (uri == null) { 379 uri = XMLSymbols.EMPTY_STRING; 380 } 381 if (!namespaces.contains(prefix)) { 382 if(prefix == XMLSymbols.EMPTY_STRING) { 384 fAnnotationBuffer.append("xmlns").append("=\"").append(processAttValue(uri)).append("\" "); 385 } 386 else { 387 fAnnotationBuffer.append("xmlns:").append(prefix).append("=\"").append(processAttValue(uri)).append("\" "); 388 } 389 } 390 } 391 fAnnotationBuffer.append(">\n"); 392 } 393 void startAnnotationElement(QName elemName, XMLAttributes attributes) { 394 fAnnotationBuffer.append("<").append(elemName.rawname); 395 for(int i=0; i<attributes.getLength(); i++) { 396 String aValue = attributes.getValue(i); 397 fAnnotationBuffer.append(" ").append(attributes.getQName(i)).append("=\"").append(processAttValue(aValue)).append("\""); 398 } 399 fAnnotationBuffer.append(">"); 400 } 401 402 private static String processAttValue(String original) { 403 final int length = original.length(); 404 for (int i = 0; i < length; ++i) { 406 char currChar = original.charAt(i); 407 if (currChar == '"' || currChar == '<' || currChar == '&' || 408 currChar == 0x09 || currChar == 0x0A || currChar == 0x0D) { 409 return escapeAttValue(original, i); 410 } 411 } 412 return original; 413 } 414 415 private static String escapeAttValue(String original, int from) { 416 int i; 417 final int length = original.length(); 418 StringBuffer newVal = new StringBuffer (length); 419 newVal.append(original.substring(0, from)); 420 for (i = from; i < length; ++i) { 421 char currChar = original.charAt(i); 422 if (currChar == '"') { 423 newVal.append("""); 424 } 425 else if (currChar == '<') { 426 newVal.append("<"); 427 } 428 else if (currChar == '&') { 429 newVal.append("&"); 430 } 431 else if (currChar == 0x09) { 435 newVal.append("	"); 436 } 437 else if (currChar == 0x0A) { 438 newVal.append("
"); 439 } 440 else if (currChar == 0x0D) { 441 newVal.append("
"); 442 } 443 else { 444 newVal.append(currChar); 445 } 446 } 447 return newVal.toString(); 448 } 449 } 450 | Popular Tags |