001: package com.sun.portal.webxmlmerge;
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.webxmlmerge.*;
013:
014: public class WebxmlMerge {
015:
016: static public void main(String[] args) {
017: WebxmlMerge xmlMerge = new WebxmlMerge(args);
018: }
019:
020: WebxmlMerge(String[] args) {
021: try {
022: DocumentBuilderFactory dbf = DocumentBuilderFactory
023: .newInstance();
024: dbf.setValidating(false);
025:
026: DocumentBuilder db = dbf.newDocumentBuilder();
027:
028: MyErrorHandler myErrorHandler = new MyErrorHandler();
029: db.setErrorHandler(myErrorHandler);
030:
031: Document destDoc = null;
032: String doctypePublic = null;
033: String doctypeSystem = null;
034:
035: for (int i = 0; i < args.length; i++) {
036: myErrorHandler.setFilename(args[i]);
037:
038: if (destDoc == null) {
039: destDoc = db.parse(new InputSource(
040: new InputStreamReader(new FileInputStream(
041: args[i]), "UTF-8")));
042: DocumentType doctype = destDoc.getDoctype();
043: if (doctype != null) {
044: doctypePublic = doctype.getPublicId();
045: doctypeSystem = doctype.getSystemId();
046: }
047: } else {
048: Document doc = db.parse(new InputSource(
049: new InputStreamReader(new FileInputStream(
050: args[i]), "UTF-8")));
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: Try(destDoc, nodeList2.item(k));
057: }
058: }
059: }
060: }
061:
062: NodeList nodeList1 = destDoc.getChildNodes();
063: for (int i = 0; i < nodeList1.getLength(); i++) {
064: NodeList nodeList2 = nodeList1.item(i).getChildNodes();
065: for (int j = 0; j < nodeList2.getLength(); j++) {
066: Node testNode = nodeList2.item(j);
067: if (testNode instanceof Element
068: && !testNode.hasChildNodes()) {
069: destDoc.getDocumentElement().removeChild(
070: testNode);
071: }
072: }
073: }
074:
075: Transformer t = TransformerFactory.newInstance()
076: .newTransformer();
077: if (doctypePublic != null) {
078: t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
079: doctypePublic);
080: }
081: if (doctypeSystem != null) {
082: t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
083: doctypeSystem);
084: }
085: DOMSource ds = new DOMSource(destDoc);
086: StringWriter sw = new StringWriter();
087: StreamResult sr = new StreamResult(sw);
088: t.transform(ds, sr);
089: System.out.println(sw.getBuffer().toString());
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093: }
094:
095: private void Try(Document destDoc, Node node) {
096: boolean found = false;
097: if (node instanceof Element) {
098: NodeList nodeList1 = destDoc.getChildNodes();
099: for (int i = 0; i < nodeList1.getLength(); i++) {
100: NodeList nodeList2 = nodeList1.item(i).getChildNodes();
101: for (int j = 0; j < nodeList2.getLength(); j++) {
102: Node testNode = nodeList2.item(j);
103: if (testNode.getNodeName().equals(
104: node.getNodeName())) {
105: found = true;
106: } else if (found == true) {
107: destDoc.getDocumentElement().insertBefore(
108: destDoc.importNode(node, true),
109: testNode);
110: return;
111: }
112: }
113: }
114: }
115: if (found == true) {
116: destDoc.getDocumentElement().insertBefore(
117: destDoc.importNode(node, true), null);
118: }
119: }
120:
121: }
|