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