/trunk/BTalk-highOS/src/net/sourceforge/jxa/XmlReader.java

http://btalk.googlecode.com/ · Java · 239 lines · 174 code · 32 blank · 33 comment · 48 complexity · eb9dc1accf218971b24b2e9167b1df2a MD5 · raw file

  1. /*
  2. * Copyright 2004 Grzegorz Grasza groz@gryf.info
  3. *
  4. * This file is part of mobber. Mobber is free software; you can redistribute it
  5. * and/or modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version. Mobber is distributed in the hope that
  8. * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details. You should have received a copy of
  11. * the GNU General Public License along with mobber; if not, write to the Free
  12. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  13. * USA .
  14. */
  15. package net.sourceforge.jxa;
  16. import java.io.*;
  17. import java.util.*;
  18. /**
  19. * XML-Reader
  20. *
  21. * @author Grzegorz Grasza
  22. * @version 1.0
  23. * @since 1.0
  24. */
  25. public class XmlReader {
  26. private InputStream is;
  27. public final static int START_DOCUMENT = 0;
  28. public final static int END_DOCUMENT = 1;
  29. public final static int START_TAG = 2;
  30. public final static int END_TAG = 3;
  31. public final static int TEXT = 4;
  32. private Stack tags;
  33. private boolean inside_tag;
  34. private String tagName;
  35. private String text;
  36. private final Hashtable attributes = new Hashtable();
  37. private int c;
  38. private int type = START_DOCUMENT;
  39. //public XmlReader(final InputStream in) throws IOException, UnsupportedEncodingException {
  40. public XmlReader(final InputStream in) throws IOException {
  41. //reader = new InputStreamReader(in, "UTF-8");
  42. this.is = in;
  43. this.tags = new Stack();
  44. this.inside_tag = false;
  45. }
  46. //http://discussion.forum.nokia.com/forum/showthread.php?t=76814
  47. //by abirr
  48. private int getNextCharacter() throws IOException {
  49. int a = is.read();
  50. int t=a;
  51. if((t|0xC0)==t){
  52. int b = is.read();
  53. if( b == 0xFF ){ // Check if legal
  54. t=-1;
  55. }else if( b < 0x80 ){ // Check for UTF8 compliancy
  56. throw new IOException("Bad UTF-8 Encoding encountered");
  57. }else if((t|0xE0)==t) {
  58. int c = is.read();
  59. if( c == 0xFF ){ // Check if legal
  60. t=-1;
  61. }else if( c < 0x80 ){ // Check for UTF8 compliancy
  62. throw new IOException("Bad UTF-8 Encoding encountered");
  63. }else
  64. t=((a & 0x0F)<<12) | ((b & 0x3F)<<6) | (c & 0x3F);
  65. }else
  66. t=((a & 0x1F)<<6)|(b&0x3F);
  67. }
  68. return t;
  69. }
  70. public void close() {
  71. /*try {
  72. reader.close();
  73. } catch (IOException e) {}*/
  74. }
  75. public int next() throws IOException {
  76. /* while (!this.ready())
  77. try {
  78. java.lang.Thread.sleep(100);
  79. } catch (InterruptedException e) {}*/
  80. this.c = getNextCharacter();
  81. if (this.c <= ' ') {
  82. while (((this.c = getNextCharacter()) <= ' ') && (this.c != -1)) {
  83. ;
  84. }
  85. }
  86. if (this.c == -1) {
  87. this.type = END_DOCUMENT;
  88. return this.type;
  89. }
  90. if ((this.c == '<') || ((this.c == '/') && !this.inside_tag)) {
  91. this.inside_tag = true;
  92. // reset all
  93. this.tagName = null;
  94. this.text = null;
  95. this.attributes.clear();
  96. if (this.c == '<') {
  97. this.c = getNextCharacter();
  98. }
  99. if (this.c == '/') {
  100. this.type = END_TAG;
  101. this.c = getNextCharacter();
  102. this.tagName = this.readName('>');
  103. } else if ((this.c == '?') || (this.c == '!')) {// ignore xml heading & // comments
  104. while ((this.c = getNextCharacter()) != '>') {
  105. ;
  106. }
  107. this.next();
  108. } else {
  109. this.type = START_TAG;
  110. this.tagName = this.readName(' ');
  111. String attribute = "";
  112. String value = "";
  113. while (this.c == ' ') {
  114. this.c = getNextCharacter();
  115. attribute = this.readName('=');
  116. int quote = getNextCharacter();//this.c = this.read(); // '''
  117. this.c = getNextCharacter();
  118. value = this.readText(quote); //change from value = this.readText(''');
  119. this.c = getNextCharacter();
  120. this.attributes.put(attribute, value);
  121. }
  122. if (this.c != '/') {
  123. this.inside_tag = false;
  124. }
  125. }
  126. } else if ((this.c == '>') && this.inside_tag) // last tag ended
  127. {
  128. this.type = END_TAG;
  129. this.inside_tag = false;
  130. } else {
  131. this.tagName = null;
  132. this.attributes.clear();
  133. this.type = TEXT;
  134. this.text = this.readText('<');
  135. }
  136. return this.type;
  137. }
  138. public int getType() {
  139. return this.type;
  140. }
  141. public String getName() {
  142. return this.tagName;
  143. }
  144. public String getAttribute(final String name) {
  145. return (String) this.attributes.get(name);
  146. }
  147. public Enumeration getAttributes() {
  148. return this.attributes.keys();
  149. }
  150. public String getText() {
  151. return this.text;
  152. }
  153. private String readText(final int end) throws IOException {
  154. final StringBuffer output = new StringBuffer("");
  155. while (this.c != end) {
  156. if (this.c == '&') {
  157. this.c = getNextCharacter();
  158. switch (this.c) {
  159. case 'l':
  160. output.append('<');
  161. break;
  162. case 'g':
  163. output.append('>');
  164. break;
  165. case 'a':
  166. if (getNextCharacter() == 'm') {
  167. output.append('&');
  168. } else {
  169. output.append('\'');
  170. }
  171. break;
  172. case 'q':
  173. output.append('"');
  174. break;
  175. case 'n':
  176. output.append(' ');
  177. break;
  178. default:
  179. output.append('?');
  180. }
  181. while ((this.c = getNextCharacter()) != ';') {
  182. ;
  183. }
  184. } else if (this.c == '\\') {
  185. if ((this.c = getNextCharacter()) == '<') {
  186. break;
  187. } else {
  188. output.append((char) this.c);
  189. }
  190. } else {
  191. output.append((char) this.c);
  192. }
  193. this.c = getNextCharacter();
  194. }
  195. // while((c = read()) != end);
  196. return output.toString();
  197. }
  198. private String readName(final int end) throws IOException {
  199. final StringBuffer output = new StringBuffer("");
  200. do {
  201. output.append((char) this.c);
  202. } while (((this.c = getNextCharacter()) != end) && (this.c != '>') && (this.c != '/'));
  203. return output.toString();
  204. }
  205. };