BowlerKernel
ConfigManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.common;
2 
3 import java.io.File;
4 
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7 
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12 
13 import com.neuronrobotics.sdk.network.BowlerTCPClient;
14 import com.neuronrobotics.sdk.network.UDPBowlerConnection;
15 
16 
17 // TODO: Auto-generated Javadoc
21 public class ConfigManager {
22 
29  public static BowlerAbstractConnection loadDefaultConnection(String filename) {
30  try {
31 
32  File file = new File(filename);
33  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
34  DocumentBuilder db = dbf.newDocumentBuilder();
35  Document doc = db.parse(file);
36  doc.getDocumentElement().normalize();
37  NodeList nodeLst = doc.getElementsByTagName("connection");
38 
39  for (int s = 0; s < nodeLst.getLength(); s++) {
40 
41  Node fstNode = nodeLst.item(s);
42 
43  if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
44 
45  Element e = (Element) fstNode;
46 
47  String type = getElementValue(e, "type", "");
48  String port = getElementValue(e, "port", "");
49  String baud = getElementValue(e, "baud", "0");
50  String host = getElementValue(e, "host", "127.0.0.1");
51 
52  if(type.equalsIgnoreCase("serial")) {
53  //return new SerialConnection(port, Integer.parseInt(baud));
54  }
55 
56  if(type.equalsIgnoreCase("udp")) {
57  return new UDPBowlerConnection(Integer.parseInt(port));
58  }
59 
60  if(type.equalsIgnoreCase("tcp")) {
61  return new BowlerTCPClient(host, Integer.parseInt(port));
62  }
63  }
64 
65  }
66  } catch (Exception e) {
67  e.printStackTrace();
68  }
69 
70  return null;
71  }
72 
81  private static String getElementValue(Element e, String key, String dval) {
82  NodeList elmntLst = e.getElementsByTagName(key);
83  if(elmntLst.getLength() > 0) {
84  Element elmnt = (Element) elmntLst.item(0);
85  NodeList value = elmnt.getChildNodes();
86  ((Node) value.item(0)).getNodeValue();
87  return ((Node) value.item(0)).getNodeValue();
88  }
89  return dval;
90  }
91 }
static String getElementValue(Element e, String key, String dval)
static BowlerAbstractConnection loadDefaultConnection(String filename)