source: main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/ExtXMLHelper.java@ 21919

Last change on this file since 21919 was 21919, checked in by sjm84, 14 years ago

New code for GAI extension manager

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