View Javadoc

1   package org.apache.maven.hibernate.beans;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.File;
21  import java.io.FileWriter;
22  import java.util.Iterator;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.maven.hibernate.HibernateEntityResolver;
27  import org.dom4j.Document;
28  import org.dom4j.DocumentHelper;
29  import org.dom4j.Element;
30  import org.dom4j.io.OutputFormat;
31  import org.dom4j.io.SAXReader;
32  import org.dom4j.io.XMLWriter;
33  
34  /**
35   * 
36   * @author <a href="as851@columbia.edu">Alex Shneyderman</a>
37   * @since  $Id: MappingsAggregatorBean.java 170200 2005-05-15 06:24:19Z brett $
38   */
39  public class MappingsAggregatorBean extends CommonOperationsBean
40  {
41  
42  	private String aggregateOutputFile = null;
43  	
44  	private static final Log LOG = LogFactory.getLog(MappingsAggregatorBean.class);
45  
46      public void execute()
47          throws Exception
48      {
49          String version = null;
50          File files[] = getFileDescriptors();
51          if(files == null || files.length <= 0)
52          {
53              LOG.info("nothing to process");
54              return;
55          }
56          LOG.info("Aggregating to " + getAggregateOutputFile());
57          File f = new File(getAggregateOutputFile());
58          OutputFormat format = OutputFormat.createPrettyPrint();
59          XMLWriter writer = new XMLWriter(new FileWriter(f), format);
60          writer.setEntityResolver(new HibernateEntityResolver());
61          //writer.setResolveEntityRefs(false);
62          Document finalDoc = DocumentHelper.createDocument();
63          Element rootHM = null;
64          for(int i = 0; i < files.length; i++)
65          {
66              SAXReader reader = new SAXReader(false);
67              reader.setEntityResolver(new HibernateEntityResolver());
68              //reader.setIncludeExternalDTDDeclarations(false);
69              //reader.setIncludeExternalDTDDeclarations(false);
70              Document current = reader.read(files[i]);
71              String currentVersion = getVersion(current);
72              if(version == null)
73              {
74                  version = currentVersion;
75                  finalDoc.setProcessingInstructions(current.processingInstructions());
76                  finalDoc.setDocType(current.getDocType());
77                  rootHM = finalDoc.addElement("hibernate-mapping");
78              } else
79              if(!version.equals(currentVersion))
80              {
81                  LOG.warn("Mapping in " + files[i].getName() + " is not of the same mapping version as " + files[0].getName() + " mapping, so merge is impossible. Skipping");
82                  continue;
83              }
84              for(Iterator iter = current.selectSingleNode("hibernate-mapping").selectNodes("class").iterator(); iter.hasNext(); rootHM.add((Element)((Element)iter.next()).clone()));
85          }
86  
87          writer.write(finalDoc);
88          writer.close();
89      }
90  
91      public String getAggregateOutputFile()
92      {
93          return aggregateOutputFile;
94      }
95  
96      public void setAggregateOutputFile(String aggregateOutputFile)
97      {
98          this.aggregateOutputFile = aggregateOutputFile;
99      }
100 
101     private String getVersion(Document current)
102     {
103         String docType = current.getDocType().getText();
104         if(docType == null || "".equals(docType.trim()))
105             return "";
106         if(docType.indexOf("hibernate-mapping-2.0.dtd") > 0)
107             return "2.0";
108         if(docType.indexOf("hibernate-mapping-1.1.dtd") > 0)
109             return "1.1";
110         else
111             return null;
112     }
113 
114 }