001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.DocumentException;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portal.util.InitUtil;
024    
025    import java.io.File;
026    import java.io.IOException;
027    
028    import java.util.Iterator;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.TreeMap;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class WSDDMerger {
037    
038            public static void main(String[] args) {
039                    InitUtil.initWithSpring();
040    
041                    new WSDDMerger(args[0], args[1]);
042            }
043    
044            public static void merge(String source, String destination)
045                    throws DocumentException, IOException {
046    
047                    // Source
048    
049                    File sourceFile = new File(source);
050    
051                    Document doc = SAXReaderUtil.read(sourceFile);
052    
053                    Element root = doc.getRootElement();
054    
055                    List<Element> sourceServices = root.elements("service");
056    
057                    if (sourceServices.size() == 0) {
058                            return;
059                    }
060    
061                    // Destination
062    
063                    File destinationFile = new File(destination);
064    
065                    doc = SAXReaderUtil.read(destinationFile);
066    
067                    root = doc.getRootElement();
068    
069                    Map<String, Element> servicesMap = new TreeMap<String, Element>();
070    
071                    Iterator<Element> itr = root.elements("service").iterator();
072    
073                    while (itr.hasNext()) {
074                            Element service = itr.next();
075    
076                            String name = service.attributeValue("name");
077    
078                            servicesMap.put(name, service);
079    
080                            service.detach();
081                    }
082    
083                    itr = sourceServices.iterator();
084    
085                    while (itr.hasNext()) {
086                            Element service = itr.next();
087    
088                            String name = service.attributeValue("name");
089    
090                            servicesMap.put(name, service);
091    
092                            service.detach();
093                    }
094    
095                    for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
096                            Element service = entry.getValue();
097    
098                            root.add(service);
099                    }
100    
101                    String content = doc.formattedString();
102    
103                    content = StringUtil.replace(content, "\"/>", "\" />");
104    
105                    FileUtil.write(destination, content, true);
106            }
107    
108            public WSDDMerger(String source, String destination) {
109                    try {
110                            merge(source, destination);
111                    }
112                    catch (Exception e) {
113                            e.printStackTrace();
114                    }
115            }
116    
117    }