1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class XMLMergerRunner {
47  
48      public static void main(String[] args)
49          throws ClassNotFoundException, DocumentException,
50                 IllegalAccessException, InstantiationException, IOException {
51  
52          if ((args != null) && (args.length == 4)) {
53              XMLMergerRunner runner = new XMLMergerRunner(args[3]);
54  
55              runner.mergeAndSave(args[0], args[1], args[2]);
56          }
57          else {
58              throw new IllegalArgumentException();
59          }
60      }
61  
62      public XMLMergerRunner(String descriptorClassName) {
63          if (Validator.isNotNull(descriptorClassName)) {
64              _descriptorClassName = descriptorClassName;
65          }
66      }
67  
68      public void mergeAndSave(
69              String masterFile, String slaveFile, String mergedFile)
70          throws ClassNotFoundException, DocumentException,
71                 IllegalAccessException, InstantiationException, IOException {
72  
73          mergeAndSave(
74              new File(masterFile), new File(slaveFile), new File(mergedFile));
75      }
76  
77      public void mergeAndSave(File masterFile, File slaveFile, File mergedFile)
78          throws ClassNotFoundException, DocumentException,
79                 IllegalAccessException, InstantiationException, IOException {
80  
81          String xml1 = FileUtil.read(masterFile);
82          String xml2 = FileUtil.read(slaveFile);
83  
84          String mergedXml = _merge(xml1, xml2);
85  
86          FileUtil.write(mergedFile, mergedXml);
87      }
88  
89      private String _documentToString(Document doc, String docType)
90          throws IOException {
91  
92          ByteArrayMaker bam = new ByteArrayMaker();
93  
94          OutputFormat format = OutputFormat.createPrettyPrint();
95  
96          format.setIndent("\t");
97          format.setLineSeparator("\n");
98  
99          XMLWriter writer = new XMLWriter(bam, format);
100 
101         writer.write(doc);
102 
103         String xml = bam.toString();
104 
105         int pos = xml.indexOf("<?");
106 
107         String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);
108 
109         xml = StringUtil.replace(xml, header, "");
110         xml = header + "\n" + docType + "\n" + xml;
111 
112         return xml;
113     }
114 
115     private String _merge(String masterXml, String slaveXml)
116         throws ClassNotFoundException, DocumentException,
117                IllegalAccessException, InstantiationException, IOException {
118 
119         int pos = masterXml.indexOf("<!DOCTYPE");
120 
121         String masterDoctype = "";
122 
123         if (pos >= 0) {
124             masterDoctype = masterXml.substring(
125                 pos, masterXml.indexOf(">", pos) + 1);
126             masterXml = StringUtil.replace(masterXml, masterDoctype, "");
127         }
128 
129         pos = slaveXml.indexOf("<!DOCTYPE");
130 
131         String slaveDoctype = "";
132 
133         if (pos >= 0) {
134             slaveDoctype = slaveXml.substring(
135                 pos, slaveXml.indexOf(">", pos) + 1);
136             slaveXml = StringUtil.replace(slaveXml, slaveDoctype, "");
137         }
138 
139         String doctype = null;
140 
141         if (Validator.isNotNull(masterDoctype)) {
142             doctype = masterDoctype;
143         }
144         else {
145             doctype = slaveDoctype;
146         }
147 
148         SAXReader reader = new SAXReader();
149 
150         Document masterDoc = reader.read(new StringReader(masterXml));
151         Document slaveDoc = reader.read(new StringReader(slaveXml));
152 
153         XMLDescriptor descriptor = null;
154 
155         if (_descriptorClassName.equals(_AUTO_DESCRIPTOR)) {
156             descriptor = XMLTypeDetector.determineType(doctype, masterDoc);
157         }
158         else {
159             descriptor = (XMLDescriptor)Class.forName(
160                 _descriptorClassName).newInstance();
161         }
162 
163         XMLMerger merger = new XMLMerger(descriptor);
164 
165         Document mergedDoc = merger.merge(masterDoc, slaveDoc);
166 
167         return _documentToString(mergedDoc, doctype);
168     }
169 
170     private static final String _AUTO_DESCRIPTOR = "auto";
171 
172     private String _descriptorClassName = _AUTO_DESCRIPTOR;
173 
174 }