PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/xsom-20110809svn/src/com/sun/xml/xsom/impl/scd/SimpleCharStream.java

#
Java | 479 lines | 353 code | 71 blank | 55 comment | 42 complexity | ffe6f0f267c97b973f8cbaf6b12d337f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  12. * or packager/legal/LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at packager/legal/LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
  41. package com.sun.xml.xsom.impl.scd;
  42. /**
  43. * An implementation of interface CharStream, where the stream is assumed to
  44. * contain only ASCII characters (without unicode processing).
  45. */
  46. public class SimpleCharStream
  47. {
  48. public static final boolean staticFlag = false;
  49. int bufsize;
  50. int available;
  51. int tokenBegin;
  52. public int bufpos = -1;
  53. protected int bufline[];
  54. protected int bufcolumn[];
  55. protected int column = 0;
  56. protected int line = 1;
  57. protected boolean prevCharIsCR = false;
  58. protected boolean prevCharIsLF = false;
  59. protected java.io.Reader inputStream;
  60. protected char[] buffer;
  61. protected int maxNextCharInd = 0;
  62. protected int inBuf = 0;
  63. protected int tabSize = 8;
  64. protected void setTabSize(int i) { tabSize = i; }
  65. protected int getTabSize(int i) { return tabSize; }
  66. protected void ExpandBuff(boolean wrapAround)
  67. {
  68. char[] newbuffer = new char[bufsize + 2048];
  69. int newbufline[] = new int[bufsize + 2048];
  70. int newbufcolumn[] = new int[bufsize + 2048];
  71. try
  72. {
  73. if (wrapAround)
  74. {
  75. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  76. System.arraycopy(buffer, 0, newbuffer,
  77. bufsize - tokenBegin, bufpos);
  78. buffer = newbuffer;
  79. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  80. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  81. bufline = newbufline;
  82. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  83. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  84. bufcolumn = newbufcolumn;
  85. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  86. }
  87. else
  88. {
  89. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  90. buffer = newbuffer;
  91. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  92. bufline = newbufline;
  93. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  94. bufcolumn = newbufcolumn;
  95. maxNextCharInd = (bufpos -= tokenBegin);
  96. }
  97. }
  98. catch (Throwable t)
  99. {
  100. throw new Error(t.getMessage());
  101. }
  102. bufsize += 2048;
  103. available = bufsize;
  104. tokenBegin = 0;
  105. }
  106. protected void FillBuff() throws java.io.IOException
  107. {
  108. if (maxNextCharInd == available)
  109. {
  110. if (available == bufsize)
  111. {
  112. if (tokenBegin > 2048)
  113. {
  114. bufpos = maxNextCharInd = 0;
  115. available = tokenBegin;
  116. }
  117. else if (tokenBegin < 0)
  118. bufpos = maxNextCharInd = 0;
  119. else
  120. ExpandBuff(false);
  121. }
  122. else if (available > tokenBegin)
  123. available = bufsize;
  124. else if ((tokenBegin - available) < 2048)
  125. ExpandBuff(true);
  126. else
  127. available = tokenBegin;
  128. }
  129. int i;
  130. try {
  131. if ((i = inputStream.read(buffer, maxNextCharInd,
  132. available - maxNextCharInd)) == -1)
  133. {
  134. inputStream.close();
  135. throw new java.io.IOException();
  136. }
  137. else
  138. maxNextCharInd += i;
  139. return;
  140. }
  141. catch(java.io.IOException e) {
  142. --bufpos;
  143. backup(0);
  144. if (tokenBegin == -1)
  145. tokenBegin = bufpos;
  146. throw e;
  147. }
  148. }
  149. public char BeginToken() throws java.io.IOException
  150. {
  151. tokenBegin = -1;
  152. char c = readChar();
  153. tokenBegin = bufpos;
  154. return c;
  155. }
  156. protected void UpdateLineColumn(char c)
  157. {
  158. column++;
  159. if (prevCharIsLF)
  160. {
  161. prevCharIsLF = false;
  162. line += (column = 1);
  163. }
  164. else if (prevCharIsCR)
  165. {
  166. prevCharIsCR = false;
  167. if (c == '\n')
  168. {
  169. prevCharIsLF = true;
  170. }
  171. else
  172. line += (column = 1);
  173. }
  174. switch (c)
  175. {
  176. case '\r' :
  177. prevCharIsCR = true;
  178. break;
  179. case '\n' :
  180. prevCharIsLF = true;
  181. break;
  182. case '\t' :
  183. column--;
  184. column += (tabSize - (column % tabSize));
  185. break;
  186. default :
  187. break;
  188. }
  189. bufline[bufpos] = line;
  190. bufcolumn[bufpos] = column;
  191. }
  192. public char readChar() throws java.io.IOException
  193. {
  194. if (inBuf > 0)
  195. {
  196. --inBuf;
  197. if (++bufpos == bufsize)
  198. bufpos = 0;
  199. return buffer[bufpos];
  200. }
  201. if (++bufpos >= maxNextCharInd)
  202. FillBuff();
  203. char c = buffer[bufpos];
  204. UpdateLineColumn(c);
  205. return (c);
  206. }
  207. /**
  208. * @deprecated
  209. * @see #getEndColumn
  210. */
  211. public int getColumn() {
  212. return bufcolumn[bufpos];
  213. }
  214. /**
  215. * @deprecated
  216. * @see #getEndLine
  217. */
  218. public int getLine() {
  219. return bufline[bufpos];
  220. }
  221. public int getEndColumn() {
  222. return bufcolumn[bufpos];
  223. }
  224. public int getEndLine() {
  225. return bufline[bufpos];
  226. }
  227. public int getBeginColumn() {
  228. return bufcolumn[tokenBegin];
  229. }
  230. public int getBeginLine() {
  231. return bufline[tokenBegin];
  232. }
  233. public void backup(int amount) {
  234. inBuf += amount;
  235. if ((bufpos -= amount) < 0)
  236. bufpos += bufsize;
  237. }
  238. public SimpleCharStream(java.io.Reader dstream, int startline,
  239. int startcolumn, int buffersize)
  240. {
  241. inputStream = dstream;
  242. line = startline;
  243. column = startcolumn - 1;
  244. available = bufsize = buffersize;
  245. buffer = new char[buffersize];
  246. bufline = new int[buffersize];
  247. bufcolumn = new int[buffersize];
  248. }
  249. public SimpleCharStream(java.io.Reader dstream, int startline,
  250. int startcolumn)
  251. {
  252. this(dstream, startline, startcolumn, 4096);
  253. }
  254. public SimpleCharStream(java.io.Reader dstream)
  255. {
  256. this(dstream, 1, 1, 4096);
  257. }
  258. public void ReInit(java.io.Reader dstream, int startline,
  259. int startcolumn, int buffersize)
  260. {
  261. inputStream = dstream;
  262. line = startline;
  263. column = startcolumn - 1;
  264. if (buffer == null || buffersize != buffer.length)
  265. {
  266. available = bufsize = buffersize;
  267. buffer = new char[buffersize];
  268. bufline = new int[buffersize];
  269. bufcolumn = new int[buffersize];
  270. }
  271. prevCharIsLF = prevCharIsCR = false;
  272. tokenBegin = inBuf = maxNextCharInd = 0;
  273. bufpos = -1;
  274. }
  275. public void ReInit(java.io.Reader dstream, int startline,
  276. int startcolumn)
  277. {
  278. ReInit(dstream, startline, startcolumn, 4096);
  279. }
  280. public void ReInit(java.io.Reader dstream)
  281. {
  282. ReInit(dstream, 1, 1, 4096);
  283. }
  284. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  285. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  286. {
  287. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  288. }
  289. public SimpleCharStream(java.io.InputStream dstream, int startline,
  290. int startcolumn, int buffersize)
  291. {
  292. this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  293. }
  294. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  295. int startcolumn) throws java.io.UnsupportedEncodingException
  296. {
  297. this(dstream, encoding, startline, startcolumn, 4096);
  298. }
  299. public SimpleCharStream(java.io.InputStream dstream, int startline,
  300. int startcolumn)
  301. {
  302. this(dstream, startline, startcolumn, 4096);
  303. }
  304. public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  305. {
  306. this(dstream, encoding, 1, 1, 4096);
  307. }
  308. public SimpleCharStream(java.io.InputStream dstream)
  309. {
  310. this(dstream, 1, 1, 4096);
  311. }
  312. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  313. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  314. {
  315. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  316. }
  317. public void ReInit(java.io.InputStream dstream, int startline,
  318. int startcolumn, int buffersize)
  319. {
  320. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  321. }
  322. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  323. {
  324. ReInit(dstream, encoding, 1, 1, 4096);
  325. }
  326. public void ReInit(java.io.InputStream dstream)
  327. {
  328. ReInit(dstream, 1, 1, 4096);
  329. }
  330. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  331. int startcolumn) throws java.io.UnsupportedEncodingException
  332. {
  333. ReInit(dstream, encoding, startline, startcolumn, 4096);
  334. }
  335. public void ReInit(java.io.InputStream dstream, int startline,
  336. int startcolumn)
  337. {
  338. ReInit(dstream, startline, startcolumn, 4096);
  339. }
  340. public String GetImage()
  341. {
  342. if (bufpos >= tokenBegin)
  343. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  344. else
  345. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  346. new String(buffer, 0, bufpos + 1);
  347. }
  348. public char[] GetSuffix(int len)
  349. {
  350. char[] ret = new char[len];
  351. if ((bufpos + 1) >= len)
  352. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  353. else
  354. {
  355. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  356. len - bufpos - 1);
  357. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  358. }
  359. return ret;
  360. }
  361. public void Done()
  362. {
  363. buffer = null;
  364. bufline = null;
  365. bufcolumn = null;
  366. }
  367. /**
  368. * Method to adjust line and column numbers for the start of a token.
  369. */
  370. public void adjustBeginLineColumn(int newLine, int newCol)
  371. {
  372. int start = tokenBegin;
  373. int len;
  374. if (bufpos >= tokenBegin)
  375. {
  376. len = bufpos - tokenBegin + inBuf + 1;
  377. }
  378. else
  379. {
  380. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  381. }
  382. int i = 0, j = 0, k = 0;
  383. int nextColDiff = 0, columnDiff = 0;
  384. while (i < len &&
  385. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  386. {
  387. bufline[j] = newLine;
  388. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  389. bufcolumn[j] = newCol + columnDiff;
  390. columnDiff = nextColDiff;
  391. i++;
  392. }
  393. if (i < len)
  394. {
  395. bufline[j] = newLine++;
  396. bufcolumn[j] = newCol + columnDiff;
  397. while (i++ < len)
  398. {
  399. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  400. bufline[j] = newLine++;
  401. else
  402. bufline[j] = newLine;
  403. }
  404. }
  405. line = bufline[j];
  406. column = bufcolumn[j];
  407. }
  408. }