summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/com/hackingroomba/roombacomm/XML.java
blob: e64ce0fa4e57ccb28fa0fb3740c9e9b2f3972480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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 <String, String>table = new Hashtable<String, String>();
  Vector <String>list = new Vector<String>();

  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.
    &amp; -> &
    &quote; -> "
    &lt; -> <
    &gt; -> >
  */
  private String unescape(String txt)
  {
    replace(txt, "&amp;", "&");
    replace(txt, "&quote;", "\"");
    replace(txt, "&lt;", "<");
    replace(txt, "&gt;", ">");
    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 <String, String>h, Vector <String>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<len;)
    {
      // read in key
      if (txt[i]=='<')
      {
        i++;
        if (txt[i]=='/')
        {
          isEndTag = true;
          i++;
        }
        else
          isEndTag = false;

        keyTop++;
        keys[keyTop].setLength(0);
        while ((i<len)&&(txt[i]!='>'))
        {
          keys[keyTop].append((char)txt[i]);
          i++;
        }
        if (txt[i++]!='>')
        {
          System.out.println("Missing close > 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 ((i<len)&&(txt[i]!='<'))
        {
          value.append((char)txt[i]);
          i++;
        }

        StringBuffer key = new StringBuffer();
        for (j=0;j<=keyTop;j++)
        {
          if (j>0) 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("<response><width>100</width><height>200</height></response>");
    System.out.println(xml.getInt("response.width"));
    System.out.println(xml.getInt("response.height"));
  }
*/
}