Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / GetAttributeValue.groovy @ 1688

History | View | Annotate | Download (1.6 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import java.util.HashMap;
4 881 mdecorde
import java.util.ArrayList;
5 881 mdecorde
import java.io.File;
6 881 mdecorde
import javax.xml.stream.*;
7 881 mdecorde
import java.net.URL;
8 881 mdecorde
9 881 mdecorde
class GetAttributeValue {
10 881 mdecorde
        public static String process(File xmlFile, String path, String attribute) {
11 881 mdecorde
                String value = "N/A";
12 881 mdecorde
                String currentpath = "";
13 881 mdecorde
                boolean done = false;
14 881 mdecorde
15 1688 mdecorde
                def inputData = null;
16 1688 mdecorde
                def factory = null;
17 1688 mdecorde
                XMLStreamReader parser = null;
18 881 mdecorde
                try {
19 881 mdecorde
                        URL url = xmlFile.toURI().toURL();
20 1688 mdecorde
                        inputData = url.openStream();
21 1688 mdecorde
                        factory = XMLInputFactory.newInstance();
22 1688 mdecorde
                        parser = factory.createXMLStreamReader(inputData);
23 881 mdecorde
                        for (int event = parser.next(); !done && event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
24 881 mdecorde
                        {
25 881 mdecorde
                                switch (event) {
26 881 mdecorde
                                        case XMLStreamConstants.START_ELEMENT:
27 881 mdecorde
                                                currentpath += "/"+parser.getLocalName();// append the current tag to the current path
28 881 mdecorde
                                                if (path == currentpath) {
29 881 mdecorde
                                                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
30 881 mdecorde
                                                                if (parser.getAttributeName(i) == attribute) {
31 881 mdecorde
                                                                        value = parser.getAttributeValue(i)
32 881 mdecorde
                                                                        done = true;
33 881 mdecorde
                                                                        break;
34 881 mdecorde
                                                                }
35 881 mdecorde
                                                        }
36 881 mdecorde
                                                }
37 881 mdecorde
38 881 mdecorde
                                                break;
39 881 mdecorde
                                        case XMLStreamConstants.END_ELEMENT:
40 881 mdecorde
                                                currentpath = currentpath.substring(0,currentpath.length() -1 - parser.getLocalName().length()) // remove tag from the path
41 881 mdecorde
                                                break;
42 881 mdecorde
43 881 mdecorde
                                }
44 881 mdecorde
                        }
45 881 mdecorde
                }
46 1688 mdecorde
                catch(Exception e){println("File "+xmlFile+"\n"+e);
47 1688 mdecorde
                        if (parser != null) parser.close();
48 1688 mdecorde
                        if (inputData != null) inputData.close();
49 1688 mdecorde
                        return "N/A";}
50 1688 mdecorde
51 1688 mdecorde
                if (parser != null) parser.close();
52 1688 mdecorde
                if (inputData != null) inputData.close();
53 881 mdecorde
                return value;
54 881 mdecorde
        }
55 881 mdecorde
}