Search in sources :

Example 56 with Pattern

use of java.util.regex.Pattern in project androidquery by androidquery.

the class AbstractAjaxCallback method getCharset.

//This is an adhoc way to get charset without html parsing library, might not cover all cases.
private String getCharset(String html) {
    String pattern = "<meta [^>]*http-equiv[^>]*\"Content-Type\"[^>]*>";
    Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(html);
    if (!m.find())
        return null;
    String tag = m.group();
    return parseCharset(tag);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 57 with Pattern

use of java.util.regex.Pattern in project openhab1-addons by openhab.

the class HDanywhereBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    HDanywhereBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    if (provider == null) {
        logger.trace("doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
        return;
    }
    List<String> hosts = provider.getHosts(itemName);
    int sourcePort = Integer.valueOf(command.toString());
    for (String aHost : hosts) {
        Integer numberOfPorts = matrixCache.get(aHost);
        if (numberOfPorts == null) {
            // we default to the smallest matrix currently sold by HDanywhere
            numberOfPorts = 4;
        }
        if (sourcePort > numberOfPorts) {
            // nice try - we can switch to a port that does not physically exist
            logger.warn("{} goes beyond the physical number of {} ports available on the matrix {}", new Object[] { sourcePort, numberOfPorts, aHost });
        } else {
            List<Integer> ports = provider.getPorts(aHost, itemName);
            String httpMethod = "GET";
            String url = "http://" + aHost + "/switch.cgi?command=3&data0=";
            for (Integer aPort : ports) {
                url = url + aPort.toString() + "&data1=";
                url = url + command.toString() + "&checksum=";
                int checksum = 3 + aPort + sourcePort;
                url = url + String.valueOf(checksum);
                if (isNotBlank(httpMethod) && isNotBlank(url)) {
                    String response = HttpUtil.executeUrl(httpMethod, url, null, null, null, timeout);
                    Pattern p = Pattern.compile("The output " + aPort + " select input (.*).");
                    Matcher m = p.matcher(response);
                    while (m.find()) {
                        List<Class<? extends State>> stateTypeList = new ArrayList<Class<? extends State>>();
                        stateTypeList.add(DecimalType.class);
                        State state = TypeParser.parseState(stateTypeList, m.group(1));
                        if (!portMappingCache.containsKey(aHost + ":" + aPort)) {
                            portMappingCache.put(aHost + ":" + aPort, Integer.valueOf(m.group(1)));
                            eventPublisher.postUpdate(itemName, state);
                        } else {
                            int cachedValue = portMappingCache.get(aHost + ":" + aPort);
                            if (cachedValue != Integer.valueOf(m.group(1))) {
                                portMappingCache.put(aHost + ":" + aPort, Integer.valueOf(m.group(1)));
                                eventPublisher.postUpdate(itemName, state);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) HDanywhereBindingProvider(org.openhab.binding.hdanywhere.HDanywhereBindingProvider) Matcher(java.util.regex.Matcher) State(org.openhab.core.types.State) ArrayList(java.util.ArrayList)

Example 58 with Pattern

use of java.util.regex.Pattern in project platform_frameworks_base by android.

the class PatternsTest method testIanaTopLevelDomains_matchesIriTLD.

@SmallTest
public void testIanaTopLevelDomains_matchesIriTLD() throws Exception {
    Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS);
    assertTrue("Should match IRI TLD", pattern.matcher("한국").matches());
}
Also used : Pattern(java.util.regex.Pattern) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 59 with Pattern

use of java.util.regex.Pattern in project platform_frameworks_base by android.

the class PatternsTest method testIanaTopLevelDomains_matchesValidNewTld.

@SmallTest
public void testIanaTopLevelDomains_matchesValidNewTld() throws Exception {
    Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS);
    assertTrue("Should match 'me'", pattern.matcher("me").matches());
}
Also used : Pattern(java.util.regex.Pattern) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 60 with Pattern

use of java.util.regex.Pattern in project platform_frameworks_base by android.

the class PatternsTest method testIanaTopLevelDomains_doesNotMatchWrongTld.

@SmallTest
public void testIanaTopLevelDomains_doesNotMatchWrongTld() throws Exception {
    Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS);
    assertFalse("Should not match 'mem'", pattern.matcher("mem").matches());
}
Also used : Pattern(java.util.regex.Pattern) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

Pattern (java.util.regex.Pattern)3181 Matcher (java.util.regex.Matcher)2116 ArrayList (java.util.ArrayList)387 IOException (java.io.IOException)247 Test (org.junit.Test)238 File (java.io.File)193 HashMap (java.util.HashMap)163 BufferedReader (java.io.BufferedReader)127 Field (java.lang.reflect.Field)119 PatternSyntaxException (java.util.regex.PatternSyntaxException)119 Map (java.util.Map)110 List (java.util.List)93 HashSet (java.util.HashSet)79 InputStreamReader (java.io.InputStreamReader)67 InputStream (java.io.InputStream)43 FileReader (java.io.FileReader)41 FileInputStream (java.io.FileInputStream)40 URL (java.net.URL)35 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 LinkedHashMap (java.util.LinkedHashMap)31