Statistics
| Revision:

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

History | View | Annotate | Download (2.7 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import java.io.File;
4 881 mdecorde
import java.util.ArrayList;
5 881 mdecorde
import java.util.List;
6 881 mdecorde
7 986 mdecorde
import org.txm.importer.StaxIdentityParser
8 881 mdecorde
import org.txm.metadatas.Entry
9 881 mdecorde
import org.txm.utils.Pair;
10 881 mdecorde
11 881 mdecorde
/**
12 881 mdecorde
 * Add a attribute value map in a XML file
13 881 mdecorde
 * Warning: if an attribute already exists its value won't be changed
14 881 mdecorde
 */
15 881 mdecorde
class AddAttributeValuesInXML extends StaxIdentityParser {
16 881 mdecorde
        File xmlFile;
17 881 mdecorde
        String tag, attribute;
18 881 mdecorde
        List<String> values;
19 881 mdecorde
        HashMap<String, String> attributesMap;
20 881 mdecorde
        boolean result;
21 881 mdecorde
22 881 mdecorde
        public AddAttributeValuesInXML(File xmlFile, String tag, String attribute, List<String> values)
23 881 mdecorde
        {
24 881 mdecorde
                super(xmlFile.toURI().toURL());
25 881 mdecorde
                this.xmlFile = xmlFile;
26 881 mdecorde
                this.tag = tag;
27 881 mdecorde
                this.attribute = attribute;
28 881 mdecorde
                this.values = values;
29 881 mdecorde
        }
30 881 mdecorde
31 881 mdecorde
        public boolean process(File outfile) {
32 881 mdecorde
                this.result = false;
33 881 mdecorde
                boolean ret = super.process(outfile)
34 881 mdecorde
                return this.result & ret;
35 881 mdecorde
        }
36 881 mdecorde
37 881 mdecorde
        /**
38 881 mdecorde
         * Rewrite the processStartElement() to update/add attributes
39 881 mdecorde
         */
40 881 mdecorde
        int n = 0;
41 881 mdecorde
        public void processStartElement()
42 881 mdecorde
        {
43 881 mdecorde
                if (localname != tag) {
44 881 mdecorde
                        super.processStartElement()
45 881 mdecorde
                } else {
46 881 mdecorde
                        String prefix = parser.getPrefix();
47 881 mdecorde
                        if (INCLUDE == localname && XI == prefix) {
48 881 mdecorde
                                processingXInclude();
49 881 mdecorde
                                return;
50 881 mdecorde
                        }
51 881 mdecorde
52 881 mdecorde
                        if (prefix.length() > 0)
53 881 mdecorde
                                writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
54 881 mdecorde
                        else
55 881 mdecorde
                                writer.writeStartElement(localname);
56 881 mdecorde
57 881 mdecorde
                        for (int i = 0 ; i < parser.getNamespaceCount() ; i++) {
58 881 mdecorde
                                writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
59 881 mdecorde
                        }
60 881 mdecorde
61 881 mdecorde
                        // get attributes
62 881 mdecorde
                        HashMap<String, String> attributes = new HashMap<String, String>();
63 881 mdecorde
                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
64 881 mdecorde
                                attributes[parser.getAttributeLocalName(i)] = parser.getAttributeValue(i);
65 881 mdecorde
                        }
66 881 mdecorde
                        // add/update the value
67 881 mdecorde
                        if (n < values.size()) {
68 881 mdecorde
                                attributes[attribute] = values[n];
69 881 mdecorde
                        } else {
70 881 mdecorde
                                println "ERROR: not enough values to insert for file $xmlFile, at XML parser location: l="+parser.getLocation().getLineNumber()+",c="+parser.getLocation().getColumnNumber()+")."
71 881 mdecorde
                        }
72 881 mdecorde
                        n++
73 881 mdecorde
74 881 mdecorde
                        // write attributes
75 881 mdecorde
                        for (def k : attributes.keySet()) {
76 881 mdecorde
                                writer.writeAttribute(k, attributes[k])
77 881 mdecorde
                        }
78 881 mdecorde
                }
79 881 mdecorde
        }
80 881 mdecorde
81 881 mdecorde
        @Override
82 881 mdecorde
        public void after() {
83 881 mdecorde
                super.after();
84 881 mdecorde
85 881 mdecorde
                if (n != values.size()) {
86 881 mdecorde
                        println "ERROR: number of $tag ("+n+") missmatch the number of values to insert: "+values.size()
87 881 mdecorde
                }
88 881 mdecorde
        }
89 881 mdecorde
90 881 mdecorde
        public static void main(String[] args)
91 881 mdecorde
        {
92 881 mdecorde
                File xmlfile = new File("/home/mdecorde/xml/macroaddfacsattribute/src/test1.xml")
93 881 mdecorde
                File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
94 881 mdecorde
95 881 mdecorde
                AddAttributeValuesInXML builder = new AddAttributeValuesInXML(xmlfile, "pb", "facs", ["V1","V1.2", "V2", "V3"]);
96 881 mdecorde
                builder.process(temp);
97 881 mdecorde
98 881 mdecorde
                println "Done"
99 881 mdecorde
        }
100 881 mdecorde
}