Java tutorial
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil -*- * * Copyright (c) 2010-2011 Edugility LLC. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * * The original copy of this license is available at * http://www.opensource.org/license/mit-license.html. */ package com.edugility.jaxb.maven.plugin; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.util.List; import java.util.Set; import javax.xml.bind.annotation.adapters.XmlAdapter; // for javadoc only import com.edugility.jaxb.XmlAdapterGenerator; import com.edugility.jaxb.PackageInfoModifier; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; /** * Given a {@link Map} whose keys consist of interface names and whose * values consist of the names of classes that (a) implement those * interfaces and (b) are valid JAXB-annotated classes suitable for * annotation-based JAXB binding, this mojo generates the source code * for appropriate {@link XmlAdapter}s. * * @author <a href="mailto:ljnelson@gmail.com">Laird Nelson</a> * * @goal generate-xml-adapter * * @phase generate-sources * * @requiresDependencyResolution test * * @see XmlAdapter * * @see XmlAdapterGenerator * * @since 1.0-SNAPSHOT * * @deprecated The real class you want to look at right now is {@link * PackageInfoModifier}. */ @Deprecated public class GenerateXmlAdapterMojo extends AbstractJAXBMojo { /** * The {@link XmlAdapterGenerator} instance to use. See the Maven * plugin documentation for how to specify an alternate, * fully-configured instance. * * @parameter property="xmlAdapterGenerator" */ private XmlAdapterGenerator generator; /** * The name of the package that all {@link XmlAdapter}s generated by * this {@link GenerateXmlAdapterMojo} will belong to. * * @parameter property="xmlAdapterPackage" required */ private String xmlAdapterPackage; /** * The directory into which new {@link XmlAdapter}s will be * generated. This directory will be created if it does not exist. * * @parameter default-value="${project.build.directory}/generated-sources/jaxb" property="directory" */ private File directory; /** * A {@link Map} of interface names to implementation class names * that specifies which JAXB-compliant implementation classes will * be used to implement said interfaces. * * @parameter property="implementations" */ private Map<String, String> implementations; /** * Reads the {@linkplain #getImplementations() implementations map} * and generates the appropriate {@link XmlAdapter} source code in * the {@linkplain #getDirectory() specified directory}. * * @exception MojoExecutionException if an error occurs */ @Override public void execute() throws MojoExecutionException { final Log log = this.getLog(); XmlAdapterGenerator generator = this.getXmlAdapterGenerator(); if (generator == null) { generator = new XmlAdapterGenerator(); } File directory = this.getDirectory(); if (directory == null) { directory = generator.getDirectory(); if (directory == null) { throw new MojoExecutionException("No directory set", new IllegalStateException("No directory set")); } } else if (!directory.exists()) { if (!directory.mkdirs()) { throw new MojoExecutionException(String.format("Could not create directory path %s", directory), new IOException(String.format("Could not create directory path %s", directory))); } } assert directory.isDirectory(); assert directory.canWrite(); generator.setDirectory(directory); final Map<String, String> implementations = this.getImplementations(); if (implementations != null && !implementations.isEmpty()) { final Set<Entry<String, String>> entrySet = implementations.entrySet(); if (entrySet != null && !entrySet.isEmpty()) { final String xmlAdapterPackage = this.getXmlAdapterPackage(); for (final Entry<String, String> entry : entrySet) { if (entry != null) { String interfaceName = entry.getKey(); if (interfaceName != null) { interfaceName = interfaceName.trim(); if (!interfaceName.isEmpty()) { String className = entry.getValue(); if (className != null) { className = className.trim(); if (!className.isEmpty()) { try { final File file = generator.generate(xmlAdapterPackage, interfaceName, className); if (log != null && log.isDebugEnabled()) { log.debug(String.format( "Generated %s in package %s, adapting %s to %s", file, xmlAdapterPackage, interfaceName, className)); } } catch (final IOException kaboom) { throw new MojoExecutionException( "Encountered an IOException during XMLAdapter generation", kaboom); } } } } } } } } } } /** * Returns the {@link XmlAdapterGenerator} that will generate {@link * XmlAdapter} source code. This method may return {@code null}. * * @return the {@link XmlAdapterGenerator} that will generate {@link * XmlAdapter} source code, or {@code null} */ public XmlAdapterGenerator getXmlAdapterGenerator() { return this.generator; } /** * Sets the {@link XmlAdapterGenerator} that will generate {@link * XmlAdapter} source code. * * @param generator the {@link XmlAdapterGenerator} to use; may be * {@code null} */ public void setXmlAdapterGenerator(final XmlAdapterGenerator generator) { this.generator = generator; } /** * Returns a {@link File} representing the directory into which * {@link XmlAdapter} source code will be generated. This method * may return {@code null}. * * @return a {@link File} representing the directory into which * {@link XmlAdapter} source code will be generated, or {@code null} */ public File getDirectory() { File returnValue = null; if (this.directory == null) { final XmlAdapterGenerator generator = this.getXmlAdapterGenerator(); if (generator != null) { returnValue = generator.getDirectory(); } } return returnValue; } /** * Sets the {@link File} representing the directory into which * {@link XmlAdapter} source code will be generated. * * @param directory the {@link File} representing the directory into * which {@link XmlAdapter} source code will be generated; must not * be {@code null} */ public void setDirectory(final File directory) { if (directory == null) { throw new IllegalArgumentException("directory", new NullPointerException("directory")); } this.directory = directory; } /** * Returns a {@link Map} of interface names to JAXB-compliant implementation class names. * * @return a {@link Map}, or {@code null} */ public Map<String, String> getImplementations() { return this.implementations; } /** * Sets a {@link Map} of interface names to JAXB-comliant implementation class names. * * @param implementations a non-{@code null} {@link Map} of * interface names to JAXB-compliant implementation class names */ public void setImplementations(final Map<String, String> implementations) { if (implementations == null) { throw new IllegalArgumentException("implementations", new NullPointerException("implementations")); } this.implementations = implementations; } /** * Returns the name of the package that all generated {@link * XmlAdapter}s will belong to. This method never returns {@code * null}. * * @return the name of the package that all generated {@link * XmlAdapter}s will belong to; never {@code null} */ public String getXmlAdapterPackage() { return this.xmlAdapterPackage; } /** * Sets the name of the package that all generated {@link * XmlAdapter}s will belong to. * * @param adapterPackage the package; must not be {@code null} */ public void setXmlAdapterPackage(final String adapterPackage) { if (adapterPackage == null) { this.xmlAdapterPackage = ""; } else { this.xmlAdapterPackage = adapterPackage; } } }