1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.commons.jelly.tags.antlr; |
18 |
|
|
19 |
|
import org.apache.commons.jelly.XMLOutput; |
20 |
|
import org.apache.commons.jelly.TagSupport; |
21 |
|
import org.apache.commons.jelly.MissingAttributeException; |
22 |
|
import org.apache.commons.jelly.JellyTagException; |
23 |
|
|
24 |
|
import antlr.Tool; |
25 |
|
|
26 |
|
import java.io.File; |
27 |
|
import java.io.FileReader; |
28 |
|
import java.io.BufferedReader; |
29 |
|
import java.security.Permission; |
30 |
|
import java.util.List; |
31 |
|
import java.util.ArrayList; |
32 |
|
import java.util.Iterator; |
33 |
|
|
34 |
|
public class AntlrTag extends TagSupport |
35 |
|
{ |
36 |
|
private List grammars; |
37 |
|
private File outputDir; |
38 |
|
|
39 |
|
public AntlrTag() |
40 |
0 |
{ |
41 |
0 |
this.grammars = new ArrayList( 1 ); |
42 |
0 |
} |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException |
53 |
|
{ |
54 |
0 |
if ( this.outputDir == null ) |
55 |
|
{ |
56 |
0 |
throw new MissingAttributeException( "outputDir" ); |
57 |
|
} |
58 |
|
|
59 |
0 |
invokeBody( output ); |
60 |
|
|
61 |
0 |
Iterator grammarIter = this.grammars.iterator(); |
62 |
0 |
String eachGrammar = null; |
63 |
|
|
64 |
0 |
String sourceDir = (String) getContext().getVariable( "maven.antlr.src.dir" ); |
65 |
0 |
File grammar = null; |
66 |
|
|
67 |
0 |
while ( grammarIter.hasNext() ) |
68 |
|
{ |
69 |
0 |
eachGrammar = ((String) grammarIter.next()).trim(); |
70 |
|
|
71 |
0 |
grammar = new File( sourceDir, |
72 |
|
eachGrammar ); |
73 |
|
|
74 |
0 |
File generated = getGeneratedFile( grammar.getPath() ); |
75 |
|
|
76 |
0 |
if ( generated.exists() ) |
77 |
|
{ |
78 |
0 |
if ( generated.lastModclass="keyword">ified() > grammar.lastModclass="keyword">ified() ) |
79 |
|
{ |
80 |
|
|
81 |
0 |
return; |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
0 |
if ( ! generated.getParentFile().exists() ) |
86 |
|
{ |
87 |
0 |
generated.getParentFile().mkdirs(); |
88 |
|
} |
89 |
|
|
90 |
0 |
String[] args = new String[] |
91 |
|
{ |
92 |
|
"-o", |
93 |
|
generated.getParentFile().getPath(), |
94 |
|
grammar.getPath(), |
95 |
|
}; |
96 |
|
|
97 |
0 |
SecurityManager oldSm = System.getSecurityManager(); |
98 |
|
|
99 |
0 |
System.setSecurityManager( NoExitSecurityManager.INSTANCE ); |
100 |
|
|
101 |
|
try |
102 |
|
{ |
103 |
0 |
Tool.main( args ); |
104 |
0 |
} |
105 |
|
catch (SecurityException e) |
106 |
|
{ |
107 |
0 |
if ( ! e.getMessage().equals( "exitVM-0" ) ) |
108 |
|
{ |
109 |
0 |
throw new JellyTagException( e ); |
110 |
|
} |
111 |
0 |
} |
112 |
|
finally |
113 |
|
{ |
114 |
0 |
System.setSecurityManager( oldSm ); |
115 |
|
} |
116 |
|
} |
117 |
0 |
} |
118 |
|
|
119 |
|
protected File getGeneratedFile(String grammar) throws JellyTagException |
120 |
|
{ |
121 |
0 |
File grammarFile = new File( grammar ); |
122 |
|
|
123 |
0 |
String generatedFileName = null; |
124 |
|
|
125 |
0 |
String className = null; |
126 |
0 |
String packageName = ""; |
127 |
|
|
128 |
|
try { |
129 |
|
|
130 |
0 |
BufferedReader in = new BufferedReader(class="keyword">new FileReader(grammar)); |
131 |
|
|
132 |
|
String line; |
133 |
0 |
while ((line = in.readLine()) != null) { |
134 |
0 |
line = line.trim(); |
135 |
0 |
int extendsIndex = line.indexOf(" extends "); |
136 |
0 |
if (line.startsWith("class ") && extendsIndex > -1) { |
137 |
0 |
generatedFileName = line.substring(6, extendsIndex).trim(); |
138 |
0 |
break; |
139 |
|
} |
140 |
0 |
else if ( line.startsWith( "package" ) ) { |
141 |
0 |
packageName = line.substring( 8 ).trim(); |
142 |
|
} |
143 |
|
} |
144 |
0 |
in.close(); |
145 |
0 |
} catch (Exception e) { |
146 |
0 |
throw new JellyTagException("Unable to determine generated class", |
147 |
|
e); |
148 |
|
} |
149 |
0 |
if (generatedFileName == null) { |
150 |
0 |
return null; |
151 |
|
} |
152 |
|
|
153 |
0 |
File genFile = null; |
154 |
|
|
155 |
0 |
if ( "".equals( packageName ) ) |
156 |
|
{ |
157 |
0 |
genFile = new File( getOutputDir(), |
158 |
|
generatedFileName + ".java" ); |
159 |
|
} |
160 |
|
else |
161 |
|
{ |
162 |
0 |
String packagePath = packageName.replace( '.', |
163 |
|
File.separatorChar ); |
164 |
|
|
165 |
0 |
packagePath = packagePath.replace( ';', |
166 |
|
File.separatorChar ); |
167 |
|
|
168 |
0 |
genFile = new File( class="keyword">new File( getOutputDir(), packagePath), |
169 |
|
generatedFileName + ".java" ); |
170 |
|
} |
171 |
|
|
172 |
0 |
return genFile; |
173 |
|
} |
174 |
|
|
175 |
|
void addGrammar(String grammar) |
176 |
|
{ |
177 |
0 |
this.grammars.add( grammar ); |
178 |
0 |
} |
179 |
|
|
180 |
|
public void setOutputDir(File outputDir) |
181 |
|
{ |
182 |
0 |
this.outputDir = outputDir; |
183 |
0 |
} |
184 |
|
|
185 |
|
public File getOutputDir() |
186 |
|
{ |
187 |
0 |
return this.outputDir; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
class NoExitSecurityManager extends SecurityManager |
192 |
|
{ |
193 |
|
static final NoExitSecurityManager INSTANCE = new NoExitSecurityManager(); |
194 |
|
|
195 |
|
private NoExitSecurityManager() |
196 |
|
{ |
197 |
|
} |
198 |
|
|
199 |
|
public void checkPermission(Permission permission) |
200 |
|
{ |
201 |
|
} |
202 |
|
|
203 |
|
public void checkExit(int status) |
204 |
|
{ |
205 |
|
throw new SecurityException( "exitVM-" + status ); |
206 |
|
} |
207 |
|
} |