1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.xml;
24  
25  import com.liferay.portal.kernel.util.ByteArrayMaker;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.util.xml.descriptor.XMLDescriptor;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.StringReader;
34  
35  import org.dom4j.Document;
36  import org.dom4j.DocumentException;
37  import org.dom4j.io.OutputFormat;
38  import org.dom4j.io.SAXReader;
39  import org.dom4j.io.XMLWriter;
40  
41  /**
42   * <a href="XMLMergerRunner.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   *
46   */
47  public class XMLMergerRunner {
48  
49      public static void main(String[] args)
50          throws ClassNotFoundException, DocumentException,
51                 IllegalAccessException, InstantiationException, IOException {
52  
53          if ((args != null) && (args.length == 4)) {
54              XMLMergerRunner runner = new XMLMergerRunner(args[3]);
55  
56              runner.mergeAndSave(args[0], args[1], args[2]);
57          }
58          else {
59              throw new IllegalArgumentException();
60          }
61      }
62  
63      public XMLMergerRunner(String descriptorClassName) {
64          if (Validator.isNotNull(descriptorClassName)) {
65              _descriptorClassName = descriptorClassName;
66          }
67      }
68  
69      public void mergeAndSave(
70              String masterFile, String slaveFile, String mergedFile)
71          throws ClassNotFoundException, DocumentException,
72                 IllegalAccessException, InstantiationException, IOException {
73  
74          mergeAndSave(
75              new File(masterFile), new File(slaveFile), new File(mergedFile));
76      }
77  
78      public void mergeAndSave(File masterFile, File slaveFile, File mergedFile)
79          throws ClassNotFoundException, DocumentException,
80                 IllegalAccessException, InstantiationException, IOException {
81  
82          String xml1 = FileUtil.read(masterFile);
83          String xml2 = FileUtil.read(slaveFile);
84  
85          String mergedXml = _merge(xml1, xml2);
86  
87          FileUtil.write(mergedFile, mergedXml);
88      }
89  
90      private String _documentToString(Document doc, String docType)
91          throws IOException {
92  
93          ByteArrayMaker bam = new ByteArrayMaker();
94  
95          OutputFormat format = OutputFormat.createPrettyPrint();
96  
97          format.setIndent("\t");
98          format.setLineSeparator("\n");
99  
100         XMLWriter writer = new XMLWriter(bam, format);
101 
102         writer.write(doc);
103 
104         String xml = bam.toString();
105 
106         int pos = xml.indexOf("<?");
107 
108         String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);
109 
110         xml = StringUtil.replace(xml, header, "");
111         xml = header + "\n" + docType + "\n" + xml;
112 
113         return xml;
114     }
115 
116     private String _merge(String masterXml, String slaveXml)
117         throws ClassNotFoundException, DocumentException,
118                IllegalAccessException, InstantiationException, IOException {
119 
120         int pos = masterXml.indexOf("<!DOCTYPE");
121 
122         String masterDoctype = "";
123 
124         if (pos >= 0) {
125             masterDoctype = masterXml.substring(
126                 pos, masterXml.indexOf(">", pos) + 1);
127             masterXml = StringUtil.replace(masterXml, masterDoctype, "");
128         }
129 
130         pos = slaveXml.indexOf("<!DOCTYPE");
131 
132         String slaveDoctype = "";
133 
134         if (pos >= 0) {
135             slaveDoctype = slaveXml.substring(
136                 pos, slaveXml.indexOf(">", pos) + 1);
137             slaveXml = StringUtil.replace(slaveXml, slaveDoctype, "");
138         }
139 
140         String doctype = null;
141 
142         if (Validator.isNotNull(masterDoctype)) {
143             doctype = masterDoctype;
144         }
145         else {
146             doctype = slaveDoctype;
147         }
148 
149         SAXReader reader = new SAXReader();
150 
151         Document masterDoc = reader.read(new StringReader(masterXml));
152         Document slaveDoc = reader.read(new StringReader(slaveXml));
153 
154         XMLDescriptor descriptor = null;
155 
156         if (_descriptorClassName.equals(_AUTO_DESCRIPTOR)) {
157             descriptor = XMLTypeDetector.determineType(doctype, masterDoc);
158         }
159         else {
160             descriptor = (XMLDescriptor)Class.forName(
161                 _descriptorClassName).newInstance();
162         }
163 
164         XMLMerger merger = new XMLMerger(descriptor);
165 
166         Document mergedDoc = merger.merge(masterDoc, slaveDoc);
167 
168         return _documentToString(mergedDoc, doctype);
169     }
170 
171     private static final String _AUTO_DESCRIPTOR = "auto";
172 
173     private String _descriptorClassName = _AUTO_DESCRIPTOR;
174 
175 }