001    /**
002     * Copyright (c) 2000-2013 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.UnsecureSAXReaderUtil;
023    import com.liferay.portal.util.InitUtil;
024    
025    import java.io.File;
026    import java.io.IOException;
027    
028    import java.util.List;
029    import java.util.Map;
030    import java.util.TreeMap;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class WSDDMerger {
036    
037            public static void main(String[] args) {
038                    InitUtil.initWithSpring();
039    
040                    new WSDDMerger(args[0], args[1]);
041            }
042    
043            public static void merge(String source, String destination)
044                    throws DocumentException, IOException {
045    
046                    // Source
047    
048                    File sourceFile = new File(source);
049    
050                    Document document = UnsecureSAXReaderUtil.read(sourceFile);
051    
052                    Element rootElement = document.getRootElement();
053    
054                    List<Element> sourceServiceElements = rootElement.elements("service");
055    
056                    if (sourceServiceElements.isEmpty()) {
057                            return;
058                    }
059    
060                    // Destination
061    
062                    File destinationFile = new File(destination);
063    
064                    document = UnsecureSAXReaderUtil.read(destinationFile);
065    
066                    rootElement = document.getRootElement();
067    
068                    Map<String, Element> servicesMap = new TreeMap<String, Element>();
069    
070                    List<Element> serviceElements = rootElement.elements("service");
071    
072                    for (Element serviceElement : serviceElements) {
073                            String name = serviceElement.attributeValue("name");
074    
075                            servicesMap.put(name, serviceElement);
076    
077                            serviceElement.detach();
078                    }
079    
080                    for (Element serviceElement : sourceServiceElements) {
081                            String name = serviceElement.attributeValue("name");
082    
083                            servicesMap.put(name, serviceElement);
084    
085                            serviceElement.detach();
086                    }
087    
088                    for (Map.Entry<String, Element> entry : servicesMap.entrySet()) {
089                            Element serviceElement = entry.getValue();
090    
091                            rootElement.add(serviceElement);
092                    }
093    
094                    String content = document.formattedString();
095    
096                    content = StringUtil.replace(content, "\"/>", "\" />");
097    
098                    FileUtil.write(destination, content, true);
099            }
100    
101            public WSDDMerger(String source, String destination) {
102                    try {
103                            merge(source, destination);
104                    }
105                    catch (Exception e) {
106                            e.printStackTrace();
107                    }
108            }
109    
110    }