BowlerKernel
Hexml.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.bootloader;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10 
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
15 import org.xml.sax.SAXException;
16 
17 
18 // TODO: Auto-generated Javadoc
22 public class Hexml {
23 
25  ArrayList<Core> cores = new ArrayList<Core>();
26 
28  private String revision="";
29 
38  public Hexml(File hexml) throws ParserConfigurationException, SAXException, IOException{
43  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
44  DocumentBuilder dBuilder;
45  Document doc = null;
46 
47  dBuilder = dbFactory.newDocumentBuilder();
48  doc = dBuilder.parse(hexml);
49  doc.getDocumentElement().normalize();
50 
52  loadRevision(doc);
53  //NodeList nList = doc.getElementsByTagName("revision");
54  //revision = getTagValue("revision",(Element)nList.item(0));
56  NodeList nList = doc.getElementsByTagName("core");
57  for (int temp = 0; temp < nList.getLength(); temp++) {
58  Node nNode = nList.item(temp);
59  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
60  Element eElement = (Element) nNode;
61  int index = Integer.parseInt(getTagValue("index",eElement));
62  //int word = Integer.parseInt(getTagValue("wordSize",eElement));
63  NRBootCoreType type = NRBootCoreType.find(getTagValue("type",eElement));
64  if (type == null) {
65  System.err.println("Failed to get a core type for: "+getTagValue("type",eElement));
66  continue;
67  }
68  String hexFile = getTagValue("hex",eElement);
69  ArrayList<hexLine> lines=new ArrayList<hexLine>();
70  String[] tokens = hexFile.split("\n");
71  for (int i=0;i<tokens.length;i++){
72  try {
73  lines.add(new hexLine(tokens[i]));
74  } catch (Exception e) {
75  // TODO Auto-generated catch block
76  e.printStackTrace();
77  }
78  }
79  Core tmp = new Core(index, lines, type);
81  cores.add(tmp);
82  }
83  }
84 
85 
86  }
87 
93  private void loadRevision(Document doc) {
94  try{
95  NodeList nlList= doc.getElementsByTagName("revision").item(0).getChildNodes();
96  Node nValue = (Node) nlList.item(0);
97  revision = nValue.getNodeValue();
98  }catch(NullPointerException e){
99  revision = "0.0.0";
100  }
101  }
102 
110  private static String getTagValue(String sTag, Element eElement){
111  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
112  Node nValue = (Node) nlList.item(0);
113  return nValue.getNodeValue();
114  }
115 
121  public ArrayList<Core> getCores(){
122  return cores;
123  }
124 
130  public String getRevision() {
131  return revision;
132  }
133 }
static String getTagValue(String sTag, Element eElement)
Definition: Hexml.java:110
static NRBootCoreType find(String tagValue)