Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macro / edition / AddAttributeValuesInXML.groovy @ 187

History | View | Annotate | Download (2.3 kB)

1
package org.txm.macro.edition
2

    
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.List;
6
import org.txm.importer.*
7

    
8
/**
9
 * Add a attribute value map in a XML file
10
 * Warning: if an attribute already exists its value won't be changed
11
 */
12
class AddAttributeValuesInXML extends StaxIdentityParser {
13
        File xmlFile;
14
        String tag, attribute;
15
        List<String> values;
16
        HashMap<String, String> attributesMap;
17
        boolean result;
18

    
19
        public AddAttributeValuesInXML(File xmlFile, String tag, String attribute, List<String> values)
20
        {
21
                super(xmlFile.toURI().toURL());
22
                this.xmlFile = xmlFile;
23
                this.tag = tag;
24
                this.attribute = attribute;
25
                this.values = values;
26
        }
27

    
28
        public boolean process(File outfile) {
29
                this.result = false;
30
                boolean ret = super.process(outfile)
31
                return this.result & ret;
32
        }
33

    
34
        
35
        /**
36
         * Rewrite the processStartElement() to update/add attributes
37
         */
38
        int n = 0;
39
        public void processStartElement()
40
        {
41
                if (localname != tag) {
42
                        super.processStartElement()
43
                } else {
44
                        String prefix = parser.getPrefix();
45
//TODO: uncomment for TXM 0.7.6
46
//                        if (INCLUDE == localname && XI == prefix) {
47
//                                processingXInclude();
48
//                                return;
49
//                        }
50

    
51
                        if (prefix.length() > 0)
52
                                writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
53
                        else
54
                                writer.writeStartElement(localname);
55

    
56
                        for (int i = 0 ; i < parser.getNamespaceCount() ; i++) {
57
                                writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
58
                        }
59

    
60
                        // get attributes
61
                        HashMap<String, String> attributes = new HashMap<String, String>();
62
                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
63
                                attributes[parser.getAttributeLocalName(i)] = parser.getAttributeValue(i);
64
                        }
65
                        // add/update the value
66
                        if (n < values.size()) {
67
                                attributes[attribute] = values[n]; 
68
                        } else {
69
                                println "ERROR: not enough values to insert for file $xmlFile, at XML parser location: l="+parser.getLocation().getLineNumber()+",c="+parser.getLocation().getColumnNumber()+")."
70
                        }
71
                        n++
72
                        
73
                        // write attributes
74
                        for (def k : attributes.keySet()) {
75
                                writer.writeAttribute(k, attributes[k])
76
                        }
77
                }
78
        }
79
        
80
        @Override
81
        public void after() {
82
                super.after();
83
                
84
                if (n != values.size()) {
85
                        println "ERROR: number of $tag ("+n+") missmatch the number of values to insert: "+values.size()
86
                }
87
        }
88
}