1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class WSDDMerger {
46  
47      public static void main(String[] args) {
48          InitUtil.initWithSpring();
49  
50          new WSDDMerger(args[0], args[1]);
51      }
52  
53      public static void merge(String source, String destination)
54          throws DocumentException, IOException {
55  
56          // Source
57  
58          File sourceFile = new File(source);
59  
60          Document doc = SAXReaderUtil.read(sourceFile);
61  
62          Element root = doc.getRootElement();
63  
64          List<Element> sourceServices = root.elements("service");
65  
66          if (sourceServices.size() == 0) {
67              return;
68          }
69  
70          // Destination
71  
72          File destinationFile = new File(destination);
73  
74          doc = SAXReaderUtil.read(destinationFile);
75  
76          root = doc.getRootElement();
77  
78          Map<String, Element> servicesMap = new TreeMap<String, Element>();
79  
80          Iterator<Element> itr = root.elements("service").iterator();
81  
82          while (itr.hasNext()) {
83              Element service = itr.next();
84  
85              String name = service.attributeValue("name");
86  
87              servicesMap.put(name, service);
88  
89              service.detach();
90          }
91  
92          itr = sourceServices.iterator();
93  
94          while (itr.hasNext()) {
95              Element service = itr.next();
96  
97              String name = service.attributeValue("name");
98  
99              servicesMap.put(name, service);
100 
101             service.detach();
102         }
103 
104         for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
105             Element service = entry.getValue();
106 
107             root.add(service);
108         }
109 
110         FileUtil.write(destination, doc.formattedString(), true);
111     }
112 
113     public WSDDMerger(String source, String destination) {
114         try {
115             merge(source, destination);
116         }
117         catch (Exception e) {
118             e.printStackTrace();
119         }
120     }
121 
122 }