001    /**
002     * Copyright (c) 2000-2010 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.util.xml;
016    
017    import java.io.File;
018    
019    import org.apache.tools.ant.BuildException;
020    import org.apache.tools.ant.Task;
021    
022    /**
023     * @author Jorge Ferrer
024     */
025    public class XMLMergerTask extends Task {
026    
027            public void setMasterFile(File masterFile) {
028                    _masterFile = masterFile;
029            }
030    
031            public void setOutputFile(File outputFile) {
032                    _outputFile = outputFile;
033            }
034    
035            public void setSlaveFile(File slaveFile) {
036                    _slaveFile = slaveFile;
037            }
038    
039            public void setType(String type) {
040                    _type = type;
041            }
042    
043            public void execute() throws BuildException {
044                    _validateAttributes();
045    
046                    try {
047                            XMLMergerRunner runner = new XMLMergerRunner(_type);
048    
049                            runner.mergeAndSave(_masterFile, _slaveFile, _outputFile);
050                    }
051                    catch (Exception e) {
052                            throw new BuildException(e);
053                    }
054            }
055    
056            private void _validateAttributes() {
057                    _validateMandatoryAttribute(_masterFile, "masterFile");
058                    _validateMandatoryAttribute(_slaveFile, "slaveFile");
059                    _validateMandatoryAttribute(_outputFile, "outputFile");
060            }
061    
062            private void _validateMandatoryAttribute(File value, String name) {
063                    if (value == null) {
064                            throw new BuildException(name + " is a required attribute");
065                    }
066            }
067    
068            private File _masterFile;
069            private File _slaveFile;
070            private File _outputFile;
071            private String _type;
072    
073    }