001: package dom;
002:
003: import java.lang.ClassNotFoundException;
004: import java.lang.IllegalAccessException;
005:
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.OutputStream;
009: import java.io.OutputStreamWriter;
010: import java.io.PrintWriter;
011: import java.io.StringBufferInputStream;
012: import java.io.StringWriter;
013: import java.io.UnsupportedEncodingException;
014: import java.io.UTFDataFormatException;
015: import java.io.Writer;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileNotFoundException;
019: import java.io.Reader;
020: import java.io.FileReader;
021: import java.io.InputStreamReader;
022: import java.io.UnsupportedEncodingException;
023:
024: import java.util.Vector;
025:
026: import org.w3c.dom.*;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXNotRecognizedException;
029: import org.xml.sax.SAXNotSupportedException;
030: import org.xml.sax.SAXException;
031:
032: import com.knowgate.dfs.FileSystem;
033: import com.knowgate.debug.DebugFile;
034:
035: public class DOMDocument {
036:
037: //---------------------------------------------------------
038: // Private Member Variables
039:
040: private String sEncoding;
041: private boolean bCanonical;
042: private boolean bValidation;
043: private boolean bNamespaces;
044: private Writer oPrtWritter;
045: private Document oDocument;
046:
047: //---------------------------------------------------------
048: // Constructors
049:
050: /**
051: * <p>Create new DOMDocument</p>
052: * Default properties:<br>
053: * canonical = <b>false</b>
054: * validation = <b>false</b>
055: * namespaces = <b>true</b>
056: * enconding = ISO-8859-1
057: */
058: public DOMDocument() {
059: if (DebugFile.trace)
060: DebugFile.writeln("new DOMDocument()");
061:
062: bCanonical = false;
063: bValidation = false;
064: bNamespaces = true;
065: sEncoding = "UTF-8";
066: } // DOMDocument()
067:
068: /**
069: * <p>Create new DOMDocument</p>
070: * @param sEncodingType Encoding {ISO-8859-1, UTF-8, UTF-16, ASCII-7, ...}
071: * @param bValidateXML Activate/Deactivate schema-validation
072: * @param bCanonicalXML Activate/Deactivate Canonical XML
073: */
074: public DOMDocument(String sEncodingType, boolean bValidateXML,
075: boolean bCanonicalXML) {
076: if (DebugFile.trace)
077: DebugFile.writeln("new DOMDocument(" + sEncodingType + ","
078: + String.valueOf(bValidateXML) + ","
079: + String.valueOf(bCanonicalXML) + ")");
080:
081: bCanonical = bCanonicalXML;
082: bValidation = bValidateXML;
083: bNamespaces = true;
084: sEncoding = sEncodingType;
085: } // DOMDocument()
086:
087: /**
088: * <p>Create a DOMDocument from an XML Document</p>
089: * @param oW3CDoc org.w3c.dom.Document
090: */
091: public DOMDocument(Document oW3CDoc) {
092: if (DebugFile.trace)
093: DebugFile.writeln("new DOMDocument([Document])");
094:
095: bCanonical = false;
096: bValidation = false;
097: bNamespaces = true;
098: sEncoding = "UTF-8";
099: oDocument = oW3CDoc;
100: } // DOMDocument()
101:
102: //---------------------------------------------------------
103: // Methods
104:
105: public Document getDocument() {
106: return oDocument;
107: } // getDocument()
108:
109: //---------------------------------------------------------
110:
111: public Node getRootNode() {
112: return oDocument;
113: } // getRootNode()
114:
115: //---------------------------------------------------------
116:
117: public Element getRootElement() {
118: return oDocument.getDocumentElement();
119: } // getRootElement()
120:
121: //---------------------------------------------------------
122:
123: public String getAttribute(Node oNode, String sAttrName) {
124: NamedNodeMap oMap = oNode.getAttributes();
125: Node oItem = oMap.getNamedItem(sAttrName);
126:
127: if (null == oItem)
128: return null;
129: else
130: return oItem.getNodeValue();
131: } // getAttribute()
132:
133: //---------------------------------------------------------
134:
135: public void setAttribute(Node oNode, String sAttrName,
136: String sAttrValue) {
137: Attr oAttr = oDocument.createAttribute(sAttrName);
138: oAttr.setNodeValue(sAttrValue);
139: ((Element) oNode).setAttributeNode(oAttr);
140: } // setAttribute()
141:
142: //---------------------------------------------------------
143:
144: public boolean getValidation() {
145: return bValidation;
146: }
147:
148: //---------------------------------------------------------
149:
150: public void setValidation(boolean bValidate) {
151: bValidation = bValidate;
152: }
153:
154: //---------------------------------------------------------
155:
156: public boolean getNamesSpaces() {
157: return bNamespaces;
158: }
159:
160: //---------------------------------------------------------
161:
162: public void setNamesSpaces(boolean bNames) {
163: bNamespaces = bNames;
164: }
165:
166: //---------------------------------------------------------
167:
168: public String getWriterEncoding() {
169: return sEncoding;
170: } // getWriterEncoding()
171:
172: // ----------------------------------------------------------
173:
174: public String getTextValue(Element oElement) {
175: return oElement.getFirstChild().getNodeValue();
176: } // getTextValue()
177:
178: // ----------------------------------------------------------
179:
180: public Element getFirstElement(Node oParent) {
181: Node oCurrentNode = null;
182:
183: for (oCurrentNode = oParent.getFirstChild(); oCurrentNode != null; oCurrentNode = oCurrentNode
184: .getNextSibling())
185: if (Node.ELEMENT_NODE == oCurrentNode.getNodeType())
186: break;
187:
188: return (Element) oCurrentNode;
189: } // getFirstElement()
190:
191: // ----------------------------------------------------------
192:
193: public Element getNextElement(Node oPreviousSibling) {
194: Node oCurrentNode = null;
195:
196: for (oCurrentNode = oPreviousSibling.getNextSibling(); oCurrentNode != null; oCurrentNode = oCurrentNode
197: .getNextSibling())
198: if (Node.ELEMENT_NODE == oCurrentNode.getNodeType())
199: break;
200:
201: return (Element) oCurrentNode;
202: } // getNextElement()
203:
204: // ----------------------------------------------------------
205:
206: public Element seekChildByName(Node oParent, String sName) {
207: // Busca el nodo hijo que tenga un nombre dado
208: Node oCurrentNode = null;
209: String sCurrentName;
210:
211: for (oCurrentNode = getFirstElement(oParent); oCurrentNode != null; oCurrentNode = getNextElement(oCurrentNode)) {
212: sCurrentName = oCurrentNode.getNodeName();
213: if (sName.equals(sCurrentName))
214: break;
215: } // next(oCurrentNode)
216:
217: return (Element) oCurrentNode;
218: } // seekChildByName()
219:
220: // ----------------------------------------------------------
221:
222: public Element seekChildByAttr(Node oParent, String sAttrName,
223: String sAttrValue) {
224: // Busca el nodo hijo que tenga un atributo con un valor determinado
225: Node oCurrentNode = null;
226:
227: for (oCurrentNode = getFirstElement(oParent); oCurrentNode != null; oCurrentNode = getNextElement(oCurrentNode)) {
228: if (getAttribute(oCurrentNode, sAttrName)
229: .equals(sAttrValue))
230: ;
231: break;
232: } // next(iNode)
233:
234: return (Element) oCurrentNode;
235: } // seekChildByName()
236:
237: //---------------------------------------------------------
238:
239: public void parseURI(String sURI, String sEncoding)
240: throws ClassNotFoundException, UTFDataFormatException,
241: IllegalAccessException, InstantiationException,
242: FileNotFoundException, UnsupportedEncodingException,
243: IOException, SAXNotSupportedException,
244: SAXNotRecognizedException, Exception {
245:
246: Class oXerces;
247: Reader oReader;
248: DOMParserWrapper oParserWrapper;
249:
250: if (DebugFile.trace) {
251: DebugFile.writeln("Begin DOMDocument.parseURI(" + sURI
252: + "," + sEncoding + ")");
253: DebugFile.incIdent();
254: DebugFile
255: .writeln("Class.forName(dom.wrappers.Xerces).newInstance()");
256: }
257:
258: oXerces = Class.forName("dom.wrappers.Xerces");
259:
260: if (null == oXerces)
261: throw new ClassNotFoundException("dom.wrappers.Xerces");
262:
263: oParserWrapper = (DOMParserWrapper) oXerces.newInstance();
264:
265: if (DebugFile.trace) {
266: DebugFile.writeln("validation="
267: + String.valueOf(bValidation));
268: DebugFile.writeln("namesapces="
269: + String.valueOf(bNamespaces));
270: }
271:
272: oParserWrapper.setFeature(
273: "http://xml.org/sax/features/namespaces", bNamespaces);
274:
275: // Validación XML-Schema
276: oParserWrapper.setFeature(
277: "http://xml.org/sax/features/validation", bValidation);
278: oParserWrapper.setFeature(
279: "http://apache.org/xml/features/validation/schema",
280: bValidation);
281: oParserWrapper
282: .setFeature(
283: "http://apache.org/xml/features/validation/schema-full-checking",
284: bValidation);
285:
286: if (sURI.startsWith("file://")) {
287: File oXMLFile = new File(sURI.substring(7));
288: if (!oXMLFile.exists())
289: throw new FileNotFoundException("DOMDocument.parseURI("
290: + sURI.substring(7) + ") file not found");
291: if (null == sEncoding) {
292: if (DebugFile.trace)
293: DebugFile.writeln("new FileReader("
294: + sURI.substring(7) + ")");
295: oReader = new FileReader(oXMLFile);
296: if (DebugFile.trace)
297: DebugFile
298: .writeln("DOMParserWrapper.parse([InputSource])");
299: } else {
300: oReader = new InputStreamReader(new FileInputStream(
301: oXMLFile), sEncoding);
302: }
303: oDocument = oParserWrapper.parse(new InputSource(oReader));
304: oReader.close();
305: } else {
306: if (DebugFile.trace)
307: DebugFile.writeln("DOMParserWrapper.parse(" + sURI
308: + ")");
309: oDocument = oParserWrapper.parse(sURI);
310: }
311:
312: if (DebugFile.trace) {
313: DebugFile.decIdent();
314: DebugFile.writeln("End DOMDocument.parseURI()");
315: }
316:
317: } // parseURI()
318:
319: //---------------------------------------------------------
320:
321: public void parseURI(String sURI) throws ClassNotFoundException,
322: UTFDataFormatException, FileNotFoundException,
323: IllegalAccessException, InstantiationException,
324: IOException, Exception, SAXNotSupportedException,
325: SAXNotRecognizedException {
326: parseURI(sURI, null);
327: }
328:
329: //---------------------------------------------------------
330:
331: public void parseStream(InputStream oStrm)
332: throws ClassNotFoundException, IllegalAccessException,
333: UTFDataFormatException, InstantiationException,
334: SAXNotSupportedException, SAXNotRecognizedException,
335: Exception {
336:
337: Class oXerces;
338: DOMParserWrapper oParserWrapper;
339:
340: if (DebugFile.trace) {
341: DebugFile.writeln("Begin DOMDocument.parseStream()");
342: DebugFile.incIdent();
343: DebugFile
344: .writeln("Class.forName(dom.wrappers.Xerces).newInstance()");
345: }
346:
347: oXerces = Class.forName("dom.wrappers.Xerces");
348:
349: if (null == oXerces)
350: throw new ClassNotFoundException("dom.wrappers.Xerces");
351:
352: oParserWrapper = (DOMParserWrapper) oXerces.newInstance();
353:
354: if (DebugFile.trace) {
355: DebugFile.writeln("validation="
356: + String.valueOf(bValidation));
357: DebugFile.writeln("namesapces="
358: + String.valueOf(bNamespaces));
359: }
360:
361: oParserWrapper.setFeature(
362: "http://xml.org/sax/features/namespaces", bNamespaces);
363:
364: // Validación XML-Schema
365: oParserWrapper.setFeature(
366: "http://xml.org/sax/features/validation", bValidation);
367: oParserWrapper.setFeature(
368: "http://apache.org/xml/features/validation/schema",
369: bValidation);
370: oParserWrapper
371: .setFeature(
372: "http://apache.org/xml/features/validation/schema-full-checking",
373: bValidation);
374:
375: if (DebugFile.trace)
376: DebugFile.writeln("DOMParserWrapper.parse([InputSource])");
377:
378: oDocument = oParserWrapper.parse(new InputSource(oStrm));
379:
380: if (DebugFile.trace) {
381: DebugFile.decIdent();
382: DebugFile.writeln("End DOMDocument.parseStream()");
383: }
384:
385: } // parseStream()
386:
387: //---------------------------------------------------------
388:
389: /*
390: public void parseString(String sXML)
391: throws ClassNotFoundException, IllegalAccessException, UTFDataFormatException, Exception {
392:
393: Class oXerces;
394: DOMParserWrapper oParserWrapper;
395:
396: if (DebugFile.trace) {
397: DebugFile.writeln("Begin DOMDocument.parseString(" + sXML + ")");
398: DebugFile.incIdent();
399: DebugFile.writeln("Class.forName(dom.wrappers.Xerces).newInstance()");
400: }
401:
402: oXerces = Class.forName("dom.wrappers.Xerces");
403:
404: if (null==oXerces)
405: throw new ClassNotFoundException("dom.wrappers.Xerces");
406:
407: oParserWrapper = (DOMParserWrapper) oXerces.newInstance();
408:
409: if (DebugFile.trace) {
410: DebugFile.writeln("validation=" + String.valueOf(bValidation));
411: DebugFile.writeln("namesapces=" + String.valueOf(bNamespaces));
412: }
413:
414: oParserWrapper.setFeature("http://xml.org/sax/features/namespaces", bNamespaces);
415:
416: // Validación XML-Schema
417: oParserWrapper.setFeature("http://xml.org/sax/features/validation", bValidation);
418: oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema", bValidation);
419: oParserWrapper.setFeature("http://apache.org/xml/features/validation/schema-full-checking", bValidation);
420:
421: if (DebugFile.trace) DebugFile.writeln("new StringBufferInputStream(\n" + sXML + "\n)");
422:
423: StringBufferInputStream oStrStream = new StringBufferInputStream(sXML);
424: InputSource oInputSrc = new InputSource(oStrStream);
425:
426: if (DebugFile.trace) DebugFile.writeln("DOMParserWrapper.parse([InputSource])");
427:
428: oDocument = oParserWrapper.parse(oInputSrc);
429:
430: oInputSrc = null;
431: oStrStream.close();
432: oStrStream = null;
433:
434: if (DebugFile.trace) {
435: DebugFile.decIdent();
436: DebugFile.writeln("End DOMDocument.parseString()");
437: }
438: } // parseString()
439: */
440:
441: //---------------------------------------------------------
442: public void print(OutputStream oOutStream) throws IOException,
443: UnsupportedEncodingException {
444: oPrtWritter = new PrintWriter(new OutputStreamWriter(
445: oOutStream, sEncoding));
446: print(oDocument);
447: } // print()
448:
449: //---------------------------------------------------------
450:
451: public String print() throws IOException,
452: UnsupportedEncodingException {
453: oPrtWritter = new StringWriter();
454: print(oDocument);
455: return oPrtWritter.toString();
456: } // print()
457:
458: //---------------------------------------------------------
459:
460: private void print(Node node) throws IOException {
461: // is there anything to do?
462: if (node == null) {
463: return;
464: }
465:
466: int type = node.getNodeType();
467: switch (type) {
468: // print document
469: case Node.DOCUMENT_NODE: {
470: if (!bCanonical) {
471: String Encoding = this .getWriterEncoding();
472: if (Encoding.equalsIgnoreCase("DEFAULT"))
473: Encoding = "ISO-8859-1";
474: else if (Encoding.equalsIgnoreCase("Unicode"))
475: Encoding = "UTF-16";
476: else
477: //Encoding = MIME2Java.reverse( Encoding );
478: Encoding = "ISO-8859-1";
479:
480: oPrtWritter.write("<?xml version=\"1.0\" encoding=\""
481: + Encoding + "\"?>\n");
482: }
483: //print(((Document)node).getDocumentElement());
484:
485: NodeList children = node.getChildNodes();
486: for (int iChild = 0; iChild < children.getLength(); iChild++) {
487: print(children.item(iChild));
488: }
489: oPrtWritter.flush();
490: break;
491: }
492:
493: // print element with attributes
494: case Node.ELEMENT_NODE: {
495: oPrtWritter.write('<');
496: oPrtWritter.write(node.getNodeName());
497: Attr attrs[] = sortAttributes(node.getAttributes());
498: for (int i = 0; i < attrs.length; i++) {
499: Attr attr = attrs[i];
500: oPrtWritter.write(' ');
501: oPrtWritter.write(attr.getNodeName());
502: oPrtWritter.write("=\"");
503: oPrtWritter.write(normalize(attr.getNodeValue()));
504: oPrtWritter.write('"');
505: }
506: oPrtWritter.write('>');
507: NodeList children = node.getChildNodes();
508: if (children != null) {
509: int len = children.getLength();
510: for (int i = 0; i < len; i++) {
511: print(children.item(i));
512: }
513: }
514: break;
515: }
516:
517: // handle entity reference nodes
518: case Node.ENTITY_REFERENCE_NODE: {
519: if (bCanonical) {
520: NodeList children = node.getChildNodes();
521: if (children != null) {
522: int len = children.getLength();
523: for (int i = 0; i < len; i++) {
524: print(children.item(i));
525: }
526: }
527: } else {
528: oPrtWritter.write('&');
529: oPrtWritter.write(node.getNodeName());
530: oPrtWritter.write(';');
531: }
532: break;
533: }
534:
535: // print cdata sections
536: case Node.CDATA_SECTION_NODE: {
537: if (bCanonical) {
538: oPrtWritter.write(normalize(node.getNodeValue()));
539: } else {
540: oPrtWritter.write("<![CDATA[");
541: oPrtWritter.write(node.getNodeValue());
542: oPrtWritter.write("]]>");
543: }
544: break;
545: }
546:
547: // print text
548: case Node.TEXT_NODE: {
549: oPrtWritter.write(normalize(node.getNodeValue()));
550: break;
551: }
552:
553: // print processing instruction
554: case Node.PROCESSING_INSTRUCTION_NODE: {
555: oPrtWritter.write("<?");
556: oPrtWritter.write(node.getNodeName());
557: String data = node.getNodeValue();
558: if (data != null && data.length() > 0) {
559: oPrtWritter.write(' ');
560: oPrtWritter.write(data);
561: }
562: oPrtWritter.write("?>\n");
563: break;
564: }
565: }
566:
567: if (type == Node.ELEMENT_NODE) {
568: oPrtWritter.write("</");
569: oPrtWritter.write(node.getNodeName());
570: oPrtWritter.write('>');
571: }
572:
573: oPrtWritter.flush();
574: } // print(Node)
575:
576: //---------------------------------------------------------
577:
578: private String normalize(String s) {
579: StringBuffer str = new StringBuffer();
580:
581: int len = (s != null) ? s.length() : 0;
582: for (int i = 0; i < len; i++) {
583: char ch = s.charAt(i);
584: switch (ch) {
585: case '<': {
586: str.append("<");
587: break;
588: }
589: case '>': {
590: str.append(">");
591: break;
592: }
593: case '&': {
594: str.append("&");
595: break;
596: }
597: case '"': {
598: str.append(""");
599: break;
600: }
601: case '\'': {
602: str.append("'");
603: break;
604: }
605: case '\r':
606: case '\n': {
607: if (bCanonical) {
608: str.append("&#");
609: str.append(Integer.toString(ch));
610: str.append(';');
611: break;
612: }
613: // else, default append char
614: }
615: default: {
616: str.append(ch);
617: }
618: }
619: }
620: return (str.toString());
621: } // normalize(String):String
622:
623: //---------------------------------------------------------
624:
625: protected Attr[] sortAttributes(NamedNodeMap attrs) {
626:
627: int len = (attrs != null) ? attrs.getLength() : 0;
628: Attr array[] = new Attr[len];
629: for (int i = 0; i < len; i++) {
630: array[i] = (Attr) attrs.item(i);
631: }
632: for (int i = 0; i < len - 1; i++) {
633: String name = array[i].getNodeName();
634: int index = i;
635: for (int j = i + 1; j < len; j++) {
636: String curName = array[j].getNodeName();
637: if (curName.compareTo(name) < 0) {
638: name = curName;
639: index = j;
640: }
641: }
642: if (index != i) {
643: Attr temp = array[i];
644: array[i] = array[index];
645: array[index] = temp;
646: }
647: }
648: return (array);
649: } // sortAttributes(NamedNodeMap):Attr[]
650:
651: //---------------------------------------------------------
652:
653: public Vector filterChildsByName(Element oParent, String sChildsName) {
654: Element oContainers;
655: NodeList oNodeList;
656: Vector oLinkVctr;
657:
658: // Obtener una referencia al nodo de nivel superior en el documento
659: Node oPageSetNode = getRootNode().getFirstChild();
660: if (oPageSetNode.getNodeName().equals("xml-stylesheet"))
661: oPageSetNode = oPageSetNode.getNextSibling();
662:
663: // Obtener una lista de nodos cuyo nombre sea <container>
664: oNodeList = oParent.getElementsByTagName(sChildsName);
665:
666: // Crear el vector
667: oLinkVctr = new Vector(oNodeList.getLength());
668:
669: // Convertir los nodos DOM en objetos de tipo Page
670: for (int i = 0; i < oNodeList.getLength(); i++)
671: oLinkVctr.add(new DOMSubDocument(oNodeList.item(i)));
672:
673: return oLinkVctr;
674: } // filterChildsByName()
675:
676: } // DOMDocument
|