commit 5764dceb2a39ddf63425808c3dc46030be4587f6
parent 1c867082966d8df814c710a8618db9070809ba67
Author: Brian Swetland <swetland@frotz.net>
Date: Sun, 24 Jul 2011 13:31:36 -0700
pull XML.Sequence out into XMLSequence to make things tidier
Signed-off-by: Brian Swetland <swetland@frotz.net>
Diffstat:
3 files changed, 160 insertions(+), 139 deletions(-)
diff --git a/net/frotz/sonos/Sonos.java b/net/frotz/sonos/Sonos.java
@@ -111,11 +111,11 @@ public class Sonos {
"<SortCriteria></SortCriteria>"
);
try {
- XML.Sequence name = new XML.Sequence();
- XML.Sequence value = new XML.Sequence();
+ XMLSequence name = new XMLSequence();
+ XMLSequence value = new XMLSequence();
xml.open("u:BrowseResponse");
- XML.Sequence tmp = xml.read("Result");
- xml.unescape(tmp);
+ XMLSequence tmp = xml.read("Result");
+ tmp.unescape();
//System.out.println(tmp);
result.init(tmp);
@@ -144,11 +144,11 @@ public class Sonos {
}
while (result.tryRead(name,value)) {
if ("dc:title".contentEquals(name)) {
- title = xml.unescape(value).copy();
+ title = value.unescape().copy();
continue;
}
if ("upnp:album".contentEquals(name)) {
- album = xml.unescape(value).copy();
+ album = value.unescape().copy();
continue;
}
if ("res".contentEquals(name)) {
diff --git a/net/frotz/sonos/XML.java b/net/frotz/sonos/XML.java
@@ -31,9 +31,9 @@ import java.io.PrintStream;
// TODO: ' -> '
public class XML {
- XML.Sequence seq; /* entire buffer */
- XML.Sequence tag; /* most recent tag */
- XML.Sequence tmp; /* for content return */
+ XMLSequence seq; /* entire buffer */
+ XMLSequence tag; /* most recent tag */
+ XMLSequence tmp; /* for content return */
char[] xml;
int offset;
int count;
@@ -47,9 +47,9 @@ public class XML {
CharBuffer buf;
public XML(int size) {
- seq = new XML.Sequence();
- tag = new XML.Sequence();
- tmp = new XML.Sequence();
+ seq = new XMLSequence();
+ tag = new XMLSequence();
+ tmp = new XMLSequence();
mTag = pTag.matcher(seq);
mEntity = pEntity.matcher(tmp);
mAttr = pAttr.matcher(tmp);
@@ -64,7 +64,7 @@ public class XML {
buf.flip();
reset();
}
- public void init(XML.Sequence s) {
+ public void init(XMLSequence s) {
buf.clear();
buf.put(s.data, s.offset, s.count);
buf.flip();
@@ -82,53 +82,8 @@ public class XML {
offset = 0;
nextTag();
}
-
- /* modifies the sequence in-place, escaping basic entities */
- public XML.Sequence unescape(XML.Sequence s) {
- s.count = unescape(s, s.data, s.offset) - s.offset;
- return s;
- }
-
- /* copies the sequence into a char[] + offset, escaping basic entities */
- public int unescape(XML.Sequence s, char[] out, int off) {
- char[] in = s.data;
- int n = s.offset;
- int max = n + s.count;
-
- while (n < max) {
- char c = in[n++];
- if (c != '&') {
- out[off++] = c;
- continue;
- }
- int e = n;
- while (n < max) {
- if (in[n++] != ';')
- continue;
- switch(in[e]) {
- case 'l': // lt
- out[off++] = '<';
- break;
- case 'g': // gt
- out[off++] = '>';
- break;
- case 'q': // quot
- out[off++] = '"';
- break;
- case 'a': // amp | apos
- if (in[e+1] == 'm')
- out[off++] = '&';
- else if (in[e+1] == 'p')
- out[off++] = '\'';
- break;
- }
- break;
- }
- }
- return off;
- }
-
- public XML.Sequence getAttr(String name) {
+
+ public XMLSequence getAttr(String name) {
int off = mTag.start(3);
int end = off + mTag.end(3);
@@ -154,7 +109,7 @@ public class XML {
/* set sequence to the text between the end of the current tag
* and the beginning of the next tag.
*/
- public XML.Sequence getText() {
+ public XMLSequence getText() {
char[] data = xml;
int n;
tmp.data = data;
@@ -177,7 +132,7 @@ public class XML {
print(out, max, 0, buf);
}
void print(PrintStream out, int max, int indent, char[] buf) {
- XML.Sequence s;
+ XMLSequence s;
int n;
if (!isOpen) {
out.println("ERROR");
@@ -199,10 +154,10 @@ public class XML {
} else {
if (s.count > max) {
s.count = max;
- n = unescape(s, buf, 0);
+ n = s.unescape(buf, 0);
out.println("" + new String(buf, 0, n) + "..." + str());
} else {
- n = unescape(s, buf, 0);
+ n = s.unescape(buf, 0);
out.println("" + new String(buf, 0, n) + str());
}
}
@@ -228,7 +183,7 @@ public class XML {
}
/* require <tag> text </tag> and return text */
- public XML.Sequence read(String name) throws XML.Oops {
+ public XMLSequence read(String name) throws XML.Oops {
int start = mTag.end();
open(name);
tmp.adjust(start, mTag.start());
@@ -237,7 +192,7 @@ public class XML {
}
/* read the next <name> value </name> returns false if no open tag */
- public boolean tryRead(XML.Sequence name, XML.Sequence value) throws XML.Oops {
+ public boolean tryRead(XMLSequence name, XMLSequence value) throws XML.Oops {
if (!isOpen)
return false;
@@ -255,7 +210,7 @@ public class XML {
return true;
}
- public void close(XML.Sequence name) throws XML.Oops {
+ public void close(XMLSequence name) throws XML.Oops {
if (isOpen)
throw new XML.Oops("1expected </"+name+">, found <"+tag+">");
if (!name.eq(tag))
@@ -263,7 +218,7 @@ public class XML {
nextTag();
}
- public boolean tryRead(String name, XML.Sequence value) throws XML.Oops {
+ public boolean tryRead(String name, XMLSequence value) throws XML.Oops {
if (!isOpen || !name.contentEquals(tag))
return false;
value.data = xml;
@@ -319,75 +274,4 @@ public class XML {
super(msg);
}
}
-
- static class Sequence implements CharSequence {
- private char[] data;
- private int offset;
- private int count;
-
- public Sequence() {
- }
-
- void init(char[] data, int start, int end) {
- this.data = data;
- offset = start;
- count = end - start;
- }
- void adjust(int start, int end) {
- offset = start;
- count = end - start;
- }
- boolean eq(Sequence other) {
- int count = this.count;
- if (count != other.count)
- return false;
- char[] a = this.data;
- int ao = this.offset;
- char[] b = other.data;
- int bo = other.offset;
- while (count-- > 0)
- if (a[ao++] != b[bo++])
- return false;
- return true;
- }
- CharSequence copy() {
- Sequence s = new Sequence();
- s.init(data, offset, offset + count);
- return s;
- }
- void trim() {
- while (count > 0) {
- char c = data[offset];
- if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) {
- offset++;
- count--;
- continue;
- }
- c = data[offset + count - 1];
- if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) {
- count--;
- continue;
- }
- break;
- }
- }
-
- /* CharSequence interface */
- public int length() {
- return count;
- }
- public char charAt(int index) {
- //System.err.print("["+data[offset+index]+"]");
- return data[offset + index];
- }
- public CharSequence subSequence(int start, int end) {
- //System.err.println("[subSequence("+start+","+end+")]");
- Sequence x = new Sequence();
- x.init(data, offset + start, offset + end);
- return x;
- }
- public String toString() {
- return new String(data, offset, count);
- }
- }
}
diff --git a/net/frotz/sonos/XMLSequence.java b/net/frotz/sonos/XMLSequence.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2011 Brian Swetland
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.frotz.sonos;
+
+class XMLSequence implements CharSequence {
+ char[] data;
+ int offset;
+ int count;
+
+ public XMLSequence() { }
+
+ public void init(char[] data, int start, int end) {
+ this.data = data;
+ offset = start;
+ count = end - start;
+ }
+ public void init(XMLSequence s) {
+ data = s.data;
+ offset = s.offset;
+ count = s.count;
+ }
+ void adjust(int start, int end) {
+ offset = start;
+ count = end - start;
+ }
+ boolean eq(XMLSequence other) {
+ int count = this.count;
+ if (count != other.count)
+ return false;
+ char[] a = this.data;
+ int ao = this.offset;
+ char[] b = other.data;
+ int bo = other.offset;
+ while (count-- > 0)
+ if (a[ao++] != b[bo++])
+ return false;
+ return true;
+ }
+ /* modifies the sequence in-place, escaping basic entities */
+ public XMLSequence unescape() {
+ count = unescape(data, offset) - offset;
+ return this;
+ }
+
+ /* copies the sequence into a char[] + offset, escaping basic entities */
+ public int unescape(char[] out, int off) {
+ char[] in = data;
+ int n = offset;
+ int max = n + count;
+
+ while (n < max) {
+ char c = in[n++];
+ if (c != '&') {
+ out[off++] = c;
+ continue;
+ }
+ int e = n;
+ while (n < max) {
+ if (in[n++] != ';')
+ continue;
+ switch(in[e]) {
+ case 'l': // lt
+ out[off++] = '<';
+ break;
+ case 'g': // gt
+ out[off++] = '>';
+ break;
+ case 'q': // quot
+ out[off++] = '"';
+ break;
+ case 'a': // amp | apos
+ if (in[e+1] == 'm')
+ out[off++] = '&';
+ else if (in[e+1] == 'p')
+ out[off++] = '\'';
+ break;
+ }
+ break;
+ }
+ }
+ return off;
+ }
+
+ CharSequence copy() {
+ XMLSequence s = new XMLSequence();
+ s.init(data, offset, offset + count);
+ return s;
+ }
+ public void trim() {
+ while (count > 0) {
+ char c = data[offset];
+ if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) {
+ offset++;
+ count--;
+ continue;
+ }
+ c = data[offset + count - 1];
+ if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) {
+ count--;
+ continue;
+ }
+ break;
+ }
+ }
+
+ /* CharSequence interface */
+ public int length() {
+ return count;
+ }
+ public char charAt(int index) {
+ //System.err.print("["+data[offset+index]+"]");
+ return data[offset + index];
+ }
+ public CharSequence subSequence(int start, int end) {
+ //System.err.println("[subSequence("+start+","+end+")]");
+ XMLSequence x = new XMLSequence();
+ x.init(data, offset + start, offset + end);
+ return x;
+ }
+ public String toString() {
+ return new String(data, offset, count);
+ }
+}