Search in sources :

Example 81 with Pattern

use of java.util.regex.Pattern in project groovy-core by groovy.

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 82 with Pattern

use of java.util.regex.Pattern in project gwt-test-utils by gwt-test-utils.

the class DirectoryTestReader method initCsvMacros.

private void initCsvMacros(CsvMacros csvMacros) throws FileNotFoundException, IOException {
    macros = new HashMap<String, List<List<String>>>();
    if (csvMacros == null) {
        return;
    }
    Pattern macroNamePattern = csvMacros.pattern() != null ? Pattern.compile(csvMacros.pattern()) : null;
    File macrosDirectory = getDirectory(csvMacros.value());
    for (File file : macrosDirectory.listFiles()) {
        String fileName = file.getName();
        if (macroNamePattern == null || macroNamePattern.matcher(file.getName()).matches()) {
            FileReader reader = new FileReader(file);
            List<List<String>> sheet = CsvReader.readCsv(reader);
            macros.put(fileName, sheet);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern)

Example 83 with Pattern

use of java.util.regex.Pattern in project invokebinder by headius.

the class SmartBinder method filter.

/**
     * Filter the arguments matching the given pattern using the given filter function.
     *
     * @param pattern the regular expression pattern to match arguments
     * @param filter the MethodHandle to use to filter the arguments
     * @return a new SmartBinder with the filter applied
     */
public SmartBinder filter(String pattern, MethodHandle filter) {
    String[] argNames = signature().argNames();
    Pattern pat = Pattern.compile(pattern);
    Binder newBinder = binder();
    Signature newSig = signature();
    for (int i = 0; i < argNames.length; i++) {
        if (pat.matcher(argNames[i]).matches()) {
            newBinder = newBinder.filter(i, filter);
            newSig = newSig.argType(i, filter.type().returnType());
        }
    }
    return new SmartBinder(newSig, newBinder);
}
Also used : Pattern(java.util.regex.Pattern)

Example 84 with Pattern

use of java.util.regex.Pattern in project translationstudio8 by heartsome.

the class TBSearchCellRenderer method paint.

/**
	 * (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.grid.IRenderer#paint(org.eclipse.swt.graphics.GC, java.lang.Object)
	 */
public void paint(GC gc, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    boolean drawAsSelected = isSelected();
    boolean drawBackground = true;
    if (isCellSelected()) {
        // (!isCellFocus());
        drawAsSelected = true;
    }
    if (drawAsSelected) {
        gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    } else {
        if (item.getParent().isEnabled()) {
            Color back = item.getBackground(getColumn());
            if (back != null) {
                gc.setBackground(back);
            } else {
                drawBackground = false;
            }
        } else {
            gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }
        gc.setForeground(item.getForeground(getColumn()));
    }
    if (drawBackground)
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    int x = leftMargin;
    if (isTree()) {
        boolean renderBranches = item.getParent().getTreeLinesVisible();
        if (renderBranches) {
            branchRenderer.setBranches(getBranches(item));
            branchRenderer.setIndent(treeIndent);
            branchRenderer.setBounds(getBounds().x + x, getBounds().y, getToggleIndent(item), // Take into account border
            getBounds().height + 1);
        }
        x += getToggleIndent(item);
        toggleRenderer.setExpanded(item.isExpanded());
        toggleRenderer.setHover(getHoverDetail().equals("toggle"));
        toggleRenderer.setLocation(getBounds().x + x, (getBounds().height - toggleRenderer.getBounds().height) / 2 + getBounds().y);
        if (item.hasChildren())
            toggleRenderer.paint(gc, null);
        if (renderBranches) {
            branchRenderer.setToggleBounds(toggleRenderer.getBounds());
            branchRenderer.paint(gc, null);
        }
        x += toggleRenderer.getBounds().width + insideMargin;
    }
    if (isCheck()) {
        checkRenderer.setChecked(item.getChecked(getColumn()));
        checkRenderer.setGrayed(item.getGrayed(getColumn()));
        if (!item.getParent().isEnabled()) {
            checkRenderer.setGrayed(true);
        }
        checkRenderer.setHover(getHoverDetail().equals("check"));
        if (isCenteredCheckBoxOnly(item)) {
            // Special logic if this column only has a checkbox and is centered
            checkRenderer.setBounds(getBounds().x + ((getBounds().width - checkRenderer.getBounds().width) / 2), (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
        } else {
            checkRenderer.setBounds(getBounds().x + x, (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
            x += checkRenderer.getBounds().width + insideMargin;
        }
        checkRenderer.paint(gc, null);
    }
    Image image = item.getImage(getColumn());
    if (image != null) {
        int y = getBounds().y;
        y += (getBounds().height - image.getBounds().height) / 2;
        gc.drawImage(image, getBounds().x + x, y);
        x += image.getBounds().width + insideMargin;
    }
    if (drawAsSelected) {
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    } else {
        gc.setForeground(item.getForeground(getColumn()));
    }
    int width = getBounds().width - x - rightMargin;
    int height = getBounds().height - bottomMargin;
    int y = getBounds().y + textTopMargin + topMargin;
    if (textLayout == null) {
        textLayout = new TextLayout(gc.getDevice());
        item.getParent().addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                textLayout.dispose();
            }
        });
    }
    textLayout.setOrientation(item.getParent().getOrientation());
    textLayout.setFont(gc.getFont());
    String itemText = item.getText(getColumn());
    textLayout.setText(item.getText(getColumn()));
    textLayout.setAlignment(getAlignment());
    textLayout.setWidth(width < 1 ? 1 : width);
    if (styleColumn != -1 && getColumn() == styleColumn) {
        if (style == null) {
            final Font font = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont().getFontData()[0].getHeight(), SWT.BOLD);
            style = new TextStyle(font, null, null);
            item.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    font.dispose();
                }
            });
        }
        if (strText != null) {
            if (blnIsApplyRegular) {
                Pattern pattern = null;
                if (blnIsCaseSensitive) {
                    pattern = Pattern.compile(strText);
                } else {
                    pattern = Pattern.compile(strText, Pattern.CASE_INSENSITIVE);
                }
                Matcher matcher = pattern.matcher(itemText);
                while (matcher.find()) {
                    textLayout.setStyle(style, matcher.start(), matcher.end() - 1);
                }
            } else {
                int index;
                if (blnIsCaseSensitive) {
                    index = itemText.indexOf(strText);
                } else {
                    index = itemText.toUpperCase().indexOf(strText.toUpperCase());
                }
                if (index != -1) {
                    textLayout.setStyle(null, 0, itemText.length() - 1);
                    for (int i = 1; i < strText.length(); i++) {
                        int j = TextUtil.indexOf(itemText, strText, i, blnIsCaseSensitive);
                        if (j != -1) {
                            textLayout.setStyle(style, j, j + strText.length() - 1);
                        } else {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (item.getParent().isAutoHeight()) {
        int textHeight = topMargin + textTopMargin;
        for (int cnt = 0; cnt < textLayout.getLineCount(); cnt++) textHeight += textLayout.getLineBounds(cnt).height;
        textHeight += textBottomMargin + bottomMargin;
        Object obj = item.getData("itemHeight");
        if (getColumn() != item.getParent().getColumnCount() - 1) {
            if (obj != null) {
                int heigth = (Integer) obj;
                textHeight = Math.max(textHeight, heigth);
            }
            item.setData("itemHeight", textHeight);
        } else {
            int heigth = (Integer) obj;
            textHeight = Math.max(textHeight, heigth);
            if (textHeight != item.getHeight()) {
                item.setHeight(textHeight);
            }
            item.setData("itemHeight", null);
        }
    }
    y += getVerticalAlignmentAdjustment(textLayout.getBounds().height, height);
    textLayout.draw(gc, getBounds().x + x, y);
    if (item.getParent().getLinesVisible()) {
        if (isCellSelected()) {
            gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
        } else {
            gc.setForeground(item.getParent().getLineColor());
        }
        gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
        gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
    }
    if (isCellFocus()) {
        Rectangle focusRect = new Rectangle(getBounds().x, getBounds().y, getBounds().width - 1, getBounds().height);
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        gc.drawRectangle(focusRect);
        if (isFocus()) {
            focusRect.x++;
            focusRect.width -= 2;
            focusRect.y++;
            focusRect.height -= 2;
            gc.drawRectangle(focusRect);
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle)

Example 85 with Pattern

use of java.util.regex.Pattern in project iosched by google.

the class GPlusURLConverter method convert.

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }
    String str = value.getAsString();
    if (str.isEmpty()) {
        return value;
    }
    for (Pattern p : plusRecognizedPatterns) {
        Matcher m = p.matcher(str);
        if (m.find()) {
            return new JsonPrimitive(plusFormat.format(new String[] { m.group(1) }));
        }
    }
    // If URL starts with http/https:
    if (acceptableUrlPattern.matcher(str).matches()) {
        return value;
    }
    // Otherwise, just add https://:
    str = "https://" + str;
    return new JsonPrimitive(str);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) JsonPrimitive(com.google.gson.JsonPrimitive)

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