Search in sources :

Example 16 with Pattern

use of java.util.regex.Pattern in project groovy by apache.

the class GroovyDocToolTest method testInnerEnumReference.

public void testInnerEnumReference() throws Exception {
    List<String> srcList = new ArrayList<String>();
    srcList.add("org/codehaus/groovy/tools/groovydoc/testfiles/InnerEnum.groovy");
    srcList.add("org/codehaus/groovy/tools/groovydoc/testfiles/InnerClassProperty.groovy");
    htmlTool.add(srcList);
    MockOutputTool output = new MockOutputTool();
    htmlTool.renderToOutput(output, MOCK_DIR);
    String derivDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/InnerClassProperty.html");
    Pattern p = Pattern.compile("<a(.+?)testfiles/InnerEnum.Enum.html'>(.+?)</a>");
    Matcher m = p.matcher(derivDoc);
    assertTrue(m.find());
    assertEquals("There has to be a reference to class Enum", "Enum", m.group(2));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 17 with Pattern

use of java.util.regex.Pattern in project groovy by apache.

the class Sql method nullify.

/**
     * Hook to allow derived classes to override null handling.
     * Default behavior is to replace ?'"? references with NULLish
     *
     * @param sql the SQL statement
     * @return the modified SQL String
     */
protected String nullify(String sql) {
    /*
         * Some drivers (Oracle classes12.zip) have difficulty resolving data
         * type if setObject(null). We will modify the query to pass 'null', 'is
         * null', and 'is not null'
         */
    //could be more efficient by compiling expressions in advance.
    int firstWhere = findWhereKeyword(sql);
    if (firstWhere >= 0) {
        Pattern[] patterns = { Pattern.compile("(?is)^(.{" + firstWhere + "}.*?)!=\\s{0,1}(\\s*)\\?'\"\\?(.*)"), Pattern.compile("(?is)^(.{" + firstWhere + "}.*?)<>\\s{0,1}(\\s*)\\?'\"\\?(.*)"), Pattern.compile("(?is)^(.{" + firstWhere + "}.*?[^<>])=\\s{0,1}(\\s*)\\?'\"\\?(.*)") };
        String[] replacements = { "$1 is not $2null$3", "$1 is not $2null$3", "$1 is $2null$3" };
        for (int i = 0; i < patterns.length; i++) {
            Matcher matcher = patterns[i].matcher(sql);
            while (matcher.matches()) {
                sql = matcher.replaceAll(replacements[i]);
                matcher = patterns[i].matcher(sql);
            }
        }
    }
    return sql.replaceAll("\\?'\"\\?", "null");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GString(groovy.lang.GString)

Example 18 with Pattern

use of java.util.regex.Pattern in project groovy by apache.

the class GroovyDocToolTest method testClassAliasing.

public void testClassAliasing() throws Exception {
    List<String> srcList = new ArrayList<String>();
    srcList.add("org/codehaus/groovy/tools/groovydoc/testfiles/Alias.groovy");
    htmlTool.add(srcList);
    MockOutputTool output = new MockOutputTool();
    htmlTool.renderToOutput(output, MOCK_DIR);
    String derivDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/Alias.html");
    Pattern p = Pattern.compile("<a(.+?)java/util/ArrayList.html' title='ArrayList'>(.+?)</a>");
    Matcher m = p.matcher(derivDoc);
    assertTrue(m.find());
    assertEquals("There has to be a reference to class ArrayList", "ArrayList", m.group(2));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 19 with Pattern

use of java.util.regex.Pattern in project groovy by apache.

the class GroovyDocToolTest method testArrayPropertyLinkWithExternalReference.

public void testArrayPropertyLinkWithExternalReference() throws Exception {
    List<String> srcList = new ArrayList<String>();
    srcList.add("org/codehaus/groovy/tools/groovydoc/testfiles/PropertyLink.groovy");
    srcList.add("org/codehaus/groovy/tools/groovydoc/testfiles/ArrayPropertyLink.groovy");
    htmlTool.add(srcList);
    MockOutputTool output = new MockOutputTool();
    htmlTool.renderToOutput(output, MOCK_DIR);
    String propertyLinkDoc = output.getText(MOCK_DIR + "/org/codehaus/groovy/tools/groovydoc/testfiles/PropertyLink.html");
    Pattern p = Pattern.compile("<a(.+?)ArrayPropertyLink.html'>(.+?)</a>\\[\\]");
    Matcher m = p.matcher(propertyLinkDoc);
    assertTrue(m.find());
    assertEquals("There has to be at least a single reference to the ArrayPropertyLink[]", "ArrayPropertyLink", m.group(2));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 20 with Pattern

use of java.util.regex.Pattern in project groovy by apache.

the class InvokerHelper method matchRegex.

/**
     * Find the right hand regex within the left hand string and return a matcher.
     *
     * @param left  string to compare
     * @param right regular expression to compare the string to
     */
public static boolean matchRegex(Object left, Object right) {
    if (left == null || right == null)
        return false;
    Pattern pattern;
    if (right instanceof Pattern) {
        pattern = (Pattern) right;
    } else {
        pattern = Pattern.compile(toString(right));
    }
    String stringToCompare = toString(left);
    Matcher matcher = pattern.matcher(stringToCompare);
    RegexSupport.setLastMatcher(matcher);
    return matcher.matches();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GString(groovy.lang.GString)

Aggregations

Pattern (java.util.regex.Pattern)2745 Matcher (java.util.regex.Matcher)1806 ArrayList (java.util.ArrayList)334 Test (org.junit.Test)209 IOException (java.io.IOException)206 File (java.io.File)172 HashMap (java.util.HashMap)144 Field (java.lang.reflect.Field)118 BufferedReader (java.io.BufferedReader)108 Map (java.util.Map)97 PatternSyntaxException (java.util.regex.PatternSyntaxException)97 List (java.util.List)81 HashSet (java.util.HashSet)69 InputStreamReader (java.io.InputStreamReader)59 FileInputStream (java.io.FileInputStream)40 InputStream (java.io.InputStream)39 FileReader (java.io.FileReader)36 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 URL (java.net.URL)29 LinkedHashMap (java.util.LinkedHashMap)28