1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.util.FileUtil;
26  import com.liferay.util.xml.XMLFormatter;
27  
28  import java.io.File;
29  import java.io.IOException;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.TreeMap;
35  
36  import org.dom4j.Document;
37  import org.dom4j.DocumentException;
38  import org.dom4j.Element;
39  import org.dom4j.io.SAXReader;
40  
41  /**
42   * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class WSDDMerger {
48  
49      public static void main(String[] args) {
50          new WSDDMerger(args[0], args[1]);
51      }
52  
53      public static void merge(String source, String destination)
54          throws DocumentException, IOException {
55  
56          // Source
57  
58          File sourceFile = new File(source);
59  
60          SAXReader reader = new SAXReader();
61  
62          Document doc = reader.read(sourceFile);
63  
64          Element root = doc.getRootElement();
65  
66          List<Element> sourceServices = root.elements("service");
67  
68          if (sourceServices.size() == 0) {
69              return;
70          }
71  
72          // Destination
73  
74          File destinationFile = new File(destination);
75  
76          reader = new SAXReader();
77  
78          doc = reader.read(destinationFile);
79  
80          root = doc.getRootElement();
81  
82          Map<String, Element> servicesMap = new TreeMap<String, Element>();
83  
84          Iterator<Element> itr = root.elements("service").iterator();
85  
86          while (itr.hasNext()) {
87              Element service = itr.next();
88  
89              String name = service.attributeValue("name");
90  
91              servicesMap.put(name, service);
92  
93              service.detach();
94          }
95  
96          itr = sourceServices.iterator();
97  
98          while (itr.hasNext()) {
99              Element service = itr.next();
100 
101             String name = service.attributeValue("name");
102 
103             servicesMap.put(name, service);
104 
105             service.detach();
106         }
107 
108         for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
109             Element service = entry.getValue();
110 
111             root.add(service);
112         }
113 
114         FileUtil.write(destination, XMLFormatter.toString(doc), true);
115     }
116 
117     public WSDDMerger(String source, String destination) {
118         try {
119             merge(source, destination);
120         }
121         catch (Exception e) {
122             e.printStackTrace();
123         }
124     }
125 
126 }