001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.xml;
022:
023: import com.liferay.portal.kernel.util.ByteArrayMaker;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.util.FileUtil;
027: import com.liferay.util.xml.descriptor.XMLDescriptor;
028:
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.StringReader;
032:
033: import org.dom4j.Document;
034: import org.dom4j.DocumentException;
035: import org.dom4j.io.OutputFormat;
036: import org.dom4j.io.SAXReader;
037: import org.dom4j.io.XMLWriter;
038:
039: /**
040: * <a href="XMLMergerRunner.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Jorge Ferrer
043: *
044: */
045: public class XMLMergerRunner {
046:
047: public static void main(String[] args)
048: throws ClassNotFoundException, DocumentException,
049: IllegalAccessException, InstantiationException, IOException {
050:
051: if ((args != null) && (args.length == 4)) {
052: XMLMergerRunner runner = new XMLMergerRunner(args[3]);
053:
054: runner.mergeAndSave(args[0], args[1], args[2]);
055: } else {
056: throw new IllegalArgumentException();
057: }
058: }
059:
060: public XMLMergerRunner(String descriptorClassName) {
061: if (Validator.isNotNull(descriptorClassName)) {
062: _descriptorClassName = descriptorClassName;
063: }
064: }
065:
066: public void mergeAndSave(String masterFile, String slaveFile,
067: String mergedFile) throws ClassNotFoundException,
068: DocumentException, IllegalAccessException,
069: InstantiationException, IOException {
070:
071: mergeAndSave(new File(masterFile), new File(slaveFile),
072: new File(mergedFile));
073: }
074:
075: public void mergeAndSave(File masterFile, File slaveFile,
076: File mergedFile) throws ClassNotFoundException,
077: DocumentException, IllegalAccessException,
078: InstantiationException, IOException {
079:
080: String xml1 = FileUtil.read(masterFile);
081: String xml2 = FileUtil.read(slaveFile);
082:
083: String mergedXml = _merge(xml1, xml2);
084:
085: FileUtil.write(mergedFile, mergedXml);
086: }
087:
088: private String _documentToString(Document doc, String docType)
089: throws IOException {
090:
091: ByteArrayMaker bam = new ByteArrayMaker();
092:
093: OutputFormat format = OutputFormat.createPrettyPrint();
094:
095: format.setIndent("\t");
096: format.setLineSeparator("\n");
097:
098: XMLWriter writer = new XMLWriter(bam, format);
099:
100: writer.write(doc);
101:
102: String xml = bam.toString();
103:
104: int pos = xml.indexOf("<?");
105:
106: String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);
107:
108: xml = StringUtil.replace(xml, header, "");
109: xml = header + "\n" + docType + "\n" + xml;
110:
111: return xml;
112: }
113:
114: private String _merge(String masterXml, String slaveXml)
115: throws ClassNotFoundException, DocumentException,
116: IllegalAccessException, InstantiationException, IOException {
117:
118: int pos = masterXml.indexOf("<!DOCTYPE");
119:
120: String masterDoctype = "";
121:
122: if (pos >= 0) {
123: masterDoctype = masterXml.substring(pos, masterXml.indexOf(
124: ">", pos) + 1);
125: masterXml = StringUtil
126: .replace(masterXml, masterDoctype, "");
127: }
128:
129: pos = slaveXml.indexOf("<!DOCTYPE");
130:
131: String slaveDoctype = "";
132:
133: if (pos >= 0) {
134: slaveDoctype = slaveXml.substring(pos, slaveXml.indexOf(
135: ">", pos) + 1);
136: slaveXml = StringUtil.replace(slaveXml, slaveDoctype, "");
137: }
138:
139: String doctype = null;
140:
141: if (Validator.isNotNull(masterDoctype)) {
142: doctype = masterDoctype;
143: } else {
144: doctype = slaveDoctype;
145: }
146:
147: SAXReader reader = new SAXReader();
148:
149: Document masterDoc = reader.read(new StringReader(masterXml));
150: Document slaveDoc = reader.read(new StringReader(slaveXml));
151:
152: XMLDescriptor descriptor = null;
153:
154: if (_descriptorClassName.equals(_AUTO_DESCRIPTOR)) {
155: descriptor = XMLTypeDetector.determineType(doctype,
156: masterDoc);
157: } else {
158: descriptor = (XMLDescriptor) Class.forName(
159: _descriptorClassName).newInstance();
160: }
161:
162: XMLMerger merger = new XMLMerger(descriptor);
163:
164: Document mergedDoc = merger.merge(masterDoc, slaveDoc);
165:
166: return _documentToString(mergedDoc, doctype);
167: }
168:
169: private static final String _AUTO_DESCRIPTOR = "auto";
170:
171: private String _descriptorClassName = _AUTO_DESCRIPTOR;
172:
173: }
|