DPMerge.java :  » Portal » Open-Portal » com » sun » portal » dpmerge » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » dpmerge » DPMerge.java
package com.sun.portal.dpmerge;

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;

import com.sun.portal.dpmerge.*;
import com.sun.portal.desktop.dp.xml.*;

public class DPMerge {

  static public void main(String[] args) {
    DPMerge xmlMerge = new DPMerge(args);
  }

  DPMerge(String[] args) {
    try {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setValidating(false);

      DocumentBuilder db = dbf.newDocumentBuilder();

      XMLDPEntityResolver entityResolver = new XMLDPEntityResolver();
      db.setEntityResolver(entityResolver);

      MyErrorHandler myErrorHandler = new MyErrorHandler();
      db.setErrorHandler(myErrorHandler);

      Document destDoc = null;
      String doctypePublic = null;
      String doctypeSystem = null;

      for (int i = 0; i < args.length; i++) {
  if (destDoc == null) {
    destDoc = db.parse(new InputSource(new FileReader(args[i])));
    DocumentType doctype = destDoc.getDoctype();
    if (doctype != null) {
      doctypePublic = doctype.getPublicId();
      doctypeSystem = doctype.getSystemId();
    }
  } else {
    Document doc = db.parse(new InputSource(new FileReader(args[i])));
    NodeList nodeList1 = doc.getChildNodes();
    for (int j = 0; j < nodeList1.getLength(); j++) {
      NodeList nodeList2 = nodeList1.item(j).getChildNodes();
      for (int k = 0; k < nodeList2.getLength(); k++) {
        Node node = FindDestNode(destDoc, nodeList2.item(k));
        if (node != null) { 
    NodeList nodeList3 = nodeList2.item(k).getChildNodes();
    for (int l = 0; l < nodeList3.getLength(); l++) {
      node.appendChild(destDoc.importNode(nodeList3.item(l), true));
    }
        }
      }
    }
  }
      }

      Transformer t = TransformerFactory.newInstance().newTransformer();
      if (doctypePublic != null) {
  t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctypePublic);
      }
      if (doctypeSystem != null) {
  t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctypeSystem);
      }
      DOMSource ds = new DOMSource(destDoc);
      StringWriter sw = new StringWriter();
      StreamResult sr = new StreamResult(sw);
      t.transform(ds, sr);
      System.out.println(sw.getBuffer().toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private Node FindDestNode(Document destDoc, Node node) {
    if (node instanceof Element) {
      NodeList nodeList1 = destDoc.getChildNodes();
      for (int j = 0; j < nodeList1.getLength(); j++) {
  NodeList nodeList2 = nodeList1.item(j).getChildNodes();
  for (int k = 0; k < nodeList2.getLength(); k++) {
    Node testNode = nodeList2.item(k);
    if (testNode.getNodeName().equals(node.getNodeName())) {
      return testNode;
    }
  }
      }
    }
    return null;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.