001: package com.sun.portal.dpmerge;
002:
003: import java.io.*;
004: import javax.xml.parsers.*;
005: import javax.xml.transform.*;
006: import javax.xml.transform.dom.*;
007: import javax.xml.transform.stream.*;
008: import org.xml.sax.*;
009: import org.xml.sax.helpers.*;
010: import org.w3c.dom.*;
011:
012: import com.sun.portal.dpmerge.*;
013: import com.sun.portal.desktop.dp.xml.*;
014:
015: public class DPMerge {
016:
017: static public void main(String[] args) {
018: DPMerge xmlMerge = new DPMerge(args);
019: }
020:
021: DPMerge(String[] args) {
022: try {
023: DocumentBuilderFactory dbf = DocumentBuilderFactory
024: .newInstance();
025: dbf.setValidating(false);
026:
027: DocumentBuilder db = dbf.newDocumentBuilder();
028:
029: XMLDPEntityResolver entityResolver = new XMLDPEntityResolver();
030: db.setEntityResolver(entityResolver);
031:
032: MyErrorHandler myErrorHandler = new MyErrorHandler();
033: db.setErrorHandler(myErrorHandler);
034:
035: Document destDoc = null;
036: String doctypePublic = null;
037: String doctypeSystem = null;
038:
039: for (int i = 0; i < args.length; i++) {
040: if (destDoc == null) {
041: destDoc = db.parse(new InputSource(new FileReader(
042: args[i])));
043: DocumentType doctype = destDoc.getDoctype();
044: if (doctype != null) {
045: doctypePublic = doctype.getPublicId();
046: doctypeSystem = doctype.getSystemId();
047: }
048: } else {
049: Document doc = db.parse(new InputSource(
050: new FileReader(args[i])));
051: NodeList nodeList1 = doc.getChildNodes();
052: for (int j = 0; j < nodeList1.getLength(); j++) {
053: NodeList nodeList2 = nodeList1.item(j)
054: .getChildNodes();
055: for (int k = 0; k < nodeList2.getLength(); k++) {
056: Node node = FindDestNode(destDoc, nodeList2
057: .item(k));
058: if (node != null) {
059: NodeList nodeList3 = nodeList2.item(k)
060: .getChildNodes();
061: for (int l = 0; l < nodeList3
062: .getLength(); l++) {
063: node.appendChild(destDoc
064: .importNode(nodeList3
065: .item(l), true));
066: }
067: }
068: }
069: }
070: }
071: }
072:
073: Transformer t = TransformerFactory.newInstance()
074: .newTransformer();
075: if (doctypePublic != null) {
076: t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
077: doctypePublic);
078: }
079: if (doctypeSystem != null) {
080: t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
081: doctypeSystem);
082: }
083: DOMSource ds = new DOMSource(destDoc);
084: StringWriter sw = new StringWriter();
085: StreamResult sr = new StreamResult(sw);
086: t.transform(ds, sr);
087: System.out.println(sw.getBuffer().toString());
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: private Node FindDestNode(Document destDoc, Node node) {
094: if (node instanceof Element) {
095: NodeList nodeList1 = destDoc.getChildNodes();
096: for (int j = 0; j < nodeList1.getLength(); j++) {
097: NodeList nodeList2 = nodeList1.item(j).getChildNodes();
098: for (int k = 0; k < nodeList2.getLength(); k++) {
099: Node testNode = nodeList2.item(k);
100: if (testNode.getNodeName().equals(
101: node.getNodeName())) {
102: return testNode;
103: }
104: }
105: }
106: }
107: return null;
108: }
109:
110: }
|