Search in sources :

Example 26 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project robovm by robovm.

the class PatternTest method testCompileStringInt.

public void testCompileStringInt() {
    /*
         * these tests are needed to verify that appropriate exceptions are
         * thrown
         */
    String pattern = "b)a";
    try {
        Pattern.compile(pattern);
        fail("Expected a PatternSyntaxException when compiling pattern: " + pattern);
    } catch (PatternSyntaxException e) {
    // pass
    }
    pattern = "bcde)a";
    try {
        Pattern.compile(pattern);
        fail("Expected a PatternSyntaxException when compiling pattern: " + pattern);
    } catch (PatternSyntaxException e) {
    // pass
    }
    pattern = "bbg())a";
    try {
        Pattern pat = Pattern.compile(pattern);
        fail("Expected a PatternSyntaxException when compiling pattern: " + pattern);
    } catch (PatternSyntaxException e) {
    // pass
    }
    pattern = "cdb(?i))a";
    try {
        Pattern pat = Pattern.compile(pattern);
        fail("Expected a PatternSyntaxException when compiling pattern: " + pattern);
    } catch (PatternSyntaxException e) {
    // pass
    }
    /*
         * This pattern should compile - HARMONY-2127
         * icu4c doesn't support canonical equivalence.
         */
    //        pattern = "x(?c)y";
    //        Pattern.compile(pattern);
    /*
         * this pattern doesn't match any string, but should be compiled anyway
         */
    pattern = "(b\\1)a";
    Pattern.compile(pattern);
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 27 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project robovm by robovm.

the class PatternTest method testOrphanQuantifiers2.

public void testOrphanQuantifiers2() {
    try {
        Pattern pat = Pattern.compile("\\d+*");
        fail("PatternSyntaxException expected");
    } catch (PatternSyntaxException pse) {
    }
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 28 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project bazel by bazelbuild.

the class ResourceUsageAnalyzer method keepPossiblyReferencedResources.

private void keepPossiblyReferencedResources() {
    if ((!foundGetIdentifier && !foundWebContent) || strings == null) {
        // to worry about string references to resources
        return;
    }
    if (!model.isSafeMode()) {
        // explicitly mark them as kept if necessary instead
        return;
    }
    List<String> sortedStrings = new ArrayList<String>(strings);
    Collections.sort(sortedStrings);
    logger.fine("android.content.res.Resources#getIdentifier present: " + foundGetIdentifier);
    logger.fine("Web content present: " + foundWebContent);
    logger.fine("Referenced Strings:");
    for (String string : sortedStrings) {
        string = string.trim().replace("\n", "\\n");
        if (string.length() > 40) {
            string = string.substring(0, 37) + "...";
        } else if (string.isEmpty()) {
            continue;
        }
        logger.fine("  " + string);
    }
    int shortest = Integer.MAX_VALUE;
    Set<String> names = Sets.newHashSetWithExpectedSize(50);
    for (Resource resource : model.getResources()) {
        String name = resource.name;
        names.add(name);
        int length = name.length();
        if (length < shortest) {
            shortest = length;
        }
    }
    for (String string : strings) {
        if (string.length() < shortest) {
            continue;
        }
        //  (4) If foundWebContent is true, look for android_res/ URL strings as well
        if (foundWebContent) {
            Resource resource = model.getResourceFromFilePath(string);
            if (resource != null) {
                ResourceUsageModel.markReachable(resource);
                continue;
            } else {
                int start = 0;
                int slash = string.lastIndexOf('/');
                if (slash != -1) {
                    start = slash + 1;
                }
                int dot = string.indexOf('.', start);
                String name = string.substring(start, dot != -1 ? dot : string.length());
                if (names.contains(name)) {
                    for (Map<String, Resource> map : model.getResourceMaps()) {
                        resource = map.get(name);
                        if (resource != null) {
                            logger.fine(String.format("Marking %s used because it matches string pool constant %s", resource, string));
                        }
                        ResourceUsageModel.markReachable(resource);
                    }
                }
            }
        }
        // Look for normal getIdentifier resource URLs
        int n = string.length();
        boolean justName = true;
        boolean formatting = false;
        boolean haveSlash = false;
        for (int i = 0; i < n; i++) {
            char c = string.charAt(i);
            if (c == '/') {
                haveSlash = true;
                justName = false;
            } else if (c == '.' || c == ':' || c == '%') {
                justName = false;
                if (c == '%') {
                    formatting = true;
                }
            } else if (!Character.isJavaIdentifierPart(c)) {
                // the {@link #referencedString} method
                assert false : string;
                break;
            }
        }
        String name;
        if (justName) {
            // Check name (below)
            name = string;
            // getResources().getIdentifier("ic_video_codec_" + codecName, "drawable", ...)
            for (Resource resource : model.getResources()) {
                if (resource.name.startsWith(name)) {
                    logger.fine(String.format("Marking %s used because its prefix matches string pool constant %s", resource, string));
                    ResourceUsageModel.markReachable(resource);
                }
            }
        } else if (!haveSlash) {
            if (formatting) {
                //   int res = getContext().getResources().getIdentifier(name, "drawable", ...)
                try {
                    Pattern pattern = Pattern.compile(convertFormatStringToRegexp(string));
                    for (Resource resource : model.getResources()) {
                        if (pattern.matcher(resource.name).matches()) {
                            logger.fine(String.format("Marking %s used because it format-string matches string pool constant %s", resource, string));
                            ResourceUsageModel.markReachable(resource);
                        }
                    }
                } catch (PatternSyntaxException ignored) {
                // Might not have been a formatting string after all!
                }
            }
            //noinspection UnnecessaryContinue
            continue;
        } else {
            // Try to pick out the resource name pieces; if we can find the
            // resource type unambiguously; if not, just match on names
            int slash = string.indexOf('/');
            // checked with haveSlash above
            assert slash != -1;
            name = string.substring(slash + 1);
            if (name.isEmpty() || !names.contains(name)) {
                continue;
            }
            // See if have a known specific resource type
            if (slash > 0) {
                int colon = string.indexOf(':');
                String typeName = string.substring(colon != -1 ? colon + 1 : 0, slash);
                ResourceType type = ResourceType.getEnum(typeName);
                if (type == null) {
                    continue;
                }
                Resource resource = model.getResource(type, name);
                if (resource != null) {
                    logger.fine(String.format("Marking %s used because it matches string pool constant %s", resource, string));
                }
                ResourceUsageModel.markReachable(resource);
                continue;
            }
        // fall through and check the name
        }
        if (names.contains(name)) {
            for (Map<String, Resource> map : model.getResourceMaps()) {
                Resource resource = map.get(name);
                if (resource != null) {
                    logger.fine(String.format("Marking %s used because it matches string pool constant %s", resource, string));
                }
                ResourceUsageModel.markReachable(resource);
            }
        } else if (Character.isDigit(name.charAt(0))) {
            // "android.resource://com.android.alarmclock/2130837524".
            try {
                int id = Integer.parseInt(name);
                if (id != 0) {
                    ResourceUsageModel.markReachable(model.getResource(id));
                }
            } catch (NumberFormatException e) {
            // pass
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) Resource(com.android.tools.lint.checks.ResourceUsageModel.Resource) ResourceType(com.android.resources.ResourceType) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 29 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project zaproxy by zaproxy.

the class ManageAddOnsDialog method createFilterPanel.

private static JPanel createFilterPanel(final JXTable table) {
    JPanel filterPanel = new JPanel();
    filterPanel.setLayout(new GridBagLayout());
    JLabel filterLabel = new JLabel(Constant.messages.getString("cfu.label.addons.filter"));
    final JTextField filterTextField = new JTextField();
    filterLabel.setLabelFor(filterTextField);
    filterPanel.add(filterLabel, LayoutHelper.getGBC(0, 0, 1, 0.0D));
    filterPanel.add(filterTextField, LayoutHelper.getGBC(1, 0, 1, 1.0D));
    String tooltipText = Constant.messages.getString("cfu.label.addons.filter.tooltip");
    filterLabel.setToolTipText(tooltipText);
    filterTextField.setToolTipText(tooltipText);
    // Set filter listener
    filterTextField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateFilter();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateFilter();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateFilter();
        }

        public void updateFilter() {
            String filterText = filterTextField.getText();
            if (filterText.isEmpty()) {
                table.setRowFilter(null);
                filterTextField.setForeground(UIManager.getColor("TextField.foreground"));
            } else {
                try {
                    table.setRowFilter(RowFilter.regexFilter("(?i)" + filterText));
                    filterTextField.setForeground(UIManager.getColor("TextField.foreground"));
                } catch (PatternSyntaxException e) {
                    filterTextField.setForeground(Color.RED);
                }
            }
        }
    });
    return filterPanel;
}
Also used : JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) GridBagLayout(java.awt.GridBagLayout) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 30 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project uavstack by uavorg.

the class MonitorUrlFilterMgr method refreshList.

public void refreshList(String listname) {
    String patternConfig = System.getProperty(listname);
    if (patternConfig != null) {
        try {
            Pattern pattern = Pattern.compile(patternConfig);
            bwlistRepository.put(listname, pattern);
        } catch (PatternSyntaxException e) {
            UAVServer.instance().getLog().error("PatternSyntax Illegal", e);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

PatternSyntaxException (java.util.regex.PatternSyntaxException)355 Pattern (java.util.regex.Pattern)190 Matcher (java.util.regex.Matcher)115 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)25 List (java.util.List)19 File (java.io.File)14 Map (java.util.Map)12 HashMap (java.util.HashMap)9 URL (java.net.URL)7 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 BufferedReader (java.io.BufferedReader)6 Collection (java.util.Collection)6 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 SQLException (java.sql.SQLException)4 LinkedHashMap (java.util.LinkedHashMap)4