diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:03:55 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:03:55 -0800 |
commit | dd828f42a5c83b4270d4fbf6fce2da1878f1e84a (patch) | |
tree | fdd4b68fa1020f2b6426034c94823419a7236200 /xml/src/main/java/org/kxml2 | |
parent | fdb2704414a9ed92394ada0d1395e4db86889465 (diff) | |
download | libcore-dd828f42a5c83b4270d4fbf6fce2da1878f1e84a.zip libcore-dd828f42a5c83b4270d4fbf6fce2da1878f1e84a.tar.gz libcore-dd828f42a5c83b4270d4fbf6fce2da1878f1e84a.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'xml/src/main/java/org/kxml2')
-rw-r--r-- | xml/src/main/java/org/kxml2/io/KXmlParser.java | 12 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/kdom/Element.java | 37 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/kdom/Node.java | 12 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/wap/WbxmlParser.java | 121 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/wap/WbxmlSerializer.java | 166 |
5 files changed, 234 insertions, 114 deletions
diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java index 8125745..0727bc7 100644 --- a/xml/src/main/java/org/kxml2/io/KXmlParser.java +++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java @@ -45,7 +45,7 @@ public class KXmlParser implements XmlPullParser { private boolean processNsp; private boolean relaxed; - private HashMap entityMap; + private Hashtable entityMap; private int depth; private String[] elementStack = new String[16]; private String[] nspStack = new String[8]; @@ -95,10 +95,12 @@ public class KXmlParser implements XmlPullParser { private boolean token; public KXmlParser() { -// srcBuf = new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128]; - - // XXX: We don't have a Runtime class at this time. + // BEGIN android-changed + // We don't have a Runtime class at this time. + // srcBuf = + // new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128]; srcBuf = new char[8192]; + // END android-changed } private final boolean isProp(String n1, boolean prop, String n2) { @@ -964,7 +966,7 @@ public class KXmlParser implements XmlPullParser { peekCount = 0; depth = 0; - entityMap = new HashMap(); + entityMap = new Hashtable(); entityMap.put("amp", "&"); entityMap.put("apos", "'"); entityMap.put("gt", ">"); diff --git a/xml/src/main/java/org/kxml2/kdom/Element.java b/xml/src/main/java/org/kxml2/kdom/Element.java index 61d5111..6a5777b 100644 --- a/xml/src/main/java/org/kxml2/kdom/Element.java +++ b/xml/src/main/java/org/kxml2/kdom/Element.java @@ -34,9 +34,9 @@ public class Element extends Node { protected String namespace; protected String name; - protected ArrayList attributes; + protected Vector attributes; protected Node parent; - protected ArrayList prefixes; + protected Vector prefixes; public Element() { } @@ -81,24 +81,22 @@ public class Element extends Node { } public String getAttributeNamespace (int index) { - return ((String []) attributes.get(index)) [0]; + return ((String []) attributes.elementAt (index)) [0]; } +/* public String getAttributePrefix (int index) { + return ((String []) attributes.elementAt (index)) [1]; + }*/ + public String getAttributeName (int index) { - return ((String []) attributes.get(index)) [1]; + return ((String []) attributes.elementAt (index)) [1]; } public String getAttributeValue (int index) { - return ((String []) attributes.get(index)) [2]; + return ((String []) attributes.elementAt (index)) [2]; } - - public String - getAttributeValue(String name) - { - return getAttributeValue(null, name); - } public String getAttributeValue (String namespace, String name) { for (int i = 0; i < getAttributeCount (); i++) { @@ -165,11 +163,11 @@ public class Element extends Node { public String getNamespacePrefix (int i) { - return ((String []) prefixes.get(i)) [0]; + return ((String []) prefixes.elementAt (i)) [0]; } public String getNamespaceUri (int i) { - return ((String []) prefixes.get(i)) [1]; + return ((String []) prefixes.elementAt (i)) [1]; } @@ -240,18 +238,18 @@ public class Element extends Node { public void setAttribute (String namespace, String name, String value) { if (attributes == null) - attributes = new ArrayList(); + attributes = new Vector (); if (namespace == null) namespace = ""; for (int i = attributes.size()-1; i >=0; i--){ - String[] attribut = (String[]) attributes.get(i); + String[] attribut = (String[]) attributes.elementAt(i); if (attribut[0].equals(namespace) && attribut[1].equals(name)){ if (value == null) { - attributes.remove(i); + attributes.removeElementAt(i); } else { attribut[2] = value; @@ -260,7 +258,8 @@ public class Element extends Node { } } - attributes.add(new String[] {namespace, name, value}); + attributes.addElement + (new String [] {namespace, name, value}); } @@ -269,8 +268,8 @@ public class Element extends Node { * prefix */ public void setPrefix (String prefix, String namespace) { - if (prefixes == null) prefixes = new ArrayList(); - prefixes.add(new String [] {prefix, namespace}); + if (prefixes == null) prefixes = new Vector (); + prefixes.addElement (new String [] {prefix, namespace}); } diff --git a/xml/src/main/java/org/kxml2/kdom/Node.java b/xml/src/main/java/org/kxml2/kdom/Node.java index 4855893..a3cc78d 100644 --- a/xml/src/main/java/org/kxml2/kdom/Node.java +++ b/xml/src/main/java/org/kxml2/kdom/Node.java @@ -38,7 +38,7 @@ public class Node { //implements XmlIO{ public static final int COMMENT = 9; public static final int DOCDECL = 10; - protected ArrayList children; + protected Vector children; protected StringBuffer types; /** inserts the given child object of the given type at the @@ -50,7 +50,7 @@ public class Node { //implements XmlIO{ throw new NullPointerException(); if (children == null) { - children = new ArrayList(); + children = new Vector(); types = new StringBuffer(); } @@ -63,7 +63,7 @@ public class Node { //implements XmlIO{ else if (!(child instanceof String)) throw new RuntimeException("String expected"); - children.add(index, child); + children.insertElementAt(child, index); types.insert(index, (char) type); } @@ -94,7 +94,7 @@ public class Node { //implements XmlIO{ types, a String is returned. */ public Object getChild(int index) { - return children.get(index); + return children.elementAt(index); } /** Returns the number of child objects */ @@ -272,7 +272,7 @@ public class Node { //implements XmlIO{ /** Removes the child object at the given index */ public void removeChild(int idx) { - children.remove(idx); + children.removeElementAt(idx); /*** Modification by HHS - start ***/ // types.deleteCharAt (index); @@ -324,7 +324,7 @@ public class Node { //implements XmlIO{ for (int i = 0; i < len; i++) { int type = getType(i); - Object child = children.get(i); + Object child = children.elementAt(i); switch (type) { case ELEMENT : ((Element) child).write(writer); diff --git a/xml/src/main/java/org/kxml2/wap/WbxmlParser.java b/xml/src/main/java/org/kxml2/wap/WbxmlParser.java index c3852eb..617e1d4 100644 --- a/xml/src/main/java/org/kxml2/wap/WbxmlParser.java +++ b/xml/src/main/java/org/kxml2/wap/WbxmlParser.java @@ -20,7 +20,7 @@ // Contributors: Bjorn Aadland, Chris Bartley, Nicola Fankhauser, // Victor Havin, Christian Kurzke, Bogdan Onoiu, -// Jain Sanjay, David Santoro. +// Elias Ross, Jain Sanjay, David Santoro. package org.kxml2.wap; @@ -32,6 +32,11 @@ import org.xmlpull.v1.*; public class WbxmlParser implements XmlPullParser { + + static final String HEX_DIGITS = "0123456789abcdef"; + + /** Parser event type for Wbxml-specific events. The Wbxml event code can be + * accessed with getWapCode() */ public static final int WAP_EXTENSION = 64; @@ -64,9 +69,8 @@ public class WbxmlParser implements XmlPullParser { private Vector tables = new Vector(); - int version; - int publicIdentifierId; - int charSet; + private int version; + private int publicIdentifierId; // StartTag current; // ParseEvent next; @@ -75,16 +79,15 @@ public class WbxmlParser implements XmlPullParser { private String namespace; private String name; private String text; - // private String encoding; + private Object wapExtensionData; - private int wapExtensionCode; + private int wapCode; private int type; - private int codePage; private boolean degenerated; private boolean isWhitespace; - private String encoding = null; + private String encoding; public boolean getFeature(String feature) { if (XmlPullParser @@ -96,7 +99,6 @@ public class WbxmlParser implements XmlPullParser { } public String getInputEncoding() { - // should return someting depending on charSet here!!!!! return encoding; } @@ -315,6 +317,10 @@ public class WbxmlParser implements XmlPullParser { return type; } + + // TODO: Reuse resolveWapExtension here? Raw Wap extensions would still be accessible + // via nextToken(); ....? + public int next() throws XmlPullParserException, IOException { isWhitespace = true; @@ -338,6 +344,7 @@ public class WbxmlParser implements XmlPullParser { switch(peekId()) { case Wbxml.ENTITY: case Wbxml.STR_I: + case Wbxml.STR_T: case Wbxml.LITERAL: case Wbxml.LITERAL_C: case Wbxml.LITERAL_A: @@ -408,7 +415,7 @@ public class WbxmlParser implements XmlPullParser { || (namespace != null && !namespace.equals(getNamespace())) || (name != null && !name.equals(getName()))) exception( - "expected: " + TYPES[type] + " {" + namespace + "}" + name); + "expected: " + (type == WAP_EXTENSION ? "WAP Ext." : (TYPES[type] + " {" + namespace + "}" + name))); } @@ -698,7 +705,10 @@ public class WbxmlParser implements XmlPullParser { case Wbxml.EXT_1 : case Wbxml.EXT_2 : case Wbxml.OPAQUE : - parseWapExtension(id); + + type = WAP_EXTENSION; + wapCode = id; + wapExtensionData = parseWapExtension(id); break; case Wbxml.PI : @@ -722,48 +732,42 @@ public class WbxmlParser implements XmlPullParser { // return next; } + /** Overwrite this method to intercept all wap events */ - - public void parseWapExtension(int id) - throws IOException, XmlPullParserException { - - type = WAP_EXTENSION; - wapExtensionCode = id; + public Object parseWapExtension(int id) throws IOException, XmlPullParserException { switch (id) { case Wbxml.EXT_I_0 : case Wbxml.EXT_I_1 : case Wbxml.EXT_I_2 : - wapExtensionData = readStrI(); - break; + return readStrI(); case Wbxml.EXT_T_0 : case Wbxml.EXT_T_1 : case Wbxml.EXT_T_2 : - wapExtensionData = new Integer(readInt()); - break; + return new Integer(readInt()); case Wbxml.EXT_0 : case Wbxml.EXT_1 : case Wbxml.EXT_2 : - break; + return null; case Wbxml.OPAQUE : { - int len = readInt(); - byte[] buf = new byte[len]; + int count = readInt(); + byte[] buf = new byte[count]; - for (int i = 0; - i < len; - i++) // enhance with blockread! - buf[i] = (byte) readByte(); + while(count > 0){ + count -= in.read(buf, buf.length-count, count); + } - wapExtensionData = buf; + return buf; } // case OPAQUE - break; + default: exception("illegal id: "+id); + return null; // dead code } // SWITCH } @@ -824,20 +828,8 @@ public class WbxmlParser implements XmlPullParser { case Wbxml.EXT_1 : case Wbxml.EXT_2 : case Wbxml.OPAQUE : - - throw new RuntimeException("wap extension in attr not supported yet"); - - /* - ParseEvent e = parseWapExtension(id); - if (!(e.getType() != Xml.TEXT - && e.getType() != Xml.WHITESPACE)) - throw new RuntimeException("parse WapExtension must return Text Event in order to work inside Attributes!"); - - value.append(e.getText()); - - //value.append (handleExtension (id)); // skip EXT in ATTR - //break; - */ + value.append(resolveWapExtension(id, parseWapExtension(id))); + break; case Wbxml.STR_T : value.append(readStrT()); @@ -869,19 +861,39 @@ public class WbxmlParser implements XmlPullParser { return nextId; } + /** overwrite for own WAP extension handling in attributes and high level parsing + * (above nextToken() level) */ - + protected String resolveWapExtension(int id, Object data){ + + if(data instanceof byte[]){ + StringBuffer sb = new StringBuffer(); + byte[] b = (byte[]) data; + + for (int i = 0; i < b.length; i++) { + sb.append(HEX_DIGITS.charAt((b[i] >> 4) & 0x0f)); + sb.append(HEX_DIGITS.charAt(b[i] & 0x0f)); + } + return sb.toString(); + } + + return "$("+data+")"; + } String resolveId(String[] tab, int id) throws IOException { int idx = (id & 0x07f) - 5; - if (idx == -1) + if (idx == -1){ + wapCode = -1; return readStrT(); + } if (idx < 0 || tab == null || idx >= tab.length || tab[idx] == null) throw new IOException("id " + id + " undef."); + wapCode = idx+5; + return tab[idx]; } @@ -1022,7 +1034,7 @@ public class WbxmlParser implements XmlPullParser { } /** Sets the attribute start Table for a given page. - * The first string in the array defines attribute + * The first string in the array defines attribute * 5, the second attribute 6 etc. Please use the * character '=' (without quote!) as delimiter * between the attribute name and the (start of the) value @@ -1036,7 +1048,7 @@ public class WbxmlParser implements XmlPullParser { } /** Sets the attribute value Table for a given page. - * The first string in the array defines attribute value 0x85, + * The first string in the array defines attribute value 0x85, * the second attribute value 0x86 etc. */ @@ -1047,4 +1059,17 @@ public class WbxmlParser implements XmlPullParser { setTable(page, ATTR_VALUE_TABLE, table); } + /** Returns the token ID for start tags or the event type for wap proprietary events + * such as OPAQUE. + */ + + public int getWapCode(){ + return wapCode; + } + + public Object getWapExtensionData(){ + return wapExtensionData; + } + + } diff --git a/xml/src/main/java/org/kxml2/wap/WbxmlSerializer.java b/xml/src/main/java/org/kxml2/wap/WbxmlSerializer.java index e4447b0..8c1b598 100644 --- a/xml/src/main/java/org/kxml2/wap/WbxmlSerializer.java +++ b/xml/src/main/java/org/kxml2/wap/WbxmlSerializer.java @@ -18,7 +18,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ -//Contributors: Bogdan Onoiu (Genderal character encoding abstraction and UTF-8 Support) +//Contributors: Jonathan Cox, Bogdan Onoiu, Jerry Tian package org.kxml2.wap; @@ -38,6 +38,7 @@ import org.xmlpull.v1.*; public class WbxmlSerializer implements XmlSerializer { + Hashtable stringTable = new Hashtable(); OutputStream out; @@ -58,7 +59,7 @@ public class WbxmlSerializer implements XmlSerializer { private int attrPage; private int tagPage; - private String encoding = null; + private String encoding; public XmlSerializer attribute(String namespace, String name, String value) { @@ -137,7 +138,7 @@ public class WbxmlSerializer implements XmlSerializer { /** ATTENTION: flush cannot work since Wbxml documents require - need buffering. Thus, this call does nothing. */ + buffering. Thus, this call does nothing. */ public void flush() { } @@ -158,12 +159,12 @@ public class WbxmlSerializer implements XmlSerializer { ? (degenerated ? Wbxml.LITERAL : Wbxml.LITERAL_C) : (degenerated ? Wbxml.LITERAL_A : Wbxml.LITERAL_AC)); - writeStrT(pending); + writeStrT(pending, false); } else { if(idx[0] != tagPage){ tagPage=idx[0]; - buf.write(0); + buf.write(Wbxml.SWITCH_PAGE); buf.write(tagPage); } @@ -181,11 +182,11 @@ public class WbxmlSerializer implements XmlSerializer { if (idx == null) { buf.write(Wbxml.LITERAL); - writeStrT((String) attributes.elementAt(i)); + writeStrT((String) attributes.elementAt(i), false); } else { if(idx[0] != attrPage){ - attrPage = idx[1]; + attrPage = idx[0]; buf.write(0); buf.write(attrPage); } @@ -193,12 +194,11 @@ public class WbxmlSerializer implements XmlSerializer { } idx = (int[]) attrValueTable.get(attributes.elementAt(++i)); if (idx == null) { - buf.write(Wbxml.STR_I); - writeStrI(buf, (String) attributes.elementAt(i)); + writeStr((String) attributes.elementAt(i)); } else { if(idx[0] != attrPage){ - attrPage = idx[1]; + attrPage = idx[0]; buf.write(0); buf.write(attrPage); } @@ -232,8 +232,7 @@ public class WbxmlSerializer implements XmlSerializer { public void setOutput (OutputStream out, String encoding) throws IOException { - if (encoding != null) throw new IllegalArgumentException ("encoding not yet supported for WBXML"); - + this.encoding = encoding == null ? "UTF-8" : encoding; this.out = out; buf = new ByteArrayOutputStream(); @@ -258,12 +257,14 @@ public class WbxmlSerializer implements XmlSerializer { out.write(0x01); // unknown or missing public identifier // default encoding is UTF-8 - String[] encodings = { "UTF-8", "ISO-8859-1" }; - if (s == null || s.toUpperCase().equals(encodings[0])){ - encoding = encodings[0]; + + if(s != null){ + encoding = s; + } + + if (encoding.toUpperCase().equals("UTF-8")){ out.write(106); - }else if (true == s.toUpperCase().equals(encodings[1])){ - encoding = encodings[1]; + }else if (encoding.toUpperCase().equals("ISO-8859-1")){ out.write(0x04); }else{ throw new UnsupportedEncodingException(s); @@ -286,11 +287,10 @@ public class WbxmlSerializer implements XmlSerializer { } public XmlSerializer text(char[] chars, int start, int len) throws IOException { - + checkPending(false); - buf.write(Wbxml.STR_I); - writeStrI(buf, new String(chars, start, len)); + writeStr(new String(chars, start, len)); return this; } @@ -299,13 +299,61 @@ public class WbxmlSerializer implements XmlSerializer { checkPending(false); - buf.write(Wbxml.STR_I); - writeStrI(buf, text); + writeStr(text); return this; } + /** Used in text() and attribute() to write text */ + + private void writeStr(String text) throws IOException{ + int p0 = 0; + int lastCut = 0; + int len = text.length(); + + while(p0 < len){ + while(p0 < len && text.charAt(p0) < 'A' ){ // skip interpunctation + p0++; + } + int p1 = p0; + while(p1 < len && text.charAt(p1) >= 'A'){ + p1++; + } + + if(p1 - p0 > 10) { + + if(p0 > lastCut && text.charAt(p0-1) == ' ' + && stringTable.get(text.substring(p0, p1)) == null){ + + buf.write(Wbxml.STR_T); + writeStrT(text.substring(lastCut, p1), false); + } + else { + + if(p0 > lastCut && text.charAt(p0-1) == ' '){ + p0--; + } + + if(p0 > lastCut){ + buf.write(Wbxml.STR_T); + writeStrT(text.substring(lastCut, p0), false); + } + buf.write(Wbxml.STR_T); + writeStrT(text.substring(p0, p1), true); + } + lastCut = p1; + } + p0 = p1; + } + + if(lastCut < len){ + buf.write(Wbxml.STR_T); + writeStrT(text.substring(lastCut, len), false); + } + } + + public XmlSerializer endTag(String namespace, String name) throws IOException { @@ -321,9 +369,39 @@ public class WbxmlSerializer implements XmlSerializer { return this; } - /** currently ignored! */ + /** + * @throws IOException */ - public void writeLegacy(int type, String data) { + public void writeWapExtension(int type, Object data) throws IOException { + checkPending(false); + buf.write(type); + switch(type){ + case Wbxml.EXT_0: + case Wbxml.EXT_1: + case Wbxml.EXT_2: + break; + + case Wbxml.OPAQUE: + byte[] bytes = (byte[]) data; + writeInt(buf, bytes.length); + buf.write(bytes); + break; + + case Wbxml.EXT_I_0: + case Wbxml.EXT_I_1: + case Wbxml.EXT_I_2: + writeStrI(buf, (String) data); + break; + + case Wbxml.EXT_T_0: + case Wbxml.EXT_T_1: + case Wbxml.EXT_T_2: + writeStrT((String) data, false); + break; + + default: + throw new IllegalArgumentException(); + } } // ------------- internal methods -------------------------- @@ -344,25 +422,43 @@ public class WbxmlSerializer implements XmlSerializer { out.write(buf[0]); } - static void writeStrI(OutputStream out, String s) throws IOException { - for (int i = 0; i < s.length(); i++) { - out.write((byte) s.charAt(i)); - } + void writeStrI(OutputStream out, String s) throws IOException { + byte[] data = s.getBytes(encoding); + out.write(data); out.write(0); } - void writeStrT(String s) throws IOException { + private final void writeStrT(String s, boolean mayPrependSpace) throws IOException { Integer idx = (Integer) stringTable.get(s); - if (idx == null) { - idx = new Integer(stringTableBuf.size()); - stringTable.put(s, idx); + if (idx != null) { + writeInt(buf, idx.intValue()); + } + else{ + int i = stringTableBuf.size(); + if(s.charAt(0) >= '0' && mayPrependSpace){ + s = ' ' + s; + writeInt(buf, i+1); + } + else{ + writeInt(buf, i); + } + + stringTable.put(s, new Integer(i)); + if(s.charAt(0) == ' '){ + stringTable.put(s.substring(1), new Integer(i+1)); + } + int j = s.lastIndexOf(' '); + if(j > 1){ + stringTable.put(s.substring(j), new Integer(i+j)); + stringTable.put(s.substring(j+1), new Integer(i+j+1)); + } + writeStrI(stringTableBuf, s); stringTableBuf.flush(); } - writeInt(buf, idx.intValue()); } /** @@ -371,9 +467,7 @@ public class WbxmlSerializer implements XmlSerializer { */ public void setTagTable(int page, String[] tagTable) { - // clear entries in tagTable! - if (page != 0) - return; + // TODO: clear entries in tagTable? for (int i = 0; i < tagTable.length; i++) { if (tagTable[i] != null) { |