1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.xml.Document;
19  import com.liferay.portal.kernel.xml.DocumentException;
20  import com.liferay.portal.kernel.xml.Element;
21  import com.liferay.portal.kernel.xml.SAXReaderUtil;
22  import com.liferay.portal.util.InitUtil;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.TreeMap;
31  
32  /**
33   * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class WSDDMerger {
38  
39      public static void main(String[] args) {
40          InitUtil.initWithSpring();
41  
42          new WSDDMerger(args[0], args[1]);
43      }
44  
45      public static void merge(String source, String destination)
46          throws DocumentException, IOException {
47  
48          // Source
49  
50          File sourceFile = new File(source);
51  
52          Document doc = SAXReaderUtil.read(sourceFile);
53  
54          Element root = doc.getRootElement();
55  
56          List<Element> sourceServices = root.elements("service");
57  
58          if (sourceServices.size() == 0) {
59              return;
60          }
61  
62          // Destination
63  
64          File destinationFile = new File(destination);
65  
66          doc = SAXReaderUtil.read(destinationFile);
67  
68          root = doc.getRootElement();
69  
70          Map<String, Element> servicesMap = new TreeMap<String, Element>();
71  
72          Iterator<Element> itr = root.elements("service").iterator();
73  
74          while (itr.hasNext()) {
75              Element service = itr.next();
76  
77              String name = service.attributeValue("name");
78  
79              servicesMap.put(name, service);
80  
81              service.detach();
82          }
83  
84          itr = sourceServices.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          for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
97              Element service = entry.getValue();
98  
99              root.add(service);
100         }
101 
102         FileUtil.write(destination, doc.formattedString(), true);
103     }
104 
105     public WSDDMerger(String source, String destination) {
106         try {
107             merge(source, destination);
108         }
109         catch (Exception e) {
110             e.printStackTrace();
111         }
112     }
113 
114 }