From 53b947df484159f5934898ba6c0aa69a9c869007 Mon Sep 17 00:00:00 2001 From: Ido Hadanny Date: Thu, 22 Sep 2011 21:30:13 +0300 Subject: added stuff --- .../src/com/hackingroomba/roombacomm/XML.java | 173 +++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 roombacomm-client/src/com/hackingroomba/roombacomm/XML.java (limited to 'roombacomm-client/src/com/hackingroomba/roombacomm/XML.java') diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/XML.java b/roombacomm-client/src/com/hackingroomba/roombacomm/XML.java new file mode 100644 index 0000000..e64ce0f --- /dev/null +++ b/roombacomm-client/src/com/hackingroomba/roombacomm/XML.java @@ -0,0 +1,173 @@ +package com.hackingroomba.roombacomm; + +import java.util.Hashtable; +import java.util.Vector; + +/* The following XML class is a simple XML class meant to process the primitive XML + * that comes from RoboRealm. This class can be removed and replaced with a more + * extensive XML processing class as needed but is guaranteed to work with the RR + * XML. Do NOT use this class for generic XML processing as it is included for + * completeness and is intentionally kept simplistic to ease understanding + * */ + +class XML +{ + Hashtable table = new Hashtable(); + Vector list = new Vector(); + + private String replace(String txt, String src, String dest) + { + if (txt==null) return new String(""); + int i,j; + int len=src.length(); + StringBuffer sb=new StringBuffer(txt.length()); + + j=0; + while ((i=txt.indexOf(src,j))>=0) + { + sb.append(txt.substring(j,i)); + sb.append(dest); + i+=len; + j=i; + } + sb.append(txt.substring(j)); + + return sb.toString(); + } + + /* + Unescapes strings that have been included in an XML message. This can be + accomplished by a sequence of replace statements. + & -> & + "e; -> " + < -> < + > -> > + */ + private String unescape(String txt) + { + replace(txt, "&", "&"); + replace(txt, ""e;", "\""); + replace(txt, "<", "<"); + replace(txt, ">", ">"); + return txt; + } + + public boolean parse(String s) + { + table.clear(); + return parse(s, table, null); + } + + public Vector parseVector(String s) + { + list.removeAllElements(); + if (parse(s, null, list)) + return list; + else + return null; + } + + public boolean parse(String s, Hashtable h, Vector v) + { + boolean isEndTag; + byte txt[] = s.getBytes(); + int i, j; + int len = s.length(); + StringBuffer keys[] = new StringBuffer[10]; + StringBuffer value = new StringBuffer(); + for (i=0;i<10;i++) + keys[i] = new StringBuffer(); + int keyTop=-1; + + for (i=0;i tag"); + return false; + } + + if (isEndTag) + { + if (!keys[keyTop].toString().equals(keys[keyTop-1].toString())) + { + System.out.println("Mismatched XML tags "+keys[keyTop]+" -> "+keys[keyTop-1]); + return false; + } + keyTop-=2; + } + } + else + { + // read in value + value.setLength(0); + + while ((i0) key.append('.'); + key.append(keys[j]); + } + + String escapedValue = unescape(value.toString()); + if (h!=null) h.put(key.toString(), escapedValue); + if (v!=null) v.addElement(escapedValue); + } + } + + return true; + } + + public int getInt(String txt) + { + String s = (String)table.get(txt); + if (s!=null) + { + return Integer.parseInt(s); + } + return 0; + } + + public String getFirst() + { + if (table.isEmpty()) + return null; + else + return (String)table.elements().nextElement(); + } +/* + // This is where the program first starts + public static void main(String[] args) + { + XML xml = new XML(); + xml.parse("100200"); + System.out.println(xml.getInt("response.width")); + System.out.println(xml.getInt("response.height")); + } +*/ +} -- cgit v1.3.1