1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.glxdesktop.hdlparser;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27
28 import net.sf.glxdesktop.parser.ConfigFileTools;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.oro.text.regex.MalformedPatternException;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.SAXException;
37
38 public class HardwareListParser {
39
40 private static final Log LOG = LogFactory.getLog(ConfigFileTools.class);
41
42 private String lspciString;
43
44 private List hardwareList;
45
46 private Document dom;
47
48 private String xmlFile;
49
50 public HardwareListParser(String xmlFile) throws HardwareListParserException {
51
52 hardwareList = new ArrayList();
53 this.xmlFile = xmlFile;
54
55 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
56 try {
57
58 DocumentBuilder db = dbf.newDocumentBuilder();
59
60 dom = db.parse(xmlFile);
61 } catch (ParserConfigurationException pce) {
62 LOG.error(pce.getMessage());
63 throw new HardwareListParserException("Cannot loading hardware list, caused by a serious parser configuration exception.", pce);
64 } catch (SAXException se) {
65 LOG.error(se.getMessage());
66 throw new HardwareListParserException("Cannot loading hardware list, caused by a Sax exception.", se);
67 } catch (IOException ioe) {
68 LOG.error(ioe.getMessage());
69 throw new HardwareListParserException("Cannot loading hardware list, caused by a I/O exception.", ioe);
70 }
71 updateHardwareList();
72 }
73
74 private void updateHardwareList() {
75 hardwareList = new ArrayList();
76
77 Element docEle = dom.getDocumentElement();
78
79 NodeList nl = docEle.getElementsByTagName("Vendor");
80 if (nl != null && nl.getLength() > 0) {
81 for (int i = 0; i < nl.getLength(); i++) {
82 Element el = (Element) nl.item(i);
83 Vendor vendor = getVendor(el);
84 hardwareList.add(vendor);
85 }
86 }
87 }
88
89
90
91
92
93
94 public String toString() {
95 String ret = "";
96 Iterator it = hardwareList.iterator();
97 while (it.hasNext()) {
98 ret += it.next().toString();
99 }
100 return ret;
101 }
102
103 /***
104 * I take an vendor element and read the values in, create an vendor object
105 * and return it
106 *
107 * @param element
108 * @return a vendor
109 */
110 private Vendor getVendor(Element element) {
111 String name = getTextValue(element, "Name");
112 List packageList = getCardList(element);
113 List cardList = getPackageList(element);
114 String xglOptions = getXglOptions(element);
115 return new Vendor(name, cardList, packageList, xglOptions);
116 }
117
118 /***
119 * I take an vendor element and read the values in, create an Card object
120 * and return it.
121 *
122 * @param element
123 * @return
124 */
125 private Card getCard(Element element) {
126 String name = getTextValue(element, "CardName");
127 String status = getTextValue(element, "CardStatus");
128 return new Card(name, status);
129 }
130
131 /***
132 * I take an card element and read the values, create a String object
133 * contain the xgl options and return it
134 *
135 * @param element
136 * @return
137 */
138 private String getXglOptions(Element element) {
139 String ret = getTextValue(element, "xgl_options");
140 return ret;
141 }
142
143 /***
144 * Get Vendor Compatibility list.
145 *
146 * @param element
147 * @return a vendor List
148 */
149 private List getCardList(Element element) {
150 List compatibles = new ArrayList();
151 NodeList nl = element.getElementsByTagName("Card");
152 if (nl != null && nl.getLength() > 0) {
153 for (int i = 0; i < nl.getLength(); i++) {
154 Element el = (Element) nl.item(i);
155 Card card = getCard(el);
156 compatibles.add(card);
157 }
158 }
159 return compatibles;
160 }
161
162 /***
163 * Get Vendor package list.
164 *
165 * @param element
166 * @return a package List
167 */
168 private List getPackageList(Element element) {
169 List packages = new ArrayList();
170 NodeList nl = element.getElementsByTagName("Package");
171 if (nl != null && nl.getLength() > 0) {
172 for (int i = 0; i < nl.getLength(); i++) {
173 Element el = (Element) nl.item(i);
174 packages.add(getTextValue(el, "PackageName"));
175 }
176 }
177 return packages;
178 }
179
180 /***
181 * Get package list by vendor.
182 *
183 * @param vendor
184 * @return a package List
185 */
186 public List getPackageListByVendor(String vendor) {
187
188 Element rootElement = dom.getDocumentElement();
189 NodeList nl = rootElement.getElementsByTagName("Vendor");
190 if (nl != null && nl.getLength() > 0) {
191 for (int i = 0; i < nl.getLength(); i++) {
192 Element el = (Element) nl.item(i);
193 if (getTextValue(el, "Name").compareTo(vendor) == 0) {
194 return getPackageList(el);
195 }
196 }
197 }
198 LOG.trace("getPackageListByVendor return new empty ArrayList()");
199 return new ArrayList();
200 }
201
202 /***
203 * Get package list for system (used) card.
204 *
205 * @return a package List
206 * @throws HardwareListParserException
207 */
208 public List getPackageListByVendor() throws HardwareListParserException {
209 return getPackageListByVendor(getSystemVendor());
210 }
211
212 /***
213 * Get card list by vendor.
214 *
215 * @param element
216 * @return a package List
217 */
218 public List getCardListByVendor(String vendor) {
219
220 Element rootElement = dom.getDocumentElement();
221 NodeList nl = rootElement.getElementsByTagName("Vendor");
222 if (nl != null && nl.getLength() > 0) {
223 for (int i = 0; i < nl.getLength(); i++) {
224 Element el = (Element) nl.item(i);
225 if (getTextValue(el, "Name").compareTo(vendor) == 0) {
226 return getCardList(el);
227 }
228 }
229 }
230 return null;
231 }
232
233 /***
234 * Get package list by vendor.
235 *
236 * @param Vendor
237 * Name
238 * @return a package List
239 */
240 public String getXglOptionsByVendor(String vendor) {
241
242 Element rootElement = dom.getDocumentElement();
243 NodeList nl = rootElement.getElementsByTagName("Vendor");
244 if (nl != null && nl.getLength() > 0) {
245 for (int i = 0; i < nl.getLength(); i++) {
246 Element el = (Element) nl.item(i);
247 if (getTextValue(el, "Name").compareTo(vendor) == 0) {
248 return getXglOptions(el);
249 }
250 }
251 }
252 return null;
253 }
254
255 /***
256 * Get package list for current vendor.
257 *
258 * @return a package List
259 * @throws HardwareListParserException
260 */
261 public String getXglOptionsByVendor() throws HardwareListParserException {
262 return getXglOptionsByVendor(getSystemVendor());
263 }
264
265 public String getCardStatus(String card) throws HardwareListParserException {
266 String cardType = getSystemVendorCardType();
267 List cardList = getCardListByVendor(card);
268 for (int i = 0; i < cardList.size(); i++) {
269 Card testCard = (Card) cardList.get(i);
270 if (testCard.getName().equals(cardType)) {
271 LOG.trace(xmlFile + " - Card Status for " + card + ": " + testCard.getStatus());
272 return testCard.getStatus();
273 }
274 }
275 LOG.trace("getCardStatus return new String()");
276 return new String();
277 }
278
279 public String getCardStatus() throws HardwareListParserException {
280 return getCardStatus(getSystemVendor());
281 }
282
283 public String getSystemVendor() throws HardwareListParserException {
284 String[] tmp = {};
285 String ret;
286 try {
287 tmp = ConfigFileTools.findValueByKey(lspciString,
288 "VGA compatible controller", ":// ").split(" ", 3);
289 } catch (MalformedPatternException e) {
290 throw new HardwareListParserException("", e);
291 }
292 ret = tmp[0] + " " + tmp[1];
293 return ret;
294 }
295
296 public String getSystemVendorCardType() throws HardwareListParserException {
297 String[] tmp = {};
298 String ret;
299 try {
300 tmp = ConfigFileTools.findValueByKey(lspciString,
301 "VGA compatible controller", ":// ").split(" ", 5);
302 } catch (MalformedPatternException e) {
303 throw new HardwareListParserException("", e);
304 }
305 ret = tmp[0] + " " + tmp[1];
306 return ret;
307 }
308
309 private String getTextValue(Element ele, String tagName) {
310 String textVal = null;
311 NodeList nl = ele.getElementsByTagName(tagName);
312 if (nl != null && nl.getLength() > 0) {
313 Element el = (Element) nl.item(0);
314 textVal = el.getFirstChild().getNodeValue();
315 }
316 return textVal;
317 }
318
319 }