1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* 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.
*/
/* $Id$ */
package org.apache.fop.tools;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Node;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.selectors.FilenameSelector;
/**
* Ant task which inspects a file set for Java interfaces which extend the
* {@link org.apache.fop.events.EventProducer} interface. For all such interfaces an event model
* file and a translation file for the human-readable messages generated by the events is
* created and/or updated.
*/
public class EventProducerCollectorTask extends Task {
private List filesets = new java.util.ArrayList();
private File modelFile;
private File translationFile;
/** {@inheritDoc} */
public void execute() throws BuildException {
try {
EventProducerCollector collector = new EventProducerCollector();
processFileSets(collector);
getModelFile().getParentFile().mkdirs();
collector.saveModelToXML(getModelFile());
log("Event model written to " + getModelFile());
if (getTranslationFile() != null) {
updateTranslationFile();
}
} catch (ClassNotFoundException e) {
throw new BuildException(e);
} catch (EventConventionException ece) {
throw new BuildException(ece);
} catch (IOException ioe) {
throw new BuildException(ioe);
}
}
private static final String MODEL2TRANSLATION = "model2translation.xsl";
private static final String MERGETRANSLATION = "merge-translation.xsl";
/**
* Updates the translation file with new entries for newly found event producer methods.
* @throws IOException if an I/O error occurs
*/
protected void updateTranslationFile() throws IOException {
try {
boolean resultExists = getTranslationFile().exists();
SAXTransformerFactory tFactory
= (SAXTransformerFactory)SAXTransformerFactory.newInstance();
//Generate fresh generated translation file as template
Source src = new StreamSource(getModelFile());
StreamSource xslt1 = new StreamSource(
getClass().getResourceAsStream(MODEL2TRANSLATION));
if (xslt1.getInputStream() == null) {
throw new FileNotFoundException(MODEL2TRANSLATION + " not found");
}
DOMResult domres = new DOMResult();
Transformer transformer = tFactory.newTransformer(xslt1);
transformer.transform(src, domres);
final Node generated = domres.getNode();
Node sourceDocument;
if (resultExists) {
//Load existing translation file into memory (because we overwrite it later)
src = new StreamSource(getTranslationFile());
domres = new DOMResult();
transformer = tFactory.newTransformer();
transformer.transform(src, domres);
sourceDocument = domres.getNode();
} else {
//Simply use generated as source document
sourceDocument = generated;
}
//Generate translation file (with potentially new translations)
src = new DOMSource(sourceDocument);
Result res = new StreamResult(getTranslationFile());
StreamSource xslt2 = new StreamSource(
getClass().getResourceAsStream(MERGETRANSLATION));
if (xslt2.getInputStream() == null) {
throw new FileNotFoundException(MERGETRANSLATION + " not found");
}
transformer = tFactory.newTransformer(xslt2);
transformer.setURIResolver(new URIResolver() {
public Source resolve(String href, String base) throws TransformerException {
if ("my:dom".equals(href)) {
return new DOMSource(generated);
}
return null;
}
});
if (resultExists) {
transformer.setParameter("generated-url", "my:dom");
}
transformer.transform(src, res);
if (resultExists) {
log("Translation file updated: " + getTranslationFile());
} else {
log("Translation file generated: " + getTranslationFile());
}
} catch (TransformerException te) {
throw new IOException(te.getMessage());
}
}
/**
* Processes the file sets defined for the task.
* @param collector the collector to use for collecting the event producers
* @throws IOException if an I/O error occurs
* @throws EventConventionException if the EventProducer conventions are violated
* @throws ClassNotFoundException if a required class cannot be found
*/
protected void processFileSets(EventProducerCollector collector)
throws IOException, EventConventionException, ClassNotFoundException {
Iterator iter = filesets.iterator();
while (iter.hasNext()) {
FileSet fs = (FileSet)iter.next();
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] srcFiles = ds.getIncludedFiles();
File directory = fs.getDir(getProject());
for (int i = 0, c = srcFiles.length; i < c; i++) {
String filename = srcFiles[i];
File src = new File(directory, filename);
collector.scanFile(src);
}
}
}
/**
* Adds a file set.
* @param set the file set
*/
public void addFileset(FileSet set) {
filesets.add(set);
}
/**
* Sets the model file to be written.
* @param f the model file
*/
public void setModelFile(File f) {
this.modelFile = f;
}
/**
* Returns the model file to be written.
* @return the model file
*/
public File getModelFile() {
return this.modelFile;
}
/**
* Sets the translation file for the event producer methods.
* @param f the translation file
*/
public void setTranslationFile(File f) {
this.translationFile = f;
}
/**
* Returns the translation file for the event producer methods.
* @return the translation file
*/
public File getTranslationFile() {
return this.translationFile;
}
/**
* Command-line interface for testing purposes.
* @param args the command-line arguments
*/
public static void main(String[] args) {
try {
Project project = new Project();
EventProducerCollectorTask generator = new EventProducerCollectorTask();
generator.setProject(project);
project.setName("Test");
FileSet fileset = new FileSet();
fileset.setDir(new File("test/java"));
FilenameSelector selector = new FilenameSelector();
selector.setName("**/*.java");
fileset.add(selector);
generator.addFileset(fileset);
File targetDir = new File("build/codegen1");
targetDir.mkdirs();
generator.setModelFile(new File("D:/out.xml"));
generator.setTranslationFile(new File("D:/out1.xml"));
generator.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|