/** * Parses the message argument from the specified string starting at the * index specified by {@code position}. If the string is successfully * parsed then the index of the {@code ParsePosition} is updated to the * index following the parsed text. On error, the index is unchanged and the * error index of {@code ParsePosition} is set to the index where the error * occurred. * * @param string * the string to parse. * @param position * input/output parameter, specifies the start index in * {@code string} from where to start parsing. If parsing is * successful, it is updated with the index following the parsed * text; on error, the index is unchanged and the error index is * set to the index where the error occurred. * @return the array of objects resulting from the parse, or {@code null} if * there is an error. */ @Override public Object parseObject(String string, ParsePosition position) { return parse(string, position); }
/** * Parses the message arguments from the specified string using the rules of * this message format. * * @param string * the string to parse. * @return the array of {@code Object} arguments resulting from the parse. * @throws ParseException * if an error occurs during parsing. */ public Object[] parse(String string) throws ParseException { ParsePosition position = new ParsePosition(0); Object[] result = parse(string, position); if (position.getIndex() == 0) { throw new ParseException("Parse failure", position.getErrorIndex()); } return result; }
/** * reads the checksum from a file using the specified format. * * @since 1.7 */ private String readChecksum(File f) { try (BufferedReader diskChecksumReader = new BufferedReader(new FileReader(f))) { Object[] result = format.parse(diskChecksumReader.readLine()); if (result == null || result.length == 0 || result[0] == null) { throw new BuildException("failed to find a checksum"); } return (String) result[0]; } catch (IOException | ParseException e) { throw new BuildException("Couldn't read checksum file " + f, e); } }
private ParsedId parseOrderId(String compositeId) { try { Object[] parts = ID_FORMAT.parse(compositeId); return new ParsedId(CampBX.OrderType.valueOf(parts[0].toString()), parts[1].toString()); } catch (ParseException e) { throw new IllegalArgumentException("Can't parse order id: " + compositeId); } }
&& String.valueOf(line.charAt(POS_2)).equals(":") && String.valueOf(line.charAt(POS_3)).equals("\\"))) { Object[] objs = mf.parse(line); String f = (String) objs[1];
public SessionModel decode(Object data, Factory factory) { SessionModel model = null; if (data instanceof String) { log.trace("Trying to parse session model data: {}", data); try { Object[] values = new MessageFormat(pattern).parse((String) data); String sessionID = trimToNull((String) values[0]); long creationTime = (Long) values[1]; long lastAccessedTime = (Long) values[2]; int maxInactiveInterval = ((Long) values[3]).intValue(); model = factory.newInstance(sessionID, creationTime, lastAccessedTime, maxInactiveInterval); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Could not parse session model data: " + data, e); } } } return model; }
public SessionModel decode(Object data, Factory factory) { SessionModel model = null; if (data instanceof String) { log.trace("Trying to parse session model data: {}", data); try { Object[] values = new MessageFormat(pattern).parse((String) data); String sessionID = trimToNull((String) values[0]); long creationTime = (Long) values[1]; long lastAccessedTime = (Long) values[2]; int maxInactiveInterval = ((Long) values[3]).intValue(); model = factory.newInstance(sessionID, creationTime, lastAccessedTime, maxInactiveInterval); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Could not parse session model data: " + data, e); } } } return model; }
public static String parseStartTagType(String line) { try { synchronized(START_TAG_PATTERN) { final Object[] fields = START_TAG_PATTERN.parse(line); return (String)fields[0]; } } catch(Exception e) { return null; } }
protected synchronized Object[] splitByElement(String template, MessageFormat s) { try { return s.parse(template); } catch (ParseException e) { } return null; }
final MessageFormat inputFormat = new MessageFormat("{0}:{1,number,integer}:{2,number,integer}") final String line = sc.nextLine(); //throws an exception if input is invalid - possibly catch and report final Object[] input = inputFormat.parse(line); final String gameName = input[0]; //will be a long final int score = (int) input[1]; final int time = (int) input[2];
public long parseToSeconds(final String duration) throws ParseException { final MessageFormat durationFormat = new MessageFormat("{0,number,#}:{1,number,#}:{2,number,#}"); final Object[] parsedTimeRemaining = durationFormat.parse(duration); final long totalDuration = TimeUnit.HOURS.toSeconds((Long) parsedTimeRemaining[0]) + TimeUnit.MINUTES.toSeconds((Long) parsedTimeRemaining[1]) + (Long) parsedTimeRemaining[2]; return totalDuration; }
MessageFormat format = new MessageFormat("String1 String2 {0,date} String3 String4"); try { Object[] parse = format.parse("String1 String2 31.8.2000 String3 String4"); Date date = (Date) parse[0]; System.out.println(date); } catch (ParseException e) { e.printStackTrace(); }
public static void main(String[] args) throws ParseException { String input="hello 1:2.3.4"; MessageFormat form = new MessageFormat("{0} {1,number}:{2,number,integer}.{3,number}"); Object[] data = form.parse(input); for(Object o : data) { System.out.println(o.getClass()+" : "+o.toString()); } }
public static Date parseStartTagTime(String line) { try { final String dateField; synchronized(START_TAG_PATTERN) { final Object[] fields = START_TAG_PATTERN.parse(line); dateField = (String) fields[1]; } return parseDate(dateField); } catch(Exception e) { return null; } }
public StockTick load() throws ParseException, IOException { String line = in.readLine(); if (line == null) { System.out.println("End of file"); return null; } Object[] results = format.parse(line); StockTick tick = new StockTick( (String)results[1], ((Number)results[2]).doubleValue(), ((Number)results[0]).longValue()+baseTimestamp ); return tick; }
public static StatisticAdder fromString(String value) { final Object[] fields; try { synchronized(FORMAT) { fields = FORMAT.parse(value); } } catch(Exception e) { return null; } final UnclosedStats out = new UnclosedStats(); out.count = ((Number)fields[0]).intValue(); out.maxAge = ((Number)fields[1]).longValue(); return out; }
private ParsedId parseOrderId(String compositeId) { try { Object[] parts = ID_FORMAT.parse(compositeId); return new ParsedId(CampBX.OrderType.valueOf(parts[0].toString()), parts[1].toString()); } catch (ParseException e) { throw new IllegalArgumentException("Can't parse order id: " + compositeId); } }
Object[] parseNameAndIndexFromFileName( String specialFileName ) { try { Object[] x = makeFormat().parse( specialFileName ); return x; } catch (ParseException e) { throw new FileNameFormatException( "Could not parse " + specialFileName + ": " + e.getMessage() ); } }
@Deprecated public Playlist(String fmt) { try { Object[] objects = format.parse(fmt); setName((String) objects[0]); setGroupBy((String) objects[1]); setLibraryView(Boolean.valueOf((String) objects[2])); } catch (Exception e) { e.printStackTrace(); } }