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