Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / WriteIdAndNAttributesUltraGeneric.groovy @ 2473

History | View | Annotate | Download (3.6 kB)

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