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