PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/html/parser/html/SimpleCharStream.java

#
Java | 439 lines | 353 code | 70 blank | 16 comment | 42 complexity | bad1a92044c43eb1d56d75a5f3dc9e85 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
  2. package sidekick.html.parser.html;
  3. /**
  4. * An implementation of interface CharStream, where the stream is assumed to
  5. * contain only ASCII characters (without unicode processing).
  6. */
  7. public class SimpleCharStream
  8. {
  9. public static final boolean staticFlag = false;
  10. int bufsize;
  11. int available;
  12. int tokenBegin;
  13. public int bufpos = -1;
  14. protected int bufline[];
  15. protected int bufcolumn[];
  16. protected int column = 0;
  17. protected int line = 1;
  18. protected boolean prevCharIsCR = false;
  19. protected boolean prevCharIsLF = false;
  20. protected java.io.Reader inputStream;
  21. protected char[] buffer;
  22. protected int maxNextCharInd = 0;
  23. protected int inBuf = 0;
  24. protected int tabSize = 8;
  25. protected void setTabSize(int i) { tabSize = i; }
  26. protected int getTabSize(int i) { return tabSize; }
  27. protected void ExpandBuff(boolean wrapAround)
  28. {
  29. char[] newbuffer = new char[bufsize + 2048];
  30. int newbufline[] = new int[bufsize + 2048];
  31. int newbufcolumn[] = new int[bufsize + 2048];
  32. try
  33. {
  34. if (wrapAround)
  35. {
  36. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  37. System.arraycopy(buffer, 0, newbuffer,
  38. bufsize - tokenBegin, bufpos);
  39. buffer = newbuffer;
  40. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  41. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  42. bufline = newbufline;
  43. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  44. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  45. bufcolumn = newbufcolumn;
  46. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  47. }
  48. else
  49. {
  50. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  51. buffer = newbuffer;
  52. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  53. bufline = newbufline;
  54. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  55. bufcolumn = newbufcolumn;
  56. maxNextCharInd = (bufpos -= tokenBegin);
  57. }
  58. }
  59. catch (Throwable t)
  60. {
  61. throw new Error(t.getMessage());
  62. }
  63. bufsize += 2048;
  64. available = bufsize;
  65. tokenBegin = 0;
  66. }
  67. protected void FillBuff() throws java.io.IOException
  68. {
  69. if (maxNextCharInd == available)
  70. {
  71. if (available == bufsize)
  72. {
  73. if (tokenBegin > 2048)
  74. {
  75. bufpos = maxNextCharInd = 0;
  76. available = tokenBegin;
  77. }
  78. else if (tokenBegin < 0)
  79. bufpos = maxNextCharInd = 0;
  80. else
  81. ExpandBuff(false);
  82. }
  83. else if (available > tokenBegin)
  84. available = bufsize;
  85. else if ((tokenBegin - available) < 2048)
  86. ExpandBuff(true);
  87. else
  88. available = tokenBegin;
  89. }
  90. int i;
  91. try {
  92. if ((i = inputStream.read(buffer, maxNextCharInd,
  93. available - maxNextCharInd)) == -1)
  94. {
  95. inputStream.close();
  96. throw new java.io.IOException();
  97. }
  98. else
  99. maxNextCharInd += i;
  100. return;
  101. }
  102. catch(java.io.IOException e) {
  103. --bufpos;
  104. backup(0);
  105. if (tokenBegin == -1)
  106. tokenBegin = bufpos;
  107. throw e;
  108. }
  109. }
  110. public char BeginToken() throws java.io.IOException
  111. {
  112. tokenBegin = -1;
  113. char c = readChar();
  114. tokenBegin = bufpos;
  115. return c;
  116. }
  117. protected void UpdateLineColumn(char c)
  118. {
  119. column++;
  120. if (prevCharIsLF)
  121. {
  122. prevCharIsLF = false;
  123. line += (column = 1);
  124. }
  125. else if (prevCharIsCR)
  126. {
  127. prevCharIsCR = false;
  128. if (c == '\n')
  129. {
  130. prevCharIsLF = true;
  131. }
  132. else
  133. line += (column = 1);
  134. }
  135. switch (c)
  136. {
  137. case '\r' :
  138. prevCharIsCR = true;
  139. break;
  140. case '\n' :
  141. prevCharIsLF = true;
  142. break;
  143. case '\t' :
  144. column--;
  145. column += (tabSize - (column % tabSize));
  146. break;
  147. default :
  148. break;
  149. }
  150. bufline[bufpos] = line;
  151. bufcolumn[bufpos] = column;
  152. }
  153. public char readChar() throws java.io.IOException
  154. {
  155. if (inBuf > 0)
  156. {
  157. --inBuf;
  158. if (++bufpos == bufsize)
  159. bufpos = 0;
  160. return buffer[bufpos];
  161. }
  162. if (++bufpos >= maxNextCharInd)
  163. FillBuff();
  164. char c = buffer[bufpos];
  165. UpdateLineColumn(c);
  166. return (c);
  167. }
  168. /**
  169. * @deprecated
  170. * @see #getEndColumn
  171. */
  172. public int getColumn() {
  173. return bufcolumn[bufpos];
  174. }
  175. /**
  176. * @deprecated
  177. * @see #getEndLine
  178. */
  179. public int getLine() {
  180. return bufline[bufpos];
  181. }
  182. public int getEndColumn() {
  183. return bufcolumn[bufpos];
  184. }
  185. public int getEndLine() {
  186. return bufline[bufpos];
  187. }
  188. public int getBeginColumn() {
  189. return bufcolumn[tokenBegin];
  190. }
  191. public int getBeginLine() {
  192. return bufline[tokenBegin];
  193. }
  194. public void backup(int amount) {
  195. inBuf += amount;
  196. if ((bufpos -= amount) < 0)
  197. bufpos += bufsize;
  198. }
  199. public SimpleCharStream(java.io.Reader dstream, int startline,
  200. int startcolumn, int buffersize)
  201. {
  202. inputStream = dstream;
  203. line = startline;
  204. column = startcolumn - 1;
  205. available = bufsize = buffersize;
  206. buffer = new char[buffersize];
  207. bufline = new int[buffersize];
  208. bufcolumn = new int[buffersize];
  209. }
  210. public SimpleCharStream(java.io.Reader dstream, int startline,
  211. int startcolumn)
  212. {
  213. this(dstream, startline, startcolumn, 4096);
  214. }
  215. public SimpleCharStream(java.io.Reader dstream)
  216. {
  217. this(dstream, 1, 1, 4096);
  218. }
  219. public void ReInit(java.io.Reader dstream, int startline,
  220. int startcolumn, int buffersize)
  221. {
  222. inputStream = dstream;
  223. line = startline;
  224. column = startcolumn - 1;
  225. if (buffer == null || buffersize != buffer.length)
  226. {
  227. available = bufsize = buffersize;
  228. buffer = new char[buffersize];
  229. bufline = new int[buffersize];
  230. bufcolumn = new int[buffersize];
  231. }
  232. prevCharIsLF = prevCharIsCR = false;
  233. tokenBegin = inBuf = maxNextCharInd = 0;
  234. bufpos = -1;
  235. }
  236. public void ReInit(java.io.Reader dstream, int startline,
  237. int startcolumn)
  238. {
  239. ReInit(dstream, startline, startcolumn, 4096);
  240. }
  241. public void ReInit(java.io.Reader dstream)
  242. {
  243. ReInit(dstream, 1, 1, 4096);
  244. }
  245. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  246. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  247. {
  248. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  249. }
  250. public SimpleCharStream(java.io.InputStream dstream, int startline,
  251. int startcolumn, int buffersize)
  252. {
  253. this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  254. }
  255. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  256. int startcolumn) throws java.io.UnsupportedEncodingException
  257. {
  258. this(dstream, encoding, startline, startcolumn, 4096);
  259. }
  260. public SimpleCharStream(java.io.InputStream dstream, int startline,
  261. int startcolumn)
  262. {
  263. this(dstream, startline, startcolumn, 4096);
  264. }
  265. public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  266. {
  267. this(dstream, encoding, 1, 1, 4096);
  268. }
  269. public SimpleCharStream(java.io.InputStream dstream)
  270. {
  271. this(dstream, 1, 1, 4096);
  272. }
  273. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  274. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  275. {
  276. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  277. }
  278. public void ReInit(java.io.InputStream dstream, int startline,
  279. int startcolumn, int buffersize)
  280. {
  281. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  282. }
  283. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  284. {
  285. ReInit(dstream, encoding, 1, 1, 4096);
  286. }
  287. public void ReInit(java.io.InputStream dstream)
  288. {
  289. ReInit(dstream, 1, 1, 4096);
  290. }
  291. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  292. int startcolumn) throws java.io.UnsupportedEncodingException
  293. {
  294. ReInit(dstream, encoding, startline, startcolumn, 4096);
  295. }
  296. public void ReInit(java.io.InputStream dstream, int startline,
  297. int startcolumn)
  298. {
  299. ReInit(dstream, startline, startcolumn, 4096);
  300. }
  301. public String GetImage()
  302. {
  303. if (bufpos >= tokenBegin)
  304. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  305. else
  306. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  307. new String(buffer, 0, bufpos + 1);
  308. }
  309. public char[] GetSuffix(int len)
  310. {
  311. char[] ret = new char[len];
  312. if ((bufpos + 1) >= len)
  313. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  314. else
  315. {
  316. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  317. len - bufpos - 1);
  318. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  319. }
  320. return ret;
  321. }
  322. public void Done()
  323. {
  324. buffer = null;
  325. bufline = null;
  326. bufcolumn = null;
  327. }
  328. /**
  329. * Method to adjust line and column numbers for the start of a token.
  330. */
  331. public void adjustBeginLineColumn(int newLine, int newCol)
  332. {
  333. int start = tokenBegin;
  334. int len;
  335. if (bufpos >= tokenBegin)
  336. {
  337. len = bufpos - tokenBegin + inBuf + 1;
  338. }
  339. else
  340. {
  341. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  342. }
  343. int i = 0, j = 0, k = 0;
  344. int nextColDiff = 0, columnDiff = 0;
  345. while (i < len &&
  346. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  347. {
  348. bufline[j] = newLine;
  349. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  350. bufcolumn[j] = newCol + columnDiff;
  351. columnDiff = nextColDiff;
  352. i++;
  353. }
  354. if (i < len)
  355. {
  356. bufline[j] = newLine++;
  357. bufcolumn[j] = newCol + columnDiff;
  358. while (i++ < len)
  359. {
  360. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  361. bufline[j] = newLine++;
  362. else
  363. bufline[j] = newLine;
  364. }
  365. }
  366. line = bufline[j];
  367. column = bufcolumn[j];
  368. }
  369. }