-
Notifications
You must be signed in to change notification settings - Fork 5
/
AntProcessReader.java
227 lines (193 loc) · 6.5 KB
/
AntProcessReader.java
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
package com.oxygenxml.ant.dita;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.dita.dost.util.CatalogUtils;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
public class AntProcessReader implements XMLReader {
/**
* Entity resolver
*/
private EntityResolver resolver;
/**
* Content Handler
*/
private ContentHandler handler;
/**
* Error Handler.
*/
private ErrorHandler errorHandler;
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return false;
}
@Override
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
}
@Override
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return null;
}
@Override
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
}
@Override
public void setEntityResolver(EntityResolver resolver) {
this.resolver = resolver;
}
@Override
public EntityResolver getEntityResolver() {
return resolver;
}
@Override
public void setDTDHandler(DTDHandler handler) {
//
}
@Override
public DTDHandler getDTDHandler() {
return null;
}
@Override
public void setContentHandler(ContentHandler handler) {
this.handler = handler;
}
@Override
public ContentHandler getContentHandler() {
return handler;
}
@Override
public void setErrorHandler(ErrorHandler handler) {
errorHandler = handler;
}
@Override
public ErrorHandler getErrorHandler() {
return errorHandler;
}
@Override
public void parse(InputSource input) throws IOException, SAXException {
try {
InputStream is = input.getByteStream();
URL url = new URL(input.getSystemId());
if(is == null) {
URL urlForConnect = url;
if("file".equals(url.getProtocol())) {
String urlStr = urlForConnect.toString();
if(urlStr.contains("?")) {
//Remove query part
urlForConnect = new URL(urlStr.substring(0, urlStr.indexOf("?")));
}
}
is = urlForConnect.openStream();
}
File originalFile = new File(url.toURI());
if(originalFile != null) {
//Expected output file
File outputFile = File.createTempFile("test", ".out");
Source resolvedBuildXML = CatalogUtils.getCatalogResolver().resolve("plugin:com.oxygenxml.ant.parser.dita:" + "/resources/build.xml", new File(".").toURI().toURL().toString());
if(resolvedBuildXML != null) {
//We need to run the build file.
boolean success = executeAntBuildFile(
new File(new URL(resolvedBuildXML.getSystemId()).toURI()).getAbsolutePath(),
originalFile.getAbsolutePath(), outputFile.getAbsolutePath());
if(success) {
StringBuilder contentBuilder = new StringBuilder();
FileInputStream fis = new FileInputStream(outputFile);
InputStreamReader reader = null;
try {
char[] chars = new char[1024];
reader = new InputStreamReader(fis, "UTF-8");
int len = -1;
while((len = reader.read(chars)) != -1) {
contentBuilder.append(chars, 0, len);
}
} finally {
if(reader != null) {
//Close the input stream
reader.close();
}
}
TransformerFactory factory = TransformerFactory.newInstance();
factory.setURIResolver(CatalogUtils.getCatalogResolver());
SAXResult result = new SAXResult(handler);
Transformer newTransformer = factory.newTransformer();
newTransformer.transform(new StreamSource(new StringReader(contentBuilder.toString()), url.toString()),
result);
}
}
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
/**
* Exec an ANT build file.
* @param buildXmlFileFullPath Path to the build file
* @return <code>true</code> if success
*/
private static boolean executeAntBuildFile(String buildXmlFileFullPath, String inputFilePath, String outputFilePath) {
boolean success = false;
DefaultLogger consoleLogger = getConsoleLogger();
// Prepare Ant project
Project project = new Project();
File buildFile = new File(buildXmlFileFullPath);
project.setUserProperty("ant.file", buildFile.getAbsolutePath());
project.setUserProperty("input.file", inputFilePath);
project.setUserProperty("output.file", outputFilePath);
project.addBuildListener(consoleLogger);
// Capture event for Ant script build start / stop / failure
try {
project.fireBuildStarted();
project.init();
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", projectHelper);
projectHelper.parse(project, buildFile);
// If no target specified then default target will be executed.
project.executeTarget("dist");
project.fireBuildFinished(null);
success = true;
} catch (BuildException buildException) {
project.fireBuildFinished(buildException);
throw new RuntimeException("Unable to execute build file", buildException);
}
return success;
}
/**
* Logger to log output generated while executing ant script in console
*
* @return
*/
private static DefaultLogger getConsoleLogger() {
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
return consoleLogger;
}
@Override
public void parse(String systemId) throws IOException, SAXException {
parse(new InputSource(systemId));
}
}