Révision 1739
tmp/org.txm.groovy.core/src/groovy/org/txm/macro/xml/ForceWordIDsMacro.groovy (revision 1739) | ||
---|---|---|
1 |
// Copyright © 2019 ENS de Lyon, CNRS, University of Franche-Comté |
|
2 |
// Licensed under the terms of the GNU General Public License version 3 or any later version (https://www.gnu.org/licenses/gpl.html) |
|
3 |
// @author mdecorde |
|
4 |
// @author sheiden |
|
5 |
|
|
1 | 6 |
// STANDARD DECLARATIONS |
2 | 7 |
package org.txm.macro.xml |
3 | 8 |
|
... | ... | |
3 | 8 |
import org.kohsuke.args4j.* |
4 | 9 |
import groovy.transform.Field |
5 |
import org.txm.rcp.swt.widget.parameters.* |
|
10 |
import org.txm.rcpapplication.swt.widget.parameters.*
|
|
6 | 11 |
import org.txm.importer.StaxIdentityParser |
7 | 12 |
import javax.xml.stream.XMLStreamException |
8 | 13 |
|
9 |
@Field @Option(name="inputFile", usage="input XML file", widget="FileOpen", required=true, def="input.xml")
|
|
14 |
@Field @Option(name="inputFile", usage="input file (XML)", widget="FileOpen", required=true, def="input.xml")
|
|
10 | 15 |
def inputFile |
11 | 16 |
|
12 |
@Field @Option(name="outputFile", usage="output XML file", widget="FileSave", required=true, def="ouput.xml")
|
|
17 |
@Field @Option(name="outputFile", usage="output file", widget="FileSave", required=true, def="output.xml")
|
|
13 | 18 |
def outputFile |
14 | 19 |
|
15 | 20 |
// Open the parameters input dialog box |
16 |
if (!ParametersDialog.open(this)) return;
|
|
21 |
if (!ParametersDialog.open(this)) return |
|
17 | 22 |
|
18 | 23 |
// END OF PARAMETERS |
19 | 24 |
|
20 |
if (!(inputFile.exists() && inputFile.canRead())) { println "Error: cannot found $inputFile"; return false;} |
|
25 |
if (!(inputFile.exists() && inputFile.canRead())) { |
|
26 |
println "** ForceWordIDs: cannot find $inputFile" |
|
27 |
return false |
|
28 |
} |
|
21 | 29 |
|
30 |
print "Processing "+inputFile+"..." |
|
31 |
|
|
22 | 32 |
outputFile.getParentFile().mkdirs() |
23 | 33 |
|
24 | 34 |
// build text id |
25 |
textid = inputFile.getName();
|
|
35 |
textid = inputFile.getName() |
|
26 | 36 |
if (textid.indexOf(".xml") > 0) { |
27 | 37 |
textid = textid.substring(0, textid.indexOf(".xml")) |
... | ... | |
30 | 40 |
|
31 | 41 |
def parser = new StaxIdentityParser(inputFile) { |
32 | 42 |
|
33 |
int wordnumber = 1;
|
|
43 |
int wordnumber = 1 |
|
34 | 44 |
|
45 |
/* ne fonctionne pas |
|
46 |
protected void writeStartElement() throws XMLStreamException { |
|
47 |
println localname |
|
48 |
if (localname ==~ "w|pc") { // a word |
|
49 |
localname = "w" |
|
50 |
} |
|
51 |
|
|
52 |
super.writeStartElement() |
|
53 |
} |
|
54 |
*/ |
|
55 |
|
|
35 | 56 |
protected void writeAttributes() throws XMLStreamException { |
36 |
if (localname == "w") { // a word |
|
37 |
boolean idwritten = false; |
|
38 |
for (int i = 0 ; i < parser.getAttributeCount() ; i++) { |
|
57 |
if (localname ==~ "w|pc") { // a word |
|
58 |
boolean idwritten = false |
|
59 |
attCount = parser.getAttributeCount() |
|
60 |
attCount.times { i -> |
|
39 | 61 |
if (parser.getAttributeLocalName(i) == "id") { // update & backup |
40 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), "w_"+textid+"_"+(wordnumber++)); // force id
|
|
41 |
writeAttribute(parser.getAttributePrefix(i), "foreign-id", parser.getAttributeValue(i)); // backup
|
|
42 |
idwritten = true;
|
|
43 |
} else { |
|
44 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), parser.getAttributeValue(i));
|
|
62 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), "w_"+textid+"_"+(wordnumber++)) // force id
|
|
63 |
writeAttribute(parser.getAttributePrefix(i), "foreign-id", parser.getAttributeValue(i)) // backup
|
|
64 |
idwritten = true |
|
65 |
} else if (parser.getAttributeLocalName(i) != "foreign-id") {
|
|
66 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), parser.getAttributeValue(i)) |
|
45 | 67 |
} |
46 | 68 |
} |
47 | 69 |
|
48 |
if (!idwritten) { // create id |
|
49 |
writeAttribute(null, "id", "w_"+textid+"_"+(wordnumber++));
|
|
70 |
if (!idwritten) { // create id
|
|
71 |
writeAttribute(null, "id", "w_"+textid+"_"+(wordnumber++)) |
|
50 | 72 |
} |
51 | 73 |
} else { |
52 | 74 |
super.writeAttributes() |
... | ... | |
54 | 76 |
} |
55 | 77 |
} |
56 | 78 |
|
57 |
return parser.process(outputFile) |
|
79 |
res = parser.process(outputFile) |
|
80 |
|
|
81 |
println " done." |
|
82 |
|
|
83 |
return res |
Formats disponibles : Unified diff