1 57 58 package org.enhydra.apache.xerces.validators.schema; 59 60 import org.enhydra.apache.xerces.dom.AttrImpl; 61 import org.enhydra.apache.xerces.dom.DocumentImpl; 62 import org.w3c.dom.Attr ; 63 import org.w3c.dom.DOMException ; 64 import org.w3c.dom.Document ; 65 import org.w3c.dom.Element ; 66 import org.w3c.dom.NamedNodeMap ; 67 import org.w3c.dom.Node ; 68 69 72 public class XUtil { 73 74 78 79 protected XUtil() {} 80 81 85 92 public static void copyInto(Node src, Node dest) throws DOMException { 93 94 Document factory = dest.getOwnerDocument(); 96 boolean domimpl = factory instanceof DocumentImpl; 97 98 Node start = src; 100 Node parent = src; 101 Node place = src; 102 103 while (place != null) { 105 106 Node node = null; 108 int type = place.getNodeType(); 109 switch (type) { 110 case Node.CDATA_SECTION_NODE: { 111 node = factory.createCDATASection(place.getNodeValue()); 112 break; 113 } 114 case Node.COMMENT_NODE: { 115 node = factory.createComment(place.getNodeValue()); 116 break; 117 } 118 case Node.ELEMENT_NODE: { 119 Element element = factory.createElement(place.getNodeName()); 120 node = element; 121 NamedNodeMap attrs = place.getAttributes(); 122 int attrCount = attrs.getLength(); 123 for (int i = 0; i < attrCount; i++) { 124 Attr attr = (Attr )attrs.item(i); 125 String attrName = attr.getNodeName(); 126 String attrValue = attr.getNodeValue(); 127 element.setAttribute(attrName, attrValue); 128 if (domimpl && !attr.getSpecified()) { 129 ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false); 130 } 131 } 132 break; 133 } 134 case Node.ENTITY_REFERENCE_NODE: { 135 node = factory.createEntityReference(place.getNodeName()); 136 break; 137 } 138 case Node.PROCESSING_INSTRUCTION_NODE: { 139 node = factory.createProcessingInstruction(place.getNodeName(), 140 place.getNodeValue()); 141 break; 142 } 143 case Node.TEXT_NODE: { 144 node = factory.createTextNode(place.getNodeValue()); 145 break; 146 } 147 default: { 148 throw new IllegalArgumentException ("can't copy node type, "+ 149 type+" ("+ 150 node.getNodeName()+')'); 151 } 152 } 153 dest.appendChild(node); 154 155 if (place.hasChildNodes()) { 157 parent = place; 158 place = place.getFirstChild(); 159 dest = node; 160 } 161 162 else { 164 place = place.getNextSibling(); 165 while (place == null && parent != start) { 166 place = parent.getNextSibling(); 167 parent = parent.getParentNode(); 168 dest = dest.getParentNode(); 169 } 170 } 171 172 } 173 174 } 176 177 public static Element getFirstChildElement(Node parent) { 178 179 Node child = parent.getFirstChild(); 181 while (child != null) { 182 if (child.getNodeType() == Node.ELEMENT_NODE) { 183 return (Element )child; 184 } 185 child = child.getNextSibling(); 186 } 187 188 return null; 190 191 } 193 194 public static Element getLastChildElement(Node parent) { 195 196 Node child = parent.getLastChild(); 198 while (child != null) { 199 if (child.getNodeType() == Node.ELEMENT_NODE) { 200 return (Element )child; 201 } 202 child = child.getPreviousSibling(); 203 } 204 205 return null; 207 208 } 210 211 public static Element getNextSiblingElement(Node node) { 212 213 Node sibling = node.getNextSibling(); 215 while (sibling != null) { 216 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 217 return (Element )sibling; 218 } 219 sibling = sibling.getNextSibling(); 220 } 221 222 return null; 224 225 } 227 228 public static Element getFirstChildElement(Node parent, String elemName) { 229 230 Node child = parent.getFirstChild(); 232 while (child != null) { 233 if (child.getNodeType() == Node.ELEMENT_NODE) { 234 if (child.getNodeName().equals(elemName)) { 235 return (Element )child; 236 } 237 } 238 child = child.getNextSibling(); 239 } 240 241 return null; 243 244 } 246 247 public static Element getLastChildElement(Node parent, String elemName) { 248 249 Node child = parent.getLastChild(); 251 while (child != null) { 252 if (child.getNodeType() == Node.ELEMENT_NODE) { 253 if (child.getNodeName().equals(elemName)) { 254 return (Element )child; 255 } 256 } 257 child = child.getPreviousSibling(); 258 } 259 260 return null; 262 263 } 265 266 public static Element getNextSiblingElement(Node node, String elemName) { 267 268 Node sibling = node.getNextSibling(); 270 while (sibling != null) { 271 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 272 if (sibling.getNodeName().equals(elemName)) { 273 return (Element )sibling; 274 } 275 } 276 sibling = sibling.getNextSibling(); 277 } 278 279 return null; 281 282 } 284 285 public static Element getFirstChildElementNS(Node parent, 286 String uri, String localpart) { 287 288 Node child = parent.getFirstChild(); 290 while (child != null) { 291 if (child.getNodeType() == Node.ELEMENT_NODE) { 292 String childURI = child.getNamespaceURI(); 293 if (childURI != null && childURI.equals(uri) && 294 child.getLocalName().equals(localpart)) { 295 return (Element )child; 296 } 297 } 298 child = child.getNextSibling(); 299 } 300 301 return null; 303 304 } 306 307 public static Element getLastChildElementNS(Node parent, 308 String uri, String localpart) { 309 310 Node child = parent.getLastChild(); 312 while (child != null) { 313 if (child.getNodeType() == Node.ELEMENT_NODE) { 314 String childURI = child.getNamespaceURI(); 315 if (childURI != null && childURI.equals(uri) && 316 child.getLocalName().equals(localpart)) { 317 return (Element )child; 318 } 319 } 320 child = child.getPreviousSibling(); 321 } 322 323 return null; 325 326 } 328 329 public static Element getNextSiblingElementNS(Node node, 330 String uri, String localpart) { 331 332 Node sibling = node.getNextSibling(); 334 while (sibling != null) { 335 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 336 String siblingURI = sibling.getNamespaceURI(); 337 if (siblingURI != null && siblingURI.equals(uri) && 338 sibling.getLocalName().equals(localpart)) { 339 return (Element )sibling; 340 } 341 } 342 sibling = sibling.getNextSibling(); 343 } 344 345 return null; 347 348 } 350 351 public static Element getFirstChildElement(Node parent, String elemNames[]) { 352 353 Node child = parent.getFirstChild(); 355 while (child != null) { 356 if (child.getNodeType() == Node.ELEMENT_NODE) { 357 for (int i = 0; i < elemNames.length; i++) { 358 if (child.getNodeName().equals(elemNames[i])) { 359 return (Element )child; 360 } 361 } 362 } 363 child = child.getNextSibling(); 364 } 365 366 return null; 368 369 } 371 372 public static Element getLastChildElement(Node parent, String elemNames[]) { 373 374 Node child = parent.getLastChild(); 376 while (child != null) { 377 if (child.getNodeType() == Node.ELEMENT_NODE) { 378 for (int i = 0; i < elemNames.length; i++) { 379 if (child.getNodeName().equals(elemNames[i])) { 380 return (Element )child; 381 } 382 } 383 } 384 child = child.getPreviousSibling(); 385 } 386 387 return null; 389 390 } 392 393 public static Element getNextSiblingElement(Node node, String elemNames[]) { 394 395 Node sibling = node.getNextSibling(); 397 while (sibling != null) { 398 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 399 for (int i = 0; i < elemNames.length; i++) { 400 if (sibling.getNodeName().equals(elemNames[i])) { 401 return (Element )sibling; 402 } 403 } 404 } 405 sibling = sibling.getNextSibling(); 406 } 407 408 return null; 410 411 } 413 414 public static Element getFirstChildElementNS(Node parent, 415 String [][] elemNames) { 416 417 Node child = parent.getFirstChild(); 419 while (child != null) { 420 if (child.getNodeType() == Node.ELEMENT_NODE) { 421 for (int i = 0; i < elemNames.length; i++) { 422 String uri = child.getNamespaceURI(); 423 if (uri != null && uri.equals(elemNames[i][0]) && 424 child.getLocalName().equals(elemNames[i][1])) { 425 return (Element )child; 426 } 427 } 428 } 429 child = child.getNextSibling(); 430 } 431 432 return null; 434 435 } 437 438 public static Element getLastChildElementNS(Node parent, 439 String [][] elemNames) { 440 441 Node child = parent.getLastChild(); 443 while (child != null) { 444 if (child.getNodeType() == Node.ELEMENT_NODE) { 445 for (int i = 0; i < elemNames.length; i++) { 446 String uri = child.getNamespaceURI(); 447 if (uri != null && uri.equals(elemNames[i][0]) && 448 child.getLocalName().equals(elemNames[i][1])) { 449 return (Element )child; 450 } 451 } 452 } 453 child = child.getPreviousSibling(); 454 } 455 456 return null; 458 459 } 461 462 public static Element getNextSiblingElementNS(Node node, 463 String [][] elemNames) { 464 465 Node sibling = node.getNextSibling(); 467 while (sibling != null) { 468 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 469 for (int i = 0; i < elemNames.length; i++) { 470 String uri = sibling.getNamespaceURI(); 471 if (uri != null && uri.equals(elemNames[i][0]) && 472 sibling.getLocalName().equals(elemNames[i][1])) { 473 return (Element )sibling; 474 } 475 } 476 } 477 sibling = sibling.getNextSibling(); 478 } 479 480 return null; 482 483 } 485 489 public static Element getFirstChildElement(Node parent, 490 String elemName, 491 String attrName, 492 String attrValue) { 493 494 Node child = parent.getFirstChild(); 496 while (child != null) { 497 if (child.getNodeType() == Node.ELEMENT_NODE) { 498 Element element = (Element )child; 499 if (element.getNodeName().equals(elemName) && 500 element.getAttribute(attrName).equals(attrValue)) { 501 return element; 502 } 503 } 504 child = child.getNextSibling(); 505 } 506 507 return null; 509 510 } 512 516 public static Element getLastChildElement(Node parent, 517 String elemName, 518 String attrName, 519 String attrValue) { 520 521 Node child = parent.getLastChild(); 523 while (child != null) { 524 if (child.getNodeType() == Node.ELEMENT_NODE) { 525 Element element = (Element )child; 526 if (element.getNodeName().equals(elemName) && 527 element.getAttribute(attrName).equals(attrValue)) { 528 return element; 529 } 530 } 531 child = child.getPreviousSibling(); 532 } 533 534 return null; 536 537 } 539 544 public static Element getNextSiblingElement(Node node, 545 String elemName, 546 String attrName, 547 String attrValue) { 548 549 Node sibling = node.getNextSibling(); 551 while (sibling != null) { 552 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 553 Element element = (Element )sibling; 554 if (element.getNodeName().equals(elemName) && 555 element.getAttribute(attrName).equals(attrValue)) { 556 return element; 557 } 558 } 559 sibling = sibling.getNextSibling(); 560 } 561 562 return null; 564 565 } 567 576 public static String getChildText(Node node) { 577 578 if (node == null) { 580 return null; 581 } 582 583 StringBuffer str = new StringBuffer (); 585 Node child = node.getFirstChild(); 586 while (child != null) { 587 short type = child.getNodeType(); 588 if (type == Node.TEXT_NODE) { 589 str.append(child.getNodeValue()); 590 } 591 else if (type == Node.CDATA_SECTION_NODE) { 592 str.append(getChildText(child)); 593 } 594 child = child.getNextSibling(); 595 } 596 597 return str.toString(); 599 600 } 602 } | Popular Tags |