1 | package org.greenstone.admin.guiext;
|
---|
2 |
|
---|
3 | import org.w3c.dom.Element;
|
---|
4 | import org.w3c.dom.NodeList;
|
---|
5 | import org.w3c.dom.Node;
|
---|
6 |
|
---|
7 | public class ExtXMLHelper
|
---|
8 | {
|
---|
9 | static public final String EXTENSION = "extension";
|
---|
10 | static public final String GROUP = "group";
|
---|
11 | static public final String BASE_EXT = "base_ext";
|
---|
12 | static public final String FILE_STEM = "file_stem";
|
---|
13 | static public final String NAME = "name";
|
---|
14 | static public final String DOWNLOAD = "download";
|
---|
15 | static public final String MAIN_SOURCE = "mainSource";
|
---|
16 | static public final String AUX_SOURCE = "auxSource";
|
---|
17 | static public final String SOURCE = "source";
|
---|
18 | static public final String INSTALL = "install";
|
---|
19 | static public final String UNINSTALL = "uninstall";
|
---|
20 | static public final String ENABLE = "enable";
|
---|
21 | static public final String DISABLE = "disable";
|
---|
22 | static public final String DESTINATION = "destination";
|
---|
23 | static public final String DESCRIPTION = "description";
|
---|
24 | static public final String PROPERTY = "property";
|
---|
25 | static public final String SEQUENCE_LIST = "sequence_list";
|
---|
26 | static public final String STEP ="step";
|
---|
27 | static public final String COMMAND ="command";
|
---|
28 | static public final String CALLBACK ="callback";
|
---|
29 | static public final String OPTION_LIST = "optionList";
|
---|
30 | static public final String OPTION = "option";
|
---|
31 | static public final String CLASS = "class";
|
---|
32 | static public final String OS = "os";
|
---|
33 |
|
---|
34 | private ExtXMLHelper(){}
|
---|
35 |
|
---|
36 | public static String getValueFromSingleElement(Element parent, String valueName, boolean strict)
|
---|
37 | {
|
---|
38 | if(parent == null){
|
---|
39 | System.err.println("Parent element is null");
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 | else if(valueName == null){
|
---|
43 | System.err.println("Value name element is null");
|
---|
44 | return null;
|
---|
45 | }
|
---|
46 |
|
---|
47 | Element e = getSingleChildElement(parent, valueName, strict);
|
---|
48 | if(e == null){
|
---|
49 | System.err.println("Cannot get a value from element \"" + valueName +"\" as it does not exist in this parent");
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | String s = getValueFromSingleElement(e, strict);
|
---|
54 | if(s == null){
|
---|
55 | if(strict){
|
---|
56 | System.err.println("Cannot get a value from the element \"" + valueName + "\" as it is empty");
|
---|
57 | }
|
---|
58 |
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | return s;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static String getValueFromSingleElement(Element e, boolean strict)
|
---|
66 | {
|
---|
67 | if(e == null){
|
---|
68 | System.err.println("Element is null");
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 |
|
---|
72 | Node textChild = e.getFirstChild();
|
---|
73 | if(textChild == null){
|
---|
74 | if(strict){
|
---|
75 | System.err.println("This element has no value");
|
---|
76 | }
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | String value = textChild.getNodeValue();
|
---|
81 | if(value.equals("")){
|
---|
82 | if(strict){
|
---|
83 | System.err.println("The value for this element is empty");
|
---|
84 | }
|
---|
85 | return null;
|
---|
86 | }
|
---|
87 | return value;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public static Element getSingleChildElement(Element parent, String elementName, boolean strict)
|
---|
91 | {
|
---|
92 | if(parent == null){
|
---|
93 | System.err.println("Parent element is null");
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | else if(elementName == null){
|
---|
97 | System.err.println("Element name is null");
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 |
|
---|
101 | NodeList nodeList = parent.getElementsByTagName(elementName);
|
---|
102 | if(nodeList == null || nodeList.getLength() == 0){
|
---|
103 | if(strict){
|
---|
104 | System.err.println("The element " + elementName + " does not exist in this parent element");
|
---|
105 | }
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | else if(nodeList.getLength() > 1){
|
---|
109 | System.err.println("The element " + elementName + " has more than one element in this parent element");
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return (Element)nodeList.item(0);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static Element[] getMultipleChildElements(Element parent, String elementName, boolean strict)
|
---|
117 | {
|
---|
118 | if(parent == null){
|
---|
119 | System.err.println("Parent element is null");
|
---|
120 | return null;
|
---|
121 | }
|
---|
122 | else if(elementName == null){
|
---|
123 | System.err.println("Element name is null");
|
---|
124 | return null;
|
---|
125 | }
|
---|
126 |
|
---|
127 | NodeList nodeList = parent.getElementsByTagName(elementName);
|
---|
128 | if(nodeList == null || nodeList.getLength() == 0){
|
---|
129 | if(strict){
|
---|
130 | System.err.println("The element " + elementName + " does not exist in this parent element");
|
---|
131 | }
|
---|
132 | return null;
|
---|
133 | }
|
---|
134 |
|
---|
135 | Element[] elements = new Element[nodeList.getLength()];
|
---|
136 |
|
---|
137 | for(int i = 0; i < nodeList.getLength(); i++){
|
---|
138 | elements[i] = (Element)nodeList.item(i);
|
---|
139 | }
|
---|
140 |
|
---|
141 | return elements;
|
---|
142 | }
|
---|
143 | } |
---|