Example usage for org.apache.commons.lang3 StringUtils countMatches

List of usage examples for org.apache.commons.lang3 StringUtils countMatches

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils countMatches.

Prototype

public static int countMatches(final CharSequence str, final char ch) 

Source Link

Document

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", 0)  = 0 StringUtils.countMatches("abba", 'a')   = 2 StringUtils.countMatches("abba", 'b')  = 2 StringUtils.countMatches("abba", 'x') = 0 

Usage

From source file:functionaltests.RestSchedulerJobTaskTest.java

@Test
public void testJobLogsWithError() throws Exception {
    int executionAttempts = 2;
    String jobId = submitFinishedJob(LogTaskWithError.class, executionAttempts);
    String logs = getResourceAsString("jobs/" + jobId + "/result/log/all");
    System.out.println("job testJobLogsWithError all logs:");
    System.out.println(logs);/*  w  w w. j a v a 2 s.  com*/
    Assert.assertTrue("Logs should contain stdout of the task", logs.contains(LogTask.HELLO_WORLD));
    Assert.assertTrue("Logs should contain stderr of the task", logs.contains(LogTask.ERROR_MESSAGE));

    Assert.assertEquals("Job standard logs should contain " + 1 + " occurrence.", 1,
            StringUtils.countMatches(logs, LogTask.HELLO_WORLD));
    Assert.assertEquals("Job standard logs should contain " + 1 + " occurrence.", 1,
            StringUtils.countMatches(logs, LogTask.ERROR_MESSAGE));

    logs = getResourceAsString("jobs/" + jobId + "/log/full");
    System.out.println("job testJobLogsWithError full logs:");
    System.out.println(logs);
    Assert.assertTrue("Logs should contain output of the task", logs.contains(LogTask.HELLO_WORLD));
    Assert.assertTrue("Logs should contain stderr of the task", logs.contains(LogTask.ERROR_MESSAGE));

    Assert.assertEquals("Job full logs should contain " + executionAttempts + " occurrence.", executionAttempts,
            StringUtils.countMatches(logs, LogTask.HELLO_WORLD));
    Assert.assertEquals("Job full logs should contain " + executionAttempts + " occurrence.", executionAttempts,
            StringUtils.countMatches(logs, LogTask.ERROR_MESSAGE));

    logs = getResourceAsString(
            "jobs/" + jobId + "/tasks/" + getTaskNameForClass(LogTaskWithError.class) + "/result/log/all");
    System.out.println("task testJobLogsWithError all logs:");
    System.out.println(logs);
    Assert.assertEquals("Task logs should contain " + executionAttempts + " occurrence.", executionAttempts,
            StringUtils.countMatches(logs, LogTask.HELLO_WORLD));
    Assert.assertEquals("Task logs should contain " + executionAttempts + " occurrence.", executionAttempts,
            StringUtils.countMatches(logs, LogTask.ERROR_MESSAGE));

    logs = getResourceAsString(
            "jobs/" + jobId + "/tasks/" + getTaskNameForClass(LogTaskWithError.class) + "/result/log/out");
    System.out.println("task testJobLogsWithError out logs:");
    System.out.println(logs);
    Assert.assertEquals("Task stdout logs should contain " + executionAttempts + " occurrence.",
            executionAttempts, StringUtils.countMatches(logs, LogTask.HELLO_WORLD));
    Assert.assertEquals("Task stdout logs should contain " + 0 + " occurrence.", 0,
            StringUtils.countMatches(logs, LogTask.ERROR_MESSAGE));

    logs = getResourceAsString(
            "jobs/" + jobId + "/tasks/" + getTaskNameForClass(LogTaskWithError.class) + "/result/log/err");
    System.out.println("task testJobLogsWithError err logs:");
    System.out.println(logs);
    Assert.assertEquals("Task stderr logs should contain " + 0 + " occurrence.", 0,
            StringUtils.countMatches(logs, LogTask.HELLO_WORLD));
    Assert.assertEquals("Task stderr logs should contain " + executionAttempts + " occurrence.",
            executionAttempts, StringUtils.countMatches(logs, LogTask.ERROR_MESSAGE));
}

From source file:ch.mlutz.plugins.t4e.tapestry.editor.TagContentAssistProcessor.java

protected ICompletionProposal[] computeAttributeCompletionProposals(int offset, IDocument document,
        IFile documentFile) {//w  w w . ja v a  2 s .  c  o  m
    List<ICompletionProposal> result = new ArrayList<ICompletionProposal>();

    ITypedRegion partition = null;
    try {
        partition = document.getPartition(offset);
    } catch (BadLocationException e) {
        log.warn("Could not compute content assist: ", e);
        return result.toArray(new ICompletionProposal[result.size()]);
    }

    // get the partition's content
    String partitionContent;
    try {
        partitionContent = document.get(partition.getOffset(), partition.getLength());
    } catch (BadLocationException e) {
        log.warn("Could not compute content assist:", e);
        return null;
    }

    String contentBefore = partitionContent.substring(0, offset - partition.getOffset());
    String contentAfter = partitionContent.substring(offset - partition.getOffset());

    // check that we are not in an attribute value, since we don't want
    // completion proposals in there
    if (StringUtils.countMatches(contentBefore, "\"") % 2 != 0) {
        return null;
    }

    String attributePrefix = "";
    Matcher attributePrefixMatcher = Pattern.compile("([^\\s<>\"]*)$").matcher(contentBefore);

    if (attributePrefixMatcher.find()) {
        attributePrefix = attributePrefixMatcher.group(1);
    }

    Map<String, String> attributeMap = StringTools.extractAttributeMap(partitionContent);

    String jwcId = attributeMap.get("jwcid");
    List<Parameter> parameterList;
    if (jwcId != null) {
        String[] componentIdArray = jwcId.split("@");
        String componentId = componentIdArray[componentIdArray.length - 1];

        TapestryIndex tapestryIndex = Activator.getDefault().getTapestryIndex();
        IComponent component = tapestryIndex.getStandardComponentMap().get(componentId);

        parameterList = new ArrayList<Parameter>(component.getParameters());
    } else {
        parameterList = new ArrayList<Parameter>();
        parameterList.add(getJwcidParameter());
    }

    // create a Map mapping parameter names to parameters for filtering
    Map<String, Parameter> parameterMap = new HashMap<String, Parameter>();
    List<String> parameterKeyList = new ArrayList<String>();
    for (Parameter p : parameterList) {
        parameterMap.put(p.getName(), p);
        parameterKeyList.add(p.getName());
    }

    // filter the keys and remove filtered ones from map
    List<String> removedParameterKeyList = completionProposalFilter
            .filterStringListAndReturnFiltered(parameterKeyList, attributePrefix);

    for (String filteredKey : removedParameterKeyList) {
        parameterList.remove(parameterMap.get(filteredKey));
    }

    String suffix = "=\"\"";
    if (contentAfter.matches("(?s)^\"(?![\"\\w])")) {
        // a " follows immediately after the caret, so don't add a closing "
        suffix = "=\"";
    }
    int additionalCursorOffset = 2;
    for (Parameter p : parameterList) {
        String completionProposalString = p.getName();
        int attributePrefixLength = attributePrefix.length();
        String displayName = p.getName() + " : " + p.getType().getValue() + " (" + p.getDirection().getValue()
                + ")";

        ICompletionProposal completionProposal = new CompletionProposal(completionProposalString + suffix,
                offset - attributePrefixLength, attributePrefixLength,
                completionProposalString.length() + additionalCursorOffset, Activator.getImage("icons/t4e.png"),
                displayName, getContextInformation("", ""), completionProposalString);

        result.add(completionProposal);
    }

    return result.toArray(new ICompletionProposal[result.size()]);
}

From source file:com.google.dart.engine.services.internal.refactoring.ExtractLocalRefactoringImpl.java

/**
 * @return all occurrences of the source which matches given selection, sorted by offset. First
 *         {@link SourceRange} is same as the given selection. May be empty, but not
 *         <code>null</code>.
 *//*  w ww .  j  ava 2 s  .  co  m*/
private List<SourceRange> getOccurrences() {
    List<SourceRange> occurrences = Lists.newArrayList();
    // prepare selection
    String selectionSource = utils.getText(selectionRange);
    List<com.google.dart.engine.scanner.Token> selectionTokens = TokenUtils.getTokens(selectionSource);
    selectionSource = StringUtils.join(selectionTokens, TOKEN_SEPARATOR);
    // prepare enclosing function
    ASTNode enclosingFunction;
    {
        ASTNode selectionNode = new NodeLocator(selectionStart).searchWithin(unitNode);
        enclosingFunction = CorrectionUtils.getEnclosingExecutableNode(selectionNode);
    }
    // ...we need function
    if (enclosingFunction != null) {
        int functionOffset = enclosingFunction.getOffset();
        String functionSource = utils.getText(functionOffset, enclosingFunction.getLength());
        // prepare function tokens
        List<com.google.dart.engine.scanner.Token> functionTokens = TokenUtils.getTokens(functionSource);
        functionSource = StringUtils.join(functionTokens, TOKEN_SEPARATOR);
        // string part occurrences
        if (stringLiteralPart != null) {
            int occuLength = stringLiteralPart.length();
            for (com.google.dart.engine.scanner.Token token : functionTokens) {
                if (token.getType() == TokenType.STRING) {
                    String tokenValue = token.getLexeme();
                    int lastIndex = 0;
                    while (true) {
                        int index = tokenValue.indexOf(stringLiteralPart, lastIndex);
                        if (index == -1) {
                            break;
                        }
                        lastIndex = index + occuLength;
                        int occuStart = functionOffset + token.getOffset() + index;
                        SourceRange occuRange = rangeStartLength(occuStart, occuLength);
                        occurrences.add(occuRange);
                    }
                }
            }
            return occurrences;
        }
        // find "selection" in "function" tokens
        int lastIndex = 0;
        while (true) {
            // find next occurrence
            int index = functionSource.indexOf(selectionSource, lastIndex);
            if (index == -1) {
                break;
            }
            lastIndex = index + selectionSource.length();
            // find start/end tokens
            int startTokenIndex = StringUtils.countMatches(functionSource.substring(0, index), TOKEN_SEPARATOR);
            int endTokenIndex = StringUtils.countMatches(functionSource.substring(0, lastIndex),
                    TOKEN_SEPARATOR);
            com.google.dart.engine.scanner.Token startToken = functionTokens.get(startTokenIndex);
            com.google.dart.engine.scanner.Token endToken = functionTokens.get(endTokenIndex);
            // add occurrence range
            int occuStart = functionOffset + startToken.getOffset();
            int occuEnd = functionOffset + endToken.getOffset() + endToken.getLength();
            SourceRange occuRange = rangeStartEnd(occuStart, occuEnd);
            if (occuRange.intersects(selectionRange)) {
                occurrences.add(selectionRange);
            } else {
                occurrences.add(occuRange);
            }
        }
    }
    // done
    return occurrences;
}

From source file:com.thomaztwofast.uhc.commands.CmdUhc.java

private String getHelpCenterMessage(String[] a) {
    Jc b = new Jc();
    b.add("--------------------------------------------\n", new int[] { 3 }, 8, null, null);
    switch (a[1].toLowerCase().hashCode()) {
    case 3433103:
        if (cBa.isConfigurationSection("H." + a[2])) {
            b.add(" " + cBa.getString("H." + a[2] + ".T") + "\n \n", new int[] { 0 }, 15, null, null);
            for (int c = 0; c < cBa.getStringList("H." + a[2] + ".D").size(); c++) {
                b.add(" " + (c + 1) + ": ", null, 6, null, null);
                String d = cBa.getStringList("H." + a[2] + ".D").get(c);
                for (int e = 0; e < StringUtils.countMatches(d, "++") / 2; e++) {
                    d = d.replaceFirst("\\++", "\u00a7e").replaceFirst("\\++", "\u00a77");
                }//  w w  w  . j  a  v a 2s.  com
                b.add(d + "\n", null, 7, null, null);
            }
            b.add(" \n <<\n", new int[] { 0 }, 14, "2|/uhc help", "\u00a7aBack to help center");
            break;
        }
    case 100346066:
        if (cBa.isList("I.P." + a[2])) {
            b.add(" " + cBa.getString("I.T") + " ", new int[] { 0 }, 15, null, null);
            b.add("[", new int[] { 0 }, 8, null, null);
            b.add(a[2] + " - " + cBb, new int[] { 0 }, 10, null, null);
            b.add("] \n \n", new int[] { 0 }, 8, null, null);
            for (int c = 0; c < cBa.getStringList("I.P." + a[2]).size(); c++) {
                String[] d = cBa.getStringList("I.P." + a[2]).get(c).split("\\|");
                b.add(" " + d[1] + "\n", null, 10, "2|/uhc help page " + d[0], "\u00A7eOpen?");
            }
            int e = Integer.valueOf(a[2]);
            if (e != 1) {
                b.add(" \n <<", null, 14, "2|/uhc help " + (e - 1), "Previous page");
            }
            if (e != cBb) {
                b.add(" \n >>", null, 14, "2|/uhc help " + (e + 1), "Next page");
            }
            break;
        }
    default:
        b.add(" UHC - Help Center\n \n", new int[] { 0 }, 15, null, null);
        b.add(" Error\n", null, 12, null, null);
        b.add(" Oh no, something went wrong here.\n The help page you are looking for do not exist.\n \n", null,
                7, null, null);
        b.add(" Use '", null, 7, null, null);
        b.add("/UHC Help", null, 10, "2|/uhc help", "\u00A7e\u00A7l>\u00A7r\u00A7a /UHC Help");
        b.add("' for help.", null, 7, null, null);
        break;
    }
    b.add("--------------------------------------------", new int[] { 3 }, 8, null, null);
    return b.o();
}

From source file:info.pancancer.arch3.test.TestWorker.java

@Test
public void testWorker_endlessFromConfig() throws Exception {
    HierarchicalINIConfiguration configObj = new HierarchicalINIConfiguration();
    configObj.addProperty("rabbit.rabbitMQQueueName", "seqware");
    configObj.addProperty("rabbit.rabbitMQHost", "localhost");
    configObj.addProperty("rabbit.rabbitMQUser", "guest");
    configObj.addProperty("rabbit.rabbitMQPass", "guest");
    configObj.addProperty("worker.heartbeatRate", "2.5");
    configObj.addProperty("worker.max-runs", "1");
    configObj.addProperty("worker.preworkerSleep", "1");
    configObj.addProperty("worker.postworkerSleep", "1");
    configObj.addProperty("worker.endless", "true");
    configObj.addProperty("worker.hostUserName", System.getProperty("user.name"));

    byte[] body = setupMessage();
    Delivery testDelivery = new Delivery(mockEnvelope, mockProperties, body);
    setupMockQueue(testDelivery);/*from w  w w .  j  a va2 s.  c om*/
    Mockito.when(Utilities.parseConfig(anyString())).thenReturn(configObj);

    //Because the code that does cleanup in calls resultHandler.waitFor(); we need to actually execute something, even if it does nothing.
    Mockito.doNothing().when(mockExecutor).execute(any(CommandLine.class),
            any(DefaultExecuteResultHandler.class));

    // This is to mock the cleanup command - we don't really want to execute the command for deleting contents of /datastore, at least not when unit testing on a workstation!
    PowerMockito.whenNew(DefaultExecutor.class).withNoArguments().thenReturn(mockExecutor);

    Mockito.when(mockExecHandler.hasResult()).thenReturn(true);
    PowerMockito.whenNew(DefaultExecuteResultHandler.class).withNoArguments().thenReturn(mockExecHandler);

    final FutureTask<String> tester = new FutureTask<>(new Callable<String>() {
        @Override
        public String call() {
            LOG.info("tester thread started");
            try {
                Worker.main(new String[] { "--config", "src/test/resources/workerConfig.ini", "--uuid",
                        "vm123456", "--pidFile", "/var/run/arch3_worker.pid" });
            } catch (CancellationException | InterruptedException e) {
                LOG.error("Exception caught: " + e.getMessage());
                return e.getMessage();
            } catch (Exception e) {
                e.printStackTrace();
                fail("Unexpected exception");
                return null;
            } finally {
                Mockito.verify(mockAppender, Mockito.atLeastOnce()).doAppend(argCaptor.capture());
                String s = appendEventsIntoString(argCaptor.getAllValues());
                return s;
            }
        }

    });

    final Thread killer = new Thread(new Runnable() {

        @Override
        public void run() {
            LOG.info("killer thread started");
            try {
                // The endless worker will not end on its own (because it's endless) so we need to wait a little bit (0.5 seconds) and
                // then kill it as if it were killed by the command-line script (kill_worker_daemon.sh).
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                LOG.error(e.getMessage());
            }
            tester.cancel(true);
        }
    });

    ExecutorService es = Executors.newFixedThreadPool(2);
    es.execute(tester);
    es.execute(killer);

    try {
        tester.get();
    } catch (CancellationException e) {
        Mockito.verify(mockAppender, Mockito.atLeastOnce()).doAppend(argCaptor.capture());
        List<LoggingEvent> tmpList = new LinkedList<LoggingEvent>(argCaptor.getAllValues());
        String output = this.appendEventsIntoString(tmpList);

        assertTrue("--endless flag was detected and set",
                output.contains("The \"--endless\" flag was set, this worker will run endlessly!"));
        int numJobsPulled = StringUtils.countMatches(output, " WORKER IS PREPARING TO PULL JOB FROM QUEUE ");
        LOG.info("Number of jobs attempted: " + numJobsPulled);
        assertTrue("number of jobs attempted > 1", numJobsPulled > 1);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}

From source file:com.zero_x_baadf00d.partialize.Partialize.java

/**
 * Build a JSON object from data taken from the scanner and
 * the given class type and instance./*from w w  w  .  j a  v  a 2s  .co  m*/
 *
 * @param depth         The current depth
 * @param fields        The field names to requests
 * @param clazz         The class of the object to render
 * @param instance      The instance of the object to render
 * @param partialObject The partial JSON document
 * @return A JSON Object
 * @since 16.01.18
 */
private ObjectNode buildPartialObject(final int depth, String fields, final Class<?> clazz,
        final Object instance, final ObjectNode partialObject) {
    if (depth <= this.maximumDepth) {
        if (clazz.isAnnotationPresent(com.zero_x_baadf00d.partialize.annotation.Partialize.class)) {
            final List<String> closedFields = new ArrayList<>();
            List<String> allowedFields = Arrays.asList(clazz
                    .getAnnotation(com.zero_x_baadf00d.partialize.annotation.Partialize.class).allowedFields());
            List<String> defaultFields = Arrays.asList(clazz
                    .getAnnotation(com.zero_x_baadf00d.partialize.annotation.Partialize.class).defaultFields());
            if (allowedFields.isEmpty()) {
                allowedFields = new ArrayList<>();
                for (final Method m : clazz.getDeclaredMethods()) {
                    final String methodName = m.getName();
                    if (methodName.startsWith("get") || methodName.startsWith("has")) {
                        final char[] c = methodName.substring(3).toCharArray();
                        c[0] = Character.toLowerCase(c[0]);
                        allowedFields.add(new String(c));
                    } else if (methodName.startsWith("is")) {
                        final char[] c = methodName.substring(2).toCharArray();
                        c[0] = Character.toLowerCase(c[0]);
                        allowedFields.add(new String(c));
                    }
                }
            }
            if (defaultFields.isEmpty()) {
                defaultFields = allowedFields.stream().map(f -> {
                    if (this.aliases != null && this.aliases.containsValue(f)) {
                        for (Map.Entry<String, String> e : this.aliases.entrySet()) {
                            if (e.getValue().compareToIgnoreCase(f) == 0) {
                                return e.getKey();
                            }
                        }
                    }
                    return f;
                }).collect(Collectors.toList());
            }
            if (fields == null || fields.length() == 0) {
                fields = defaultFields.stream().collect(Collectors.joining(","));
            }
            Scanner scanner = new Scanner(fields);
            scanner.useDelimiter(com.zero_x_baadf00d.partialize.Partialize.SCANNER_DELIMITER);
            while (scanner.hasNext()) {
                String word = scanner.next();
                String args = null;
                if (word.compareTo("*") == 0) {
                    final StringBuilder sb = new StringBuilder();
                    if (scanner.hasNext()) {
                        scanner.useDelimiter("\n");
                        sb.append(",");
                        sb.append(scanner.next());
                    }
                    final Scanner newScanner = new Scanner(
                            allowedFields.stream().filter(f -> !closedFields.contains(f)).map(f -> {
                                if (this.aliases != null && this.aliases.containsValue(f)) {
                                    for (Map.Entry<String, String> e : this.aliases.entrySet()) {
                                        if (e.getValue().compareToIgnoreCase(f) == 0) {
                                            return e.getKey();
                                        }
                                    }
                                }
                                return f;
                            }).collect(Collectors.joining(",")) + sb.toString());
                    newScanner.useDelimiter(com.zero_x_baadf00d.partialize.Partialize.SCANNER_DELIMITER);
                    scanner.close();
                    scanner = newScanner;
                }
                if (word.contains("(")) {
                    while (scanner.hasNext()
                            && (StringUtils.countMatches(word, "(") != StringUtils.countMatches(word, ")"))) {
                        word += "," + scanner.next();
                    }
                    final Matcher m = this.fieldArgsPattern.matcher(word);
                    if (m.find()) {
                        word = m.group(1);
                        args = m.group(2);
                    }
                }
                final String aliasField = word;
                final String field = this.aliases != null && this.aliases.containsKey(aliasField)
                        ? this.aliases.get(aliasField)
                        : aliasField;
                if (allowedFields.stream().anyMatch(
                        f -> f.toLowerCase(Locale.ENGLISH).compareTo(field.toLowerCase(Locale.ENGLISH)) == 0)) {
                    if (this.accessPolicyFunction != null
                            && !this.accessPolicyFunction.apply(new AccessPolicy(clazz, instance, field))) {
                        continue;
                    }
                    closedFields.add(aliasField);
                    try {
                        final Method method = clazz.getMethod("get" + WordUtils.capitalize(field));
                        final Object object = method.invoke(instance);
                        this.internalBuild(depth, aliasField, field, args, partialObject, clazz, object);
                    } catch (IllegalAccessException | InvocationTargetException
                            | NoSuchMethodException ignore) {
                        try {
                            final Method method = clazz.getMethod(field);
                            final Object object = method.invoke(instance);
                            this.internalBuild(depth, aliasField, field, args, partialObject, clazz, object);
                        } catch (IllegalAccessException | InvocationTargetException
                                | NoSuchMethodException ex) {
                            if (this.exceptionConsumer != null) {
                                this.exceptionConsumer.accept(ex);
                            }
                        }
                    }
                }
            }
            return partialObject;
        } else if (instance instanceof Map<?, ?>) {
            if (fields == null || fields.isEmpty() || fields.compareTo("*") == 0) {
                for (Map.Entry<?, ?> e : ((Map<?, ?>) instance).entrySet()) {
                    this.internalBuild(depth, String.valueOf(e.getKey()), String.valueOf(e.getKey()), null,
                            partialObject, e.getValue() == null ? Object.class : e.getValue().getClass(),
                            e.getValue());
                }
            } else {
                final Map<?, ?> tmpMap = (Map<?, ?>) instance;
                for (final String k : fields.split(",")) {
                    if (k.compareTo("*") != 0) {
                        final Object o = tmpMap.get(k);
                        this.internalBuild(depth, k, k, null, partialObject,
                                o == null ? Object.class : o.getClass(), o);
                    } else {
                        for (Map.Entry<?, ?> e : ((Map<?, ?>) instance).entrySet()) {
                            this.internalBuild(depth, String.valueOf(e.getKey()), String.valueOf(e.getKey()),
                                    null, partialObject,
                                    e.getValue() == null ? Object.class : e.getValue().getClass(),
                                    e.getValue());
                        }
                    }
                }
            }
        } else {
            throw new RuntimeException("Can't convert " + clazz.getCanonicalName());
        }
    }
    return partialObject;
}

From source file:com.strider.datadefender.functions.CoreFunctions.java

/**
 * Generates text similar to the text passed as a parameter.
 * //from   w  w  w. j  av a 2  s.  c o  m
 * The method counts the number of paragraphs in the text, generating the
 * same number of "lorem ipsum" paragraphs.  If the text doesn't contain
 * paragraphs, the method counts the number of sentences and generates a
 * similar amount of sentences (+/- 1 sentence).
 * 
 * @param text the text to use as a basis for generation
 * @return the generated lorem ipsum text
 * @throws IOException if an error occurs reading from the lipsum text file
 */
public String lipsumSimilar(final String text) throws IOException {
    final String sParas = text.replaceAll("\r\n", "\n");
    final int nParas = StringUtils.countMatches(sParas, "\n");
    if (nParas > 0) {
        final StringBuilder sb = new StringBuilder();
        for (final String para : sParas.split("\n")) {
            if (para.trim().isEmpty()) {
                sb.append("\r\n");
                continue;
            }
            sb.append(lipsumSimilar(para)).append("\r\n");
        }
        return sb.toString().trim();
    }
    final int nSent = StringUtils.countMatches(text.replaceAll("\\.{2,}", "."), ".");
    return lipsumSentences(Math.max(1, nSent - 1), Math.max(1, nSent + 1));
}

From source file:com.norconex.commons.lang.url.URLNormalizer.java

/**
 * <p>Removes trailing question mark ("?").</p>
 * <code>http://www.example.com/display? &rarr;
 *       http://www.example.com/display </code>
 * @return this instance// w  w w. ja  v a 2 s .c o  m
 */
public URLNormalizer removeTrailingQuestionMark() {
    if (url.endsWith("?") && StringUtils.countMatches(url, "?") == 1) {
        url = StringUtils.removeEnd(url, "?");
    }
    return this;
}

From source file:eu.europa.fisheries.uvms.rules.service.mapper.fact.xpath.util.XPathRepositoryTest.java

private String cutLastAppend(String xpathVal, int lastIndexOfSlashSlashStar) {
    String newXpath = xpathVal.substring(0, lastIndexOfSlashSlashStar);
    String testPath = xpathVal.substring(lastIndexOfSlashSlashStar, xpathVal.length());

    // If it is indexed append, cut the first '(' character.
    if (StringUtils.countMatches(testPath, "[") == 2) {
        newXpath = newXpath.substring(1, newXpath.length());
    }/*from  w w  w. j  a  v  a2  s .com*/

    return newXpath;
}

From source file:com.svi.uzabase.logic.ValidationProcess.java

private List<XMLHolder> validateData(List<XMLHolder> xmlBatchHolder) {
    try {//from   w w w  . jav a2 s . co  m
        int totalCounter = 0;
        //Initialize dictionary
        String dictFileName = "file://./dic/english.jar";
        String configFile = "file://./classes/spellCheck.config";
        BasicDictionary dictionary = new BasicDictionary(dictFileName);
        SpellCheckConfiguration configuration = new SpellCheckConfiguration(configFile);
        BasicSuggester suggester = new BasicSuggester(configuration);
        suggester.attach(dictionary);

        // create SpellCheck object based on configuration and specify Suggester
        SpellCheck spellCheck = new SpellCheck(configuration);
        spellCheck.setSuggester(suggester);
        //set for jprogress bar
        for (XMLHolder h : xmlBatchHolder) {
            totalCounter += h.size();

        }
        progress = new AtomicInteger(0);
        total = new AtomicInteger(totalCounter);
        mf.setJprogressValues(total, progress);
        //validation process begins here
        String[] invalidWords = { "corporation", "inc.", "city", "corp.", "st.", "co.", "ltd." };
        String[] invalidBoardWords = { "other", "oth" };
        String[] validWords = { "loc", "to", "ext", "local" };
        String[] invalidCharacters = { ",", "/", "\\", "[", "]", "\"", ":", "^", "{", "}", "%", "+", "#", "(",
                ")" };
        String[] splitter;
        String tempURL;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/DD");
        SimpleDateFormat fiscalYear = new SimpleDateFormat("MM/DD");
        sdf.setLenient(false);
        Set<String> officerList = new HashSet<>();
        List<Double> percentOwnership = new ArrayList<>();
        UrlValidator urlValidator = new UrlValidator();
        Date date = null;
        for (XMLHolder h : xmlBatchHolder) {
            for (Field f : h) {
                mf.loader("Validating fields: ", false);
                if (!f.getType().equals("none") && !f.getValue().equals("*N/A")) {
                    switch (f.getType()) {
                    case "city":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("Address is empty");
                        } else {
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (cityList.indexOf(f.getValue()) < 0) {
                                f.add("City not found on list!");
                            }
                        }

                        break;
                    case "province":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("Address is empty");
                        } else {
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (provinceList.indexOf(f.getValue()) < 0) {
                                f.add("Province not found on list!");
                            }
                        }

                        break;
                    case "tel":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            //                                    if (f.getValue().matches("[a-z A-Z]+")) {
                            if (f.getValue().matches(".*[a-zA-Z]+.*")) {
                                for (String s : validWords) {
                                    if (!f.getValue().contains(s)) {
                                        f.add("Invalid telephone number");
                                    }
                                }
                            }

                            if (f.getValue().replace(" ", "").replace("-", "").length() < 7
                                    || f.getValue().replace(" ", "").replace("-", "").length() > 8) {
                                f.add("Invalid telephone number length");
                            }

                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            if (StringUtils.countMatches(f.getValue(), "-") > 2) {
                                f.add("Invalid telephone number");
                            }

                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                        }
                        break;
                    case "fax":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            //                                    if (f.getValue().matches("[a-z A-Z]+")) {
                            if (f.getValue().matches(".*[a-zA-Z]+.*")) {
                                for (String s : validWords) {
                                    if (!f.getValue().contains(s)) {
                                        f.add("Invalid fax number");
                                    }
                                }
                            }
                            if (f.getValue().replace(" ", "").length() < 6) {
                                f.add("Invalid fax number");
                            }
                            if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                                f.add("Invalid fax number");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                        }
                        break;
                    case "person":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\- ()]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 30 characters.");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                        }
                        break;
                    case "email":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!EmailValidator.getInstance(true).isValid(f.getValue())) {
                                f.add("Invalid email");
                            }
                        }
                        break;
                    case "website":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().contains("http")) {
                                tempURL = "http://" + f.getValue();
                            } else {
                                tempURL = f.getValue();
                            }
                            if (!urlValidator.isValid(tempURL)) {
                                f.add("Invalid website");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                        }
                        break;
                    case "name":
                        officerList.add(f.getValue());
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 50 characters.");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String s : invalidWords) {
                                if (f.getValue().contains(s)) {
                                    f.add("Contains invalid word: " + s);
                                    break;
                                }
                            }
                        }
                        break;
                    case "stockholder":
                        officerList.add(f.getValue());
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid name");
                            }
                            if (f.getValue().replace(" ", "").length() > 30) {
                                f.add("More than 50 characters.");
                            }
                            if (f.getValue().matches("[a-z ]+")) {
                                f.add("All small caps");
                            }
                            if (f.getValue().matches("\\w+")) {
                                f.add("Only one word");
                            }
                            if (f.getValue().replace(" ", "").length() < 2) {
                                f.add("Invalid name.");
                            }
                            if (hasWhiteSpaceTrailing(f.getValue())) {
                                f.add("has trailing white space ");
                            }
                            for (String s : invalidWords) {
                                if (f.getValue().contains(s)) {
                                    f.add("Contains invalid word: " + s);
                                    break;
                                }
                            }
                        }
                        break;
                    case "board":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (!f.getValue().matches("[a-zA-Z\\.,\\-() ]+")) {
                                f.add("Invalid position");
                            }
                            for (String c : invalidCharacters) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid character [ " + c + " ]");
                                    break;
                                }
                            }
                            for (String c : invalidBoardWords) {
                                if (f.getValue().contains(c)) {
                                    f.add("Contains invalid word [ " + c + " ]");
                                    break;
                                }
                            }

                            if (f.getValue().equalsIgnoreCase("N") || f.getValue().equalsIgnoreCase("Y")) {
                                f.add("is letter " + f.getValue() + " only");
                            }
                            if (Character.isLowerCase(f.getValue().charAt(0))) {
                                f.add("starts with a lower case letter");
                            }

                            spellCheck.setText(f.getValue(), Constants.DOC_TYPE_TEXT, "en");
                            spellCheck.check();
                            if (spellCheck.hasMisspelt()) {
                                f.add("word is misspelled.");
                            }

                        }
                        break;
                    case "corporation":
                        if (companyList.indexOf(f.getValue().toUpperCase()) < 0) {
                            f.add("Company name not found on table.");
                        }
                        break;
                    case "sec":
                        if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                            f.add("Invalid SEC number");
                        }
                        if (f.getValue().replace(" ", "").length() > 9) {
                            f.add("SEC number more than 9 digits.");
                        }
                        if (hasWhiteSpaceTrailing(f.getValue())) {
                            f.add("SEC has trailing white space.");
                        }
                        for (String c : invalidCharacters) {
                            if (f.getValue().contains(c)) {
                                f.add("Contains invalid character [ " + c + " ]");
                                break;
                            }
                        }
                        break;
                    case "tin":
                        if (f.getValue().isEmpty() || f.getValue().equals("")) {
                            f.add("TIN is empty");
                        }
                        if (hasWhiteSpaceTrailing(f.getValue())) {
                            f.add("TIN has trailing white space.");
                        }
                        if (!f.getValue().matches("[0-9]+")) {
                            f.add("invalid TIN number");
                        }
                        if (f.getValue().replace(" ", "").replace("-", "").length() > 12
                                || f.getValue().replace(" ", "").replace("-", "").length() < 9) {
                            f.add("TIN number invalid length.");
                        }
                        if (StringUtils.countMatches(f.getValue(), "-") > 1) {
                            f.add("Invalid TIN number");
                        }
                        for (String c : invalidCharacters) {
                            if (f.getValue().contains(c)) {
                                f.add("Contains invalid character [ " + c + " ]");
                                break;
                            }
                        }
                        break;
                    case "nationality":
                        if (!f.getValue().isEmpty() || !f.getValue().equals("")) {
                            if (nationalityList.indexOf(f.getValue()) < 0) {
                                f.add("nationality is misspelled.");
                            }
                        }
                        break;
                    case "purpose":
                        splitter = f.getValue().split(" ");
                        for (int i = 0; i < splitter.length; i++) {
                            spellCheck.setText(splitter[i], Constants.DOC_TYPE_TEXT, "en");
                            spellCheck.check();
                            if (spellCheck.hasMisspelt()) {
                                f.add("word is misspelled. ( " + spellCheck.getMisspelt() + " )");
                            }
                        }
                        break;
                    case "periodCovered":
                        try {
                            date = sdf.parse(f.getValue());
                            if (!f.getValue().equals(sdf.format(date))) {
                                f.add("Invalid date format");
                            }
                        } catch (ParseException ex) {
                            f.add("Invalid date format");
                        }
                        break;
                    case "fiscalYear":
                        try {
                            date = fiscalYear.parse(f.getValue());
                            if (!f.getValue().equals(sdf.format(date))) {
                                f.add("Invalid date format");
                            }
                        } catch (ParseException ex) {
                            f.add("Invalid date format");
                        }
                        break;
                    case "position":
                        if (f.getValue().contains("\\d+")) {
                            f.add("Invalid position/designation");
                        }
                        if (f.getValue().replace(" ", "").length() > 10
                                || f.getValue().replace(" ", "").length() < 3) {
                            f.add("More than 30 characters.");
                        }
                        break;
                    case "shareType":
                        if (f.getValue().toLowerCase().contains("total")) {
                            f.add("Share type contains total.");
                        }

                        if (f.getValue().replace(" ", "").length() > 20) {
                            f.add("Share type More than 20 characters.");
                        }
                        break;
                    case "ownership":
                        percentOwnership.add(Double.parseDouble(f.getValue()));
                        if (Double.parseDouble(f.getValue()) > 100) {
                            f.add("Percent ownership more than 100%");
                        }
                        break;
                    default:
                        break;
                    }
                } else if (f.getType().equals("tin") && f.getValue().equals("*N/A")) {
                    f.add("TIN is N/A");
                }
            }
        }

    } catch (EncryptedDocumentException | SuggesterException ex) {
        Logger.getLogger(ValidationProcess.class.getName()).log(Level.SEVERE, null, ex);
    }
    return xmlBatchHolder;
}