root / tmp / org.txm.core / src / java / org / txm / scripts / importer / WriteIdAndNAttributesUltraGeneric.groovy @ 2473
History | View | Annotate | Download (3.6 kB)
1 |
package org.txm.scripts.importer
|
---|---|
2 |
|
3 |
import org.txm.importer.StaxIdentityParser |
4 |
|
5 |
class WriteIdAndNAttributesUltraGeneric extends StaxIdentityParser { |
6 |
|
7 |
String baseID = "" |
8 |
def rules = []; |
9 |
|
10 |
def idCounters = [:]
|
11 |
def nCounters = [:]
|
12 |
def parentIds = [:]
|
13 |
|
14 |
def elements;
|
15 |
def elementShortNames
|
16 |
def elementParents
|
17 |
def elementResets
|
18 |
def elementSelection
|
19 |
public WriteIdAndNAttributesUltraGeneric(File xmlFile, def elements, def elementSelection, def elementShortNames, def elementParents, def elementResets) { |
20 |
super(xmlFile)
|
21 |
|
22 |
this.elements = elements;
|
23 |
this.elementSelection = elementSelection
|
24 |
this.elementShortNames = elementShortNames;
|
25 |
this.elementParents = elementParents
|
26 |
this.elementResets = elementResets
|
27 |
|
28 |
this.baseID = xmlFile.getName();
|
29 |
if (baseID.indexOf(".") > 0) baseID = baseID.substring(0, baseID.indexOf(".")) |
30 |
|
31 |
for (String element : elements) { |
32 |
idCounters[element] = 1;
|
33 |
nCounters[element] = 1;
|
34 |
parentIds[element] = null;
|
35 |
} |
36 |
} |
37 |
|
38 |
/**
|
39 |
* Special rule to add @corresp=#ref to its reference element to element which @attribute=@value
|
40 |
*/
|
41 |
public setReferenceRule(String element, String attribute, String value) { |
42 |
this.rules << [element, attribute, value]
|
43 |
} |
44 |
|
45 |
protected void writeAttributes() { |
46 |
if (!idCounters.containsKey(localname) ) {
|
47 |
super.writeAttributes();
|
48 |
return;
|
49 |
} |
50 |
println "write attributes $localname"
|
51 |
boolean idFound = false |
52 |
boolean nFound = false |
53 |
|
54 |
|
55 |
for (int i = 0 ; i < parser.getAttributeCount() ; i++) { |
56 |
String name = parser.getAttributeLocalName(i);
|
57 |
String value = parser.getAttributeValue(i);
|
58 |
|
59 |
println " at $name=$value"
|
60 |
|
61 |
//String element_sign = [localname, name, value]
|
62 |
|
63 |
if ("id" == name) { |
64 |
if (value == null) { |
65 |
value = getNextID() |
66 |
} |
67 |
|
68 |
parentIds[localname] = value |
69 |
} else if ("n" == name) { |
70 |
if (value == null) { |
71 |
value = getNextN() |
72 |
} else {
|
73 |
nCounters[localname] = Integer.parseInt(value)
|
74 |
} |
75 |
} |
76 |
|
77 |
String attrPrefix = parser.getAttributePrefix(i);
|
78 |
if (attrPrefix != null && attrPrefix.length() > 0) |
79 |
writer.writeAttribute(attrPrefix+":"+name, value);
|
80 |
else
|
81 |
writer.writeAttribute(name, value); |
82 |
} |
83 |
|
84 |
if (!idFound) {
|
85 |
String value = getNextID()
|
86 |
writer.writeAttribute("id", value);
|
87 |
parentIds[localname] = value |
88 |
} |
89 |
if (!nFound) {
|
90 |
String value = getNextN()
|
91 |
writer.writeAttribute("n", value);
|
92 |
} |
93 |
|
94 |
|
95 |
idCounters[localname]++ |
96 |
} |
97 |
|
98 |
protected void processStartElement() { |
99 |
|
100 |
if (idCounters.containsKey(localname)) {
|
101 |
println "Element: "+localname
|
102 |
for (String resetElement : elementResets[localname]) { |
103 |
println "reset N: "+resetElement
|
104 |
nCounters[resetElement] = 1
|
105 |
} |
106 |
} |
107 |
|
108 |
super.processStartElement();
|
109 |
} |
110 |
|
111 |
protected String getNextID() { |
112 |
String value = elementShortNames[localname]
|
113 |
|
114 |
String parent = elementParents[localname]
|
115 |
if (parent != null) |
116 |
value += "_"+parentIds[parent]
|
117 |
else
|
118 |
value += "_"+baseID
|
119 |
|
120 |
value += "-"+idCounters[localname]
|
121 |
|
122 |
return value
|
123 |
} |
124 |
|
125 |
protected String getNextN() { |
126 |
String value = Integer.toString(nCounters[localname]) |
127 |
nCounters[localname]++ |
128 |
return value
|
129 |
} |
130 |
|
131 |
public static void main(String[] args) { |
132 |
File xmlFile = new File("/home/mdecorde/TEMP/idnmissing.xml") |
133 |
File outFile = new File("/home/mdecorde/TEMP/idnmissing-fix.xml") |
134 |
|
135 |
WriteIdAndNAttributesUltraGeneric wiana = new WriteIdAndNAttributesUltraGeneric(xmlFile,
|
136 |
["milestone", "pb", "cb", "lb"], |
137 |
["milestone":"unit=surface", "pb":null, "cb":null, "lb":null], |
138 |
["milestone":"surf", "pb":"page", "cb":"col", "lb":"line"], |
139 |
["milestone":null, "pb":null, "cb":"pb", "lb":"cb"], |
140 |
["milestone":[], "pb":["cb", "lb"], "cb":[ "lb"], "lb":[]]) |
141 |
println wiana.process(outFile) |
142 |
} |
143 |
} |