View Javadoc

1   /*
2    * Copyright 2005 The Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.mojo.castor;
18  
19  import java.io.File;
20  import java.io.FileReader;
21  import java.io.FileWriter;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.exolab.castor.xml.dtd.Converter;
30  
31  /**
32   * A mojo that uses Castor XML to convert a DTD document to corresponding XML Schema document.
33   * 
34   * @goal dtdToXsd
35   * @phase process-classes
36   * @author Stevo Slavic <sslavic@gmail.com>
37   */
38  
39  @Mojo(name = "dtdToXsd", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
40  public class ConvertDTD2XSDMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * The DTD file to convert.
46       */
47      @Parameter(property = "source", required = true)
48      private File source;
49  
50      /**
51       * The schema file to output.
52       */
53      @Parameter(property = "dest", required = true)
54      private File dest;
55  
56      /**
57       * {@inheritDoc}
58       * 
59       * @see org.apache.maven.plugin.AbstractMojo#execute()
60       */
61      public void execute()
62          throws MojoExecutionException
63      {
64          if ( !source.exists() )
65          {
66              throw new MojoExecutionException( "Source DTD " + source + " does not exists." );
67          }
68  
69          File destDir = dest.getParentFile();
70  
71          if ( destDir != null && !destDir.exists() )
72          {
73              FileUtils.mkdir( destDir.getAbsolutePath() );
74          }
75  
76          try
77          {
78              FileWriter writer = new FileWriter( dest );
79              Converter converter = new Converter();
80              converter.convertDTDtoSchema( new FileReader( source ), writer );
81              writer.close();
82          }
83          catch ( Exception e )
84          {
85              throw new MojoExecutionException( "Castor execution failed", e );
86          }
87      }
88  
89      /**
90       * Sets the DTD file to convert.
91       * 
92       * @param source the DTD file to convert.
93       */
94      public void setSource( final File source )
95      {
96          this.source = source;
97      }
98  
99      /**
100      * Sets the schema file to output.
101      * 
102      * @param dest the schema file to output.
103      */
104 
105     public void setDest( final File dest )
106     {
107         this.dest = dest;
108     }
109 
110 }