1   /**
2    * Copyright (c) 2000-2009 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.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.xml.Document;
27  import com.liferay.portal.kernel.xml.DocumentException;
28  import com.liferay.portal.kernel.xml.Element;
29  import com.liferay.portal.kernel.xml.SAXReaderUtil;
30  import com.liferay.portal.util.InitUtil;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.TreeMap;
39  
40  /**
41   * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class WSDDMerger {
47  
48      public static void main(String[] args) {
49          InitUtil.initWithSpring();
50  
51          new WSDDMerger(args[0], args[1]);
52      }
53  
54      public static void merge(String source, String destination)
55          throws DocumentException, IOException {
56  
57          // Source
58  
59          File sourceFile = new File(source);
60  
61          Document doc = SAXReaderUtil.read(sourceFile);
62  
63          Element root = doc.getRootElement();
64  
65          List<Element> sourceServices = root.elements("service");
66  
67          if (sourceServices.size() == 0) {
68              return;
69          }
70  
71          // Destination
72  
73          File destinationFile = new File(destination);
74  
75          doc = SAXReaderUtil.read(destinationFile);
76  
77          root = doc.getRootElement();
78  
79          Map<String, Element> servicesMap = new TreeMap<String, Element>();
80  
81          Iterator<Element> itr = root.elements("service").iterator();
82  
83          while (itr.hasNext()) {
84              Element service = itr.next();
85  
86              String name = service.attributeValue("name");
87  
88              servicesMap.put(name, service);
89  
90              service.detach();
91          }
92  
93          itr = sourceServices.iterator();
94  
95          while (itr.hasNext()) {
96              Element service = itr.next();
97  
98              String name = service.attributeValue("name");
99  
100             servicesMap.put(name, service);
101 
102             service.detach();
103         }
104 
105         for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
106             Element service = entry.getValue();
107 
108             root.add(service);
109         }
110 
111         FileUtil.write(destination, doc.formattedString(), true);
112     }
113 
114     public WSDDMerger(String source, String destination) {
115         try {
116             merge(source, destination);
117         }
118         catch (Exception e) {
119             e.printStackTrace();
120         }
121     }
122 
123 }