Skip to content

Commit 6d73ee5

Browse files
committed
Add a velocity template based plugin
1 parent 005e00b commit 6d73ee5

File tree

10 files changed

+522
-1
lines changed

10 files changed

+522
-1
lines changed

modello-maven-plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
<artifactId>modello-plugin-xsd</artifactId>
120120
<scope>runtime</scope>
121121
</dependency>
122+
<dependency>
123+
<groupId>org.codehaus.modello</groupId>
124+
<artifactId>modello-plugin-velocity</artifactId>
125+
<scope>runtime</scope>
126+
</dependency>
122127
<dependency>
123128
<groupId>org.sonatype.plexus</groupId>
124129
<artifactId>plexus-build-api</artifactId>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.codehaus.modello.maven;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.io.File;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
import java.util.Properties;
28+
import java.util.stream.Collectors;
29+
30+
import org.apache.maven.plugins.annotations.LifecyclePhase;
31+
import org.apache.maven.plugins.annotations.Mojo;
32+
import org.apache.maven.plugins.annotations.Parameter;
33+
import org.codehaus.modello.plugin.velocity.VelocityGenerator;
34+
35+
/**
36+
* Creates files from the model using Velocity templates.
37+
*
38+
* @author <a href="mailto:[email protected]">Brett Porter</a>
39+
*/
40+
@Mojo( name = "velocity", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
41+
public class ModelloVelocityMojo
42+
extends AbstractModelloGeneratorMojo
43+
{
44+
/**
45+
* The output directory of the generated XML Schema.
46+
*/
47+
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello", required = true )
48+
private File outputDirectory;
49+
50+
@Parameter
51+
private List<String> templates;
52+
53+
@Parameter
54+
private List<String> params;
55+
56+
protected String getGeneratorType()
57+
{
58+
return "velocity";
59+
}
60+
61+
protected void customizeParameters( Properties parameters )
62+
{
63+
super.customizeParameters( parameters );
64+
Map<String, String> params = this.params != null ? this.params.stream().collect( Collectors.toMap(
65+
s -> s.substring( 0, s.indexOf( '=' ) ), s -> s.substring( s.indexOf( '=' ) + 1 )
66+
) ) : Collections.emptyMap();
67+
parameters.put( "basedir", Objects.requireNonNull( getBasedir(), "basedir is null" ) );
68+
parameters.put( VelocityGenerator.VELOCITY_TEMPLATES, String.join( ",", templates ) );
69+
parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params );
70+
}
71+
72+
protected boolean producesCompilableResult()
73+
{
74+
return true;
75+
}
76+
77+
public File getOutputDirectory()
78+
{
79+
return outputDirectory;
80+
}
81+
82+
public void setOutputDirectory( File outputDirectory )
83+
{
84+
this.outputDirectory = outputDirectory;
85+
}
86+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<parent>
4+
<artifactId>modello-plugins</artifactId>
5+
<groupId>org.codehaus.modello</groupId>
6+
<version>2.1.0-SNAPSHOT</version>
7+
</parent>
8+
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>modello-plugin-velocity</artifactId>
11+
<name>Modello Velocity Plugin</name>
12+
<description>
13+
Modello Velocity Plugin generates files from the Modello model using Velocity templates.
14+
</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.codehaus.modello</groupId>
19+
<artifactId>modello-plugin-xml</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.codehaus.plexus</groupId>
23+
<artifactId>plexus-utils</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.velocity</groupId>
27+
<artifactId>velocity-engine-core</artifactId>
28+
<version>2.3</version>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package org.codehaus.modello.plugin.velocity;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.ArrayList;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
26+
import org.codehaus.modello.ModelloRuntimeException;
27+
import org.codehaus.modello.model.ModelAssociation;
28+
import org.codehaus.modello.model.ModelClass;
29+
import org.codehaus.modello.model.ModelField;
30+
import org.codehaus.modello.model.Version;
31+
import org.codehaus.modello.plugin.AbstractModelloGenerator;
32+
import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
33+
import org.codehaus.modello.plugins.xml.metadata.XmlClassMetadata;
34+
import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
35+
import org.codehaus.plexus.util.StringUtils;
36+
37+
@SuppressWarnings( "unused" )
38+
public class Helper
39+
{
40+
private final Version version;
41+
42+
public Helper( Version version )
43+
{
44+
this.version = version;
45+
}
46+
47+
public String capitalise( String str )
48+
{
49+
return StringUtils.isEmpty( str ) ? str : Character.toTitleCase( str.charAt( 0 ) ) + str.substring( 1 );
50+
}
51+
52+
public String uncapitalise( String str )
53+
{
54+
return StringUtils.isEmpty( str ) ? str : Character.toLowerCase( str.charAt( 0 ) ) + str.substring( 1 );
55+
}
56+
57+
public String singular( String str )
58+
{
59+
return AbstractModelloGenerator.singular( str );
60+
}
61+
62+
public List<ModelClass> ancestors( ModelClass clazz )
63+
{
64+
List<ModelClass> ancestors = new ArrayList<>();
65+
for ( ModelClass cl = clazz; cl != null; cl = cl.getSuperClass() != null
66+
? cl.getModel().getClass( cl.getSuperClass(), version ) : null )
67+
{
68+
ancestors.add( 0, cl );
69+
}
70+
return ancestors;
71+
}
72+
73+
public XmlClassMetadata xmlClassMetadata( ModelClass clazz )
74+
{
75+
return (XmlClassMetadata) clazz.getMetadata( XmlClassMetadata.ID );
76+
}
77+
78+
public XmlFieldMetadata xmlFieldMetadata( ModelField field )
79+
{
80+
return (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
81+
}
82+
83+
public XmlAssociationMetadata xmAssociationMetadata( ModelField field )
84+
{
85+
return (XmlAssociationMetadata) ( (ModelAssociation) field )
86+
.getAssociationMetadata( XmlAssociationMetadata.ID );
87+
}
88+
89+
public boolean isFlatItems( ModelField field )
90+
{
91+
return field instanceof ModelAssociation && xmAssociationMetadata( field ).isFlatItems();
92+
}
93+
94+
public List<ModelField> xmlFields( ModelClass modelClass )
95+
{
96+
List<ModelClass> classes = new ArrayList<>();
97+
// get the full inheritance
98+
while ( modelClass != null )
99+
{
100+
classes.add( modelClass );
101+
String superClass = modelClass.getSuperClass();
102+
if ( superClass != null )
103+
{
104+
// superClass can be located outside (not generated by modello)
105+
modelClass = modelClass.getModel().getClass( superClass, version, true );
106+
}
107+
else
108+
{
109+
modelClass = null;
110+
}
111+
}
112+
List<ModelField> fields = new ArrayList<>();
113+
for ( int i = classes.size() - 1; i >= 0; i-- )
114+
{
115+
modelClass = classes.get( i );
116+
Iterator<ModelField> parentIter = fields.iterator();
117+
fields = new ArrayList<>();
118+
for ( ModelField field : modelClass.getFields( version ) )
119+
{
120+
XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
121+
if ( xmlFieldMetadata.isTransient() )
122+
{
123+
// just ignore xml.transient fields
124+
continue;
125+
}
126+
if ( xmlFieldMetadata.getInsertParentFieldsUpTo() != null )
127+
{
128+
// insert fields from parent up to the specified field
129+
boolean found = false;
130+
while ( !found && parentIter.hasNext() )
131+
{
132+
ModelField parentField = parentIter.next();
133+
fields.add( parentField );
134+
found = parentField.getName().equals( xmlFieldMetadata.getInsertParentFieldsUpTo() );
135+
}
136+
if ( !found )
137+
{
138+
// interParentFieldsUpTo not found
139+
throw new ModelloRuntimeException( "parent field not found: class "
140+
+ modelClass.getName() + " xml.insertParentFieldUpTo='"
141+
+ xmlFieldMetadata.getInsertParentFieldsUpTo() + "'" );
142+
}
143+
}
144+
fields.add( field );
145+
}
146+
// add every remaining fields from parent class
147+
while ( parentIter.hasNext() )
148+
{
149+
fields.add( parentIter.next() );
150+
}
151+
}
152+
return fields;
153+
}
154+
}

0 commit comments

Comments
 (0)