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.jdom.Document;
33  import org.jdom.JDOMException;
34  import org.jdom.input.SAXBuilder;
35  import org.jdom.output.XMLOutputter;
36  
37  /**
38   * Base class for common mergers.
39   * 
40   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
41   */
42  public abstract class AbstractMerger
43      implements Merger
44  {
45      /**
46       * @see org.codehaus.plexus.metadata.merge.Merger#writeMergedDocument(org.jdom.Document,
47       *      java.io.File)
48       */
49      public void writeMergedDocument( Document mergedDocument, File file )
50          throws IOException
51      {
52          if ( !file.getParentFile().exists() )
53          {
54              file.getParentFile().mkdirs();
55          }
56  
57          XMLOutputter out = new XMLOutputter();
58          Writer fw = null;
59          try
60          {
61              fw = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" );
62              out.output( mergedDocument, fw );
63          }
64          finally
65          {
66              IOUtil.close( fw );
67          }
68      }
69  
70      public void mergeDescriptors( File outputDescriptor, List<File> descriptors )
71          throws IOException
72      {
73          SAXBuilder builder = new SAXBuilder( Driver.class.getName() );
74          
75          Document finalDoc = null;
76  
77          for ( File f : descriptors )
78          {
79              try
80              {
81                  Document doc = builder.build( f );
82  
83                  if ( finalDoc != null )
84                  {
85                      // Last specified has dominance
86                      finalDoc = merge( doc, finalDoc );
87                  }
88                  else
89                  {
90                      finalDoc = doc;
91                  }
92              }
93              catch ( JDOMException e )
94              {
95                  throw new IOException( "Invalid input descriptor for merge: " + f + " --> " + e.getMessage() );
96              }
97              catch ( MergeException e )
98              {
99                  throw new IOException( "Error merging descriptor: " + f + " --> " + e.getMessage() );
100             }
101         }
102 
103         if ( finalDoc != null )
104         {
105             try
106             {
107                 writeMergedDocument( finalDoc, outputDescriptor );
108             }
109             catch ( IOException e )
110             {
111                 throw new IOException( "Error writing merged descriptor: " + outputDescriptor );
112             }
113         }
114     }
115 }