/* * Copyright 2005 The Codehaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.codehaus.mojo.castor; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.FileUtils; import org.exolab.castor.xml.dtd.Converter; /** * A mojo that uses Castor XML to convert a DTD document to corresponding XML Schema document. * * @goal dtd2xsd * @description Converts DTD 2 XML Schema * @author Stevo Slavic <sslavic@gmail.com> */ public class ConvertDTD2XSDMojo extends AbstractMojo { /** * The DTD file to convert. * * @parameter * @required * @since 2.1 */ private File source; /** * The schema file to output. * * @parameter * @required * @since 2.1 */ private File dest; /** * {@inheritDoc} * * @see org.apache.maven.plugin.AbstractMojo#execute() */ public void execute() throws MojoExecutionException { if ( !source.exists() ) { throw new MojoExecutionException( "Source DTD " + source + " does not exists." ); } File destDir = dest.getParentFile(); if ( destDir != null && !destDir.exists() ) { FileUtils.mkdir( destDir.getAbsolutePath() ); } try { FileWriter writer = new FileWriter( dest ); Converter.convertDTDtoSchema( new FileReader( source ), writer ); writer.close(); } catch ( Exception e ) { throw new MojoExecutionException( "Castor execution failed", e ); } } /** * Sets the DTD file to convert. * * @param source the DTD file to convert. */ public void setSource( final File source ) { this.source = source; } /** * Sets the schema file to output. * * @param dest the schema file to output. */ public void setDest( final File dest ) { this.dest = dest; } }