Revision 1288
tmp/org.txm.groovy.core/src/groovy/org/txm/macro/xml/NumberElementMacro.groovy (revision 1288) | ||
---|---|---|
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 |
|
|
9 |
// BEGINNING OF PARAMETERS |
|
10 |
|
|
11 |
@Field @Option(name="inputDirectory", usage="an example folder", widget="Folder", required=true, def="in") |
|
12 |
def inputDirectory = new File(System.getProperty("user.home"),"xml/TESTS2/xml") |
|
13 |
|
|
14 |
@Field @Option(name="outputDirectory", usage="an example folder", widget="Folder", required=true, def="out") |
|
15 |
def outputDirectory = new File("/tmp/xsltest") |
|
16 |
|
|
17 |
@Field @Option(name="elementName", usage="element to number", widget="String", required=true, def="pb") |
|
18 |
def elementName |
|
19 |
|
|
20 |
@Field @Option(name="elementAttribute", usage="element attribute to set", widget="String", required=true, def="n") |
|
21 |
def elementAttribute |
|
22 |
|
|
23 |
@Field @Option(name="countStart", usage="count starting number", widget="Integer", required=true, def="1") |
|
24 |
def countStart |
|
25 |
|
|
26 |
@Field @Option(name="valuePrefix", usage="attribute value prefix", widget="String", required=true, def="") |
|
27 |
def valuePrefix |
|
28 |
|
|
29 |
@Field @Option(name="valueSuffix", usage="attribute value suffix", widget="String", required=true, def="") |
|
30 |
def valueSuffix |
|
31 |
|
|
32 |
def parameters = "" |
|
33 |
|
|
34 |
@Field @Option(name="debug", usage="Show debug messages, value = true|false", widget="String", required=true, def="false") |
|
35 |
def debug |
|
36 |
|
|
37 |
if (!ParametersDialog.open(this)) return |
|
38 |
|
|
39 |
// END OF PARAMETERS |
|
40 |
|
|
41 |
NumberElementStyleSheet = """<?xml version="1.0"?> |
|
42 |
<xsl:stylesheet |
|
43 |
xmlns:xd="http://www.pnp-software.com/XSLTdoc" |
|
44 |
xmlns:edate="http://exslt.org/dates-and-times" |
|
45 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
|
46 |
xmlns:tei="http://www.tei-c.org/ns/1.0" |
|
47 |
exclude-result-prefixes="tei edate xd" |
|
48 |
version="2.0"> |
|
49 |
|
|
50 |
<xd:doc type="stylesheet"> |
|
51 |
<xd:short> |
|
52 |
Patron de feuille de style de numérotation d'éléments. |
|
53 |
</xd:short> |
|
54 |
<xd:detail> |
|
55 |
This stylesheet is free software; you can redistribute it and/or |
|
56 |
modify it under the terms of the GNU Lesser General Public |
|
57 |
License as published by the Free Software Foundation; either |
|
58 |
version 3 of the License, or (at your option) any later version. |
|
59 |
|
|
60 |
This stylesheet is distributed in the hope that it will be useful, |
|
61 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
62 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
63 |
Lesser General Public License for more details. |
|
64 |
|
|
65 |
You should have received a copy of GNU Lesser Public License with |
|
66 |
this stylesheet. If not, see http://www.gnu.org/licenses/lgpl.html |
|
67 |
</xd:detail> |
|
68 |
<xd:author>Serge Heiden slh@ens-lyon.fr</xd:author> |
|
69 |
<xd:copyright>2018, ENS de Lyon/CNRS UMR 5317 IHRIM</xd:copyright> |
|
70 |
</xd:doc> |
|
71 |
|
|
72 |
|
|
73 |
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes"/> |
|
74 |
|
|
75 |
<xsl:template match="@*|node()"> |
|
76 |
<xsl:copy> |
|
77 |
<xsl:apply-templates select="@*|node()" /> |
|
78 |
</xsl:copy> |
|
79 |
</xsl:template> |
|
80 |
|
|
81 |
<!-- On supprime tous les <sp> dont l'attribut @who vaut 'enqueteur' --> |
|
82 |
|
|
83 |
<xsl:template match="//*:$elementName"> |
|
84 |
<xsl:copy> |
|
85 |
<xsl:apply-templates select="@*"/> |
|
86 |
<xsl:attribute name="$elementAttribute"> |
|
87 |
<xsl:value-of select="concat('$valuePrefix', (count(preceding::*:$elementName) + $countStart), '$valueSuffix')"/> |
|
88 |
<!-- |
|
89 |
<xsl:value-of select="string:format(\$arg)" xmlns:string="java:java.lang.String"/> |
|
90 |
not available in Saxon-HE, use 'Integrated extension functions' instead, see http://saxonica.com/documentation/index.html#!extensibility |
|
91 |
--> |
|
92 |
</xsl:attribute> |
|
93 |
<xsl:apply-templates select="node()" /> |
|
94 |
</xsl:copy> |
|
95 |
</xsl:template> |
|
96 |
|
|
97 |
</xsl:stylesheet> |
|
98 |
""" |
|
99 |
|
|
100 |
debug = "true" == debug |
|
101 |
|
|
102 |
def XSLFile = File.createTempFile("NumberElementStyleSheet",".xsl") |
|
103 |
XSLFile.deleteOnExit() |
|
104 |
|
|
105 |
XSLFile << NumberElementStyleSheet |
|
106 |
|
|
107 |
outputDirectory.mkdir() |
|
108 |
|
|
109 |
parameters = parameters.trim() |
|
110 |
if (parameters.length() == 0) { |
|
111 |
parameters = null |
|
112 |
} else { |
|
113 |
def split = parameters.split("\n") |
|
114 |
parameters = [] |
|
115 |
for (def str : split) { |
|
116 |
def split2 = str.split("=", 2) |
|
117 |
if (split2.size() == 2) { |
|
118 |
parameters << split2 |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
println "Processed directory: $inputDirectory" |
|
124 |
|
|
125 |
def files = [] |
|
126 |
ApplyXsl2 a = new ApplyXsl2(XSLFile.getAbsolutePath()); |
|
127 |
inputDirectory.eachFileMatch(~/.+\.(xml|XML)/) { XMLFile -> |
|
128 |
String name = XMLFile.getName() |
|
129 |
try { |
|
130 |
|
|
131 |
def outFile = new File(outputDirectory, name) |
|
132 |
if (parameters != null) { |
|
133 |
for(def param : parameters) { |
|
134 |
a.setParam(param[0], param[1]) |
|
135 |
} |
|
136 |
} |
|
137 |
a.process(XMLFile, outFile); |
|
138 |
a.resetParams() |
|
139 |
files << XMLFile |
|
140 |
} catch (Exception e) { |
|
141 |
println "Warning: XSL transformation of '$name' failed with error=$e with " |
|
142 |
if (debug) e.printStackTrace(); |
|
143 |
} |
|
144 |
} |
Also available in: Unified diff