1 package dom; 2 3 import java.lang.ClassNotFoundException ; 4 import java.lang.IllegalAccessException ; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 import java.io.OutputStreamWriter ; 10 import java.io.PrintWriter ; 11 import java.io.StringBufferInputStream ; 12 import java.io.StringWriter ; 13 import java.io.UnsupportedEncodingException ; 14 import java.io.UTFDataFormatException ; 15 import java.io.Writer ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.Reader ; 20 import java.io.FileReader ; 21 import java.io.InputStreamReader ; 22 import java.io.UnsupportedEncodingException ; 23 24 import java.util.Vector ; 25 26 import org.w3c.dom.*; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXNotRecognizedException ; 29 import org.xml.sax.SAXNotSupportedException ; 30 import org.xml.sax.SAXException ; 31 32 import com.knowgate.dfs.FileSystem; 33 import com.knowgate.debug.DebugFile; 34 35 public class DOMDocument { 36 37 40 private String sEncoding; 41 private boolean bCanonical; 42 private boolean bValidation; 43 private boolean bNamespaces; 44 private Writer oPrtWritter; 45 private Document oDocument; 46 47 50 58 public DOMDocument() { 59 if (DebugFile.trace) DebugFile.writeln("new DOMDocument()"); 60 61 bCanonical = false; 62 bValidation = false; 63 bNamespaces = true; 64 sEncoding = "UTF-8"; 65 } 67 73 public DOMDocument(String sEncodingType, boolean bValidateXML, boolean bCanonicalXML) { 74 if (DebugFile.trace) DebugFile.writeln("new DOMDocument(" + sEncodingType + "," + String.valueOf(bValidateXML) + "," + String.valueOf(bCanonicalXML) + ")"); 75 76 bCanonical = bCanonicalXML; 77 bValidation = bValidateXML; 78 bNamespaces = true; 79 sEncoding = sEncodingType; 80 } 82 86 public DOMDocument(Document oW3CDoc) { 87 if (DebugFile.trace) DebugFile.writeln("new DOMDocument([Document])"); 88 89 bCanonical = false; 90 bValidation = false; 91 bNamespaces = true; 92 sEncoding = "UTF-8"; 93 oDocument = oW3CDoc; 94 } 96 99 public Document getDocument() { 100 return oDocument; 101 } 103 105 public Node getRootNode() { 106 return oDocument; 107 } 109 111 public Element getRootElement() { 112 return oDocument.getDocumentElement(); 113 } 115 117 public String getAttribute(Node oNode, String sAttrName) { 118 NamedNodeMap oMap = oNode.getAttributes(); 119 Node oItem = oMap.getNamedItem(sAttrName); 120 121 if (null==oItem) 122 return null; 123 else 124 return oItem.getNodeValue(); 125 } 127 129 public void setAttribute(Node oNode, String sAttrName, String sAttrValue) { 130 Attr oAttr = oDocument.createAttribute(sAttrName); 131 oAttr.setNodeValue(sAttrValue); 132 ((Element) oNode).setAttributeNode(oAttr); 133 } 135 136 138 public boolean getValidation() { 139 return bValidation; 140 } 141 142 144 public void setValidation(boolean bValidate) { 145 bValidation = bValidate; 146 } 147 148 150 public boolean getNamesSpaces() { 151 return bNamespaces; 152 } 153 154 156 public void setNamesSpaces(boolean bNames) { 157 bNamespaces = bNames; 158 } 159 160 162 public String getWriterEncoding() { 163 return sEncoding; 164 } 166 168 public String getTextValue(Element oElement) { 169 return oElement.getFirstChild().getNodeValue(); 170 } 172 174 public Element getFirstElement(Node oParent) { 175 Node oCurrentNode = null; 176 177 for (oCurrentNode=oParent.getFirstChild(); oCurrentNode!=null; oCurrentNode=oCurrentNode.getNextSibling()) 178 if (Node.ELEMENT_NODE==oCurrentNode.getNodeType()) 179 break; 180 181 return (Element) oCurrentNode; 182 } 184 186 public Element getNextElement(Node oPreviousSibling) { 187 Node oCurrentNode = null; 188 189 for (oCurrentNode=oPreviousSibling.getNextSibling(); oCurrentNode!=null; oCurrentNode=oCurrentNode.getNextSibling()) 190 if (Node.ELEMENT_NODE==oCurrentNode.getNodeType()) 191 break; 192 193 return (Element) oCurrentNode; 194 } 196 198 public Element seekChildByName(Node oParent, String sName) { 199 Node oCurrentNode = null; 201 String sCurrentName; 202 203 for (oCurrentNode=getFirstElement(oParent); oCurrentNode!=null; oCurrentNode=getNextElement(oCurrentNode)) { 204 sCurrentName = oCurrentNode.getNodeName(); 205 if (sName.equals(sCurrentName)) 206 break; 207 } 209 return (Element) oCurrentNode; 210 } 212 214 public Element seekChildByAttr(Node oParent, String sAttrName, String sAttrValue) { 215 Node oCurrentNode = null; 217 218 for (oCurrentNode=getFirstElement(oParent); oCurrentNode!=null; oCurrentNode=getNextElement(oCurrentNode)) { 219 if (getAttribute(oCurrentNode, sAttrName).equals(sAttrValue)); 220 break; 221 } 223 return (Element) oCurrentNode; 224 } 226 228 public void parseURI(String sURI, String sEncoding) 229 throws ClassNotFoundException , UTFDataFormatException , 230 IllegalAccessException , InstantiationException , 231 FileNotFoundException , UnsupportedEncodingException , IOException , 232 SAXNotSupportedException , SAXNotRecognizedException , Exception { 233 234 Class oXerces; 235 Reader oReader; 236 DOMParserWrapper oParserWrapper; 237 238 if (DebugFile.trace) { 239 DebugFile.writeln("Begin DOMDocument.parseURI(" + sURI + "," + sEncoding + ")"); 240 DebugFile.incIdent(); 241 DebugFile.writeln("Class.forName(dom.wrappers.Xerces).newInstance()"); 242 } 243 244 oXerces = Class.forName("dom.wrappers.Xerces"); 245 246 if (null==oXerces) 247 throw new ClassNotFoundException ("dom.wrappers.Xerces"); 248 249 oParserWrapper = (DOMParserWrapper) oXerces.newInstance(); 250 251 if (DebugFile.trace) { 252 DebugFile.writeln("validation=" + String.valueOf(bValidation)); 253 DebugFile.writeln("namesapces=" + String.valueOf(bNamespaces)); 254 } 255 256 oParserWrapper.setFeature("http://xml.org/sax/features/namespaces", bNamespaces); 257 258 oParserWrapper.setFeature("http://xml.org/sax/features/validation", bValidation); 260 oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema", bValidation); 261 oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema-full-checking", bValidation); 262 263 if (sURI.startsWith("file://")) { 264 File oXMLFile = new File (sURI.substring(7)); 265 if (!oXMLFile.exists()) throw new FileNotFoundException ("DOMDocument.parseURI(" + sURI.substring(7) + ") file not found"); 266 if (null==sEncoding) { 267 if (DebugFile.trace) DebugFile.writeln("new FileReader(" + sURI.substring(7) + ")"); 268 oReader = new FileReader (oXMLFile); 269 if (DebugFile.trace) DebugFile.writeln("DOMParserWrapper.parse([InputSource])"); 270 } else { 271 oReader = new InputStreamReader (new FileInputStream (oXMLFile), sEncoding); 272 } 273 oDocument = oParserWrapper.parse(new InputSource (oReader)); 274 oReader.close(); 275 } 276 else { 277 if (DebugFile.trace) DebugFile.writeln("DOMParserWrapper.parse(" + sURI + ")"); 278 oDocument = oParserWrapper.parse(sURI); 279 } 280 281 if (DebugFile.trace) { 282 DebugFile.decIdent(); 283 DebugFile.writeln("End DOMDocument.parseURI()"); 284 } 285 286 } 288 290 public void parseURI(String sURI) 291 throws ClassNotFoundException , UTFDataFormatException , 292 FileNotFoundException , IllegalAccessException , 293 InstantiationException , IOException , Exception , 294 SAXNotSupportedException , SAXNotRecognizedException { 295 parseURI(sURI,null); 296 } 297 298 300 public void parseStream(InputStream oStrm) 301 throws ClassNotFoundException , IllegalAccessException , UTFDataFormatException , 302 InstantiationException , SAXNotSupportedException , SAXNotRecognizedException , 303 Exception { 304 305 Class oXerces; 306 DOMParserWrapper oParserWrapper; 307 308 if (DebugFile.trace) { 309 DebugFile.writeln("Begin DOMDocument.parseStream()"); 310 DebugFile.incIdent(); 311 DebugFile.writeln("Class.forName(dom.wrappers.Xerces).newInstance()"); 312 } 313 314 oXerces = Class.forName("dom.wrappers.Xerces"); 315 316 if (null==oXerces) 317 throw new ClassNotFoundException ("dom.wrappers.Xerces"); 318 319 oParserWrapper = (DOMParserWrapper) oXerces.newInstance(); 320 321 if (DebugFile.trace) { 322 DebugFile.writeln("validation=" + String.valueOf(bValidation)); 323 DebugFile.writeln("namesapces=" + String.valueOf(bNamespaces)); 324 } 325 326 oParserWrapper.setFeature("http://xml.org/sax/features/namespaces", bNamespaces); 327 328 oParserWrapper.setFeature("http://xml.org/sax/features/validation", bValidation); 330 oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema", bValidation); 331 oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema-full-checking", bValidation); 332 333 if (DebugFile.trace) DebugFile.writeln("DOMParserWrapper.parse([InputSource])"); 334 335 oDocument = oParserWrapper.parse(new InputSource (oStrm)); 336 337 if (DebugFile.trace) { 338 DebugFile.decIdent(); 339 DebugFile.writeln("End DOMDocument.parseStream()"); 340 } 341 342 } 344 346 397 398 400 public void print(OutputStream oOutStream) throws IOException ,UnsupportedEncodingException { 401 oPrtWritter = new PrintWriter (new OutputStreamWriter (oOutStream, sEncoding)); 402 print(oDocument); 403 } 405 407 public String print() throws IOException ,UnsupportedEncodingException { 408 oPrtWritter = new StringWriter (); 409 print(oDocument); 410 return oPrtWritter.toString(); 411 } 413 415 private void print(Node node) throws IOException { 416 if ( node == null ) { 418 return; 419 } 420 421 int type = node.getNodeType(); 422 switch ( type ) { 423 case Node.DOCUMENT_NODE: { 425 if ( !bCanonical ) { 426 String Encoding = this.getWriterEncoding(); 427 if ( Encoding.equalsIgnoreCase( "DEFAULT" ) ) 428 Encoding = "ISO-8859-1"; 429 else if ( Encoding.equalsIgnoreCase( "Unicode" ) ) 430 Encoding = "UTF-16"; 431 else 432 Encoding = "ISO-8859-1"; 434 435 oPrtWritter.write("<?xml version=\"1.0\" encoding=\""+ 436 Encoding + "\"?>\n"); 437 } 438 440 NodeList children = node.getChildNodes(); 441 for ( int iChild = 0; iChild < children.getLength(); iChild++ ) { 442 print(children.item(iChild)); 443 } 444 oPrtWritter.flush(); 445 break; 446 } 447 448 case Node.ELEMENT_NODE: { 450 oPrtWritter.write('<'); 451 oPrtWritter.write(node.getNodeName()); 452 Attr attrs[] = sortAttributes(node.getAttributes()); 453 for ( int i = 0; i < attrs.length; i++ ) { 454 Attr attr = attrs[i]; 455 oPrtWritter.write(' '); 456 oPrtWritter.write(attr.getNodeName()); 457 oPrtWritter.write("=\""); 458 oPrtWritter.write(normalize(attr.getNodeValue())); 459 oPrtWritter.write('"'); 460 } 461 oPrtWritter.write('>'); 462 NodeList children = node.getChildNodes(); 463 if ( children != null ) { 464 int len = children.getLength(); 465 for ( int i = 0; i < len; i++ ) { 466 print(children.item(i)); 467 } 468 } 469 break; 470 } 471 472 case Node.ENTITY_REFERENCE_NODE: { 474 if ( bCanonical ) { 475 NodeList children = node.getChildNodes(); 476 if ( children != null ) { 477 int len = children.getLength(); 478 for ( int i = 0; i < len; i++ ) { 479 print(children.item(i)); 480 } 481 } 482 } else { 483 oPrtWritter.write('&'); 484 oPrtWritter.write(node.getNodeName()); 485 oPrtWritter.write(';'); 486 } 487 break; 488 } 489 490 case Node.CDATA_SECTION_NODE: { 492 if ( bCanonical ) { 493 oPrtWritter.write(normalize(node.getNodeValue())); 494 } else { 495 oPrtWritter.write("<![CDATA["); 496 oPrtWritter.write(node.getNodeValue()); 497 oPrtWritter.write("]]>"); 498 } 499 break; 500 } 501 502 case Node.TEXT_NODE: { 504 oPrtWritter.write(normalize(node.getNodeValue())); 505 break; 506 } 507 508 case Node.PROCESSING_INSTRUCTION_NODE: { 510 oPrtWritter.write("<?"); 511 oPrtWritter.write(node.getNodeName()); 512 String data = node.getNodeValue(); 513 if ( data != null && data.length() > 0 ) { 514 oPrtWritter.write(' '); 515 oPrtWritter.write(data); 516 } 517 oPrtWritter.write("?>\n"); 518 break; 519 } 520 } 521 522 if ( type == Node.ELEMENT_NODE ) { 523 oPrtWritter.write("</"); 524 oPrtWritter.write(node.getNodeName()); 525 oPrtWritter.write('>'); 526 } 527 528 oPrtWritter.flush(); 529 } 531 533 private String normalize(String s) { 534 StringBuffer str = new StringBuffer (); 535 536 int len = (s != null) ? s.length() : 0; 537 for ( int i = 0; i < len; i++ ) { 538 char ch = s.charAt(i); 539 switch ( ch ) { 540 case '<': { 541 str.append("<"); 542 break; 543 } 544 case '>': { 545 str.append(">"); 546 break; 547 } 548 case '&': { 549 str.append("&"); 550 break; 551 } 552 case '"': { 553 str.append("""); 554 break; 555 } 556 case '\'': { 557 str.append("'"); 558 break; 559 } 560 case '\r': 561 case '\n': { 562 if ( bCanonical ) { 563 str.append("&#"); 564 str.append(Integer.toString(ch)); 565 str.append(';'); 566 break; 567 } 568 } 570 default: { 571 str.append(ch); 572 } 573 } 574 } 575 return(str.toString()); 576 } 578 580 protected Attr[] sortAttributes(NamedNodeMap attrs) { 581 582 int len = (attrs != null) ? attrs.getLength() : 0; 583 Attr array[] = new Attr[len]; 584 for ( int i = 0; i < len; i++ ) { 585 array[i] = (Attr)attrs.item(i); 586 } 587 for ( int i = 0; i < len - 1; i++ ) { 588 String name = array[i].getNodeName(); 589 int index = i; 590 for ( int j = i + 1; j < len; j++ ) { 591 String curName = array[j].getNodeName(); 592 if ( curName.compareTo(name) < 0 ) { 593 name = curName; 594 index = j; 595 } 596 } 597 if ( index != i ) { 598 Attr temp = array[i]; 599 array[i] = array[index]; 600 array[index] = temp; 601 } 602 } 603 return(array); 604 } 606 608 public Vector filterChildsByName(Element oParent, String sChildsName) { 609 Element oContainers; 610 NodeList oNodeList; 611 Vector oLinkVctr; 612 613 Node oPageSetNode = getRootNode().getFirstChild(); 615 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 616 617 oNodeList = oParent.getElementsByTagName(sChildsName); 619 620 oLinkVctr = new Vector (oNodeList.getLength()); 622 623 for (int i=0; i<oNodeList.getLength(); i++) 625 oLinkVctr.add(new DOMSubDocument (oNodeList.item(i))); 626 627 return oLinkVctr; 628 } 630 } | Popular Tags |