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