Skip to content

Commit 035f6eb

Browse files
author
Olivier Chafik
committed
Velocity: prepare function to avoid expansion of Java comments (due to conflicts with JavaDoc notation)
1 parent 4f45bcf commit 035f6eb

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.nativelibs4java.velocity;
2+
3+
import com.google.common.base.Function;
4+
import java.io.BufferedReader;
5+
import java.io.File;
6+
import java.io.FileReader;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
import java.io.StringWriter;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
14+
public class Utils {
15+
16+
17+
protected static String processComments(String source, Function<String, String> processor) {
18+
Pattern p = Pattern.compile("(?m)(?s)/\\*\\*?.*?\\*/");
19+
Matcher m = p.matcher(source);
20+
StringBuffer sb = new StringBuffer();
21+
while (m.find()) {
22+
String comment = m.group();
23+
String replacement = processor.apply(comment);
24+
m.appendReplacement(sb, "");
25+
sb.append(replacement);
26+
}
27+
m.appendTail(sb);
28+
return sb.toString();
29+
}
30+
31+
protected static String quoteSharpsInComments(String source) {
32+
return processComments(source, new Function<String, String>() {
33+
public String apply(String f) {
34+
return f.replaceAll("#", "\\\\#");
35+
}
36+
});
37+
}
38+
39+
protected static String unquoteSharpsInComments(String source) {
40+
return processComments(source, new Function<String, String>() {
41+
public String apply(String f) {
42+
return f.replaceAll("\\\\#", "#");
43+
}
44+
});
45+
}
46+
47+
static String readTextFile(File file) throws IOException {
48+
BufferedReader sourceIn = new BufferedReader(new FileReader(file));
49+
StringWriter sourceOut = new StringWriter();
50+
PrintWriter sourcePOut = new PrintWriter(sourceOut);
51+
String line;
52+
while ((line = sourceIn.readLine()) != null) {
53+
sourcePOut.println(line);
54+
}
55+
return sourceOut.toString();
56+
}
57+
58+
static void writeTextFile(File file, String text) throws IOException {
59+
FileWriter f = new FileWriter(file);
60+
f.write(unquoteSharpsInComments(text));
61+
f.close();
62+
}
63+
}

src/main/java/com/nativelibs4java/velocity/VelocityMojo.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,30 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
import static com.nativelibs4java.velocity.Utils.*;
19+
import com.google.common.base.Function;
20+
import java.io.BufferedReader;
1821
import org.apache.maven.plugin.AbstractMojo;
1922
import org.apache.maven.plugin.MojoExecutionException;
2023
import org.apache.maven.project.MavenProject;
2124
import org.apache.maven.model.Resource;
2225

2326
import java.io.File;
27+
import java.io.FileReader;
2428
import java.io.FileWriter;
2529
import java.io.IOException;
30+
import java.io.PrintWriter;
2631
import java.io.StringWriter;
2732
import java.util.ArrayList;
2833
import java.util.Collection;
2934
import java.util.Map;
3035
import java.util.List;
36+
import java.util.regex.Matcher;
37+
import java.util.regex.Pattern;
3138
import org.apache.velocity.*;
3239
import org.apache.velocity.app.VelocityEngine;
3340
import org.apache.velocity.app.Velocity;
41+
import org.codehaus.plexus.util.IOUtil;
3442

3543
/**
3644
* Generates source code with velocity templates
@@ -254,8 +262,6 @@ private boolean executeAll(File velocitySources, boolean isTest) throws MojoExec
254262
}
255263

256264
VelocityEngine ve = createEngine(canoPath);
257-
org.apache.velocity.Template template = ve.getTemplate(cano);//file.getName());
258-
259265
VelocityContext context = new VelocityContext();//execution.getParameters());
260266
context.put("primitives", Primitive.getPrimitives());
261267
context.put("primitivesNoBool", Primitive.getPrimitivesNoBool());
@@ -279,15 +285,21 @@ private boolean executeAll(File velocitySources, boolean isTest) throws MojoExec
279285
}
280286

281287
StringWriter out = new StringWriter();
282-
template.merge(context, out);
288+
289+
boolean quoteComments = false;
290+
if (quoteComments) {
291+
String source = readTextFile(file);
292+
String quoted = quoteSharpsInComments(source);
293+
ve.evaluate(context, out, "velocity", quoted);
294+
} else {
295+
org.apache.velocity.Template template = ve.getTemplate(cano);//file.getName());
296+
template.merge(context, out);
297+
}
283298
out.close();
284299

285300
outFile.getParentFile().mkdirs();
286-
287-
288-
FileWriter f = new FileWriter(outFile);
289-
f.write(out.toString());
290-
f.close();
301+
String transformed = out.toString();
302+
writeTextFile(outFile, transformed);
291303
//getLog().info("\tGenerated '" + outFile.getName() + "'");
292304

293305
} catch (Exception ex) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package com.nativelibs4java.velocity;
6+
7+
import static com.nativelibs4java.velocity.Utils.*;
8+
import org.junit.After;
9+
import org.junit.AfterClass;
10+
import org.junit.Before;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
import static org.junit.Assert.*;
14+
15+
/**
16+
*
17+
* @author ochafik
18+
*/
19+
public class UtilsTest {
20+
@Test
21+
public void testQuote() {
22+
assertEquals(quoteSharpsInComments("#"), "#");
23+
String q = quoteSharpsInComments("a#\n/*\n#c*/");
24+
assertEquals("a#\n/*\n\\#c*/", quoteSharpsInComments("a#\n/*\n#c*/"));
25+
assertEquals("a#\n/*\n#c*/", unquoteSharpsInComments("a#\n/*\n\\#c*/"));
26+
}
27+
}

0 commit comments

Comments
 (0)