Skip to content

Commit

Permalink
Add a velocity template based plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 23, 2022
1 parent 005e00b commit 6d73ee5
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 1 deletion.
5 changes: 5 additions & 0 deletions modello-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
<artifactId>modello-plugin-xsd</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-plugin-velocity</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.codehaus.modello.maven;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.modello.plugin.velocity.VelocityGenerator;

/**
* Creates files from the model using Velocity templates.
*
* @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
*/
@Mojo( name = "velocity", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
public class ModelloVelocityMojo
extends AbstractModelloGeneratorMojo
{
/**
* The output directory of the generated XML Schema.
*/
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello", required = true )
private File outputDirectory;

@Parameter
private List<String> templates;

@Parameter
private List<String> params;

protected String getGeneratorType()
{
return "velocity";
}

protected void customizeParameters( Properties parameters )
{
super.customizeParameters( parameters );
Map<String, String> params = this.params != null ? this.params.stream().collect( Collectors.toMap(
s -> s.substring( 0, s.indexOf( '=' ) ), s -> s.substring( s.indexOf( '=' ) + 1 )
) ) : Collections.emptyMap();
parameters.put( "basedir", Objects.requireNonNull( getBasedir(), "basedir is null" ) );
parameters.put( VelocityGenerator.VELOCITY_TEMPLATES, String.join( ",", templates ) );
parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params );
}

protected boolean producesCompilableResult()
{
return true;
}

public File getOutputDirectory()
{
return outputDirectory;
}

public void setOutputDirectory( File outputDirectory )
{
this.outputDirectory = outputDirectory;
}
}
31 changes: 31 additions & 0 deletions modello-plugins/modello-plugin-velocity/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>modello-plugins</artifactId>
<groupId>org.codehaus.modello</groupId>
<version>2.1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>modello-plugin-velocity</artifactId>
<name>Modello Velocity Plugin</name>
<description>
Modello Velocity Plugin generates files from the Modello model using Velocity templates.
</description>

<dependencies>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-plugin-xml</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.codehaus.modello.plugin.velocity;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.codehaus.modello.ModelloRuntimeException;
import org.codehaus.modello.model.ModelAssociation;
import org.codehaus.modello.model.ModelClass;
import org.codehaus.modello.model.ModelField;
import org.codehaus.modello.model.Version;
import org.codehaus.modello.plugin.AbstractModelloGenerator;
import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
import org.codehaus.modello.plugins.xml.metadata.XmlClassMetadata;
import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
import org.codehaus.plexus.util.StringUtils;

@SuppressWarnings( "unused" )
public class Helper
{
private final Version version;

public Helper( Version version )
{
this.version = version;
}

public String capitalise( String str )
{
return StringUtils.isEmpty( str ) ? str : Character.toTitleCase( str.charAt( 0 ) ) + str.substring( 1 );
}

public String uncapitalise( String str )
{
return StringUtils.isEmpty( str ) ? str : Character.toLowerCase( str.charAt( 0 ) ) + str.substring( 1 );
}

public String singular( String str )
{
return AbstractModelloGenerator.singular( str );
}

public List<ModelClass> ancestors( ModelClass clazz )
{
List<ModelClass> ancestors = new ArrayList<>();
for ( ModelClass cl = clazz; cl != null; cl = cl.getSuperClass() != null
? cl.getModel().getClass( cl.getSuperClass(), version ) : null )
{
ancestors.add( 0, cl );
}
return ancestors;
}

public XmlClassMetadata xmlClassMetadata( ModelClass clazz )
{
return (XmlClassMetadata) clazz.getMetadata( XmlClassMetadata.ID );
}

public XmlFieldMetadata xmlFieldMetadata( ModelField field )
{
return (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
}

public XmlAssociationMetadata xmAssociationMetadata( ModelField field )
{
return (XmlAssociationMetadata) ( (ModelAssociation) field )
.getAssociationMetadata( XmlAssociationMetadata.ID );
}

public boolean isFlatItems( ModelField field )
{
return field instanceof ModelAssociation && xmAssociationMetadata( field ).isFlatItems();
}

public List<ModelField> xmlFields( ModelClass modelClass )
{
List<ModelClass> classes = new ArrayList<>();
// get the full inheritance
while ( modelClass != null )
{
classes.add( modelClass );
String superClass = modelClass.getSuperClass();
if ( superClass != null )
{
// superClass can be located outside (not generated by modello)
modelClass = modelClass.getModel().getClass( superClass, version, true );
}
else
{
modelClass = null;
}
}
List<ModelField> fields = new ArrayList<>();
for ( int i = classes.size() - 1; i >= 0; i-- )
{
modelClass = classes.get( i );
Iterator<ModelField> parentIter = fields.iterator();
fields = new ArrayList<>();
for ( ModelField field : modelClass.getFields( version ) )
{
XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
if ( xmlFieldMetadata.isTransient() )
{
// just ignore xml.transient fields
continue;
}
if ( xmlFieldMetadata.getInsertParentFieldsUpTo() != null )
{
// insert fields from parent up to the specified field
boolean found = false;
while ( !found && parentIter.hasNext() )
{
ModelField parentField = parentIter.next();
fields.add( parentField );
found = parentField.getName().equals( xmlFieldMetadata.getInsertParentFieldsUpTo() );
}
if ( !found )
{
// interParentFieldsUpTo not found
throw new ModelloRuntimeException( "parent field not found: class "
+ modelClass.getName() + " xml.insertParentFieldUpTo='"
+ xmlFieldMetadata.getInsertParentFieldsUpTo() + "'" );
}
}
fields.add( field );
}
// add every remaining fields from parent class
while ( parentIter.hasNext() )
{
fields.add( parentIter.next() );
}
}
return fields;
}
}
Loading

0 comments on commit 6d73ee5

Please sign in to comment.