View Javadoc
1   /*
2    *   Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: AbstractModelShowMojo.java 5043 2015-05-27 07:03:39Z schulte $
29   *
30   */
31  package org.jomc.mojo;
32  
33  import java.io.File;
34  import java.io.StringWriter;
35  import java.util.logging.Level;
36  import javax.xml.bind.JAXBElement;
37  import javax.xml.bind.Marshaller;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.jomc.modlet.ModelContext;
40  
41  /**
42   * Base class for displaying and dumping model objects.
43   *
44   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
45   * @version $JOMC: AbstractModelShowMojo.java 5043 2015-05-27 07:03:39Z schulte $
46   * @since 1.1
47   */
48  public abstract class AbstractModelShowMojo extends AbstractJomcMojo
49  {
50  
51      /**
52       * Constant for the name of the tool backing the mojo.
53       */
54      private static final String TOOLNAME = "ModelProcessor";
55  
56      /**
57       * File to write the model to.
58       *
59       * @parameter expression="${jomc.document}"
60       */
61      private File document;
62  
63      /**
64       * Encoding of the document to write.
65       *
66       * @parameter default-value="${project.build.sourceEncoding}" expression="${jomc.documentEncoding}"
67       */
68      private String documentEncoding;
69  
70      /**
71       * Creates a new {@code AbstractModelShowMojo} instance.
72       */
73      public AbstractModelShowMojo()
74      {
75          super();
76      }
77  
78      @Override
79      protected final void executeTool() throws Exception
80      {
81          this.logSeparator();
82          this.logProcessingModel( TOOLNAME, this.getModel() );
83  
84          final ModelContext modelContext = this.createModelContext( this.getDisplayClassLoader() );
85          final Marshaller m = modelContext.createMarshaller( this.getModel() );
86          final JAXBElement<?> displayModel = this.getDisplayModel( modelContext );
87          m.setSchema( modelContext.createSchema( this.getModel() ) );
88          m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
89  
90          if ( displayModel != null )
91          {
92              if ( this.document == null )
93              {
94                  final StringWriter stringWriter = new StringWriter();
95                  m.marshal( displayModel, stringWriter );
96  
97                  final boolean verbose = this.isVerbose();
98                  try
99                  {
100                     this.setVerbose( true );
101 
102                     if ( this.isLoggable( Level.INFO ) )
103                     {
104                         this.log( Level.INFO, stringWriter.toString(), null );
105                     }
106                 }
107                 finally
108                 {
109                     this.setVerbose( verbose );
110                 }
111             }
112             else
113             {
114                 if ( this.document.exists() && !this.document.delete() && this.isLoggable( Level.WARNING ) )
115                 {
116                     this.log( Level.WARNING, Messages.getMessage(
117                               "failedDeletingFile", this.document.getAbsolutePath() ), null );
118 
119                 }
120 
121                 if ( this.isLoggable( Level.INFO ) )
122                 {
123                     this.log( Level.INFO, Messages.getMessage(
124                               "writingEncoded", this.document.getAbsolutePath(), this.documentEncoding ), null );
125 
126                 }
127 
128                 m.setProperty( Marshaller.JAXB_ENCODING, this.documentEncoding );
129                 m.marshal( displayModel, this.document );
130             }
131 
132             this.logToolSuccess( TOOLNAME );
133         }
134         else if ( this.isLoggable( Level.WARNING ) )
135         {
136             this.log( Level.WARNING, Messages.getMessage( "modelObjectNotFound" ), null );
137         }
138     }
139 
140     /**
141      * Gets the model object to display or dump.
142      *
143      * @param modelContext The model context to use for getting the model.
144      *
145      * @return The model object to display or dump.
146      *
147      * @throws MojoExecutionException if getting the model fails.
148      */
149     protected abstract JAXBElement<?> getDisplayModel( ModelContext modelContext ) throws MojoExecutionException;
150 
151     /**
152      * Gets the class loader to use for displaying or dumping model objects.
153      *
154      * @return The class loader to use for displaying or dumping model objects.
155      *
156      * @throws MojoExecutionException if getting the class loader fails.
157      */
158     protected abstract ClassLoader getDisplayClassLoader() throws MojoExecutionException;
159 
160 }