Java Code Examples for java.util.Scanner#findWithinHorizon()

The following examples show how to use java.util.Scanner#findWithinHorizon() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitWrapper.java    From Stringlate with MIT License 6 votes vote down vote up
private static ArrayList<File> findIconFromManifest(final RepositoryResources resources) {
    for (File file : resources.xml) {
        if (file.getName().equals(MANIFEST)) {
            // Look for the pattern
            try {
                Scanner s = new Scanner(file);
                String found = s.findWithinHorizon(ICON_PATTERN, 0);
                s.close();

                if (found != null) {
                    Matcher m = ICON_PATTERN.matcher(found);
                    if (m.find()) {
                        // Add a slash so that we look for e.g. "/mipmap"
                        String type = "/" + m.group(1);
                        // We don't care about the extension, but we don't
                        // want to match "ic_launcher_somethingextra.png"
                        String name = m.group(2) + ".";
                        return findIcons(resources, type, name);
                    }
                }
            } catch (FileNotFoundException ignored) {
            }
        }
    }
    return new ArrayList<>();
}
 
Example 2
Source File: SystemUtils.java    From tilt-game-android with MIT License 6 votes vote down vote up
private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws SystemUtilsException {
	InputStream in = null;
	try {
		final Process process = new ProcessBuilder(new String[] {"/system/bin/cat", pSystemFile}).start();

		in = process.getInputStream();
		final Scanner scanner = new Scanner(in);

		final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
		if (matchFound) {
			return scanner.match();
		} else {
			throw new SystemUtilsException();
		}
	} catch (final IOException e) {
		throw new SystemUtilsException(e);
	} finally {
		StreamUtils.close(in);
	}
}
 
Example 3
Source File: CustomFormat.java    From mr4c with Apache License 2.0 6 votes vote down vote up
public Map<String,String> parse(String str) {
	if ( !matches(str) ) {
		throw new IllegalArgumentException(String.format("[%s] doesn't match pattern [%s]", str, m_pattern));
	}
	Scanner scanner = new Scanner(str);
	scanner.findWithinHorizon(m_regex, 0);
	MatchResult result = scanner.match();
	Map<String,String> vals = new HashMap<String,String>();
	if ( result.groupCount()!=m_nameList.size() ) {
		// this shouldn't be able to happen
		throw new IllegalStateException(String.format("[%s] doesn't match pattern [%s]; found %d matches, expected %d", str, m_pattern, result.groupCount(), m_nameList.size()));
	}
	for (int i=1; i<=result.groupCount(); i++) {
		String name = m_nameList.get(i-1);
		String val = result.group(i);
		if ( vals.containsKey(name) ) {
			if ( !vals.get(name).equals(val) ) {
				throw new IllegalArgumentException(String.format("[%s]doesnt match pattern [%s]; variable [%s] has values [%s] and [%s]", str, m_pattern, name, val, vals.get(name)));
			}
		}
		vals.put(name,result.group(i));
	}
	return vals;
}
 
Example 4
Source File: SystemUtils.java    From 30-android-libraries-in-30-days with Apache License 2.0 6 votes vote down vote up
private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws SystemUtilsException {
	InputStream in = null;
	try {
		final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();

		in = process.getInputStream();
		final Scanner scanner = new Scanner(in);

		final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
		if(matchFound) {
			return scanner.match();
		} else {
			throw new SystemUtilsException();
		}
	} catch (final IOException e) {
		throw new SystemUtilsException(e);
	} finally {
		StreamUtils.close(in);
	}
}
 
Example 5
Source File: CustomFormat.java    From mr4c with Apache License 2.0 5 votes vote down vote up
private static List<String> extractNames(String pattern) {
	List<String> names = new ArrayList<String>();
	Scanner scanner = new Scanner(pattern);
	while ( scanner.findWithinHorizon(VAR_REGEX, 0) !=null ) {
		MatchResult result = scanner.match();
		String val = result.group(1);
		names.add(val);
	}
	return names;
}
 
Example 6
Source File: JavaScannerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenFindPatternInHorizon_thenFound() throws IOException {
    final String expectedValue = "world";
    final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
    final Scanner scanner = new Scanner(inputStream);

    String result = scanner.findWithinHorizon("wo..d", 5);
    assertNull(result);

    result = scanner.findWithinHorizon("wo..d", 100);
    assertEquals(expectedValue, result);

    scanner.close();
}
 
Example 7
Source File: VersionProvider.java    From datasync with MIT License 5 votes vote down vote up
public static String stripVersion(String text) {
    Scanner scanner = new Scanner(text);
    String versionNums = scanner.findWithinHorizon("(\\d+)(\\.\\d+)(\\.\\d+)?", 0);
    String version = "";
    if (versionNums != null) {
        MatchResult groups = scanner.match();
        for (int i = 1; i <= groups.groupCount() && groups.group(i) != null; i++) // yes, truly 1-indexed
            version += groups.group(i);
    }
    return version;
}
 
Example 8
Source File: LinkFormat.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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;
}
 
Example 9
Source File: LinkFormat.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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;
}
 
Example 10
Source File: CustomFormat.java    From mr4c with Apache License 2.0 4 votes vote down vote up
public boolean matches(String str) {
	Scanner scanner = new Scanner(str);
	String check = scanner.findWithinHorizon(m_regex, 0);
	return str.equals(check);
}