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