Statistics
| Revision:

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

History | View | Annotate | Download (3.5 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 AddAttributeInXml extends StaxIdentityParser {
15
        File xmlfile;
16
        String tag;
17
        List<Pair<String, String>> attributes;
18
        HashMap<String, String> attributesMap;
19
        boolean result;
20

    
21
        public AddAttributeInXml(File xmlfile, String tag, List<Pair<String, String>> attributes)
22
        {
23
                super(xmlfile.toURI().toURL());
24
                this.xmlfile = xmlfile;
25
                this.tag = tag;
26
                this.attributes = attributes;
27
        }
28

    
29
        public AddAttributeInXml(File xmlfile, String tag, HashMap<String, String> attributesMap) {
30
                super(xmlfile.toURI().toURL());
31
                this.xmlfile = xmlfile;
32
                this.tag = tag;
33
                this.attributesMap = attributesMap;
34
        }
35

    
36
        public AddAttributeInXml(File xmlfile, String tag,        ArrayList<org.txm.metadatas.Entry> attributes) {
37
                super(xmlfile.toURI().toURL());
38
                this.xmlfile = xmlfile;
39
                this.tag = tag;
40
                this.attributes = attributes;
41
        }
42

    
43
        public boolean process(File outfile) {
44
                this.result = false;
45
                boolean ret = super.process(outfile)
46
                return this.result & ret;
47
        }
48

    
49
        
50
        /**
51
         * Rewrite the processStartElement() to update/add attributes
52
         */
53
        public void processStartElement()
54
        {
55
                String prefix = parser.getPrefix();
56
//TODO: uncomment for TXM 0.7.6 and later
57
//                        if (INCLUDE == localname && XI == prefix) {
58
//                                processingXInclude();
59
//                                return;
60
//                        }
61
                
62
                if (prefix.length() > 0)
63
                        writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
64
                else
65
                        writer.writeStartElement(localname);
66

    
67
                for (int i = 0 ; i < parser.getNamespaceCount() ; i++) {
68
                        writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
69
                }
70

    
71
                HashSet<String> exists = new HashSet<String>();
72
                for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
73
                        exists.add(parser.getAttributeLocalName(i));
74
                        writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
75
                }
76
                if (localname == tag && !stopInsertion) { // insert here
77
                        result = true
78
                        if (attributes != null) {
79
                                for (Pair<String, String> meta : attributes) {
80
                                        if(stopAtFirst)
81
                                                stopInsertion = true;
82
                                        if(!exists.contains(meta.getFirst())) // I can add the new attribute
83
                                                writer.writeAttribute(meta.getFirst(), meta.getSecond());
84
                                }
85
                        } else if (attributesMap != null) {
86
                                for (String name : attributesMap.keySet()) {
87
                                        if (stopAtFirst)
88
                                                stopInsertion = true;
89
                                        if (!exists.contains(name)) // I can add the new attribute
90
                                                writer.writeAttribute(name, attributesMap.get(name));
91
                                }
92
                        }
93
                }
94
        }
95
        
96
        boolean stopAtFirst = false;
97
        boolean stopInsertion = false;
98
        public void onlyOneElement() {
99
                stopAtFirst = true;
100
        }
101

    
102
        public static void main(String[] args)
103
        {
104
                File xmlfile = new File("/home/mdecorde/xml/xmlçàé/essai.xml")
105
                File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
106
                String tag = "titre";
107
                ArrayList<Pair<String, String>> metadatas = new ArrayList<Pair<String, String>>();
108
                metadatas.add(new Pair<String, String>("name1", "newvalue1"));
109
                metadatas.add(new Pair<String, String>("name2", "value2"));
110
                metadatas.add(new Pair<String, String>("name3", "value3"));
111

    
112
                AddAttributeInXml builder = new AddAttributeInXml(xmlfile, tag, metadatas);
113
                builder.onlyOneElement();
114
                if(builder.process(temp)) {
115
                        if (!(xmlfile.delete() && temp.renameTo(xmlfile))) println "Warning can't rename file "+temp+" to "+xmlfile
116
                } else {
117
                        println "Error during process"
118
                }
119
        }
120
}