1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.kernel.xml.Document;
20  import com.liferay.portal.kernel.xml.DocumentException;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.kernel.xml.SAXReaderUtil;
23  import com.liferay.portal.util.InitUtil;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  /**
34   * <a href="WSDDMerger.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class WSDDMerger {
39  
40      public static void main(String[] args) {
41          InitUtil.initWithSpring();
42  
43          new WSDDMerger(args[0], args[1]);
44      }
45  
46      public static void merge(String source, String destination)
47          throws DocumentException, IOException {
48  
49          // Source
50  
51          File sourceFile = new File(source);
52  
53          Document doc = SAXReaderUtil.read(sourceFile);
54  
55          Element root = doc.getRootElement();
56  
57          List<Element> sourceServices = root.elements("service");
58  
59          if (sourceServices.size() == 0) {
60              return;
61          }
62  
63          // Destination
64  
65          File destinationFile = new File(destination);
66  
67          doc = SAXReaderUtil.read(destinationFile);
68  
69          root = doc.getRootElement();
70  
71          Map<String, Element> servicesMap = new TreeMap<String, Element>();
72  
73          Iterator<Element> itr = root.elements("service").iterator();
74  
75          while (itr.hasNext()) {
76              Element service = itr.next();
77  
78              String name = service.attributeValue("name");
79  
80              servicesMap.put(name, service);
81  
82              service.detach();
83          }
84  
85          itr = sourceServices.iterator();
86  
87          while (itr.hasNext()) {
88              Element service = itr.next();
89  
90              String name = service.attributeValue("name");
91  
92              servicesMap.put(name, service);
93  
94              service.detach();
95          }
96  
97          for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
98              Element service = entry.getValue();
99  
100             root.add(service);
101         }
102 
103         String content = doc.formattedString();
104 
105         content = StringUtil.replace(content, "\"/>", "\" />");
106 
107         FileUtil.write(destination, content, true);
108     }
109 
110     public WSDDMerger(String source, String destination) {
111         try {
112             merge(source, destination);
113         }
114         catch (Exception e) {
115             e.printStackTrace();
116         }
117     }
118 
119 }