Scanner.java 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. /* java.util.Scanner -- Parses primitive types and strings using regexps
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import java.io.BufferedInputStream;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.FileNotFoundException;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.math.BigDecimal;
  39. import java.math.BigInteger;
  40. import java.nio.ByteBuffer;
  41. import java.nio.CharBuffer;
  42. import java.nio.channels.ReadableByteChannel;
  43. import java.text.DecimalFormat;
  44. import java.text.DecimalFormatSymbols;
  45. import java.text.NumberFormat;
  46. import java.text.ParseException;
  47. import java.util.Iterator;
  48. import java.util.Locale;
  49. import java.util.regex.MatchResult;
  50. import java.util.regex.Matcher;
  51. import java.util.regex.Pattern;
  52. /**
  53. * @author E0327023 Hernadi Laszlo
  54. */
  55. public class Scanner
  56. implements Iterator <String>
  57. {
  58. private static final String NOT_LONG = "\" is not a long"; //$NON-NLS-1$
  59. private static final String ERR_PREFIX = "\""; //$NON-NLS-1$
  60. private static final String NOT_INT = "\" is not an integer"; //$NON-NLS-1$
  61. private static final String NOT_DOUBLE = "\" is not a double"; //$NON-NLS-1$
  62. private static final String NOT_BYTE = "\" is not a byte"; //$NON-NLS-1$
  63. private static final String NOT_BOOLEAN = "\" is not a boolean"; //$NON-NLS-1$
  64. private static final String IS_NOT = "\" is not "; //$NON-NLS-1$
  65. private static final String DEFAULT_PATTERN_S = "\\p{javaWhitespace}+"; //$NON-NLS-1$
  66. private static final Pattern DEFAULT_PATTERN =
  67. Pattern.compile (DEFAULT_PATTERN_S);
  68. private static final String BIG_INTEGER = "BigInteger"; //$NON-NLS-1$
  69. private final static String NEW_LINE =
  70. System.getProperty ("line.separator");
  71. private IOException lastIOException = null;
  72. /**
  73. * An InputStream source if a Constructor with an InputStream source is called, otherwise it
  74. * stays <source> null </source>.
  75. */
  76. private InputStream bIS = null;
  77. /**
  78. * Length of the input Buffer, which is the maximum bytes to be read at once.
  79. */
  80. private final int MaxBufferLen = 1000000;
  81. /**
  82. * Minimum buffer length. If there are less chars in the Buffer than this value reading from
  83. * source is tried.
  84. */
  85. private final int MIN_BUF_LEN = 100;
  86. /**
  87. * Maximum number of processed chars in the Buffer. If exeeded, all processed chars from the
  88. * beginning of the Buffer will be discarded to save space. The bytes left are copyed into a new
  89. * Buffer.
  90. */
  91. private final int MAX_PREFIX = 10000;
  92. /**
  93. * The Buffer which is used by the Matcher to find given patterns. It is filled up when matcher
  94. * hits end or <code> MIN_BUF_LEN </code> is reached.
  95. */
  96. private String actBuffer = new String ();
  97. /**
  98. * The current radix to use by the methods getNextXXX and hasNextXXX.
  99. */
  100. private int currentRadix = 10;
  101. /**
  102. * The current locale.
  103. *
  104. * @see #useLocale(Locale)
  105. * @see #locale()
  106. */
  107. private Locale actLocale = Locale.getDefault ();
  108. /**
  109. * The current pattern for the matcher.
  110. */
  111. private Pattern p = DEFAULT_PATTERN;
  112. /**
  113. * The current position in the Buffer, at which the next match should start.
  114. */
  115. private int actPos = 0;
  116. /**
  117. * A global buffer to save new allocations by reading from source.
  118. */
  119. private final byte[] tmpBuffer = new byte[this.MaxBufferLen];
  120. /**
  121. * The charsetName to use with the source.
  122. */
  123. private String charsetName = null;
  124. /**
  125. * The Matcher which is used.
  126. */
  127. private Matcher myMatcher = this.p.matcher (this.actBuffer);
  128. /**
  129. * The MatchResult is generated at each match, even if match() isn't called.
  130. */
  131. private MatchResult actResult = null;
  132. /**
  133. * A Readable source if a Constructor with a Readable source is called, otherwise it stays
  134. * <source> null </source>.
  135. */
  136. private Readable readableSource = null;
  137. /**
  138. * A ReadableByteChannel source if a Constructor with a ReadableByteChannel source is called,
  139. * otherwise it stays <source> null </source>.
  140. */
  141. private ReadableByteChannel rbcSource = null;
  142. /**
  143. * Indicates if the close() method was called.
  144. */
  145. private boolean isClosed = false;
  146. /**
  147. * For performance reasons the last Found is saved, if a hasNextXXX method was called.
  148. */
  149. private String lastFound = null;
  150. private boolean lastFoundPresent = false;
  151. private int lastNextPos = 0;
  152. private int lastPatternHash = 0;
  153. private int last_RegionStart = 0;
  154. private int last_RegionEnd = 0;
  155. private boolean last_anchor = false;
  156. private boolean last_transparent = false;
  157. private MatchResult lastResult = null;
  158. /**
  159. * To keep track of the current position in the stream for the toString method, each time
  160. * processed chars are removed the amount is added to processedChars.
  161. */
  162. private int procesedChars = 0;
  163. /**
  164. * needInput is set <code> true </code> before a read method, and if there is no input it blocks
  165. * and stays <code>true</code>. Right after a read it is set to <code>false</code>.
  166. */
  167. private boolean needInput = false;
  168. private boolean skipped = false;
  169. /**
  170. * <code> {@link #doSkipp} </code> indicates that the found pattern belongs to the result. If
  171. * <code> {@link #doSkipp} </code> is false the match result ends at the beginning of the match.
  172. * In both cases the current position is set after the pattern, if the found pattern has to be
  173. * removed, a nextXXX method is called.
  174. */
  175. private boolean doSkipp = false;
  176. /**
  177. * Indicates if the last match was valid or not.
  178. */
  179. private boolean matchValid = false;
  180. private NumberFormat actFormat = NumberFormat.getInstance (this.actLocale);
  181. private DecimalFormat df = (DecimalFormat) this.actFormat;
  182. /**
  183. * Indicates if current Locale should be used at the input.
  184. */
  185. private boolean useLocale = true;
  186. private DecimalFormatSymbols dfs =
  187. new DecimalFormatSymbols (this.actLocale);
  188. /**
  189. * Constructs a new Scanner with the given File as source.
  190. * {@link #Scanner(InputStream, String)} is called with <code> null </code> as charsetName.
  191. *
  192. * @param source
  193. * The File to use as source.
  194. * @throws FileNotFoundException
  195. * If the file is not found an Exception is thrown.
  196. */
  197. public Scanner (final File source) throws FileNotFoundException // TESTED
  198. {
  199. this (source, null);
  200. }
  201. /**
  202. * Constructs a new Scanner with the given File as source. <br>
  203. * {@link #Scanner(InputStream, String)} is called with the given charsetName.
  204. *
  205. * @param source
  206. * The File to use as source.
  207. * @param charsetName
  208. * Current charset name of the file. If charsetName is null it behaves if it was not
  209. * set.
  210. * @throws FileNotFoundException
  211. * If the file is not found an Exception is thrown.
  212. */
  213. public Scanner (final File source,
  214. final String charsetName) throws FileNotFoundException
  215. {
  216. this (new FileInputStream (source), charsetName);
  217. }
  218. /**
  219. * Constructs a new Scanner with the given inputStream. <br>
  220. * {@link #Scanner(InputStream, String)} is called with <code> null </code> as charsetName.
  221. *
  222. * @param source
  223. * The InputStream to use as source.
  224. */
  225. public Scanner (final InputStream source) // TESTED
  226. {
  227. this (source, null);
  228. }
  229. /**
  230. * Constructs a new Scanner with the InputSream and a charsetName. Afterwards the Buffer is
  231. * filled.
  232. *
  233. * @param source
  234. * The InputStream to use as source.
  235. * @param charsetName
  236. * The charsetName to apply on the source's data.
  237. */
  238. public Scanner (final InputStream source, final String charsetName)
  239. {
  240. this.bIS = (new BufferedInputStream (source));
  241. this.charsetName = charsetName;
  242. myFillBuffer ();
  243. }
  244. /**
  245. * Constructs a new Scanner with a Readable input as source.
  246. *
  247. * @param source
  248. * The Readable to use as source.
  249. */
  250. public Scanner (final Readable source)
  251. {
  252. this.readableSource = source;
  253. myFillBuffer ();
  254. }
  255. /**
  256. * Constructs a new Scanner with a ReadableByteChannel as
  257. * source. Therfore the {@link #Scanner(ReadableByteChannel,
  258. * String)} is called with <code> null </code> as charsetName.
  259. *
  260. * @param source
  261. * The ReadableByteChannel to use as source.
  262. */
  263. public Scanner (final ReadableByteChannel source)
  264. {
  265. this (source, null);
  266. }
  267. /**
  268. * Constructs a new Scanner with a ReadableByteChannel as source and
  269. * a given charsetName, which is to be applied on it. <br> It also
  270. * initiates the main Buffer.
  271. *
  272. * @param source
  273. * The ReadableByteChannel to use as source.
  274. * @param charsetName
  275. * The charsetName to be applied on the source.
  276. */
  277. public Scanner (final ReadableByteChannel source, final String charsetName)
  278. {
  279. this.charsetName = charsetName;
  280. this.rbcSource = source;
  281. myFillBuffer ();
  282. }
  283. /**
  284. * Constructs a new Scanner using the given String as input only.
  285. *
  286. * @param source
  287. * The whole String to be used as source.
  288. */
  289. public Scanner (final String source) // TESTED
  290. {
  291. this.actBuffer = new String (source);
  292. this.myMatcher.reset (this.actBuffer);
  293. }
  294. /**
  295. * Closes this Scanner. If an {@link IOException} occurs it is
  296. * catched and is available under {@link #ioException()}.<br> After
  297. * the Scanner is closed, all searches will lead to a {@link
  298. * IllegalStateException}.
  299. */
  300. public void close ()
  301. {
  302. try
  303. {
  304. if (this.bIS != null)
  305. this.bIS.close ();
  306. if (this.rbcSource != null)
  307. this.rbcSource.close ();
  308. this.isClosed = true;
  309. }
  310. catch (IOException ioe)
  311. {
  312. this.lastIOException = ioe;
  313. }
  314. }
  315. /**
  316. * Returns the current delimiter.
  317. *
  318. * @return the current delimiter.
  319. */
  320. public Pattern delimiter () // TESTED
  321. {
  322. return this.p;
  323. }
  324. /**
  325. * Tries to find the pattern in the current line.
  326. *
  327. * @param pattern The pattern which should be searched in the
  328. * current line of the input.
  329. * @throws NoSuchElementException
  330. * If the pattern was not found.
  331. * @return If the search was successful, the result or otherwise a
  332. * {@link NoSuchElementException} is thrown.
  333. */
  334. public String findInLine (final Pattern pattern) throws NoSuchElementException // TESTED
  335. {
  336. String tmpStr = myNextLine (false);
  337. return myFindPInStr (pattern, tmpStr, 0);
  338. }
  339. /**
  340. * Compiles the given pattern into a {@link Pattern} and calls
  341. * {@link #findInLine(Pattern)} with the compiled pattern and
  342. * returns whatever it returns.
  343. *
  344. * @param pattern
  345. * The pattern which should be matched in the input.
  346. * @throws NoSuchElementException
  347. * If the pattern was not found.
  348. * @return The match in the current line.
  349. */
  350. public String findInLine (final String pattern) // TESTED
  351. {
  352. return findInLine (Pattern.compile (pattern));
  353. }
  354. /**
  355. * Trys to match the pattern within the given horizon.
  356. *
  357. * @param pattern
  358. * Pattern to search.
  359. * @param horizon
  360. * @return The result of the match.
  361. * @throws IllegalArgumentException
  362. * if the horizon is negative.
  363. * @throws IllegalStateException
  364. * if the Scanner is closed.
  365. */
  366. public String findWithinHorizon (final Pattern pattern, final int horizon)
  367. throws IllegalArgumentException, IllegalStateException
  368. {
  369. if (horizon < 0)
  370. {
  371. throw new IllegalArgumentException (horizon + " is negative");
  372. }
  373. if (this.isClosed)
  374. {
  375. throw new IllegalStateException ("Scanner is closed");
  376. }
  377. // doSkipp is set true to get the matching patern together with the found String
  378. this.doSkipp = true;
  379. String rc = myFindPInStr (pattern, this.actBuffer, horizon);
  380. if (rc != null)
  381. {
  382. this.actPos += rc.length ();
  383. }
  384. return rc;
  385. }
  386. /**
  387. * Compile the pattern and call {@link #findWithinHorizon(Pattern,
  388. * int)}.
  389. *
  390. * @param pattern
  391. * Pattern to search.
  392. * @param horizon
  393. * @return The result of the match.
  394. * @throws IllegalArgumentException
  395. * if the horizon is negative.
  396. * @throws IllegalStateException
  397. * if the Scanner is closed.
  398. */
  399. public String findWithinHorizon (final String pattern, final int horizon)
  400. throws IllegalArgumentException, IllegalStateException
  401. {
  402. return findWithinHorizon (Pattern.compile (pattern), horizon);
  403. }
  404. /**
  405. * Checks if there is any next String using the current
  406. * delimiter. Therefore the string must not be <code> null </code>
  407. * and the length must be greater then 0. If a {@link
  408. * NoSuchElementException} is thrown by the search method, it is
  409. * catched and false is returned.
  410. *
  411. * @return <code> true </code> if there is any result using the current delimiter. This wouldn't
  412. * lead to a {@link NoSuchElementException}.
  413. * @throws IllegalStateException
  414. * if the Scanner is closed.
  415. */
  416. public boolean hasNext () throws IllegalStateException // TESTED
  417. {
  418. String tmpStr = null;
  419. try
  420. {
  421. tmpStr = myCoreNext (false, this.p);
  422. }
  423. catch (NoSuchElementException nf)
  424. {
  425. }
  426. if (tmpStr == null || tmpStr.length () <= 0)
  427. {
  428. return false;
  429. }
  430. return true;
  431. }
  432. /**
  433. * Searches the pattern in the next subString before the next
  434. * current delimiter.
  435. *
  436. * @param pattern
  437. * The pattern to search for.
  438. * @return <code> true </code> if the pattern is found before the current delimiter.
  439. * @throws IllegalStateException
  440. * if the Scanner is closed.
  441. */
  442. public boolean hasNext (final Pattern pattern) throws IllegalStateException // TESTED
  443. {
  444. String tmpStr;
  445. tmpStr = myNext (pattern, false);
  446. if (tmpStr == null || tmpStr.length () <= 0)
  447. {
  448. return false;
  449. }
  450. return true;
  451. }
  452. /**
  453. * Compiles the pattern to a {@link Pattern} and calls {@link
  454. * #hasNext(Pattern)}.
  455. *
  456. * @see #hasNext(Pattern)
  457. * @param pattern
  458. * The pattern as string to search for.
  459. * @return <code> true </code> if the pattern is found before the current delimiter.
  460. * @throws IllegalStateException
  461. * if the Scanner is closed.
  462. */
  463. public boolean hasNext (final String pattern) throws IllegalStateException // TESTED
  464. {
  465. return hasNext (Pattern.compile (pattern));
  466. }
  467. /**
  468. * Checks if the string to the next delimiter can be interpreted as
  469. * a BigDecimal number. <br> BigDecimal numbers are always tryed
  470. * with radix 10.
  471. *
  472. * @see #nextBigDecimal()
  473. * @return <code> true </code> if the next string is a BigDecimal number.
  474. * @throws IllegalStateException
  475. * if the Scanner is closed.
  476. */
  477. public boolean hasNextBigDecimal () throws IllegalStateException // TESTED
  478. {
  479. try
  480. {
  481. myBigDecimal (false);
  482. return true;
  483. }
  484. catch (InputMismatchException nfe)
  485. {
  486. return false;
  487. }
  488. }
  489. /**
  490. * Checks if the string to the next delimiter can be interpreted as
  491. * a BigInteger number. <br> Call {@link #hasNextBigInteger(int)}
  492. * with the current radix.
  493. *
  494. * @see #nextBigInteger()
  495. * @return <code> true </code> if the next string is a BigInteger number.
  496. * @throws IllegalStateException
  497. * if the Scanner is closed.
  498. */
  499. public boolean hasNextBigInteger () throws IllegalStateException // TESTED
  500. {
  501. return hasNextBigInteger (this.currentRadix);
  502. }
  503. /**
  504. * Checks if the string to the next delimiter can be interpreted as
  505. * a BigInteger number. <br>
  506. *
  507. * @param radix
  508. * The radix to use for this check. The global radix of the Scanner will not be
  509. * changed.
  510. * @return <code> true </code> if the next string is a BigInteger number.
  511. * @throws IllegalStateException
  512. * if the Scanner is closed.
  513. */
  514. public boolean hasNextBigInteger (final int radix) throws
  515. IllegalStateException
  516. {
  517. try
  518. {
  519. myNextBigInteger (radix, false, BIG_INTEGER);
  520. return true;
  521. }
  522. catch (InputMismatchException ime)
  523. {
  524. return false;
  525. }
  526. }
  527. /**
  528. * Checks if the next string could be a boolean. The method handles
  529. * the input not case sensitiv, so "true" and "TRUE" and even "tRuE"
  530. * are <code> true </code>.
  531. *
  532. * @see #nextBoolean()
  533. * @return Return <code> true </code> if the next string is a boolean.
  534. * @throws IllegalStateException
  535. * if the Scanner is closed.
  536. */
  537. public boolean hasNextBoolean () throws IllegalStateException // TESTED
  538. {
  539. try
  540. {
  541. myNextBoolean (false);
  542. return true;
  543. }
  544. catch (InputMismatchException ime)
  545. {
  546. return false;
  547. }
  548. }
  549. /**
  550. * Checks if the string to the next delimiter can be interpreted as
  551. * a byte number. <br> Calls {@link #hasNextByte(int)} with the
  552. * current radix.
  553. *
  554. * @see #nextByte()
  555. * @return <code> true </code> if the next string is a byte number.
  556. * @throws IllegalStateException
  557. * if the Scanner is closed.
  558. */
  559. public boolean hasNextByte () throws IllegalStateException // TESTED
  560. {
  561. return hasNextByte (this.currentRadix);
  562. }
  563. /**
  564. * Checks if the string to the next delimiter can be interpreted as
  565. * a byte number with the given radix. <br> To check, the private
  566. * method {@link #myNextByte(int, boolean)} is called, and if no
  567. * error occurs the next string could be a byte.
  568. *
  569. * @see #nextByte(int)
  570. * @param radix The radix to use for this check. The global radix of
  571. * the Scanner will not be changed.
  572. * @return <code> true </code> if the next string is a byte number.
  573. * @throws IllegalStateException
  574. * if the Scanner is closed.
  575. */
  576. public boolean hasNextByte (final int radix) throws IllegalStateException
  577. {
  578. try
  579. {
  580. myNextByte (radix, false);
  581. return true;
  582. }
  583. catch (InputMismatchException ime)
  584. {
  585. return false;
  586. }
  587. }
  588. /**
  589. * Checks if the string to the next delimiter can be interpreted as
  590. * a double number. <br> To check, the private method {@link
  591. * #myNextDouble(boolean)} is called, and if no error occurs the
  592. * next string could be a double.
  593. *
  594. * @see #nextDouble()
  595. * @return <code> true </code> if the next string is a double number.
  596. * @throws IllegalStateException
  597. * if the Scanner is closed.
  598. */
  599. public boolean hasNextDouble () throws IllegalStateException // TESTED
  600. {
  601. try
  602. {
  603. myNextDouble (false);
  604. return true;
  605. }
  606. catch (InputMismatchException ime)
  607. {
  608. return false;
  609. }
  610. }
  611. /**
  612. * Checks if the string to the next delimiter can be interpreted as
  613. * a double number. Because every float is a double this is
  614. * checked.<br> To check, the private method {@link
  615. * #myNextDouble(boolean)} is called, and if no error occurs the
  616. * next string could be a double.
  617. *
  618. * @see #nextFloat()
  619. * @return <code> true </code> if the next string is a double number.
  620. * @throws IllegalStateException
  621. * if the Scanner is closed.
  622. */
  623. public boolean hasNextFloat () throws IllegalStateException // TESTED
  624. {
  625. try
  626. {
  627. myNextDouble (false);
  628. // myNextFloat(false);
  629. return true;
  630. }
  631. catch (InputMismatchException ime)
  632. {
  633. return false;
  634. }
  635. }
  636. /**
  637. * Checks if the string to the next delimiter can be interpreted as
  638. * an int number. <br> To check, the private method {@link
  639. * #myNextInt(int, boolean)} is called, and if no error occurs the
  640. * next string could be an int.
  641. *
  642. * @see #nextInt(int)
  643. * @return <code> true </code> if the next string is an int number.
  644. * @throws IllegalStateException
  645. * if the Scanner is closed.
  646. */
  647. public boolean hasNextInt () throws IllegalStateException // TESTED
  648. {
  649. return hasNextInt (this.currentRadix);
  650. }
  651. /**
  652. * Checks if the string to the next delimiter can be interpreted as
  653. * an int number with the given radix. <br> To check, the private
  654. * method {@link #myNextInt(int, boolean)} is called, and if no
  655. * error occurs the next string could be an int.
  656. *
  657. * @see #nextInt(int)
  658. * @param radix
  659. * The radix to use for this check. The global radix of the Scanner will not be
  660. * changed.
  661. * @return <code> true </code> if the next string is an int number.
  662. * @throws IllegalStateException
  663. * if the Scanner is closed.
  664. */
  665. public boolean hasNextInt (final int radix) throws IllegalStateException
  666. {
  667. try
  668. {
  669. myNextInt (radix, false);
  670. return true;
  671. }
  672. catch (InputMismatchException ime)
  673. {
  674. return false;
  675. }
  676. }
  677. /**
  678. * Checks if there is a current line, which ends at the next line
  679. * break or the end of the input.
  680. *
  681. * @return <code> true </code> if there is a current line.
  682. * @throws IllegalStateException
  683. * if the Scanner is closed.
  684. */
  685. public boolean hasNextLine () throws IllegalStateException // TESTED
  686. {
  687. return (myNextLine (false) != null);
  688. }
  689. /**
  690. * Checks if the string to the next delimiter can be interpreted as
  691. * a long number. <br> To check, the private method {@link
  692. * #myNextLong(int, boolean)} is called, and if no error occurs the
  693. * next string could be a long.
  694. *
  695. * @see #nextLong()
  696. * @return <code> true </code> if the next string is a long number.
  697. * @throws IllegalStateException
  698. * if the Scanner is closed.
  699. */
  700. public boolean hasNextLong () throws IllegalStateException // TESTED
  701. {
  702. return hasNextLong (this.currentRadix);
  703. }
  704. /**
  705. * Checks if the string to the next delimiter can be interpreted as
  706. * a long number with the given radix. <br> To check, the private
  707. * method {@link #myNextLong(int, boolean)} is called, and if no
  708. * error occurs the next string could be a long.
  709. *
  710. * @see #nextLong(int)
  711. * @param radix
  712. * The radix to use for this check. The global radix of the Scanner will not be
  713. * changed.
  714. * @return <code> true </code> if the next string is a long number.
  715. * @throws IllegalStateException
  716. * if the Scanner is closed.
  717. */
  718. public boolean hasNextLong (final int radix) throws IllegalStateException
  719. {
  720. try
  721. {
  722. myNextLong (radix, false);
  723. return true;
  724. }
  725. catch (InputMismatchException ime)
  726. {
  727. return false;
  728. }
  729. }
  730. /**
  731. * Checks if the string to the next delimiter can be interpreted as
  732. * a short number with the given radix. <br> To check, the private
  733. * method {@link #myNextShort(int, boolean)} is called, and if no
  734. * error occurs the next string could be a short.
  735. *
  736. * @see #nextShort(int)
  737. * @return <code> true </code> if the next string is a short number.
  738. * @throws IllegalStateException
  739. * if the Scanner is closed.
  740. */
  741. public boolean hasNextShort () throws IllegalStateException // TESTED
  742. {
  743. return hasNextShort (this.currentRadix);
  744. }
  745. /**
  746. * Checks if the string to the next delimiter can be interpreted as
  747. * a short number. <br> To check, the private method {@link
  748. * #myNextShort(int, boolean)} is called, and if no error occurs the
  749. * next string could be a short.
  750. *
  751. * @see #nextShort(int)
  752. * @param radix
  753. * The radix to use for this check. The global radix of the Scanner will not be
  754. * changed.
  755. * @return <code> true </code> if the next string is a short number.
  756. * @throws IllegalStateException
  757. * if the Scanner is closed.
  758. */
  759. public boolean hasNextShort (final int radix) throws IllegalStateException
  760. {
  761. try
  762. {
  763. myNextShort (radix, false);
  764. return true;
  765. }
  766. catch (InputMismatchException ime)
  767. {
  768. return false;
  769. }
  770. }
  771. /**
  772. * Returns the last {@link IOException} occured.
  773. *
  774. * @return Returns the last {@link IOException}.
  775. */
  776. public IOException ioException ()
  777. {
  778. return this.lastIOException;
  779. }
  780. /**
  781. * Returns the current value of {@link #useLocale}. This is used to
  782. * tell the Scanner if it should use the Locale format or just
  783. * handle numbers of the default format.
  784. *
  785. * @see #setUseLocale(boolean)
  786. * @return the useLoclae.
  787. */
  788. public boolean isUseLocale () // TESTED
  789. {
  790. return this.useLocale;
  791. }
  792. /**
  793. * Returns the current Locale. It is initialized with {@link
  794. * Locale#getDefault()}.
  795. *
  796. * @see #useLocale(Locale)
  797. * @return Returns the current Locale.
  798. */
  799. public Locale locale () // TESTED
  800. {
  801. return this.actLocale;
  802. }
  803. /**
  804. * Returns the last MatchResult found. This is updated after every
  805. * successfully search.
  806. *
  807. * @return Returns the last {@link MatchResult} found.
  808. */
  809. public MatchResult match () // TESTED
  810. {
  811. return this.actResult;
  812. }
  813. /**
  814. * Uses the current delimiter to find the next string in the
  815. * buffer. If a string is found the current position is set after
  816. * the delimiter, otherwise a {@link NoSuchElementException} is
  817. * thrown. A successful match sets the matchResult.
  818. *
  819. * @see #match()
  820. * @return Returns the next string of the buffer.
  821. * @throws NoSuchElementException
  822. * If no element was found an exception is thrown.
  823. * @throws IllegalStateException
  824. * If the Scanner is closed.
  825. */
  826. public String next () throws NoSuchElementException, IllegalStateException // TESTED
  827. {
  828. return myCoreNext (true, this.p);
  829. }
  830. /**
  831. * Tries to match the buffer with the given pattern. The current
  832. * delimiter will not be changed.
  833. *
  834. * @param pattern
  835. * The pattern to match.
  836. * @return Returns the next string matching the pattern.
  837. * @throws NoSuchElementException
  838. * If no element was found an exception is thrown.
  839. * @throws IllegalStateException
  840. * If the Scanner is closed.
  841. */
  842. public String next (final Pattern pattern) throws NoSuchElementException, IllegalStateException // TESTED
  843. {
  844. return myNext (pattern, true);
  845. }
  846. /**
  847. * Tries to match the buffer with the given pattern. The current
  848. * delimiter will not be changed. Calls the {@link #next(Pattern)}
  849. * with the compiled pattern.
  850. *
  851. * @see #next(Pattern)
  852. * @param pattern
  853. * The pattern to match.
  854. * @return Returns the next string matching the pattern.
  855. * @throws NoSuchElementException
  856. * If no element was found an exception is thrown.
  857. * @throws IllegalStateException
  858. * If the Scanner is closed.
  859. */
  860. public String next (final String pattern) throws NoSuchElementException, IllegalStateException // TESTED
  861. {
  862. return next (Pattern.compile (pattern));
  863. }
  864. /**
  865. * Tries to interpret the next string as a BigDecimal value.
  866. *
  867. * @return Returns the BigDecimal value of the next string.
  868. * @throws NoSuchElementException
  869. * If no string is found or the string is not a BigDecimal.
  870. * @throws IllegalStateException
  871. * If the Scanner is closed.
  872. */
  873. public BigDecimal nextBigDecimal () throws NoSuchElementException, IllegalStateException // TESTED
  874. {
  875. return myBigDecimal (true);
  876. }
  877. /**
  878. * Tries to interpret the next string as a BigInteger value. Call
  879. * {@link #nextBigInteger(int)} with the current radix as parameter,
  880. * and return the value.
  881. *
  882. * @see #nextBigInteger(int)
  883. * @return Returns the BigInteger value of the next string.
  884. * @throws NoSuchElementException
  885. * If no string is found or the string is not a BigInteger.
  886. * @throws IllegalStateException
  887. * If the Scanner is closed.
  888. */
  889. public BigInteger nextBigInteger () throws NoSuchElementException, IllegalStateException // TESTED
  890. {
  891. return nextBigInteger (this.currentRadix);
  892. }
  893. /**
  894. * Tries to interpret the next string as a BigInteger value with the
  895. * given radix.
  896. *
  897. * @param radix
  898. * The radix to be used for this BigInteger. The current radix of the Scanner is not
  899. * changed.
  900. * @return Returns the BigInteger value of the next string.
  901. * @throws NoSuchElementException
  902. * If no string is found or the string is not a BigInteger.
  903. * @throws IllegalStateException
  904. * If the Scanner is closed.
  905. */
  906. public BigInteger nextBigInteger (final int radix) throws
  907. NoSuchElementException, IllegalStateException
  908. {
  909. return myNextBigInteger (radix, true, BIG_INTEGER);
  910. }
  911. /**
  912. * Tries to interpret the next string to the delimiter as a boolean
  913. * value, ignoring case.
  914. *
  915. * @return Returns the boolean value of the next matching string or throws an exception.
  916. * @throws NoSuchElementException
  917. * If no string is found or the string is not a boolean.
  918. * @throws IllegalStateException
  919. * If the Scanner is closed.
  920. */
  921. public boolean nextBoolean () throws NoSuchElementException, IllegalStateException // TESTED
  922. {
  923. return myNextBoolean (true);
  924. }
  925. /**
  926. * Tries to interpret the next string as a byte value. Call {@link
  927. * #nextByte(int)} with the current radix as parameter, and return
  928. * the value.
  929. *
  930. * @see #nextByte(int)
  931. * @return Returns the byte value of the next string.
  932. * @throws NoSuchElementException
  933. * If no string is found or the string is not a byte
  934. * @throws IllegalStateException
  935. * If the Scanner is closed.
  936. */
  937. public byte nextByte () throws NoSuchElementException, IllegalStateException // TESTED
  938. {
  939. return nextByte (this.currentRadix);
  940. }
  941. /**
  942. * Tries to interpret the next string as a byte value with the given
  943. * radix.
  944. *
  945. * @param radix
  946. * The radix to be used for this byte. The current radix of the Scanner is not
  947. * changed.
  948. * @return Returns the byte value of the next string.
  949. * @throws NoSuchElementException
  950. * If no string is found or the string is not a byte.
  951. * @throws IllegalStateException
  952. * If the Scanner is closed.
  953. */
  954. public byte nextByte (final int radix) throws NoSuchElementException,
  955. IllegalStateException
  956. {
  957. return myNextByte (radix, true);
  958. }
  959. /**
  960. * Tries to interpret the next string as a double value.
  961. *
  962. * @return Returns the int value of the next string.
  963. * @throws NoSuchElementException
  964. * If no string is found or the string is not a double.
  965. * @throws IllegalStateException
  966. * If the Scanner is closed.
  967. */
  968. public double nextDouble () throws NoSuchElementException, IllegalStateException // TESTED
  969. {
  970. return myNextDouble (true);
  971. }
  972. /**
  973. * Tries to interpret the next string as a double value, and then
  974. * casts down to float.
  975. *
  976. * @return Returns the int value of the next string.
  977. * @throws NoSuchElementException
  978. * If no string is found or the string is not a double.
  979. * @throws IllegalStateException
  980. * If the Scanner is closed.
  981. */
  982. public float nextFloat () throws NoSuchElementException, IllegalStateException // TESTED
  983. {
  984. return (float) myNextDouble (true);
  985. // return myNextFloat(true);
  986. }
  987. /**
  988. * Tries to interpret the next string as an int value. Calls {@link
  989. * #nextInt(int)} with the current radix as parameter, and return
  990. * the value.
  991. *
  992. * @see #nextInt(int)
  993. * @return Returns the int value of the next string.
  994. * @throws NoSuchElementException
  995. * If no string is found or the string is not an int.
  996. * @throws IllegalStateException
  997. * If the Scanner is closed.
  998. */
  999. public int nextInt () throws NoSuchElementException, IllegalStateException // TESTED
  1000. {
  1001. return nextInt (this.currentRadix);
  1002. }
  1003. /**
  1004. * Tries to interpret the next string as an int value with the given
  1005. * radix.
  1006. *
  1007. * @param radix
  1008. * The radix to be used for this int. The current radix of the Scanner is not changed
  1009. * @return Returns the int value of the next string.
  1010. * @throws NoSuchElementException
  1011. * If no string is found or the string is not an int.
  1012. * @throws IllegalStateException
  1013. * If the Scanner is closed.
  1014. */
  1015. public int nextInt (final int radix) throws NoSuchElementException,
  1016. IllegalStateException
  1017. {
  1018. return myNextInt (radix, true);
  1019. }
  1020. /**
  1021. * Tries to match the system line seperator, and returns the current
  1022. * line.
  1023. *
  1024. * @return Returns the current line.
  1025. * @throws NoSuchElementException
  1026. * If the current delimiter is not found.
  1027. * @throws IllegalStateException
  1028. * If the Scanner is closed.
  1029. */
  1030. public String nextLine () throws NoSuchElementException, IllegalStateException // TESTED
  1031. {
  1032. return myNextLine (true);
  1033. }
  1034. /**
  1035. * Tries to interpret the next string as a long value. Calls {@link
  1036. * #nextLong(int)} with the current radix as parameter, and return
  1037. * the value.
  1038. *
  1039. * @see #nextLong(int)
  1040. * @return Returns the long value of the next string.
  1041. * @throws NoSuchElementException
  1042. * If no string is found or the string is not a long.
  1043. * @throws IllegalStateException
  1044. * If the Scanner is closed.
  1045. */
  1046. public long nextLong () throws NoSuchElementException, IllegalStateException // TESTED
  1047. {
  1048. return nextLong (this.currentRadix);
  1049. }
  1050. /**
  1051. * Tries to interpret the next string as a long value with the given
  1052. * radix.
  1053. *
  1054. * @param radix
  1055. * The radix to be used for this long. The current radix of the Scanner is not
  1056. * changed
  1057. * @return Returns the long value of the next string.
  1058. * @throws NoSuchElementException
  1059. * If no string is found or the string is not a long.
  1060. * @throws IllegalStateException
  1061. * If the Scanner is closed.
  1062. */
  1063. public long nextLong (final int radix) throws NoSuchElementException,
  1064. IllegalStateException
  1065. {
  1066. return myNextLong (radix, true);
  1067. }
  1068. /**
  1069. * Tries to interpret the next string as a short value. Calls {@link
  1070. * #nextShort(int)} with the current radix as parameter, and return
  1071. * the value.
  1072. *
  1073. * @see #nextShort(int)
  1074. * @return Returns the short value of the next string.
  1075. * @throws NoSuchElementException
  1076. * If no string is found or the string is not a short.
  1077. */
  1078. public short nextShort () throws NoSuchElementException // TESTED
  1079. {
  1080. return nextShort (this.currentRadix);
  1081. }
  1082. /**
  1083. * Tries to interpret the next string as a short value with the
  1084. * given radix.
  1085. *
  1086. * @param radix
  1087. * The radix to be used for this short. The current radix of the Scanner is not
  1088. * changed.
  1089. * @return Returns the short value of the next string.
  1090. * @throws NoSuchElementException
  1091. * If no string is found or the string is not a short.
  1092. */
  1093. public short nextShort (final int radix) throws NoSuchElementException
  1094. {
  1095. return myNextShort (radix, true);
  1096. }
  1097. /**
  1098. * @return Returns the current radix.
  1099. */
  1100. public int radix ()
  1101. {
  1102. return this.currentRadix;
  1103. }
  1104. /**
  1105. * The remove operation is not supported by this implementation of
  1106. * Iterator.
  1107. */
  1108. public void remove ()
  1109. {
  1110. }
  1111. /**
  1112. * @param useLocale the useLocale to set.
  1113. */
  1114. public void setUseLocale (final boolean useLocale) // TESTED
  1115. {
  1116. this.useLocale = useLocale;
  1117. }
  1118. /**
  1119. * Skips the given pattern. Sets skipped <code>true</code>.
  1120. *
  1121. * @param pattern
  1122. * Pattern which should be skipped.
  1123. * @return <code>this</code> with the skipped buffer.
  1124. * @throws NoSuchElementException
  1125. * If the Pattern is not found.
  1126. */
  1127. public Scanner skip (final Pattern pattern) throws NoSuchElementException
  1128. {
  1129. this.doSkipp = true;
  1130. int end;
  1131. boolean found;
  1132. Matcher matcher = pattern.matcher (this.actBuffer);
  1133. matcher.region (this.actPos - 1, this.actBuffer.length ());
  1134. found = matcher.find ();
  1135. found = myFillBuffer_loop (matcher, this.actPos - 1, found);
  1136. end = matcher.end ();
  1137. this.actPos = end + 1;
  1138. this.doSkipp = false;
  1139. this.skipped = true;
  1140. actResult = null;
  1141. if (!found)
  1142. {
  1143. throw new NoSuchElementException ();
  1144. }
  1145. return this;
  1146. }
  1147. /**
  1148. * Skips a given pattern. Calls {@link #skip(Pattern)} with the
  1149. * compiled pattern.
  1150. *
  1151. * @see #skip(Pattern)
  1152. * @param pattern
  1153. * Pattern which should be skipped.
  1154. * @return <code>this</code> with the skipped buffer.
  1155. */
  1156. public Scanner skip (final String pattern)
  1157. {
  1158. return skip (Pattern.compile (pattern));
  1159. }
  1160. /**
  1161. * Returns the string representation of this Scanner.
  1162. */
  1163. @Override
  1164. public String toString ()
  1165. {
  1166. String tmpStr2;
  1167. String rc = this.getClass ().getName ();
  1168. tmpStr2 = rc;
  1169. tmpStr2 = "[delimiters=" + this.p.pattern () + "]";
  1170. rc += tmpStr2;
  1171. tmpStr2 = "[position=" + (this.procesedChars + this.actPos) + "]";
  1172. rc += tmpStr2;
  1173. tmpStr2 = "[match valid=" + this.matchValid + "]";
  1174. rc += tmpStr2;
  1175. tmpStr2 = "[need input=" + this.needInput + "]";
  1176. rc += tmpStr2;
  1177. tmpStr2 = "[source closed=" + this.isClosed + "]";
  1178. rc += tmpStr2;
  1179. tmpStr2 = "[skipped=" + this.skipped + "]";
  1180. rc += tmpStr2;
  1181. tmpStr2 = "[group separator=\\" + this.dfs.getGroupingSeparator () + "]";
  1182. rc += tmpStr2;
  1183. tmpStr2 = "[decimal separator=\\" + this.dfs.getDecimalSeparator () + "]";
  1184. rc += tmpStr2;
  1185. tmpStr2 =
  1186. "[positive prefix=" + myConvert (this.df.getPositivePrefix ()) + "]";
  1187. rc += tmpStr2;
  1188. tmpStr2 =
  1189. "[negative prefix=" + myConvert (this.df.getNegativePrefix ()) + "]";
  1190. rc += tmpStr2;
  1191. tmpStr2 =
  1192. "[positive suffix=" + myConvert (this.df.getPositiveSuffix ()) + "]";
  1193. rc += tmpStr2;
  1194. tmpStr2 =
  1195. "[negative suffix=" + myConvert (this.df.getNegativeSuffix ()) + "]";
  1196. rc += tmpStr2;
  1197. tmpStr2 = "[NaN string=" + myConvert (this.dfs.getNaN ()) + "]";
  1198. rc += tmpStr2;
  1199. tmpStr2 = "[infinity string=" + myConvert (this.dfs.getInfinity ()) + "]";
  1200. rc += tmpStr2;
  1201. return rc;
  1202. }
  1203. /**
  1204. * Sets the current pattern to the given parameter, and updates the
  1205. * {@link Matcher} with the new pattern.
  1206. *
  1207. * @param pattern
  1208. * The new pattern to use.
  1209. * @return Returns the Scanner (<code>this</code>) with the new pattern.
  1210. */
  1211. public Scanner useDelimiter (final Pattern pattern) // TESTED
  1212. {
  1213. if (pattern != null)
  1214. {
  1215. this.p = pattern;
  1216. this.myMatcher = this.p.matcher (this.actBuffer);
  1217. }
  1218. return this;
  1219. }
  1220. /**
  1221. * Sets the current pattern to the given parameter. Compiles the
  1222. * pattern and calls {@link #useDelimiter(Pattern)}
  1223. *
  1224. * @see #useDelimiter(Pattern)
  1225. * @param pattern
  1226. * The new pattern to use.
  1227. * @return Returns the Scanner (<code>this</code>) with the new pattern.
  1228. */
  1229. public Scanner useDelimiter (final String pattern) // TESTED
  1230. {
  1231. return useDelimiter (Pattern.compile (pattern));
  1232. }
  1233. /**
  1234. * Sets the current Locale to the given parameter. Formats and
  1235. * Symbols are also set using the new Locale.
  1236. *
  1237. * @param locale The new Locale to use. If it is <code>null</code>
  1238. * nothing happens.
  1239. * @return Returns the Scanner (<code>this</code>) with the new Locale.
  1240. */
  1241. public Scanner useLocale (final Locale locale) // TESTED
  1242. {
  1243. if (locale != null)
  1244. {
  1245. this.actLocale = locale;
  1246. this.actFormat = NumberFormat.getInstance (this.actLocale);
  1247. this.dfs = new DecimalFormatSymbols (this.actLocale);
  1248. this.df = (DecimalFormat) this.actFormat;
  1249. }
  1250. return this;
  1251. }
  1252. /**
  1253. * Sets the current radix to the current value if the given radix is
  1254. * >= 2 and <= 36 otherwise an {@link IllegalArgumentException} is
  1255. * thrown.
  1256. *
  1257. * @param radix
  1258. * the new radix to use as default.
  1259. * @return <code> this </code> with the new radix value.
  1260. * @throws IllegalArgumentException
  1261. * When the given radix is out of bounds.
  1262. */
  1263. public Scanner useRadix (final int radix) throws IllegalArgumentException
  1264. {
  1265. if (radix < 2 || radix > 36)
  1266. {
  1267. throw new IllegalArgumentException ();
  1268. }
  1269. this.currentRadix = radix;
  1270. return this;
  1271. }
  1272. /**
  1273. * Checks if it is necessary to apply the current Locale on the
  1274. * String. If so the String is converted using the {@link
  1275. * NumberFormat#parse(String)} into a Number and then back to a
  1276. * default stringrepresentation of that Number.
  1277. *
  1278. * @see #setUseLocale(boolean)
  1279. * @param str
  1280. * String to convert into another string.
  1281. * @param radix Radix of the Number in the original string. It has
  1282. * to be 10 for anything to happen.
  1283. * @return Eighter the Stringrepresention of the number without the
  1284. * Locale or an unchanged string.
  1285. * @throws ParseException
  1286. * if {@link NumberFormat#parse(String)} fails to parse.
  1287. */
  1288. private String myApplyLocale (final String str,
  1289. final int radix) throws ParseException
  1290. {
  1291. String rc;
  1292. if (this.useLocale && radix == 10)
  1293. {
  1294. rc = this.actFormat.parse (str).toString ();
  1295. return rc;
  1296. }
  1297. return str;
  1298. }
  1299. /**
  1300. * If {@link #useLocale} is set and radix is 10 the string is tryed
  1301. * to be converted to string without Locale settings, because the
  1302. * "normal" convert from Local has only double precision and it is
  1303. * not enough for the about 50 digits of precision of the
  1304. * BigDecimal. So in the first step the string is seperated into the
  1305. * integer part which is converted to a long, and the fraction part
  1306. * is appended afterwards. Between the integer and the fraction part
  1307. * comes a ".". Finally the resulting string is returned.
  1308. *
  1309. * @see #setUseLocale(boolean)
  1310. * @param str String representation of a BigDecimal number.
  1311. * @return The default String representation (without Locale) of the
  1312. * BigInteger.
  1313. * @throws ParseException
  1314. * If the String has more than one decimal seperators a parse exception is thrown.
  1315. */
  1316. private String myApplyLocaleBD (final String str) throws ParseException
  1317. {
  1318. if (!this.useLocale || this.currentRadix != 10)
  1319. {
  1320. return str;
  1321. }
  1322. String negPrefix = this.df.getNegativePrefix ();
  1323. String negSuffix = this.df.getNegativeSuffix ();
  1324. String posPrefix = this.df.getPositivePrefix ();
  1325. String posSuffix = this.df.getPositiveSuffix ();
  1326. char d = this.dfs.getDecimalSeparator ();
  1327. int begin1, begin2;
  1328. boolean isNegativ = false;
  1329. String parts = null;
  1330. String tmpStr1 = "";
  1331. begin1 = str.indexOf (d);
  1332. begin2 = str.indexOf (d, begin1 + 1);
  1333. if (begin2 > 0)
  1334. {
  1335. throw new ParseException ("more than one Decimal seperators", begin2);
  1336. }
  1337. parts = str.substring (0, begin1);
  1338. if ((negPrefix.length () > 0
  1339. && str.substring (0, negPrefix.length ()).equals (negPrefix))
  1340. || (negSuffix.length () > 0
  1341. && str.substring (str.length () -
  1342. negSuffix.length ()).equals (negSuffix)))
  1343. {
  1344. parts += negSuffix;
  1345. isNegativ = true;
  1346. }
  1347. else
  1348. if ((posPrefix.length () > 0
  1349. && str.substring (0, posPrefix.length ()).equals (posPrefix))
  1350. || (posSuffix.length () > 0
  1351. && str.substring (str.length () -
  1352. posSuffix.length ()).equals (posSuffix)))
  1353. {
  1354. parts += posSuffix;
  1355. }
  1356. tmpStr1 = this.actFormat.parse (parts).toString ();
  1357. if (isNegativ)
  1358. {
  1359. tmpStr1 +=
  1360. "." + str.substring (str.indexOf (d) + 1,
  1361. str.length () - negSuffix.length ());
  1362. }
  1363. else
  1364. {
  1365. tmpStr1 +=
  1366. "." + str.substring (str.indexOf (d) + 1,
  1367. str.length () - posSuffix.length ());
  1368. }
  1369. return tmpStr1;
  1370. }
  1371. /**
  1372. * Tries to interpret the next String as a BigDecimal. Therfore the
  1373. * next String is get with {@link #myCoreNext(boolean, Pattern)} and
  1374. * then {@link #myApplyLocaleBD(String)} is called to convert the
  1375. * String into a BigDecimal.
  1376. *
  1377. * @param delete
  1378. * Should the found string be deleted or not.
  1379. * @return Returns the BigDecimal value of the next string.
  1380. * @throws InputMismatchException
  1381. * If the string is not a BigDecimal
  1382. */
  1383. private BigDecimal myBigDecimal (final boolean delete) throws
  1384. InputMismatchException
  1385. {
  1386. BigDecimal rc;
  1387. String tmp = myCoreNext (delete, this.p);
  1388. try
  1389. {
  1390. tmp = myApplyLocaleBD (tmp);
  1391. }
  1392. catch (ParseException e)
  1393. {
  1394. throw new InputMismatchException (ERR_PREFIX + tmp + IS_NOT +
  1395. "BigDecimal!!");
  1396. }
  1397. rc = new BigDecimal (tmp);
  1398. return rc;
  1399. }
  1400. /**
  1401. * Applies suffix ("\E") and prefix ("\Q") if str.length != 0 Used
  1402. * by the toString method.
  1403. *
  1404. * @param str
  1405. * the string on which the suffix and prefix should be applied.
  1406. * @return The new new string with the suffix and prefix.
  1407. */
  1408. private String myConvert (final String str)
  1409. {
  1410. if (str != null && str.length () > 0)
  1411. {
  1412. return "\\Q" + str + "\\E";
  1413. }
  1414. return str;
  1415. }
  1416. /**
  1417. * Searches the current Matcher for the current Pattern. If the end
  1418. * is reached during the search it tried to read again from the
  1419. * source. The search results are always saved in {@link #actResult}
  1420. * which is returned when match() is called. If doSkip is true the
  1421. * pattern is also taken.
  1422. *
  1423. * @param delete
  1424. * if true the aktPos is set.
  1425. * @param pattern
  1426. * pattern to search for.
  1427. * @return Returns the String which matches the pattern.
  1428. * @throws NoSuchElementException
  1429. * If the search has no result.
  1430. */
  1431. private String myCoreNext (final boolean delete, final Pattern pattern)
  1432. throws NoSuchElementException
  1433. {
  1434. if (this.isClosed)
  1435. {
  1436. throw new IllegalStateException ("Scanner closed");
  1437. }
  1438. if (shallUseLastFound (pattern != null ? pattern : this.p))
  1439. {
  1440. if (this.last_RegionEnd != this.myMatcher.regionEnd ())
  1441. {
  1442. System.out.println (this.last_RegionEnd + " != " +
  1443. this.myMatcher.regionEnd () + " (" +
  1444. (this.last_RegionEnd -
  1445. this.myMatcher.regionEnd ()) + ")");
  1446. }
  1447. if (delete)
  1448. {
  1449. this.actPos = this.lastNextPos;
  1450. this.lastFoundPresent = false;
  1451. this.actResult = this.lastResult;
  1452. }
  1453. return this.lastFound;
  1454. }
  1455. boolean found = false;
  1456. int left;
  1457. int endIndex;
  1458. String tmp2 = null;
  1459. if (this.actPos > this.MAX_PREFIX)
  1460. {
  1461. // skipp the processed chars so that the size of the buffer don't grow to much even with
  1462. // huge files
  1463. this.procesedChars += this.actPos;
  1464. this.actBuffer = this.actBuffer.substring (this.actPos);
  1465. this.actPos = 0;
  1466. this.myMatcher = pattern.matcher (this.actBuffer);
  1467. }
  1468. left = this.actBuffer.length () - this.actPos;
  1469. if (left < this.MIN_BUF_LEN)
  1470. {
  1471. myFillBuffer ();
  1472. }
  1473. found = this.myMatcher.find (this.actPos);
  1474. found = myFillBuffer_loop (this.myMatcher, this.actPos, found);
  1475. this.needInput = false;
  1476. if (found)
  1477. {
  1478. if (this.doSkipp)
  1479. {
  1480. endIndex = this.myMatcher.end ();
  1481. }
  1482. else
  1483. {
  1484. endIndex = this.myMatcher.start ();
  1485. }
  1486. tmp2 = this.actBuffer.substring (this.actPos, endIndex);
  1487. this.lastNextPos = this.myMatcher.end ();
  1488. /*
  1489. * if the delete flag is set, just set the current position after the end of the matched
  1490. * pattern.
  1491. */
  1492. if (delete)
  1493. {
  1494. this.actPos = this.lastNextPos;
  1495. }
  1496. else
  1497. {
  1498. this.lastFound = tmp2;
  1499. this.lastFoundPresent = true;
  1500. this.lastPatternHash = pattern.hashCode ();
  1501. }
  1502. this.last_RegionStart = this.myMatcher.regionStart ();
  1503. this.last_RegionEnd = this.myMatcher.regionEnd ();
  1504. this.last_anchor = this.myMatcher.hasAnchoringBounds ();
  1505. this.last_transparent = this.myMatcher.hasTransparentBounds ();
  1506. }
  1507. else if (this.myMatcher.hitEnd ())
  1508. // the end of input is matched
  1509. {
  1510. tmp2 = this.actBuffer.substring (this.actPos);
  1511. if (tmp2.length() == 0)
  1512. tmp2 = null;
  1513. this.lastNextPos = this.actBuffer.length ();
  1514. if (delete)
  1515. {
  1516. this.actPos = this.lastNextPos;
  1517. }
  1518. else
  1519. {
  1520. this.lastFound = tmp2;
  1521. this.lastFoundPresent = true;
  1522. this.lastPatternHash = pattern.hashCode ();
  1523. }
  1524. this.last_RegionStart = this.myMatcher.regionStart ();
  1525. this.last_RegionEnd = this.myMatcher.regionEnd ();
  1526. this.last_anchor = this.myMatcher.hasAnchoringBounds ();
  1527. this.last_transparent = this.myMatcher.hasTransparentBounds ();
  1528. }
  1529. else
  1530. {
  1531. /*
  1532. * if no match found an Exception is throwed
  1533. */
  1534. throw new NoSuchElementException ();
  1535. }
  1536. /*
  1537. * change the Result only when a nextXXX() method was called, not if a hasNextXXX() method
  1538. * is called
  1539. */
  1540. if (delete)
  1541. {
  1542. this.actResult = this.myMatcher.toMatchResult ();
  1543. this.matchValid = this.actResult != null;
  1544. }
  1545. else
  1546. {
  1547. this.lastResult = this.myMatcher.toMatchResult ();
  1548. }
  1549. this.skipped = this.doSkipp;
  1550. this.doSkipp = false;
  1551. return tmp2;
  1552. }
  1553. /**
  1554. * Used to fill the String buffer from a source. Therfore the 3
  1555. * possible sources are checked if they are not <code>null</code>
  1556. * and this not used, otherwise the read method is called on the
  1557. * source. If a charsetName is set and not <code>null</code> it is
  1558. * applied to convert to String.
  1559. */
  1560. private void myFillBuffer ()
  1561. {
  1562. int len;
  1563. String tmpStr;
  1564. CharBuffer cb = null;
  1565. ByteBuffer bb = null;
  1566. if (this.bIS != null)
  1567. {
  1568. try
  1569. {
  1570. len = this.bIS.read (this.tmpBuffer);
  1571. if (len < 0)
  1572. {
  1573. return;
  1574. }
  1575. if (this.charsetName != null)
  1576. {
  1577. tmpStr = new String (this.tmpBuffer, 0, len, this.charsetName);
  1578. }
  1579. else
  1580. {
  1581. tmpStr = new String (this.tmpBuffer, 0, len);
  1582. }
  1583. this.actBuffer += tmpStr;
  1584. }
  1585. catch (IOException e)
  1586. {
  1587. this.lastIOException = e;
  1588. }
  1589. }
  1590. else if (this.readableSource != null)
  1591. {
  1592. try
  1593. {
  1594. cb = CharBuffer.allocate (1000);
  1595. this.needInput = true;
  1596. len = this.readableSource.read (cb);
  1597. if (len < 0)
  1598. {
  1599. return;
  1600. }
  1601. this.needInput = false;
  1602. tmpStr = new String (cb.array ());
  1603. this.actBuffer += tmpStr;
  1604. }
  1605. catch (IOException e)
  1606. {
  1607. this.lastIOException = e;
  1608. }
  1609. }
  1610. else if (this.rbcSource != null)
  1611. {
  1612. try
  1613. {
  1614. bb = ByteBuffer.allocate (1000);
  1615. this.needInput = true;
  1616. len = this.rbcSource.read (bb);
  1617. this.needInput = false;
  1618. if (len < 0)
  1619. {
  1620. return;
  1621. }
  1622. if (this.charsetName != null)
  1623. {
  1624. tmpStr = new String (bb.array (), 0, len, this.charsetName);
  1625. }
  1626. else
  1627. {
  1628. tmpStr = new String (bb.array (), 0, len);
  1629. }
  1630. this.actBuffer += tmpStr;
  1631. }
  1632. catch (IOException e)
  1633. {
  1634. this.lastIOException = e;
  1635. }
  1636. }
  1637. this.myMatcher.reset (this.actBuffer);
  1638. }
  1639. /**
  1640. * A loop in which the {@link #myFillBuffer()} is called and checked
  1641. * if the pattern is found in the matcher and if the buffersize
  1642. * changes after the read.
  1643. *
  1644. * @param aktM
  1645. * The current Matcher.
  1646. * @param pos
  1647. * Position from which the matcher should start matching.
  1648. * @param found
  1649. * if already found.
  1650. * @return <code> true </code> if the matcher has found a match.
  1651. */
  1652. private boolean myFillBuffer_loop (final Matcher aktM, final int pos,
  1653. boolean found)
  1654. {
  1655. int tmp;
  1656. tmp = this.actBuffer.length ();
  1657. while (aktM.hitEnd ()
  1658. && ((this.bIS != null) || (this.readableSource != null)
  1659. || (this.rbcSource != null)))
  1660. {
  1661. myFillBuffer ();
  1662. if (tmp == this.actBuffer.length ())
  1663. {
  1664. break;
  1665. }
  1666. found = aktM.find (pos);
  1667. this.needInput = true;
  1668. }
  1669. return found;
  1670. }
  1671. /**
  1672. * Used to find the given pattern in the given string before the
  1673. * given horizon. Therfore the current matcher is copied, and
  1674. * overwritten using the given pattern and the given Sting. <br>
  1675. * After the search the original values are restored, and skipped is
  1676. * set <code> true </code>.
  1677. *
  1678. * @param pattern
  1679. * Pattern which should be matched.
  1680. * @param str
  1681. * The String in which the pattern should be matched.
  1682. * @param horizon
  1683. * the horizon whithin the match should be, if 0 then it is ignored.
  1684. * @return Returns the String in the given String that matches the pattern.
  1685. */
  1686. private String myFindPInStr (final Pattern pattern, final String str,
  1687. final int horizon)
  1688. {
  1689. String rc = null;
  1690. int curPos = this.actPos;
  1691. Matcher aktMatcher = this.myMatcher;
  1692. this.myMatcher = pattern.matcher (str);
  1693. if (horizon > 0)
  1694. {
  1695. this.myMatcher.useAnchoringBounds (true);
  1696. this.myMatcher.useTransparentBounds (true);
  1697. this.myMatcher.region (this.actPos, this.actPos + horizon);
  1698. }
  1699. rc = myCoreNext (true, pattern);
  1700. this.myMatcher = aktMatcher;
  1701. this.actPos = curPos;
  1702. this.skipped = true;
  1703. return rc;
  1704. }
  1705. /**
  1706. * Used by the {@link #hasNext(Pattern)} and {@link #next(Pattern)}
  1707. * methods. Therfore a substring is taken first to the current
  1708. * delimiter, afterwards the given pattern is searched in this
  1709. * subsring.<br> Finally the current Buffer and matcher (which have
  1710. * been temporarily changed) are set back.<br> <br> The {@link
  1711. * #skipped} is set <code> true </code>.
  1712. *
  1713. * @param pattern
  1714. * Pattern to find until the current delimiter.
  1715. * @param delete
  1716. * Is <code> true </code> if a next method is called.<br>
  1717. * Is <code> false </code> if a hasNext method is called.
  1718. * @return Returns the String which is returned by the public methods.
  1719. */
  1720. private String myNext (final Pattern pattern, final boolean delete)
  1721. {
  1722. String tmpStr;
  1723. Matcher aktMatcher = this.myMatcher;
  1724. String result;
  1725. String currBuffer = this.actBuffer;
  1726. int currAktPos;
  1727. tmpStr = myCoreNext (delete, this.p);
  1728. this.myMatcher = pattern.matcher (tmpStr);
  1729. this.actBuffer = tmpStr;
  1730. currAktPos = this.actPos;
  1731. this.actPos = 0;
  1732. result = myCoreNext (delete, pattern);
  1733. this.actPos = currAktPos;
  1734. this.actBuffer = currBuffer;
  1735. this.myMatcher = aktMatcher;
  1736. this.skipped = true;
  1737. return result;
  1738. }
  1739. /**
  1740. * Calls the next() method internally to get the next String, and
  1741. * trys to apply a locale which is only applied if the radix is 10
  1742. * and useLocale is <code> true </code>. Afterwards it is tried to
  1743. * call the Constructor of a {@link BigInteger} with the given
  1744. * radix.
  1745. *
  1746. * @param radix The radix to use.
  1747. * @param delete If the found String should be removed from input or
  1748. * not.
  1749. * @param name name of "BigInteger" in case of an Error.
  1750. * @return Returns the new BigInteger created if there is no Error.
  1751. * @throws InputMismatchException
  1752. * If there is a {@link ParseException} or a {@link NumberFormatException}.
  1753. */
  1754. private BigInteger myNextBigInteger (final int radix, final boolean delete,
  1755. final String name)
  1756. {
  1757. BigInteger rc;
  1758. String tmp = myPrepareForNext (this.p, delete);
  1759. try
  1760. {
  1761. tmp = myApplyLocale (tmp, radix);
  1762. rc = new BigInteger (tmp, radix);
  1763. return rc;
  1764. }
  1765. catch (NumberFormatException nfe)
  1766. {
  1767. }
  1768. catch (ParseException e)
  1769. {
  1770. }
  1771. throw new InputMismatchException (ERR_PREFIX + tmp + IS_NOT + name);
  1772. }
  1773. /**
  1774. * Checks if the next String is either "true" or "false", otherwise
  1775. * an {@link InputMismatchException} is thrown. It ignores the case
  1776. * of the string so that "true" and "TRUE" and even "TrUe" are
  1777. * accepted.
  1778. *
  1779. * @param delete Should the found value be removed from the input or
  1780. * not.
  1781. * @return Returns the boolean value (if it is a boolean).
  1782. * @throws InputMismatchException
  1783. * If the next String is not a boolean.
  1784. */
  1785. private boolean myNextBoolean (final boolean delete) throws
  1786. InputMismatchException
  1787. {
  1788. String tmp = myPrepareForNext (this.p, delete);
  1789. if (tmp.equalsIgnoreCase ("true"))
  1790. {
  1791. return true;
  1792. }
  1793. else if (tmp.equalsIgnoreCase ("false"))
  1794. {
  1795. return false;
  1796. }
  1797. else
  1798. {
  1799. throw new InputMismatchException (ERR_PREFIX + tmp + NOT_BOOLEAN);
  1800. }
  1801. }
  1802. /**
  1803. * Calls the {@link #myPrepareForNext(Pattern, boolean)} which calls
  1804. * the {@link #myCoreNext(boolean, Pattern)} to return the next
  1805. * String matching the current delimier. Afterwards it is tryed to
  1806. * convert the String into a byte. Any Error will lead into a {@link
  1807. * InputMismatchException}.
  1808. *
  1809. * @param radix The radix to use.
  1810. * @param delete Should the found String be removed from the input.
  1811. * @return Returns the byte value of the String.
  1812. * @throws InputMismatchException if the next String is not a byte.
  1813. */
  1814. private byte myNextByte (final int radix,
  1815. final boolean delete) throws InputMismatchException
  1816. {
  1817. byte rc;
  1818. String tmp = myPrepareForNext (this.p, delete);
  1819. try
  1820. {
  1821. tmp = myApplyLocale (tmp, radix);
  1822. rc = Byte.parseByte (tmp, radix);
  1823. return rc;
  1824. }
  1825. catch (NumberFormatException nfe)
  1826. {
  1827. }
  1828. catch (ParseException e)
  1829. {
  1830. }
  1831. throw new InputMismatchException (ERR_PREFIX + tmp + NOT_BYTE);
  1832. }
  1833. /**
  1834. * Tries to interpret the next String as a double value. To verify
  1835. * if the double value is correct, it is converted back to a String
  1836. * using the default Locale and this String is compared with the
  1837. * String from which the double was converted. If the two Strings
  1838. * don't match, an {@link InputMismatchException} is thrown.<br>
  1839. * <br> The radix used is always 10 even if the global radix is
  1840. * changed.
  1841. *
  1842. * @param delete Should the String be removed, if true it will be
  1843. * also removed if the String is not a double value.
  1844. * @return Returns the double value of the next String.
  1845. * @throws InputMismatchException if the next String is not a
  1846. * double.
  1847. */
  1848. private double myNextDouble (final boolean delete) throws
  1849. InputMismatchException
  1850. {
  1851. double rc;
  1852. String tmp = myPrepareForNext (this.p, delete);
  1853. try
  1854. {
  1855. tmp = myApplyLocale (tmp, 10);
  1856. rc = Double.parseDouble (tmp);
  1857. if (("" + rc).equals (tmp))
  1858. {
  1859. return rc;
  1860. }
  1861. }
  1862. catch (ParseException e)
  1863. {
  1864. }
  1865. throw new InputMismatchException (ERR_PREFIX + tmp + NOT_DOUBLE);
  1866. }
  1867. /**
  1868. * Tries to interpret the next String as an int value. Therfore
  1869. * {@link #myApplyLocale(String, int)} decides if the current Locale
  1870. * should be applied or not and then the result is parsed using
  1871. * {@link Integer#parseInt(String, int)}. Any Error will lead to an
  1872. * {@link InputMismatchException}.
  1873. *
  1874. * @param radix The radix to use.
  1875. * @param delete <code> true </code> if the String should be deleted
  1876. * from the input.
  1877. * @return Returns the int value of the String.
  1878. * @throws InputMismatchException if the next String is not an int.
  1879. */
  1880. private int myNextInt (final int radix,
  1881. final boolean delete) throws InputMismatchException
  1882. {
  1883. int rc;
  1884. String tmp = myPrepareForNext (this.p, delete);
  1885. try
  1886. {
  1887. tmp = myApplyLocale (tmp, radix);
  1888. rc = Integer.parseInt (tmp, radix);
  1889. return rc;
  1890. }
  1891. catch (NumberFormatException nfe)
  1892. {
  1893. }
  1894. catch (ParseException e)
  1895. {
  1896. }
  1897. throw new InputMismatchException (ERR_PREFIX + tmp + NOT_INT);
  1898. }
  1899. /**
  1900. * Finds the next line using the {@link #NEW_LINE} constant which is
  1901. * set to the system specific line seperator.
  1902. *
  1903. * @param delete should the found line be deleted from the input.
  1904. * @return the current line.
  1905. */
  1906. private String myNextLine (final boolean delete)
  1907. {
  1908. return myPrepareForNext (Pattern.compile (NEW_LINE), delete);
  1909. }
  1910. /**
  1911. * Tries to interpret the next String as a long value with the given
  1912. * radix. Therfore the {@link Long#parseLong(String, int)} is called
  1913. * and every Error will lead into a {@link InputMismatchException}.
  1914. *
  1915. * @param radix The radix to be used.
  1916. * @param delete Should the found String be deleted from the input.
  1917. * @return the long value of the next String.
  1918. * @throws InputMismatchException if the next String is not a long.
  1919. */
  1920. private long myNextLong (final int radix,
  1921. final boolean delete) throws InputMismatchException
  1922. {
  1923. long rc;
  1924. String tmp = myPrepareForNext (this.p, delete);
  1925. try
  1926. {
  1927. tmp = myApplyLocale (tmp, radix);
  1928. rc = Long.parseLong (tmp, radix);
  1929. return rc;
  1930. }
  1931. catch (NumberFormatException nfe)
  1932. {
  1933. }
  1934. catch (ParseException e)
  1935. {
  1936. }
  1937. throw new InputMismatchException (ERR_PREFIX + tmp + NOT_LONG);
  1938. }
  1939. /**
  1940. * Tries to interpret the next String as a short value with the
  1941. * given radix. Therfore the {@link Short#parseShort(String, int)}
  1942. * is called and every Error will lead into a {@link
  1943. * InputMismatchException} .
  1944. *
  1945. * @param radix
  1946. * The radix to be used.
  1947. * @param delete
  1948. * Should the found String be deleted from the input.
  1949. * @return the long value of the next String.
  1950. * @throws InputMismatchException
  1951. * if the next String is not a short.
  1952. */
  1953. private short myNextShort (final int radix,
  1954. final boolean delete) throws
  1955. InputMismatchException
  1956. {
  1957. short rc;
  1958. String tmp = myPrepareForNext (this.p, delete);
  1959. try
  1960. {
  1961. tmp = myApplyLocale (tmp, radix);
  1962. rc = Short.parseShort (tmp, radix);
  1963. return rc;
  1964. }
  1965. catch (NumberFormatException nfe)
  1966. {
  1967. }
  1968. catch (ParseException e)
  1969. {
  1970. }
  1971. throw new InputMismatchException (ERR_PREFIX + tmp +
  1972. "\" is not a short");
  1973. }
  1974. /**
  1975. * Sets the current pattern to the given pattern and calls the
  1976. * {@link #myCoreNext(boolean, Pattern)}. Finally sets the pattern
  1977. * back to its old value.
  1978. *
  1979. * @param aktPattern Pattern to be used for the next match.
  1980. * @param delete Should the found String be deleted or not.
  1981. * @return Return the String returned from {@link
  1982. * #myCoreNext(boolean, Pattern)}.
  1983. */
  1984. private String myPrepareForNext (final Pattern aktPattern,
  1985. final boolean delete)
  1986. {
  1987. String rc;
  1988. Pattern oldPattern = this.p;
  1989. useDelimiter (aktPattern);
  1990. rc = myCoreNext (delete, aktPattern);
  1991. useDelimiter (oldPattern);
  1992. return rc;
  1993. }
  1994. /**
  1995. * Determinates if the last found can be used, so that after a
  1996. * hasNextXXX the nextXXX has not to search if nothing has
  1997. * changed.<br /> Used in {@link #myCoreNext(boolean, Pattern)}.
  1998. *
  1999. * @param aktP The pattern which should be checked.
  2000. * @return <code> true </code> if the searchresult is already ready.
  2001. */
  2002. private boolean shallUseLastFound (final Pattern aktP)
  2003. {
  2004. if (this.lastFoundPresent &&
  2005. this.lastPatternHash == aktP.hashCode () &&
  2006. this.last_RegionStart == this.myMatcher.regionStart () &&
  2007. this.last_anchor == this.myMatcher.hasAnchoringBounds () &&
  2008. this.last_transparent == this.myMatcher.hasTransparentBounds ())
  2009. {
  2010. if (this.last_RegionEnd != this.myMatcher.regionEnd ())
  2011. {
  2012. int tmpVal =
  2013. this.myMatcher.regionEnd () -
  2014. this.last_RegionEnd - this.MAX_PREFIX;
  2015. if (tmpVal > 0 && tmpVal < 20)
  2016. {
  2017. this.last_RegionEnd =
  2018. this.myMatcher.regionEnd ();
  2019. return true;
  2020. }
  2021. }
  2022. else
  2023. return true;
  2024. }
  2025. return false;
  2026. }
  2027. }