1   /**
2    * Copyright (c) 2000-2007 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 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 servicesMap = new TreeMap();
83  
84          Iterator itr = root.elements("service").iterator();
85  
86          while (itr.hasNext()) {
87              Element service = (Element)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 = (Element)itr.next();
100 
101             String name = service.attributeValue("name");
102 
103             servicesMap.put(name, service);
104 
105             service.detach();
106         }
107 
108         itr = servicesMap.entrySet().iterator();
109 
110         while (itr.hasNext()) {
111             Map.Entry entry = (Map.Entry)itr.next();
112 
113             Element service = (Element)entry.getValue();
114 
115             root.add(service);
116         }
117 
118         FileUtil.write(destination, XMLFormatter.toString(doc), true);
119     }
120 
121     public WSDDMerger(String source, String destination) {
122         try {
123             merge(source, destination);
124         }
125         catch (Exception e) {
126             e.printStackTrace();
127         }
128     }
129 
130 }