Révision 3921
TXM/trunk/bundles/org.txm.groovy.core/src/groovy/org/txm/macro/xml/ExecXSLDOMMacro.groovy (revision 3921) | ||
---|---|---|
1 |
package org.txm.macro.xml; |
|
2 |
// STANDARD DECLARATIONS |
|
3 |
|
|
4 |
import org.kohsuke.args4j.* |
|
5 |
import groovy.transform.Field |
|
6 |
import org.txm.rcpapplication.swt.widget.parameters.* |
|
7 |
import org.txm.importer.ApplyXsl2; |
|
8 |
import javax.xml.transform.stream.* |
|
9 |
import javax.xml.transform.dom.DOMResult |
|
10 |
import org.w3c.dom.* |
|
11 |
|
|
12 |
|
|
13 |
// BEGINNING OF PARAMETERS |
|
14 |
@Field @Option(name="XSLFile", usage="an example file", widget="File", required=true, def="file.xsl") |
|
15 |
def XSLFile = new File(System.getProperty("user.home"),"TXM/xsl/identity.xsl") |
|
16 |
|
|
17 |
@Field @Option(name="intputDirectory", usage="an example folder", widget="Folder", required=true, def="in") |
|
18 |
def intputDirectory = new File(System.getProperty("user.home"),"xml/TESTS2/xml") |
|
19 |
|
|
20 |
//@Field @Option(name="parameters", usage="an example folder", widget="Text", required=false, def="") |
|
21 |
def parameters = [:] |
|
22 |
|
|
23 |
@Field @Option(name="dom", usage="XSLT Result is - true: a DOM Element. false - a XSLT Result is XMLStreamReader", widget="Boolean", required=true, def="true") |
|
24 |
def dom |
|
25 |
|
|
26 |
@Field @Option(name="debug", usage="Show debug messages, value = true|false", widget="Boolean", required=true, def="false") |
|
27 |
def debug |
|
28 |
|
|
29 |
if (!ParametersDialog.open(this)) return; |
|
30 |
// END OF PARAMETERS |
|
31 |
|
|
32 |
// USER MANIPULATIONS |
|
33 |
|
|
34 |
def processDOMResult(File inputXMLFile, def resultnode) { |
|
35 |
// with resultnode a Element : https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Element.html |
|
36 |
println inputXMLFile.getName()+" -> "+ resultnode.getTagName() |
|
37 |
} |
|
38 |
|
|
39 |
// END USER MANIPULATIONS |
|
40 |
|
|
41 |
println "Use XSL $XSLFile with parameters $parameters" |
|
42 |
println "Processed directory: $intputDirectory" |
|
43 |
|
|
44 |
def files = [] |
|
45 |
ApplyXsl2 a = new ApplyXsl2(XSLFile.getAbsolutePath()); |
|
46 |
intputDirectory.eachFileMatch(~/.+\.(xml|XML)/) { XMLFile -> |
|
47 |
String name = XMLFile.getName() |
|
48 |
try { |
|
49 |
def result = process(a, XMLFile, [:]); |
|
50 |
if (dom) processDOMResult(XMLFile, result.getNode().getDocumentElement()); |
|
51 |
else processSaxResult(XMLFile, result); |
|
52 |
files << XMLFile |
|
53 |
} catch (Exception e) { |
|
54 |
println "Warning: XSL transformation of '$name' failed with error=$e with " |
|
55 |
if (debug) e.printStackTrace(); |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
def process(ApplyXsl2 a, File inputXMLFile, def args) throws Exception { |
|
60 |
for (String k : args.keySet()) { |
|
61 |
if (!this.setParam(k, args[k])) |
|
62 |
return false; |
|
63 |
} |
|
64 |
|
|
65 |
def result = null; |
|
66 |
if (dom) result = new DOMResult(); |
|
67 |
else { |
|
68 |
XMLStreamReader xmlreader = new XMLStreamReader(); |
|
69 |
PipedInputStream inpipe = new PipedInputStream(xmlreader |
|
70 |
PipedOutputStream outpipe = new PipedOutputStream(); |
|
71 |
result = new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile))); |
|
72 |
} |
|
73 |
a.transformer.transform(new StreamSource(inputXMLFile), result); |
|
74 |
a.cleanMemory(); // save memory |
|
75 |
a.resetParams() |
|
76 |
return result; |
|
77 |
} |
TXM/trunk/bundles/org.txm.groovy.core/src/groovy/org/txm/macro/xml/GetXPathMacro.groovy (revision 3921) | ||
---|---|---|
16 | 16 |
|
17 | 17 |
// STANDARD DECLARATIONS |
18 | 18 |
|
19 |
import javax.xml.bind.helpers.DefaultValidationEventHandler |
|
20 | 19 |
import javax.xml.xpath.XPathConstants |
21 | 20 |
import javax.xml.namespace.NamespaceContext |
21 |
import javax.xml.transform.stream.StreamSource |
|
22 | 22 |
import javax.xml.XMLConstants |
23 | 23 |
|
24 |
import net.sf.saxon.dom.DocumentBuilderFactoryImpl
|
|
24 |
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
|
|
25 | 25 |
import net.sf.saxon.xpath.XPathFactoryImpl |
26 | 26 |
import net.sf.saxon.s9api.Processor |
27 | 27 |
import net.sf.saxon.s9api.Serializer |
... | ... | |
30 | 30 |
import org.kohsuke.args4j.Option |
31 | 31 |
import groovy.transform.Field |
32 | 32 |
|
33 |
import org.txm.importer.GetXPath |
|
33 | 34 |
import org.txm.rcp.swt.widget.parameters.* |
34 | 35 |
import org.txm.scripts.importer.XPathResult |
35 | 36 |
|
... | ... | |
41 | 42 |
// (available widget types: Query, File, Folder, String, Text, Boolean, Integer, Float and Date) |
42 | 43 |
|
43 | 44 |
@Field @Option(name="srcFile", usage="XML source file", widget="File", required=false, def="") |
44 |
def srcFile |
|
45 |
def srcFile
|
|
45 | 46 |
|
46 | 47 |
@Field @Option(name="srcDirectory", usage="XML source directory", widget="Folder", required=false, def="") |
47 |
def srcDirectory |
|
48 |
def srcDirectory
|
|
48 | 49 |
|
49 | 50 |
@Field @Option(name="filterByFileExtension", usage="filter by file extension", widget="Boolean", required=false, def="true") |
50 |
def filterByFileExtension |
|
51 |
def filterByFileExtension
|
|
51 | 52 |
|
52 | 53 |
@Field @Option(name="fileExtension", usage="file extension to filter (eg .xml)", widget="String", required=false, def=".xml") |
53 |
def fileExtension |
|
54 |
def fileExtension
|
|
54 | 55 |
|
55 | 56 |
@Field @Option(name="XPath", usage="XPath expression", widget="String", required=true, def="//tei:title/text()") |
56 |
def XPath |
|
57 |
def XPath
|
|
57 | 58 |
|
58 | 59 |
@Field @Option(name="lineNumber", usage="print line number", widget="Boolean", required=false, def="true") |
59 |
def lineNumber |
|
60 |
def lineNumber
|
|
60 | 61 |
|
61 | 62 |
@Field @Option(name="wrapLines", usage="wrap output lines longer than 100 characters", widget="Boolean", required=false, def="true") |
62 |
def wrapLines |
|
63 |
def wrapLines
|
|
63 | 64 |
|
64 | 65 |
//@Field @Option(name="interactive", usage="open parameters dialog box", widget="Boolean", required=true, def="true") |
65 | 66 |
//def interactive |
... | ... | |
75 | 76 |
lineNumberLen = 0 |
76 | 77 |
columnNumberLen = 0 |
77 | 78 |
|
78 |
def newDocBuilder = { -> |
|
79 |
|
|
80 |
DocumentBuilderFactoryImpl factory = new DocumentBuilderFactoryImpl() |
|
81 |
factory.setNamespaceAware(true) |
|
82 |
return factory.newDocumentBuilder() |
|
83 |
|
|
84 |
/* |
|
85 |
use catalog |
|
86 |
use local DTD |
|
87 |
use entity resolver |
|
88 |
|
|
89 |
import org.apache.xml.resolver.tools.CatalogResolver; |
|
90 |
import java.util.Properties; |
|
91 |
import java.io.*; |
|
92 |
import javax.xml.transform.*; |
|
93 |
import javax.xml.transform.stream.StreamResult; |
|
94 |
import javax.xml.transform.stream.StreamSource; |
|
95 |
|
|
96 |
tFactory = TransformerFactory.newInstance() |
|
97 |
resolver = new CatalogResolver() |
|
98 |
tFactory.setURIResolver(resolver) |
|
99 |
|
|
100 |
transformer.setURIResolver(resolver); |
|
101 |
transformer.transform(new StreamSource(xmlFile), new |
|
102 |
StreamResult(new FileOutputStream(outFile))); |
|
103 |
|
|
104 |
To set an EntityResolver from a Java application, you need to create a |
|
105 |
SAXSource rather than a stream source, to instantiate your own XMLReader |
|
106 |
(the SAX parser), set the EntityResolver on the XMLReader, and then do |
|
107 |
saxSource.setXMLReader(). |
|
108 |
|
|
109 |
|
|
110 |
catalog.xml |
|
111 |
|
|
112 |
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> |
|
113 |
<public publicId="-//W3C//DTD HTML 4.01 Transitional//EN" uri="dummy.dtd"/> |
|
114 |
<system systemId="http://www.w3.org/TR/html4/loose.dtd" uri="dummy.dtd"/> |
|
115 |
</catalog> |
|
116 |
|
|
117 |
dummy.dtd |
|
118 |
<!ELEMENT html ANY>srcFile |
|
119 |
|
|
120 |
|
|
121 |
*/ |
|
122 |
} |
|
123 |
|
|
124 |
def newDoc = { builder, file -> |
|
125 |
|
|
126 |
return builder.parse(file) |
|
127 |
} |
|
128 |
|
|
129 |
def newXpath = { config -> |
|
130 |
|
|
131 |
def xfactory = new XPathFactoryImpl() |
|
132 |
xfactory.setConfiguration(config) |
|
133 |
def xpath = xfactory.newXPath() |
|
134 |
|
|
135 |
xpath.setNamespaceContext(new NamespaceContext() { |
|
136 |
String TEINS = "http://www.tei-c.org/ns/1.0" |
|
137 |
String MENS = "http://www.menota.org/ns/1.0" |
|
138 |
String BFMNS = "http://bfm.ens-lsh.fr/ns/1.0" |
|
139 |
String TXMNS = "http://textometrie.org/1.0" |
|
140 |
String XINS = "http://www.w3.org/2001/XInclude" |
|
141 |
String XHTMLNS = "http://www.w3.org/1999/xhtml" |
|
142 |
String FNNS = "http://www.w3.org/2005/xpath-functions" |
|
143 |
|
|
144 |
String TEINSNAME = "tei" |
|
145 |
String MENSNAME = "me" |
|
146 |
String BFMNSNAME = "bfm" |
|
147 |
String TXMNSNAME = "txm" |
|
148 |
String XINSNAME = "xi" |
|
149 |
String XHTMLNSNAME = "xhtml" |
|
150 |
String FNNSNAME = "fn" |
|
151 |
|
|
152 |
public String getNamespaceURI(String prefix) { |
|
153 |
if (prefix == null) throw new NullPointerException("Null prefix") |
|
154 |
else if (TEINSNAME.equals(prefix)) return TEINS |
|
155 |
else if (MENSNAME.equals(prefix)) return MENS |
|
156 |
else if (BFMNSNAME.equals(prefix)) return BFMNS |
|
157 |
else if (TXMNSNAME.equals(prefix)) return TXMNS |
|
158 |
else if (XINSNAME.equals(prefix)) return XINS |
|
159 |
else if (XHTMLNSNAME.equals(prefix)) return XHTMLNS |
|
160 |
else if (FNNSNAME.equals(prefix)) return FNNS |
|
161 |
else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) return XMLConstants.XML_NS_URI |
|
162 |
return XMLConstants.NULL_NS_URI |
|
163 |
} |
|
164 |
|
|
165 |
public String getPrefix(String uri) { |
|
166 |
if (uri == null) throw new NullPointerException("Null prefix") |
|
167 |
else if (TEINS.equals(uri)) return TEINSNAME |
|
168 |
else if (MENS.equals(uri)) return MENSNAME |
|
169 |
else if (BFMNS.equals(uri)) return BFMNSNAME |
|
170 |
else if (TXMNS.equals(uri)) return TXMNSNAME |
|
171 |
else if (XINS.equals(uri)) return XINSNAME |
|
172 |
else if (XHTMLNS.equals(uri)) return XHTMLNSNAME |
|
173 |
else if (FNNS.equals(uri)) return FNNSNAME |
|
174 |
else if("http://www.w3.org/XML/1998/namespace") return XMLConstants.XML_NS_PREFIX |
|
175 |
else if (XMLConstants.XML_NS_URI.equals(uri)) return XMLConstants.XML_NS_PREFIX |
|
176 |
else XMLConstants.NULL_NS_URI |
|
177 |
} |
|
178 |
|
|
179 |
public Iterator getPrefixes(String uri) {throw new UnsupportedOperationException() } |
|
180 |
}) |
|
181 |
|
|
182 |
xpath.setNamespaceContext(new NamespaceContext() { |
|
183 |
String TEINS = "http://www.tei-c.org/ns/1.0" |
|
184 |
String MENS = "http://www.menota.org/ns/1.0" |
|
185 |
String BFMNS = "http://bfm.ens-lsh.fr/ns/1.0" |
|
186 |
String TXMNS = "http://textometrie.org/1.0" |
|
187 |
String XINS = "http://www.w3.org/2001/XInclude" |
|
188 |
String XHTMLNS = "http://www.w3.org/1999/xhtml" |
|
189 |
String FNNS = "http://www.w3.org/2005/xpath-functions" |
|
190 |
|
|
191 |
String TEINSNAME = "tei" |
|
192 |
String MENSNAME = "me" |
|
193 |
String BFMNSNAME = "bfm" |
|
194 |
String TXMNSNAME = "txm" |
|
195 |
String XINSNAME = "xi" |
|
196 |
String XHTMLNSNAME = "xhtml" |
|
197 |
String FNNSNAME = "fn" |
|
198 |
|
|
199 |
public String getNamespaceURI(String prefix) { |
|
200 |
if (prefix == null) throw new NullPointerException("Null prefix") |
|
201 |
else if (TEINSNAME.equals(prefix)) return TEINS |
|
202 |
else if (MENSNAME.equals(prefix)) return MENS |
|
203 |
else if (BFMNSNAME.equals(prefix)) return BFMNS |
|
204 |
else if (TXMNSNAME.equals(prefix)) return TXMNS |
|
205 |
else if (XINSNAME.equals(prefix)) return XINS |
|
206 |
else if (XHTMLNSNAME.equals(prefix)) return XHTMLNS |
|
207 |
else if (FNNSNAME.equals(prefix)) return FNNS |
|
208 |
else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) return XMLConstants.XML_NS_URI |
|
209 |
return XMLConstants.NULL_NS_URI |
|
210 |
} |
|
211 |
|
|
212 |
public String getPrefix(String uri) { |
|
213 |
if (uri == null) throw new NullPointerException("Null prefix") |
|
214 |
else if (TEINS.equals(uri)) return TEINSNAME |
|
215 |
else if (MENS.equals(uri)) return MENSNAME |
|
216 |
else if (BFMNS.equals(uri)) return BFMNSNAME |
|
217 |
else if (TXMNS.equals(uri)) return TXMNSNAME |
|
218 |
else if (XINS.equals(uri)) return XINSNAME |
|
219 |
else if (XHTMLNS.equals(uri)) return XHTMLNSNAME |
|
220 |
else if (FNNS.equals(uri)) return FNNSNAME |
|
221 |
else if("http://www.w3.org/XML/1998/namespace") return XMLConstants.XML_NS_PREFIX |
|
222 |
else if (XMLConstants.XML_NS_URI.equals(uri)) return XMLConstants.XML_NS_PREFIX |
|
223 |
else XMLConstants.NULL_NS_URI |
|
224 |
} |
|
225 |
|
|
226 |
public Iterator getPrefixes(String uri) {throw new UnsupportedOperationException() } |
|
227 |
}) |
|
228 |
|
|
229 |
return xpath |
|
230 |
} |
|
231 |
|
|
232 |
def getXPath = { xpath, doc -> |
|
233 |
|
|
234 |
return xpathProc.evaluate(xpath, doc, XPathConstants.NODESET) |
|
235 |
} |
|
236 |
|
|
237 |
builder = newDocBuilder() |
|
238 |
config = builder.getConfiguration() |
|
239 |
if (lineNumber) { |
|
240 |
config.setLineNumbering(true) |
|
241 |
} |
|
242 |
xpathProc = newXpath(config) |
|
243 |
|
|
244 |
def serializeNode(node) { |
|
245 |
processor = new Processor(false) |
|
246 |
serializer = processor.newSerializer() |
|
247 |
// Other properties found here: http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Serializer.Property.html |
|
248 |
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes") |
|
249 |
serializer.setOutputProperty(Serializer.Property.INDENT, "yes") |
|
250 |
xdmNode = new XdmNode(node.getUnderlyingNodeInfo()) |
|
251 |
return(serializer.serializeNodeToString(xdmNode)) |
|
252 |
} |
|
253 |
|
|
254 |
def printValue(node) { |
|
255 |
|
|
256 |
// println "beginning of printValue" |
|
257 |
//println "result type is of type "+node.getClass() |
|
258 |
switch (node.getNodeType()) { |
|
259 |
|
|
260 |
case 1: // element |
|
261 |
//println "case 1 : "+node.getClass() |
|
262 |
// println "beginning of case 1" |
|
263 |
if (lineNumber) { |
|
264 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
265 |
} |
|
266 |
if (wrapLines) { |
|
267 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
268 |
} else { |
|
269 |
println serializeNode(node) |
|
270 |
} |
|
271 |
break |
|
272 |
|
|
273 |
case 2: // attribute |
|
274 |
//println "case 2 : "+node.getClass() |
|
275 |
// println "beginning of case 2" |
|
276 |
if (lineNumber) { |
|
277 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
278 |
} |
|
279 |
if (wrapLines) { |
|
280 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
281 |
} else { |
|
282 |
println serializeNode(node) |
|
283 |
} |
|
284 |
break |
|
285 |
|
|
286 |
default: |
|
287 |
//println "other case : "+node.getClass() |
|
288 |
// println "beginning of case 3" |
|
289 |
if (lineNumber) { |
|
290 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
291 |
} |
|
292 |
if (wrapLines) { |
|
293 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
294 |
} else { |
|
295 |
println serializeNode(node) |
|
296 |
} |
|
297 |
break |
|
298 |
} |
|
299 |
// println "end of printValue" |
|
300 |
} |
|
301 |
|
|
302 |
// from http://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java |
|
303 |
def countLines(File file) throws IOException { |
|
304 |
InputStream is = new BufferedInputStream(new FileInputStream(file)); |
|
305 |
try { |
|
306 |
byte[] c = new byte[1024]; |
|
307 |
int lineCount = 0; |
|
308 |
int maxColumn = 0 |
|
309 |
int columnCount = 0; |
|
310 |
int readChars = 0; |
|
311 |
boolean endsWithoutNewLine = false; |
|
312 |
while ((readChars = is.read(c)) != -1) { |
|
313 |
for (int i = 0; i < readChars; ++i) { |
|
314 |
columnCount++ |
|
315 |
if (c[i] == '\n') { |
|
316 |
++lineCount |
|
317 |
if (columnCount > maxColumn) { maxColumn = columnCount } |
|
318 |
columnCount=0 |
|
319 |
} |
|
320 |
} |
|
321 |
endsWithoutNewLine = (c[readChars - 1] != '\n'); |
|
322 |
} |
|
323 |
if(endsWithoutNewLine) { |
|
324 |
++lineCount; |
|
325 |
} |
|
326 |
//println "maxColumn = "+maxColumn |
|
327 |
return [lineCount, maxColumn] |
|
328 |
} finally { |
|
329 |
is.close(); |
|
330 |
} |
|
331 |
} |
|
332 |
|
|
333 | 79 |
if ((srcDirectory==null || srcDirectory.size() == 0) && (srcFile==null || srcFile.size() == 0)) { println "** GetXPathMacro: at least a source file or a source directory must be specified."; return} |
334 | 80 |
|
335 | 81 |
if (srcDirectory!=null && srcDirectory.size() > 0 && srcDirectory.exists()) { |
336 |
|
|
82 |
|
|
337 | 83 |
def files = srcDirectory.listFiles() |
338 | 84 |
if (files == null || files.size() == 0) { |
339 | 85 |
println "** GetXPathMacro: No files in $srcDirectory" |
340 | 86 |
return |
341 | 87 |
} |
342 | 88 |
files.sort() |
343 |
|
|
89 |
|
|
344 | 90 |
def noFileSearched = true |
345 | 91 |
|
92 |
xpathProc = GetXPath.build() |
|
93 |
|
|
346 | 94 |
println "GetXPath: input directory = '$srcDirectory', XPath = $XPath" |
347 |
|
|
348 |
for (def xmlFile : files) { |
|
349 | 95 |
|
96 |
for (def xmlFile : files) { |
|
97 |
|
|
350 | 98 |
String name = xmlFile.getName() |
351 | 99 |
|
352 | 100 |
if (filterByFileExtension && !name.endsWith(fileExtension)) { continue } |
353 |
|
|
101 |
|
|
354 | 102 |
println "\n-- $name" |
355 |
|
|
103 |
|
|
356 | 104 |
noFileSearched = false |
357 | 105 |
|
358 | 106 |
if (lineNumber) { |
... | ... | |
360 | 108 |
lineNumberLen = Math.log10(maxLine)+1 as int |
361 | 109 |
columnNumberLen = Math.log10(maxColumn)+1 as int |
362 | 110 |
} |
363 |
|
|
364 |
doc = newDoc(builder, xmlFile) |
|
365 |
res = getXPath(XPath, doc) |
|
366 |
|
|
367 |
// println "res = "+res |
|
368 |
|
|
369 |
if (res.getLength() == 0) { |
|
111 |
|
|
112 |
//res = xpathProc.evaluateExpression(XPath, new StreamSource(xmlFile), XPathConstants.NODESET); |
|
113 |
res = GetXPath.evaluateExpressionToObject(xpathProc, xmlFile, XPath, XPathConstants.NODESET) |
|
114 |
if (res.getLength() == 0) { |
|
370 | 115 |
println "No result." |
371 | 116 |
} else res.each { printValue(it) } |
372 | 117 |
} |
373 |
|
|
118 |
|
|
374 | 119 |
if (noFileSearched) { println "** GetXPath: no file searched." } |
375 |
|
|
120 |
|
|
376 | 121 |
} else if (srcFile!=null && srcFile.exists()) { |
377 |
def xmlFile = srcFile |
|
378 |
String name = xmlFile.getName() |
|
122 |
|
|
123 |
def xmlFile = srcFile |
|
124 |
String name = xmlFile.getName() |
|
125 |
|
|
126 |
println "GetXPath: file = '$xmlFile', XPath = $XPath" |
|
127 |
|
|
128 |
if (lineNumber) { |
|
129 |
(maxLine, maxColumn) = countLines(xmlFile) |
|
130 |
lineNumberLen = Math.log10(maxLine)+1 as int |
|
131 |
columnNumberLen = Math.log10(maxColumn)+1 as int |
|
132 |
} |
|
133 |
|
|
134 |
res = GetXPath.evaluateExpressionToObject(XPath, srcFile, XPathConstants.NODESET); |
|
135 |
|
|
136 |
if (res.getLength() == 0) { |
|
137 |
println "No result." |
|
138 |
} else res.each { printValue(it) } |
|
139 |
} |
|
379 | 140 |
|
380 |
println "GetXPath: file = '$xmlFile', XPath = $XPath" |
|
381 |
|
|
141 |
// FUNCTIONS |
|
142 |
|
|
143 |
def serializeNode(node) { |
|
144 |
processor = new Processor(false) |
|
145 |
serializer = processor.newSerializer() |
|
146 |
// Other properties found here: http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Serializer.Property.html |
|
147 |
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes") |
|
148 |
serializer.setOutputProperty(Serializer.Property.INDENT, "yes") |
|
149 |
xdmNode = new XdmNode(node.getUnderlyingNodeInfo()) |
|
150 |
return(serializer.serializeNodeToString(xdmNode)) |
|
151 |
} |
|
152 |
|
|
153 |
def printValue(node) { |
|
154 |
|
|
155 |
// println "beginning of printValue" |
|
156 |
//println "result type is of type "+node.getClass() |
|
157 |
switch (node.getNodeType()) { |
|
158 |
|
|
159 |
case 1: // element |
|
160 |
//println "case 1 : "+node.getClass() |
|
161 |
// println "beginning of case 1" |
|
382 | 162 |
if (lineNumber) { |
383 |
(maxLine, maxColumn) = countLines(xmlFile) |
|
384 |
lineNumberLen = Math.log10(maxLine)+1 as int |
|
385 |
columnNumberLen = Math.log10(maxColumn)+1 as int |
|
163 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
386 | 164 |
} |
387 |
|
|
388 |
doc = newDoc(builder, srcFile) |
|
389 |
res = getXPath(XPath, doc) |
|
165 |
if (wrapLines) { |
|
166 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
167 |
} else { |
|
168 |
println serializeNode(node) |
|
169 |
} |
|
170 |
break |
|
171 |
|
|
172 |
case 2: // attribute |
|
173 |
//println "case 2 : "+node.getClass() |
|
174 |
// println "beginning of case 2" |
|
175 |
if (lineNumber) { |
|
176 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
177 |
} |
|
178 |
if (wrapLines) { |
|
179 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
180 |
} else { |
|
181 |
println serializeNode(node) |
|
182 |
} |
|
183 |
break |
|
184 |
|
|
185 |
default: |
|
186 |
//println "other case : "+node.getClass() |
|
187 |
// println "beginning of case 3" |
|
188 |
if (lineNumber) { |
|
189 |
print sprintf("%${lineNumberLen}d, %${columnNumberLen}d: ", node.getUnderlyingNodeInfo().getLineNumber(), node.getUnderlyingNodeInfo().getColumnNumber()) |
|
190 |
} |
|
191 |
if (wrapLines) { |
|
192 |
println WordUtils.wrap(serializeNode(node).replaceAll(/\r\n+/, " ").replaceAll(/\r+/, " ").replaceAll(/\n+/, " ").replaceAll(/ +/, " "), 100) |
|
193 |
} else { |
|
194 |
println serializeNode(node) |
|
195 |
} |
|
196 |
break |
|
197 |
} |
|
198 |
// println "end of printValue" |
|
199 |
} |
|
390 | 200 |
|
391 |
// println "res = "+res |
|
392 |
|
|
393 |
if (res.getLength() == 0) { |
|
394 |
println "No result." |
|
395 |
} else res.each { printValue(it) } |
|
201 |
// from https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java |
|
202 |
def countLines(File file) throws IOException { |
|
203 |
InputStream is = new BufferedInputStream(new FileInputStream(file)); |
|
204 |
try { |
|
205 |
byte[] c = new byte[1024]; |
|
206 |
int lineCount = 0; |
|
207 |
int maxColumn = 0 |
|
208 |
int columnCount = 0; |
|
209 |
int readChars = 0; |
|
210 |
boolean endsWithoutNewLine = false; |
|
211 |
while ((readChars = is.read(c)) != -1) { |
|
212 |
for (int i = 0; i < readChars; ++i) { |
|
213 |
columnCount++ |
|
214 |
if (c[i] == '\n') { |
|
215 |
++lineCount |
|
216 |
if (columnCount > maxColumn) { maxColumn = columnCount } |
|
217 |
columnCount=0 |
|
218 |
} |
|
219 |
} |
|
220 |
endsWithoutNewLine = (c[readChars - 1] != '\n'); |
|
221 |
} |
|
222 |
if(endsWithoutNewLine) { |
|
223 |
++lineCount; |
|
224 |
} |
|
225 |
//println "maxColumn = "+maxColumn |
|
226 |
return [lineCount, maxColumn] |
|
227 |
} finally { |
|
228 |
is.close(); |
|
229 |
} |
|
396 | 230 |
} |
TXM/trunk/bundles/org.txm.libs.saxon/META-INF/MANIFEST.MF (revision 3921) | ||
---|---|---|
79 | 79 |
org.xmlresolver.tools, |
80 | 80 |
org.xmlresolver.utils |
81 | 81 |
Bundle-Name: Saxon |
82 |
Bundle-Version: 9.0.0
|
|
82 |
Bundle-Version: 12.0.0
|
|
83 | 83 |
Bundle-ClassPath: ., |
84 | 84 |
lib/jline-2.14.6.jar, |
85 | 85 |
lib/saxon-he-12.2.jar, |
TXM/trunk/bundles/org.txm.core/src/java/org/txm/importer/ApplyXSLDOM.java (revision 3921) | ||
---|---|---|
1 |
package org.txm.importer; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.HashMap; |
|
6 |
|
|
7 |
import javax.xml.transform.TransformerConfigurationException; |
|
8 |
import javax.xml.transform.dom.DOMResult; |
|
9 |
import javax.xml.transform.stream.StreamSource; |
|
10 |
|
|
11 |
import org.w3c.dom.Element; |
|
12 |
|
|
13 |
/** |
|
14 |
* Process a ResultDom after applying a XSL to XML files of a directory |
|
15 |
* |
|
16 |
* Java conversion of ExelXSLDom macro |
|
17 |
* |
|
18 |
* @author mdecorde |
|
19 |
* |
|
20 |
*/ |
|
21 |
public abstract class ApplyXSLDOM { |
|
22 |
// STANDARD DECLARATIONS |
|
23 |
|
|
24 |
public boolean debug = true; |
|
25 |
|
|
26 |
public abstract void processDOMResultToFile(File inputXMLFile, Element resultnode); |
|
27 |
|
|
28 |
// END USER MANIPULATIONS |
|
29 |
|
|
30 |
public void processFile(File xslFile, File inputDirectory, File outputDirectory, HashMap<String, String> args) throws TransformerConfigurationException { |
|
31 |
System.out.println("Processing directory: $inputDirectory"); |
|
32 |
|
|
33 |
ApplyXsl2 a = new ApplyXsl2(xslFile.getAbsolutePath()); |
|
34 |
File[] toProcess = inputDirectory.listFiles(); |
|
35 |
for(File XMLFile : toProcess) { |
|
36 |
|
|
37 |
if (!XMLFile.isFile()) continue; |
|
38 |
if (!XMLFile.canRead()) continue; |
|
39 |
if (XMLFile.isHidden()) continue; |
|
40 |
if (!XMLFile.getName().toLowerCase().endsWith(".xml")) continue; |
|
41 |
|
|
42 |
String name = XMLFile.getName(); |
|
43 |
try { |
|
44 |
DOMResult result = _process(a, XMLFile, null); |
|
45 |
|
|
46 |
processDOMResultToFile(XMLFile, (Element)result.getNode()); |
|
47 |
File resultFile = new File(outputDirectory, XMLFile.getName()); |
|
48 |
} catch (Exception e) { |
|
49 |
System.out.println("Warning: XSL transformation of '$name' failed with error=$e with "); |
|
50 |
if (debug) e.printStackTrace(); |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
private DOMResult _process(ApplyXsl2 a, File inputXMLFile, HashMap<String, String> args) throws Exception { |
|
56 |
|
|
57 |
if (args != null) { |
|
58 |
for (String k : args.keySet()) { |
|
59 |
if (!a.setParam(k, args.get(k))) |
|
60 |
return null; |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
DOMResult result = new DOMResult(); |
|
65 |
|
|
66 |
a.transformer.transform(new StreamSource(inputXMLFile), result); |
|
67 |
a.resetParams(); |
|
68 |
return result; |
|
69 |
} |
|
70 |
} |
|
0 | 71 |
TXM/trunk/bundles/org.txm.core/src/java/org/txm/importer/GetXPath.java (revision 3921) | ||
---|---|---|
1 |
package org.txm.importer; |
|
2 |
|
|
3 |
|
|
4 |
import java.io.File; |
|
5 |
|
|
6 |
import javax.xml.namespace.QName; |
|
7 |
import javax.xml.transform.stream.StreamSource; |
|
8 |
import javax.xml.xpath.XPath; |
|
9 |
import javax.xml.xpath.XPathEvaluationResult; |
|
10 |
import javax.xml.xpath.XPathExpressionException; |
|
11 |
|
|
12 |
import net.sf.saxon.Configuration; |
|
13 |
import net.sf.saxon.xpath.XPathFactoryImpl; |
|
14 |
|
|
15 |
/** |
|
16 |
* XPath wrapper |
|
17 |
* |
|
18 |
* @author mdecorde |
|
19 |
* |
|
20 |
*/ |
|
21 |
public class GetXPath { |
|
22 |
|
|
23 |
public static XPath build() { |
|
24 |
|
|
25 |
XPathFactoryImpl xfactory = new XPathFactoryImpl(); |
|
26 |
Configuration config = new Configuration(); |
|
27 |
config.setLineNumbering(true); |
|
28 |
xfactory.setConfiguration(config); |
|
29 |
|
|
30 |
XPath xpath = xfactory.newXPath(); |
|
31 |
xpath.setNamespaceContext(new PersonalNamespaceContext()); |
|
32 |
return xpath; |
|
33 |
} |
|
34 |
|
|
35 |
|
|
36 |
public static XPathEvaluationResult<?> evaluateExpression(XPath xpath, File xmlFile, String xpathstring) throws XPathExpressionException { |
|
37 |
|
|
38 |
return xpath.evaluateExpression(xpathstring, new StreamSource(xmlFile)); |
|
39 |
} |
|
40 |
|
|
41 |
public static <T extends Object> T evaluateExpressionToObject(XPath xpath, File xmlFile, String xpathstring, Class<T> type) throws XPathExpressionException { |
|
42 |
|
|
43 |
return xpath.evaluateExpression(xpathstring, new StreamSource(xmlFile), type); |
|
44 |
} |
|
45 |
|
|
46 |
public static Object evaluateToObject(XPath xpath, File xmlFile, String xpathstring, QName qname) throws XPathExpressionException { |
|
47 |
|
|
48 |
return xpath.evaluate(xpathstring, new StreamSource(xmlFile), qname); |
|
49 |
} |
|
50 |
|
|
51 |
public static String evaluateToString(XPath xpath, File xmlFile, String xpathstring) throws XPathExpressionException { |
|
52 |
|
|
53 |
return xpath.evaluate(xpathstring, new StreamSource(xmlFile)); |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
public static XPathEvaluationResult<?> evaluateExpression(File xmlFile, String xpathstring) throws XPathExpressionException { |
|
58 |
|
|
59 |
XPath xpath = build(); |
|
60 |
|
|
61 |
return xpath.evaluateExpression(xpathstring, new StreamSource(xmlFile)); |
|
62 |
} |
|
63 |
|
|
64 |
public static <T extends Object> T evaluateExpressionToObject(File xmlFile, String xpathstring, Class<T> type) throws XPathExpressionException { |
|
65 |
|
|
66 |
XPath xpath = build(); |
|
67 |
|
|
68 |
return xpath.evaluateExpression(xpathstring, new StreamSource(xmlFile), type); |
|
69 |
} |
|
70 |
|
|
71 |
public static Object evaluateToObject(File xmlFile, String xpathstring, QName qname) throws XPathExpressionException { |
|
72 |
|
|
73 |
XPath xpath = build(); |
|
74 |
|
|
75 |
return xpath.evaluate(xpathstring, new StreamSource(xmlFile), qname); |
|
76 |
} |
|
77 |
|
|
78 |
public static String evaluateToString(File xmlFile, String xpathstring) throws XPathExpressionException { |
|
79 |
|
|
80 |
XPath xpath = build(); |
|
81 |
|
|
82 |
return xpath.evaluate(xpathstring, new StreamSource(xmlFile)); |
|
83 |
} |
|
84 |
} |
|
0 | 85 |
TXM/trunk/bundles/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/InstallRUserPart.java (revision 3921) | ||
---|---|---|
19 | 19 |
scriptsDirectory.mkdirs(); |
20 | 20 |
BundleUtils.copyFiles("org.txm.statsengine.r.core", "", "", "R/", scriptsDirectory, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
21 | 21 |
|
22 |
BundleUtils.getBundleFile("org.txm.statsengine.r.core"); |
|
23 |
|
|
22 | 24 |
return dir.exists() && scriptsDirectory.exists(); |
23 | 25 |
} |
24 | 26 |
|
TXM/trunk/bundles/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/preferences/RPreferences.java (revision 3921) | ||
---|---|---|
129 | 129 |
} |
130 | 130 |
File rpluginDir = new File(bundleDir, "res"); //$NON-NLS-1$ |
131 | 131 |
File rRootDir = new File(rpluginDir, os); |
132 |
File execFile = new File(rRootDir, "bin/" + extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$ |
|
133 |
File execFile2 = new File(rRootDir, "bin/exec/" + extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$ |
|
132 |
File rBinRootDir = new File(rRootDir, "bin"); |
|
133 |
File execFile = new File(rBinRootDir, extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$ |
|
134 |
File execFile2 = new File(rBinRootDir, "exec/" + extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$ |
|
134 | 135 |
if (osName.contains("windows")) { // R second exec file path is different //$NON-NLS-1$ |
135 |
execFile2 = new File(rRootDir, "bin/x64/" + extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$
|
|
136 |
execFile2 = new File(rBinRootDir, "x64/" + extraSubPath + "R" + ext); //$NON-NLS-1$ //$NON-NLS-2$
|
|
136 | 137 |
} |
137 | 138 |
if (!execFile.canExecute() && !execFile.setExecutable(true)) { // first test if file is executable then try setting it executable |
138 | 139 |
Log.warning(TXMCoreMessages.bind("Error while setting execution file rights to: {0}.", execFile.getAbsolutePath())); |
... | ... | |
142 | 143 |
Log.warning(TXMCoreMessages.bind("Error while setting execution file rights to: {0}.", execFile2.getAbsolutePath())); |
143 | 144 |
} |
144 | 145 |
|
145 |
if (!osName.contains("windows") && !execFile.canExecute()) { //$NON-NLS-1$
|
|
146 |
if (!osName.toLowerCase().contains("windows")) { //$NON-NLS-1$
|
|
146 | 147 |
try { |
147 |
Log.fine(TXMCoreMessages.bind("Setting execution file rights to: {0}.", rRootDir.getAbsolutePath())); |
|
148 |
Process p = Runtime.getRuntime().exec("chmod -R +x " + rRootDir.getAbsolutePath()); //$NON-NLS-1$ |
|
148 |
Log.fine(TXMCoreMessages.bind("Setting execution file rights to: {0}.", rBinRootDir.getAbsolutePath()));
|
|
149 |
Process p = Runtime.getRuntime().exec("chmod -R +x " + rBinRootDir.getAbsolutePath()); //$NON-NLS-1$
|
|
149 | 150 |
new StreamHog(p.getInputStream(), false); |
150 | 151 |
new StreamHog(p.getErrorStream(), false); |
151 | 152 |
p.waitFor(); |
152 | 153 |
} |
153 | 154 |
catch (Exception e) { |
154 |
Log.severe(TXMCoreMessages.bind("Error while setting execution file rights to: {0}.", rRootDir.getAbsolutePath())); |
|
155 |
Log.severe(TXMCoreMessages.bind("Error while setting execution file rights to: {0}.", rBinRootDir.getAbsolutePath()));
|
|
155 | 156 |
e.printStackTrace(); |
156 | 157 |
return; |
157 | 158 |
} |
TXM/trunk/bundles/org.txm.concordance.rcp/src/org/txm/concordance/rcp/messages/ConcordanceUIMessages.java (revision 3921) | ||
---|---|---|
13 | 13 |
public class ConcordanceUIMessages extends NLS { |
14 | 14 |
|
15 | 15 |
private static final String BUNDLE_NAME = "org.txm.concordance.rcp.messages.messages"; //$NON-NLS-1$ |
16 |
|
|
17 |
|
|
18 | 16 |
|
19 | 17 |
public static String action_text_reset_column_widths; |
20 | 18 |
|
... | ... | |
28 | 26 |
public static String ampLeftContextLengthInWords; |
29 | 27 |
public static String ampRightContextLengthInWords; |
30 | 28 |
|
31 |
|
|
32 | 29 |
public static String reference; |
33 | 30 |
public static String inf; |
34 | 31 |
public static String sup; |
TXM/trunk/bundles/org.txm.utils.core/META-INF/MANIFEST.MF (revision 3921) | ||
---|---|---|
86 | 86 |
org.eclipse.osgi.util;bundle-version="3.3.0";visibility:=reexport, |
87 | 87 |
org.eclipse.core.runtime;visibility:=reexport, |
88 | 88 |
org.eclipse.core.net;bundle-version="1.2.200";visibility:=reexport, |
89 |
org.txm.libs.saxon;bundle-version="9.0.0";visibility:=reexport,
|
|
89 |
org.txm.libs.saxon;bundle-version="12.0.0";visibility:=reexport,
|
|
90 | 90 |
org.txm.libs.colt;bundle-version="1.2.0";visibility:=reexport, |
91 | 91 |
org.txm.libs.itext;bundle-version="2.1.5";visibility:=reexport, |
92 | 92 |
org.txm.libs.msoffice;bundle-version="1.0.0";visibility:=reexport, |
Formats disponibles : Unified diff