001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }