Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2008 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Elliott Baron <ebaron@redhat.com> - initial API and implementation *******************************************************************************/ public class Main { private static final String EMPTY_STRING = ""; /** * Parses string ending with format ([FILE]:[LINE MODULE]) * Assumes syntax is: "\(.*:[0-9]+(\s.+)?\)$" * @param line - String with the above criteria * @return a tuple of [String filename, Integer line] */ public static Object[] parseFilename(String line) { String filename = null; int lineNo = 0; int ix = line.lastIndexOf('('); if (ix >= 0) { String part = line.substring(ix, line.length()); part = part.substring(1, part.length() - 1); // remove leading and trailing parentheses if ((ix = part.lastIndexOf(':')) >= 0) { String strLineNo = part.substring(ix + 1); if (isNumber(strLineNo)) { lineNo = Integer.parseInt(strLineNo); filename = part.substring(0, ix); } else { // handle format: (FILE:LINE MODULE) int ix1 = strLineNo.indexOf(' '); if (ix1 > 0) { strLineNo = strLineNo.substring(0, ix1); if (isNumber(strLineNo)) { lineNo = Integer.parseInt(strLineNo); filename = part.substring(0, ix); } } } } else { // check for "in " token (lib, with symbol) part = part.replaceFirst("^in ", EMPTY_STRING); //$NON-NLS-1$ // check for "within " token (lib, without symbol) part = part.replaceFirst("^within ", EMPTY_STRING); //$NON-NLS-1$ filename = part; // library, no line number } } return new Object[] { filename, lineNo }; } /** * Determines if argument is a number * @param string - argument to test * @return - true if argument is a number */ public static boolean isNumber(String string) { boolean result = true; char[] chars = string.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!Character.isDigit(chars[i])) { result = false; } } return result; } }