Révision 3302
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/xml/ApplyXSLToClipboardMacro.groovy (revision 3302) | ||
---|---|---|
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 org.txm.rcp.utils.IOClipboard |
|
9 |
|
|
10 |
// BEGINNING OF PARAMETERS |
|
11 |
|
|
12 |
// teitxm2xmlw = System.getProperty("user.home")+"/TXM/xsl/txm-front-teitxm2xmlw.xsl" |
|
13 |
|
|
14 |
@Field @Option(name="XSL_file", usage="an example file", widget="File", required=true, def="") |
|
15 |
def XSL_file |
|
16 |
|
|
17 |
@Field @Option(name="wrap_with_TEI_tag", usage="wrap XML fragment with TEI tag and namespace declarations", widget="Boolean", required=true, def="true") |
|
18 |
def wrap_with_TEI_tag |
|
19 |
|
|
20 |
@Field @Option(name="copy_result_to_clipboard", usage="replace the clipboard content by the result of the XSLT processing", widget="Boolean", required=true, def="true") |
|
21 |
def copy_result_to_clipboard |
|
22 |
|
|
23 |
@Field @Option(name="debug", usage="Show debug messages", widget="Boolean", required=true, def="false") |
|
24 |
def debug |
|
25 |
|
|
26 |
if (!ParametersDialog.open(this)) return |
|
27 |
|
|
28 |
// END OF PARAMETERS |
|
29 |
|
|
30 |
def inputFile = File.createTempFile("ApplyXSLToClipboard-input",".xml") |
|
31 |
inputFile.deleteOnExit() |
|
32 |
|
|
33 |
if (wrap_with_TEI_tag) { |
|
34 |
inputFile << """<?xml version="1.0" encoding="UTF-8"?> |
|
35 |
<TEI xmlns="http://www.tei-c.org/ns/1.0" xmlns:txm="http://textometrie.org/1.0"> |
|
36 |
""" |
|
37 |
} |
|
38 |
|
|
39 |
monitor.syncExec(new Runnable() { |
|
40 |
public void run() { |
|
41 |
inputFile << IOClipboard.read() |
|
42 |
} |
|
43 |
}) |
|
44 |
|
|
45 |
if (wrap_with_TEI_tag) { |
|
46 |
inputFile << """</TEI> |
|
47 |
""" |
|
48 |
} |
|
49 |
|
|
50 |
def outputFile = File.createTempFile("ApplyXSLToClipboard-output",".xml") |
|
51 |
outputFile.deleteOnExit() |
|
52 |
|
|
53 |
xslfactory = new ApplyXsl2(XSL_file.getAbsolutePath()) |
|
54 |
|
|
55 |
try { |
|
56 |
xslfactory.process(inputFile, outputFile) |
|
57 |
} catch (e) { |
|
58 |
|
|
59 |
inputFile.delete() |
|
60 |
outputFile.delete() |
|
61 |
return 0 |
|
62 |
} |
|
63 |
|
|
64 |
result = outputFile.getText() |
|
65 |
print result |
|
66 |
|
|
67 |
if (copy_result_to_clipboard) { |
|
68 |
monitor.syncExec(new Runnable() { |
|
69 |
public void run() { |
|
70 |
IOClipboard.write(result) |
|
71 |
} |
|
72 |
}) |
|
73 |
} |
|
74 |
|
|
75 |
inputFile.delete() |
|
76 |
outputFile.delete() |
|
77 |
|
|
78 |
return 1 |
|
79 |
|
Formats disponibles : Unified diff