001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: AbstractModelShowMojo.java 5228 2016-04-24 11:08:06Z schulte $ 029 * 030 */ 031package org.jomc.mojo; 032 033import java.io.File; 034import java.io.StringWriter; 035import java.util.logging.Level; 036import javax.xml.bind.JAXBElement; 037import javax.xml.bind.Marshaller; 038import org.apache.maven.plugin.MojoExecutionException; 039import org.apache.maven.plugins.annotations.Parameter; 040import org.jomc.modlet.ModelContext; 041 042/** 043 * Base class for displaying and dumping model objects. 044 * 045 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 046 * @version $JOMC: AbstractModelShowMojo.java 5228 2016-04-24 11:08:06Z schulte $ 047 * @since 1.1 048 */ 049public abstract class AbstractModelShowMojo extends AbstractJomcMojo 050{ 051 052 /** 053 * Constant for the name of the tool backing the mojo. 054 */ 055 private static final String TOOLNAME = "ModelProcessor"; 056 057 /** 058 * File to write the model to. 059 */ 060 @Parameter( name = "document", 061 property = "jomc.document" ) 062 private File document; 063 064 /** 065 * Encoding of the document to write. 066 */ 067 @Parameter( name = "documentEncoding", 068 property = "jomc.documentEncoding", 069 defaultValue = "${project.build.sourceEncoding}" ) 070 private String documentEncoding; 071 072 /** 073 * Creates a new {@code AbstractModelShowMojo} instance. 074 */ 075 public AbstractModelShowMojo() 076 { 077 super(); 078 } 079 080 @Override 081 protected final void executeTool() throws Exception 082 { 083 this.logSeparator(); 084 this.logProcessingModel( TOOLNAME, this.getModel() ); 085 086 final ModelContext modelContext = this.createModelContext( this.getDisplayClassLoader() ); 087 final Marshaller m = modelContext.createMarshaller( this.getModel() ); 088 final JAXBElement<?> displayModel = this.getDisplayModel( modelContext ); 089 m.setSchema( modelContext.createSchema( this.getModel() ) ); 090 m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); 091 092 if ( displayModel != null ) 093 { 094 if ( this.document == null ) 095 { 096 final StringWriter stringWriter = new StringWriter(); 097 m.marshal( displayModel, stringWriter ); 098 099 final boolean verbose = this.isVerbose(); 100 try 101 { 102 this.setVerbose( true ); 103 104 if ( this.isLoggable( Level.INFO ) ) 105 { 106 this.log( Level.INFO, stringWriter.toString(), null ); 107 } 108 } 109 finally 110 { 111 this.setVerbose( verbose ); 112 } 113 } 114 else 115 { 116 if ( this.document.exists() && !this.document.delete() && this.isLoggable( Level.WARNING ) ) 117 { 118 this.log( Level.WARNING, Messages.getMessage( 119 "failedDeletingFile", this.document.getAbsolutePath() ), null ); 120 121 } 122 123 if ( this.isLoggable( Level.INFO ) ) 124 { 125 this.log( Level.INFO, Messages.getMessage( 126 "writingEncoded", this.document.getAbsolutePath(), this.documentEncoding ), null ); 127 128 } 129 130 m.setProperty( Marshaller.JAXB_ENCODING, this.documentEncoding ); 131 m.marshal( displayModel, this.document ); 132 } 133 134 this.logToolSuccess( TOOLNAME ); 135 } 136 else if ( this.isLoggable( Level.WARNING ) ) 137 { 138 this.log( Level.WARNING, Messages.getMessage( "modelObjectNotFound" ), null ); 139 } 140 } 141 142 /** 143 * Gets the model object to display or dump. 144 * 145 * @param modelContext The model context to use for getting the model. 146 * 147 * @return The model object to display or dump. 148 * 149 * @throws MojoExecutionException if getting the model fails. 150 */ 151 protected abstract JAXBElement<?> getDisplayModel( ModelContext modelContext ) throws MojoExecutionException; 152 153 /** 154 * Gets the class loader to use for displaying or dumping model objects. 155 * 156 * @return The class loader to use for displaying or dumping model objects. 157 * 158 * @throws MojoExecutionException if getting the class loader fails. 159 */ 160 protected abstract ClassLoader getDisplayClassLoader() throws MojoExecutionException; 161 162}