Skip to content

Commit 4a490d9

Browse files
committed
#28 refactored Location formatting
introducing Location.StringFormatter abstract class
1 parent ce2dca6 commit 4a490d9

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ private void generateLocationBean( JClass jClass, ModelClass locationClass, Mode
920920
}
921921

922922
JType fieldType = new JMapType( "java.util.Map", new JType(locationClass.getName()), useJava5 );
923-
JType fieldImpl = new JMapType("java.util.LinkedHashMap", new JType(locationClass.getName()), useJava5);
923+
JType fieldImpl = new JMapType( "java.util.LinkedHashMap", new JType( locationClass.getName() ), useJava5 );
924924

925925
// public Map<Object, Location> getLocations()
926926
JMethod jMethod = new JMethod( "get" + capitalise( locationsField ), fieldType, null );
@@ -1044,6 +1044,15 @@ private void generateLocationBean( JClass jClass, ModelClass locationClass, Mode
10441044
sc.add( "" );
10451045
sc.add( "return result;" );
10461046
jClass.addMethod( jMethod );
1047+
1048+
JClass stringFormatterClass = jClass.createInnerClass( "StringFormatter" );
1049+
stringFormatterClass.getModifiers().setStatic( true );
1050+
stringFormatterClass.getModifiers().setAbstract( true );
1051+
1052+
jMethod = new JMethod( "toString", new JType( "String" ), null );
1053+
jMethod.getModifiers().setAbstract( true );
1054+
jMethod.addParameter( new JParameter( new JType( locationClass.getName() ), "location" ) );
1055+
stringFormatterClass.addMethod( jMethod );
10471056
}
10481057

10491058
/**

modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/AbstractXpp3Generator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void initialize( Model model, Properties parameters )
6161
if ( locationTracker == null )
6262
{
6363
throw new ModelloException( "No model class has been marked as location tracker"
64-
+ " via the attribute locationTracker=\"locations\"" + ", cannot generate extended reader." );
64+
+ " via the attribute locationTracker=\"locations\", cannot generate extended reader." );
6565
}
6666

6767
sourceTracker = model.getSourceTracker( getGeneratedVersion() );

modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ExtendedWriterGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
import org.codehaus.modello.plugin.java.javasource.JClass;
26+
import org.codehaus.modello.plugin.java.javasource.JField;
2627
import org.codehaus.modello.plugin.java.javasource.JMethod;
2728
import org.codehaus.modello.plugin.java.javasource.JParameter;
2829
import org.codehaus.modello.plugin.java.javasource.JSourceCode;
@@ -80,12 +81,26 @@ private void createLocationTrackingMethod( JClass jClass )
8081

8182
jClass.addMethod( method );
8283

84+
JField field = new JField( new JType( locationTracker.getName() + ".StringFormatter" ), "stringFormatter" );
85+
field.getModifiers().makeProtected();
86+
jClass.addField( field );
87+
88+
method = new JMethod( "setStringFormatter", null, null );
89+
method.addParameter( new JParameter( new JType( locationTracker.getName() + ".StringFormatter" ), "stringFormatter" ) );
90+
sc = method.getSourceCode();
91+
sc.add( "this.stringFormatter = stringFormatter;" );
92+
jClass.addMethod( method );
93+
8394
method = new JMethod( "toString", new JType( "String" ), null );
8495
method.getModifiers().makeProtected();
8596

8697
method.addParameter( new JParameter( new JType( locationTracker.getName() ), "location" ) );
8798

8899
sc = method.getSourceCode();
100+
sc.add( "if ( stringFormatter != null )" );
101+
sc.add( "{" );
102+
sc.addIndented( "return stringFormatter.toString( location );" );
103+
sc.add( "}" );
89104
sc.add( "return ' ' + " + ( ( sourceTracker == null ) ? "" : "location.getSource().toString() + ':' + " )
90105
+ "location.getLineNumber() + ' ';" );
91106

0 commit comments

Comments
 (0)