1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.xml;
16  
17  import java.io.File;
18  
19  import org.apache.tools.ant.BuildException;
20  import org.apache.tools.ant.Task;
21  
22  /**
23   * <a href="XMLMergerTask.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Jorge Ferrer
26   */
27  public class XMLMergerTask extends Task {
28  
29      public void setMasterFile(File masterFile) {
30          _masterFile = masterFile;
31      }
32  
33      public void setOutputFile(File outputFile) {
34          _outputFile = outputFile;
35      }
36  
37      public void setSlaveFile(File slaveFile) {
38          _slaveFile = slaveFile;
39      }
40  
41      public void setType(String type) {
42          _type = type;
43      }
44  
45      public void execute() throws BuildException {
46          _validateAttributes();
47  
48          try {
49              XMLMergerRunner runner = new XMLMergerRunner(_type);
50  
51              runner.mergeAndSave(_masterFile, _slaveFile, _outputFile);
52          }
53          catch (Exception e) {
54              throw new BuildException(e);
55          }
56      }
57  
58      private void _validateAttributes() {
59          _validateMandatoryAttribute(_masterFile, "masterFile");
60          _validateMandatoryAttribute(_slaveFile, "slaveFile");
61          _validateMandatoryAttribute(_outputFile, "outputFile");
62      }
63  
64      private void _validateMandatoryAttribute(File value, String name) {
65          if (value == null) {
66              throw new BuildException(name + " is a required attribute");
67          }
68      }
69  
70      private File _masterFile;
71      private File _slaveFile;
72      private File _outputFile;
73      private String _type;
74  
75  }