View Javadoc
1   package org.codehaus.plexus.metadata.merge;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22   */
23  
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStreamWriter;
28  import java.io.Writer;
29  import java.util.List;
30  
31  import org.codehaus.plexus.util.IOUtil;
32  import org.jdom2.Document;
33  import org.jdom2.JDOMException;
34  import org.jdom2.input.SAXBuilder;
35  import org.jdom2.input.sax.XMLReaderSAX2Factory;
36  import org.jdom2.output.XMLOutputter;
37  
38  /**
39   * Base class for common mergers.
40   *
41   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
42   */
43  public abstract class AbstractMerger implements Merger {
44      /**
45       * @see org.codehaus.plexus.metadata.merge.Merger#writeMergedDocument(org.jdom2.Document,
46       *      java.io.File)
47       */
48      public void writeMergedDocument(Document mergedDocument, File file) throws IOException {
49          if (!file.getParentFile().exists()) {
50              file.getParentFile().mkdirs();
51          }
52  
53          XMLOutputter out = new XMLOutputter();
54          Writer fw = null;
55          try {
56              fw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
57              out.output(mergedDocument, fw);
58          } finally {
59              IOUtil.close(fw);
60          }
61      }
62  
63      public void mergeDescriptors(File outputDescriptor, List<File> descriptors) throws IOException {
64          SAXBuilder builder = new SAXBuilder(new XMLReaderSAX2Factory(false, Driver.class.getName()));
65  
66          Document finalDoc = null;
67  
68          for (File f : descriptors) {
69              try {
70                  Document doc = builder.build(f);
71  
72                  if (finalDoc != null) {
73                      // Last specified has dominance
74                      finalDoc = merge(doc, finalDoc);
75                  } else {
76                      finalDoc = doc;
77                  }
78              } catch (JDOMException e) {
79                  throw new IOException("Invalid input descriptor for merge: " + f + " --> " + e.getMessage());
80              } catch (MergeException e) {
81                  throw new IOException("Error merging descriptor: " + f + " --> " + e.getMessage());
82              }
83          }
84  
85          if (finalDoc != null) {
86              try {
87                  writeMergedDocument(finalDoc, outputDescriptor);
88              } catch (IOException e) {
89                  throw new IOException("Error writing merged descriptor: " + outputDescriptor);
90              }
91          }
92      }
93  }