Statistics
| Revision:

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

History | View | Annotate | Download (2.6 kB)

1
package org.txm.importer
2

    
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.List;
6

    
7
import org.txm.metadatas.Entry
8
import org.txm.utils.Pair;
9

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

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

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

    
36
        /**
37
         * Rewrite the processStartElement() to update/add attributes
38
         */
39
        int n = 0;
40
        public void processStartElement()
41
        {
42
                if (localname != tag) {
43
                        super.processStartElement()
44
                } else {
45
                        String prefix = parser.getPrefix();
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

    
89
        public static void main(String[] args)
90
        {
91
                File xmlfile = new File("/home/mdecorde/xml/macroaddfacsattribute/src/test1.xml")
92
                File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
93

    
94
                AddAttributeValuesInXML builder = new AddAttributeValuesInXML(xmlfile, "pb", "facs", ["V1","V1.2", "V2", "V3"]);
95
                builder.process(temp);
96
                
97
                println "Done"
98
        }
99
}