Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / WriteIdAndNAttributesUltraGeneric.groovy @ 187

History | View | Annotate | Download (3.6 kB)

1
package org.txm.importer
2

    
3
class WriteIdAndNAttributesUltraGeneric extends StaxIdentityParser {
4

    
5
        String baseID = ""
6
        def rules = [];
7

    
8
        def idCounters = [:]
9
        def nCounters = [:]
10
        def parentIds = [:]
11
        
12
        def elements;
13
        def elementShortNames
14
        def elementParents
15
        def elementResets
16
        def elementSelection
17
        public WriteIdAndNAttributesUltraGeneric(File xmlFile, def elements, def elementSelection, def elementShortNames, def elementParents, def elementResets) {
18
                super(xmlFile)
19
                
20
                this.elements = elements;
21
                this.elementSelection = elementSelection
22
                this.elementShortNames = elementShortNames;
23
                this.elementParents = elementParents
24
                this.elementResets = elementResets
25
                
26
                this.baseID = xmlFile.getName();
27
                if (baseID.indexOf(".") > 0) baseID = baseID.substring(0, baseID.indexOf("."))
28
                
29
                for (String element : elements) {
30
                        idCounters[element] = 1;
31
                        nCounters[element] = 1;
32
                        parentIds[element] = null;
33
                }
34
        }
35

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