1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.xml.Document;
24  import com.liferay.portal.kernel.xml.DocumentException;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.util.InitUtil;
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  /**
38   * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class WSDDMerger {
44  
45      public static void main(String[] args) {
46          InitUtil.initWithSpring();
47  
48          new WSDDMerger(args[0], args[1]);
49      }
50  
51      public static void merge(String source, String destination)
52          throws DocumentException, IOException {
53  
54          // Source
55  
56          File sourceFile = new File(source);
57  
58          Document doc = SAXReaderUtil.read(sourceFile);
59  
60          Element root = doc.getRootElement();
61  
62          List<Element> sourceServices = root.elements("service");
63  
64          if (sourceServices.size() == 0) {
65              return;
66          }
67  
68          // Destination
69  
70          File destinationFile = new File(destination);
71  
72          doc = SAXReaderUtil.read(destinationFile);
73  
74          root = doc.getRootElement();
75  
76          Map<String, Element> servicesMap = new TreeMap<String, Element>();
77  
78          Iterator<Element> itr = root.elements("service").iterator();
79  
80          while (itr.hasNext()) {
81              Element service = itr.next();
82  
83              String name = service.attributeValue("name");
84  
85              servicesMap.put(name, service);
86  
87              service.detach();
88          }
89  
90          itr = sourceServices.iterator();
91  
92          while (itr.hasNext()) {
93              Element service = itr.next();
94  
95              String name = service.attributeValue("name");
96  
97              servicesMap.put(name, service);
98  
99              service.detach();
100         }
101 
102         for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
103             Element service = entry.getValue();
104 
105             root.add(service);
106         }
107 
108         FileUtil.write(destination, doc.formattedString(), true);
109     }
110 
111     public WSDDMerger(String source, String destination) {
112         try {
113             merge(source, destination);
114         }
115         catch (Exception e) {
116             e.printStackTrace();
117         }
118     }
119 
120 }