001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.util.FileUtil;
024: import com.liferay.util.xml.XMLFormatter;
025:
026: import java.io.File;
027: import java.io.IOException;
028:
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.TreeMap;
033:
034: import org.dom4j.Document;
035: import org.dom4j.DocumentException;
036: import org.dom4j.Element;
037: import org.dom4j.io.SAXReader;
038:
039: /**
040: * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class WSDDMerger {
046:
047: public static void main(String[] args) {
048: new WSDDMerger(args[0], args[1]);
049: }
050:
051: public static void merge(String source, String destination)
052: throws DocumentException, IOException {
053:
054: // Source
055:
056: File sourceFile = new File(source);
057:
058: SAXReader reader = new SAXReader();
059:
060: Document doc = reader.read(sourceFile);
061:
062: Element root = doc.getRootElement();
063:
064: List sourceServices = root.elements("service");
065:
066: if (sourceServices.size() == 0) {
067: return;
068: }
069:
070: // Destination
071:
072: File destinationFile = new File(destination);
073:
074: reader = new SAXReader();
075:
076: doc = reader.read(destinationFile);
077:
078: root = doc.getRootElement();
079:
080: Map servicesMap = new TreeMap();
081:
082: Iterator itr = root.elements("service").iterator();
083:
084: while (itr.hasNext()) {
085: Element service = (Element) itr.next();
086:
087: String name = service.attributeValue("name");
088:
089: servicesMap.put(name, service);
090:
091: service.detach();
092: }
093:
094: itr = sourceServices.iterator();
095:
096: while (itr.hasNext()) {
097: Element service = (Element) itr.next();
098:
099: String name = service.attributeValue("name");
100:
101: servicesMap.put(name, service);
102:
103: service.detach();
104: }
105:
106: itr = servicesMap.entrySet().iterator();
107:
108: while (itr.hasNext()) {
109: Map.Entry entry = (Map.Entry) itr.next();
110:
111: Element service = (Element) entry.getValue();
112:
113: root.add(service);
114: }
115:
116: FileUtil.write(destination, XMLFormatter.toString(doc), true);
117: }
118:
119: public WSDDMerger(String source, String destination) {
120: try {
121: merge(source, destination);
122: } catch (Exception e) {
123: e.printStackTrace();
124: }
125: }
126:
127: }
|