List of usage examples for java.text ParseException ParseException
public ParseException(String s, int errorOffset)
From source file:edu.cornell.kfs.fp.businessobject.USBankRecordFieldUtils.java
/** * Extracts a Date from a substring less any ending whitespace for a given beginning and ending position. * /*from ww w. jav a2s.co m*/ * @param line Superstring * @param begin Beginning index * @param end Ending index * @param lineCount The current line number * @return The Date parsed from the trimmed substring * @throws ParseException When unable to parse the date */ public static Date extractDate(String line, int begin, int end, int lineCount) throws ParseException { Date theDate; try { String sub = line.substring(begin, end); String year = sub.substring(0, 4); String month = sub.substring(4, 6); String day = sub.substring(6, 8); theDate = Date.valueOf(year + "-" + month + "-" + day); } catch (StringIndexOutOfBoundsException | IllegalArgumentException e) { // May encounter a StringIndexOutOfBoundsException if the string bounds do not match or // an IllegalArgumentException if the Date does not parse correctly throw new ParseException( "Unable to parse date from the value " + line.substring(begin, end) + " on line " + lineCount, lineCount); } return theDate; }
From source file:jp.go.nict.langrid.management.web.utility.DateUtil.java
/** * /*from ww w .j av a 2 s. com*/ * */ public static Date getDate(String text) throws ParseException { ParsePosition pp = new ParsePosition(0); if (text == null) { throw new ParseException("", pp.getIndex()); } int textLength = text.length(); for (SimpleDateFormat format : formats) { format.setLenient(false); pp = new ParsePosition(0); Date ret = format.parse(text, pp); if (textLength == pp.getIndex()) { return ret; } } throw new ParseException("", pp.getIndex()); }
From source file:org.phoenicis.win32.registry.RegistryParser.java
private void parseValueLine(RegistryKey lastNode, String currentLine, int lineNumber) throws ParseException { if (!currentLine.startsWith("\"")) { throw new ParseException(String.format(PARSE_ERROR_MESSAGE, lineNumber), 0); }//from w ww . j a va2 s. co m final StringBuilder nameBuilder = new StringBuilder(); final StringBuilder valueBuilder = new StringBuilder(); ParseState parseState = ParseState.INITIAL; Boolean ignoreNextQuote = false; for (int i = 0; i < currentLine.length(); i++) { char currentChar = currentLine.charAt(i); if (parseState == ParseState.INITIAL) { if (currentChar == QUOTE) { parseState = ParseState.READING_NAME; } } else if (parseState == ParseState.READING_NAME) { if (currentChar == '"' && !ignoreNextQuote) { parseState = ParseState.SEPARATOR; } else if (currentChar == '\\' && !ignoreNextQuote) { ignoreNextQuote = true; } else { nameBuilder.append(currentChar); ignoreNextQuote = false; } } else if (parseState == ParseState.SEPARATOR) { if (currentChar != '=') { throw new ParseException(String.format(PARSE_ERROR_MESSAGE, lineNumber), 0); } else { parseState = ParseState.READING_VALUE; } } else { valueBuilder.append(currentChar); } } final String name = nameBuilder.toString(); try { RegistryValue<AbstractValueType> value = RegistryValue.fromString(name, valueBuilder.toString()); lastNode.addChild(value); } catch (IllegalArgumentException e) { throw new ParseException(String.format("Error on line %s (%s-: %s", lineNumber, currentLine, e), 0); } }
From source file:com.ksa.myanmarlottery.service.parser.CSVFileParser.java
public List<Result> getResult(InputStream in) throws FileNotFoundException, IOException, ParseException { Iterable<CSVRecord> records = CSVFormat.RFC4180 .parse(new BufferedReader(new InputStreamReader(in, "UTF-8"))); int i = 0;//from ww w . ja v a 2s . co m List<Result> resultList = new ArrayList<>(); List<Prize> prizes = null; for (CSVRecord record : records) { if (OLD_LOTTERY_TYPE.equals(record.get(0))) { // for lottery type result Result result = new Result(); result.setType(ConstantUtil.OLD_LOTTERY_TYPE); result.setNumberOfTimes(Integer.valueOf(record.get(1))); result.setResultFor(new SimpleDateFormat("dd/MM/yyyy").parse(record.get(2))); result.setDataProvider(record.get(3)); result.setCompanyName(record.get(4)); prizes = new ArrayList<Prize>(); result.setPrizes(prizes); resultList.add(result); } else if (NEW_LOTTERY_TYPE.equals(record.get(0))) { Result result = new Result(); result.setType(ConstantUtil.NEW_LOTTERY_TYPE); result.setNumberOfTimes(Integer.valueOf(record.get(1))); result.setResultFor(new SimpleDateFormat("dd/MM/yyyy").parse(record.get(2))); result.setDataProvider(record.get(3)); result.setCompanyName(record.get(4)); prizes = new ArrayList<Prize>(); result.setPrizes(prizes); resultList.add(result); } else { // check validation for character. String character = record.get(0); String value = charMap.get(character); if (value == null) { throw new ParseException("Character is Not valid at Row " + i + " column:" + 0, 400); } prizes.add(new Prize(record.get(0), Integer.parseInt(record.get(1)), record.get(2), record.get(4))); } i++; } log.info("Total rows after parsing. " + i); return resultList; }
From source file:org.whispersystems.bithub.views.TransactionView.java
private String parseShaFromUrl(String url) throws ParseException { if (url == null) { throw new ParseException("No url", 0); }//from w w w .j av a 2 s .c o m String[] parts = url.split("/"); String fullHash = parts[parts.length - 1]; if (fullHash.length() < 8) { throw new ParseException("Not long enough", 0); } return fullHash.substring(0, 8); }
From source file:com.alu.e3.common.tools.WsseTools.java
public static Date getCreatedDate(String created) throws ParseException { Date dateParsed = null;/*from w w w . j av a 2 s .c om*/ for (DateFormat format : matchingFormats) { try { synchronized (format) { // some implementations of SimpleDateFormat are not thread-safe dateParsed = format.parse(created); return dateParsed; } } catch (Exception e) { // Do nothing, try next format available } } if (dateParsed == null) throw new ParseException("Unable to parse date: " + created, 0); return dateParsed; }
From source file:com.gisgraphy.util.DateUtil.java
/** * This method generates a string representation of a date/time in the * format you specify on input/*from w w w .ja v a 2 s. c o m*/ * * @param aMask * the date pattern the string is in * @param strDate * a string representation of a date * @return a converted Date object * @see java.text.SimpleDateFormat * @throws ParseException * when String doesn't match the expected format */ public static Date convertStringToDate(String aMask, String strDate) throws ParseException { SimpleDateFormat df; Date date; df = new SimpleDateFormat(aMask); if (log.isDebugEnabled()) { log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'"); } try { date = df.parse(strDate); } catch (ParseException pe) { // log.error("ParseException: " + pe); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return (date); }
From source file:org.kuali.rice.kew.rule.KRAMetaRuleEngine.java
/** * Processes a single statement and returns the result * @param context the current RouteContext * @return the expression result that resulted from the evaluation of a single statement * @throws ParseException if the statement could not be parsed *//*from www . j a v a 2s . com*/ public RuleExpressionResult processSingleStatement(RouteContext context) throws ParseException { if (isDone()) { return null; } int stmtNum = curStatement + 1; String statement = statements[curStatement]; LOG.debug("Processing statement: " + statement); String[] words = statement.split("\\s*:\\s*"); if (words.length < 2) { throw new ParseException("Invalid statement (#" + stmtNum + "): " + statement, 0); } String ruleName = words[0]; if (StringUtils.isEmpty(ruleName)) { throw new ParseException("Invalid rule in statement (#" + stmtNum + "): " + statement, 0); } String flag = words[1]; LOG.debug(flag.toUpperCase()); KRA_RULE_FLAG flagCode = KRA_RULE_FLAG.valueOf(flag.toUpperCase()); if (flagCode == null) { throw new ParseException("Invalid flag in statement (#" + stmtNum + "): " + statement, 0); } org.kuali.rice.kew.api.rule.Rule nestedRule = KewApiServiceLocator.getRuleService().getRuleByName(ruleName); if (nestedRule == null) { throw new ParseException( "Rule '" + ruleName + "' in statement (#" + stmtNum + ") not found: " + statement, 0); } Rule rule = new RuleImpl(nestedRule); RuleExpressionResult result; switch (flagCode) { case NEXT: result = rule.evaluate(rule, context); break; case TRUE: result = rule.evaluate(rule, context); if (!result.isSuccess()) { stop = true; } break; case FALSE: result = rule.evaluate(rule, context); if (result.isSuccess()) { stop = true; // we need to just invert the ultimate expression result success, because in this case // we wanted the expression to fail but it didn't result = new RuleExpressionResult(rule, false, result.getResponsibilities()); } break; default: throw new RiceIllegalStateException("Unhandled statement flag: " + flagCode); } curStatement++; LOG.debug("Result of statement '" + statement + "': " + result); return result; }
From source file:com.couchbase.client.protocol.views.ReducedOperationImpl.java
protected ViewResponseReduced parseResult(String json) throws ParseException { final Collection<ViewRow> rows = new LinkedList<ViewRow>(); final Collection<RowError> errors = new LinkedList<RowError>(); if (json != null) { try {// w w w . ja va2 s. c o m JSONObject base = new JSONObject(json); if (base.has("rows")) { JSONArray ids = base.getJSONArray("rows"); for (int i = 0; i < ids.length(); i++) { JSONObject elem = ids.getJSONObject(i); String key = elem.getString("key"); String value = elem.getString("value"); rows.add(new ViewRowReduced(key, value)); } } if (base.has("debug_info")) { LOGGER.log(Level.INFO, "Debugging View {0}: {1}", new Object[] { getView().getURI(), json }); } if (base.has("errors")) { JSONArray ids = base.getJSONArray("errors"); for (int i = 0; i < ids.length(); i++) { JSONObject elem = ids.getJSONObject(i); String from = elem.getString("from"); String reason = elem.getString("reason"); errors.add(new RowError(from, reason)); } } } catch (JSONException e) { throw new ParseException("Cannot read json: " + json, 0); } } return new ViewResponseReduced(rows, errors); }
From source file:ca.phon.session.RangeRecordFilter.java
private Range rangeFromString(String rStr) throws ParseException { Range retVal = new Range(0, 0, false); if (rStr.matches(pointRangeEx)) { // single record Integer uttIdx = Integer.parseInt(rStr) - 1; retVal = new Range(uttIdx, uttIdx, false); } else if (rStr.matches(incRangeEx)) { String v1Str = rStr.substring(0, rStr.indexOf('.')); String v2Str = rStr.substring(rStr.lastIndexOf('.') + 1); Integer v1 = Integer.parseInt(v1Str) - 1; Integer v2 = Integer.parseInt(v2Str) - 1; retVal = new Range(v1, v2, false); } else if (rStr.matches(exclRangeEx)) { String v1Str = rStr.substring(0, rStr.indexOf('.')); String v2Str = rStr.substring(rStr.lastIndexOf('.') + 1); Integer v1 = Integer.parseInt(v1Str) - 1; Integer v2 = Integer.parseInt(v2Str) - 1; retVal = new Range(v1, v2, true); } else {//from ww w . ja va 2 s.co m throw new ParseException(rStr, 0); } return retVal; }