Révision 3484
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/xml/ InjectPropertiesIntoStructureMacro.groovy (revision 3484) | ||
---|---|---|
1 |
// Copyright © 2022 MY_INSTITUTION |
|
2 |
// Licensed under the terms of the GNU General Public License version 3 (http://www.gnu.org/licenses/gpl-3.0.html) |
|
3 |
// @author mdecorde |
|
4 |
|
|
5 |
// STANDARD DECLARATIONS |
|
6 |
package org.txm.macro |
|
7 |
|
|
8 |
import org.kohsuke.args4j.* |
|
9 |
import groovy.transform.Field |
|
10 |
import org.txm.rcp.swt.widget.parameters.* |
|
11 |
|
|
12 |
// BEGINNING OF PARAMETERS |
|
13 |
|
|
14 |
@Field @Option(name="tableFile", usage="Table File (CSV, ODS or XLSX)", widget="FileOpen", required=true, def="table.ods") |
|
15 |
def tableFile |
|
16 |
|
|
17 |
@Field @Option(name="xmlFilesDirectory", usage="directory of the XML files to patch", widget="Folder", required=false, def="xmlfilesdirectory") |
|
18 |
def xmlFilesDirectory |
|
19 |
|
|
20 |
@Field @Option(name="joinElement", usage="element to patch", widget="String", required=false, def="sp") |
|
21 |
def joinElement |
|
22 |
|
|
23 |
@Field @Option(name="joinAttribute", usage="attribute to test", widget="String", required=false, def="id") |
|
24 |
def joinAttribute |
|
25 |
|
|
26 |
@Field @Option(name="debug", usage="an example boolean", widget="Boolean", required=false, def="true") |
|
27 |
def debug |
|
28 |
|
|
29 |
// Open the parameters input dialog box |
|
30 |
if (!ParametersDialog.open(this)) return |
|
31 |
|
|
32 |
// END OF PARAMETERS |
|
33 |
|
|
34 |
def reader = new org.txm.utils.TableReader(tableFile); |
|
35 |
|
|
36 |
if (!reader.readHeaders()) { |
|
37 |
println "No header." |
|
38 |
return false; |
|
39 |
} |
|
40 |
def headers = reader.getHeaders() |
|
41 |
println headers |
|
42 |
if (!headers.contains("id")) { |
|
43 |
println "No 'id' column" |
|
44 |
return false |
|
45 |
} |
|
46 |
//if (!headers.contains("text-id")) { |
|
47 |
// println "No 'text-id' column" |
|
48 |
// return false |
|
49 |
//} |
|
50 |
|
|
51 |
def newProperties = [] |
|
52 |
newProperties.addAll(headers) |
|
53 |
newProperties.remove("id") |
|
54 |
|
|
55 |
def replacements = [:] |
|
56 |
while (reader.readRecord()) { |
|
57 |
|
|
58 |
def record = reader.getRecord() |
|
59 |
def joinValue = record["id"] |
|
60 |
|
|
61 |
|
|
62 |
if (joinValue.length() > 0) { |
|
63 |
replacements[joinValue] = reader.getRecord() |
|
64 |
} |
|
65 |
} |
|
66 |
reader.close() |
|
67 |
|
|
68 |
if (debug) println "replacements=$replacements" |
|
69 |
if (debug) println "newProperties=$newProperties" |
|
70 |
|
|
71 |
for (def xmlFile : xmlFilesDirectory.listFiles()) { |
|
72 |
|
|
73 |
if (!xmlFile.isFile()) continue; |
|
74 |
|
|
75 |
if (debug) println "text $xmlFile" |
|
76 |
|
|
77 |
def parser = new groovy.util.XmlParser() |
|
78 |
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) |
|
79 |
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
|
80 |
def doc = parser.parse(xmlFile) |
|
81 |
|
|
82 |
doc.'**'.find { element -> |
|
83 |
|
|
84 |
if (element instanceof groovy.util.Node && element.name().matches(joinElement)) { |
|
85 |
|
|
86 |
String joinValue = ""+element["@$joinAttribute"] |
|
87 |
if (joinValue != null) { |
|
88 |
if (debug) println "patch "+element.name() |
|
89 |
def replacement = replacements[joinValue] |
|
90 |
if (debug) println " $joinValue -> replacement $replacement" |
|
91 |
if (replacement != null) { |
|
92 |
|
|
93 |
for (def property : newProperties) { |
|
94 |
if (property.length() > 0) { |
|
95 |
element["@$property"] = replacement[property] |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
groovy.xml.XmlUtil.serialize(doc, xmlFile.newWriter("UTF-8")) |
|
104 |
} |
|
105 |
|
Formats disponibles : Unified diff