下面列出了java.util.Scanner#findInLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void parseMessage(){
Scanner scanner = new Scanner(getMessage());
while(scanner.hasNextLine()){
//check of --variable APP_ID=value is needed
if( scanner.findInLine("(?:\\s\\-\\-variable\\s(\\w*)=value)") != null ){
MatchResult mr = scanner.match();
StringBuilder missingVars = new StringBuilder();
for(int i = 0; i<mr.groupCount();i++){
if(i>0){
missingVars.append(",");
}
missingVars.append(mr.group());
}
pluginStatus = new HybridMobileStatus(IStatus.ERROR, HybridCore.PLUGIN_ID, CordovaCLIErrors.ERROR_MISSING_PLUGIN_VARIABLE,
NLS.bind("This plugin requires {0} to be defined",missingVars), null);
}
scanner.nextLine();
}
scanner.close();
}
protected MethodInfo parseMethodInfo(String line) {
MethodInfo result = new MethodInfo();
Scanner s = new Scanner(line);
s.findInLine(methodInfoPattern());
MatchResult rexp = s.match();
if (rexp.group(4) != null && rexp.group(4).length() > 0) {
// line " at tmtools.jstack.share.utils.Utils.sleep(Utils.java:29)"
result.setName(rexp.group(1));
result.setCompilationUnit(rexp.group(2));
result.setLine(rexp.group(4));
} else {
// line " at java.lang.Thread.sleep(Native Method)"
result.setName(rexp.group(1));
}
s.close();
return result;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Define the proxy to use for all GA tracking requests.
* <p>
* Call this static method early (before creating any tracking requests).
*
* @param proxyAddr
* "addr:port" of the proxy to use; may also be given as URL
* ("http://addr:port/").
*/
public static void setProxy(String proxyAddr)
{
if(proxyAddr != null)
{
Scanner s = new Scanner(proxyAddr);
// Split into "proxyAddr:proxyPort".
proxyAddr = null;
int proxyPort = 8080;
try
{
s.findInLine("(http://|)([^:/]+)(:|)([0-9]*)(/|)");
MatchResult m = s.match();
if(m.groupCount() >= 2)
proxyAddr = m.group(2);
if(m.groupCount() >= 4 && !(m.group(4).length() == 0))
proxyPort = Integer.parseInt(m.group(4));
}finally
{
s.close();
}
if(proxyAddr != null)
{
SocketAddress sa = new InetSocketAddress(proxyAddr, proxyPort);
setProxy(new Proxy(Type.HTTP, sa));
}
}
}
private static Map<String, FastaSequenceIndexEntry> buildIndex(InputStream is) {
Scanner scanner = new Scanner(is);
int sequenceIndex = 0;
Map<String, FastaSequenceIndexEntry> index = new HashMap<String, FastaSequenceIndexEntry>();
while (scanner.hasNext()) {
// Tokenize and validate the index line.
String result = scanner.findInLine("(.+)\\t+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
if (result == null)
throw new RuntimeException("Found invalid line in index file:" + scanner.nextLine());
MatchResult tokens = scanner.match();
if (tokens.groupCount() != 5)
throw new RuntimeException("Found invalid line in index file:" + scanner.nextLine());
// Skip past the line separator
scanner.nextLine();
// Parse the index line.
String contig = tokens.group(1);
long size = Long.valueOf(tokens.group(2));
long location = Long.valueOf(tokens.group(3));
int basesPerLine = Integer.valueOf(tokens.group(4));
int bytesPerLine = Integer.valueOf(tokens.group(5));
contig = SAMSequenceRecord.truncateSequenceName(contig);
// Build sequence structure
index.put(contig, new FastaSequenceIndexEntry(contig, location, size, basesPerLine, bytesPerLine,
sequenceIndex++));
}
scanner.close();
return index;
}
private String expandProperties(final String input) {
String output = input;
Scanner s = new Scanner(output);
String foundInLine = null;
while ( (foundInLine = s.findInLine("(\\$[\\{]\\w+[\\}])")) != null) {
String envProperty = getEnvProperty(extractKey(foundInLine));
envProperty = envProperty != null ? envProperty : "";
output = output.replace(foundInLine, envProperty);
}
return output;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
protected String parseJNIGlobalRefs(String line) {
Scanner s = new Scanner(line);
s.findInLine(jniGlobalRefInfoPattern());
String result = s.match().group(1);
s.close();
return result;
}
@Test
public void testExtraction() {
String message = "Object 'com.jaxio.celerio.convention.WellKnownFolder' does not contain property 'resource' at src/main/resources/spring/springmvc-parent.p.vm.xml[line "
+ "28, column 47]";
Scanner s = new Scanner(message);
String u = s.findInLine("\\[line (\\d+), column (\\d+)\\]");
assertThat(u).isNotEmpty();
MatchResult result = s.match();
assertThat(result.groupCount()).isEqualTo(2);
assertThat(result.group(1)).isEqualTo("28");
assertThat(result.group(2)).isEqualTo("47");
}
protected LockInfo parseLockInfo(String line) {
LockInfo res = new LockInfo();
Scanner s = new Scanner(line);
s.findInLine(ownableSynchronizersPattern());
MatchResult matchRes = s.match();
String lock = matchRes.group(1).equals("None") ? matchRes.group(1) : matchRes.group(2);
res.setLock(lock);
return res;
}
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
public static Set<WebLink> parse(String linkFormat) {
Pattern DELIMITER = Pattern.compile("\\s*,+\\s*");
Set<WebLink> links = new ConcurrentSkipListSet<WebLink>();
if (linkFormat!=null) {
Scanner scanner = new Scanner(linkFormat);
String path = null;
while ((path = scanner.findInLine("<[^>]*>")) != null) {
// Trim <...>
path = path.substring(1, path.length() - 1);
WebLink link = new WebLink(path);
// Read link format attributes
String attr = null;
while (scanner.findWithinHorizon(DELIMITER, 1)==null && (attr = scanner.findInLine(WORD))!=null) {
if (scanner.findWithinHorizon("=", 1) != null) {
String value = null;
if ((value = scanner.findInLine(QUOTED_STRING)) != null) {
value = value.substring(1, value.length()-1); // trim " "
if (attr.equals(TITLE)) {
link.getAttributes().addAttribute(attr, value);
} else {
for (String part : value.split("\\s", 0)) {
link.getAttributes().addAttribute(attr, part);
}
}
} else if ((value = scanner.findInLine(WORD)) != null) {
link.getAttributes().setAttribute(attr, value);
} else if ((value = scanner.findInLine(CARDINAL)) != null) {
link.getAttributes().setAttribute(attr, value);
} else if (scanner.hasNext()) {
value = scanner.next();
}
} else {
// flag attribute without value
link.getAttributes().addAttribute(attr);
}
}
links.add(link);
}
scanner.close();
}
return links;
}
public static Set<WebLink> parse(String linkFormat) {
Pattern DELIMITER = Pattern.compile("\\s*,+\\s*");
Set<WebLink> links = new ConcurrentSkipListSet<WebLink>();
if (linkFormat!=null) {
Scanner scanner = new Scanner(linkFormat);
String path = null;
while ((path = scanner.findInLine("<[^>]*>")) != null) {
// Trim <...>
path = path.substring(1, path.length() - 1);
WebLink link = new WebLink(path);
// Read link format attributes
String attr = null;
while (scanner.findWithinHorizon(DELIMITER, 1)==null && (attr = scanner.findInLine(WORD))!=null) {
if (scanner.findWithinHorizon("=", 1) != null) {
String value = null;
if ((value = scanner.findInLine(QUOTED_STRING)) != null) {
value = value.substring(1, value.length()-1); // trim " "
if (attr.equals(TITLE)) {
link.getAttributes().addAttribute(attr, value);
} else {
for (String part : value.split("\\s", 0)) {
link.getAttributes().addAttribute(attr, part);
}
}
} else if ((value = scanner.findInLine(WORD)) != null) {
link.getAttributes().setAttribute(attr, value);
} else if ((value = scanner.findInLine(CARDINAL)) != null) {
link.getAttributes().setAttribute(attr, value);
} else if (scanner.hasNext()) {
value = scanner.next();
}
} else {
// flag attribute without value
link.getAttributes().addAttribute(attr);
}
}
links.add(link);
}
scanner.close();
}
return links;
}
/**
* Parses menu line like IDE Log L ============================ Toolbars > T
* [x] Show Editor Toolbar h [ ] Show Line Numbers S (x) Show Diff Sidebar D
*
* @param lineText
* @return parsed menu item from line
*/
public static NbMenuItem parseMenuLineText(String lineText) {
//parse line
Scanner line = new Scanner(lineText);
NbMenuItem menuitem = new NbMenuItem();
if (debug) {
System.out.println("Parsing line: " + line);
}
//is it separator? "======="
if (line.hasNext("^={5,}+\\s*")) { //at least 5x =
menuitem.setSeparator(true);
} else {
//does the line start with ( ?
String isRadio = line.findInLine("\\(.\\)");
if (isRadio != null) {
//System.out.println("parsing radiobutton: " + isRadio);
menuitem.setRadiobutton(true);
menuitem.setChecked(isRadio.indexOf("o") != -1);
} else {
//does the line start with [ ?
String isCheck = line.findInLine("\\[.\\]");
if (isCheck != null) {
//System.out.println("parsing checkbox: " + isCheck);
menuitem.setCheckbox(true);
menuitem.setChecked(isCheck.indexOf("x") != -1);
}
}
//read menu item text
StringBuffer text = new StringBuffer();
boolean read = true;
while (read && line.hasNext()) {
String partOfText = line.next();
if (partOfText.length() == 1 && partOfText.charAt(0) != '/') {
if (partOfText.charAt(0) == '>') {
menuitem.setSubmenu(new ArrayList<NbMenuItem>());
} else if (partOfText.charAt(0) == '-') {
// There is following project name, which has to be
// loaded right now. It is dynamicly changing.
partOfText = partOfText + " " + projectName;
text.append(partOfText);
text.append(" ");
} else {
//it must be the mnemonic
menuitem.setMnemo(partOfText.charAt(0));
read = false;
}
} else {
text.append(partOfText);
text.append(" ");
}
}
menuitem.setName(text.substring(0, text.length() - 1)); //remove the last " "
}
return menuitem;
}
public static String getMacAddress() throws IOException{
Process proc = Runtime.getRuntime().exec("cmd /c ipconfig /all");
Scanner s = new Scanner(proc.getInputStream());
return s.findInLine("\\p{XDigit}\\p{XDigit}(-\\p{XDigit}\\p{XDigit}){5}");
}