Example usage for java.lang StringBuilder indexOf

List of usage examples for java.lang StringBuilder indexOf

Introduction

In this page you can find the example usage for java.lang StringBuilder indexOf.

Prototype

@Override
    public int indexOf(String str) 

Source Link

Usage

From source file:org.vafer.jdeb.DataBuilderTestCase.java

/**
 * Checks if the file paths in the md5sums file use only unix file separators
 * (this test can only fail on Windows)/* ww  w .j a v a2 s.c  o  m*/
 */
public void testBuildDataWithFileSet() throws Exception {
    DataBuilder builder = new DataBuilder(new NullConsole());

    Project project = new Project();
    project.setCoreLoader(getClass().getClassLoader());
    project.init();

    FileSet fileset = new FileSet();
    fileset.setDir(new File(getClass().getResource("deb/data").toURI()));
    fileset.setIncludes("**/*");
    fileset.setProject(project);

    StringBuilder md5s = new StringBuilder();
    builder.buildData(Arrays.asList((DataProducer) new DataProducerFileSet(fileset)),
            new File("target/data.tar"), md5s, Compression.GZIP);

    assertTrue("empty md5 file", md5s.length() > 0);
    assertFalse("windows path separator found", md5s.indexOf("\\") != -1);
}

From source file:org.pentaho.amazon.AbstractAmazonJobExecutor.java

private String getKeyFromS3StagingDir() throws KettleFileException, FileSystemException {

    String pathToStagingDir = getS3FileObjectPath();
    StringBuilder sb = new StringBuilder(pathToStagingDir);

    sb.replace(0, 1, "");
    if (sb.indexOf("/") == -1) {
        return null;
    }/*w  w w.j ava 2 s .  c o m*/
    sb.replace(0, sb.indexOf("/") + 1, "");

    if (sb.length() > 0) {
        return sb.toString();
    } else {
        return null;
    }
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

protected void colorStyledDocument(final DefaultStyledDocument document) {
    EventQueue.invokeLater(new Runnable() {

        @Override//w  w w .  j  a  v a2s.  co  m
        public void run() {
            String input = "";
            try {
                input = document.getText(0, document.getLength());
            } catch (BadLocationException ex) {
                Logger.getLogger(SimpleOrangeTextPane.class.getName()).log(Level.SEVERE, null, ex);
            }

            StringBuilder inputMut = new StringBuilder(input);
            String[] split = StringUtils.split(inputMut.toString());
            int i = 0;
            for (String string : split) {
                int start = inputMut.indexOf(string);
                int end = start + string.length();
                inputMut.replace(start, end, StringUtils.repeat(" ", string.length()));
                document.setCharacterAttributes(start, string.length(), styles[i++ % styles.length], true);
            }
        }
    });
}

From source file:com.huawei.unibi.molap.csvreaderstep.CsvInput.java

/**
 * This method is borrowed from TextFileInput
 * //  w  ww . j a  v a 2s . com
 * @param log
 * @param line
 * @param delimiter
 * @param enclosure
 * @param escapeCharacter
 * @return
 * @throws KettleException
 */
public static final String[] guessStringsFromLine(LogChannelInterface log, String line, String delimiter,
        String enclosure, String escapeCharacter) throws KettleException {
    List<String> strings = new ArrayList<String>(MolapCommonConstants.CONSTANT_SIZE_TEN);
    //        int fieldnr;

    String pol; // piece of line

    try {
        if (line == null) {
            return null;
        }

        // Split string in pieces, only for CSV!

        //      fieldnr = 0;
        int pos = 0;
        int length = line.length();
        boolean dencl = false;

        int lenEncl = (enclosure == null ? 0 : enclosure.length());
        int lenEsc = (escapeCharacter == null ? 0 : escapeCharacter.length());

        while (pos < length) {
            int from = pos;
            int next;

            boolean enclFound;
            boolean containsEscapedEnclosures = false;
            boolean containsEscapedSeparators = false;

            // Is the field beginning with an enclosure?
            // "aa;aa";123;"aaa-aaa";000;...
            if (lenEncl > 0 && line.substring(from, from + lenEncl).equalsIgnoreCase(enclosure)) {
                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRowTitle"), //$NON-NLS-1$
                            BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRow", //$NON-NLS-1$
                                    line.substring(from, from + lenEncl)));
                }
                enclFound = true;
                int p = from + lenEncl;

                boolean isEnclosure = lenEncl > 0 && p + lenEncl < length
                        && line.substring(p, p + lenEncl).equalsIgnoreCase(enclosure);
                boolean isEscape = lenEsc > 0 && p + lenEsc < length
                        && line.substring(p, p + lenEsc).equalsIgnoreCase(escapeCharacter);

                boolean enclosureAfter = false;

                // Is it really an enclosure? See if it's not repeated twice or escaped!
                if ((isEnclosure || isEscape) && p < length - 1) {
                    String strnext = line.substring(p + lenEncl, p + 2 * lenEncl);
                    if (strnext.equalsIgnoreCase(enclosure)) {
                        p++;
                        enclosureAfter = true;
                        dencl = true;

                        // Remember to replace them later on!
                        if (isEscape) {
                            containsEscapedEnclosures = true;
                        }
                    }
                }

                // Look for a closing enclosure!
                while ((!isEnclosure || enclosureAfter) && p < line.length()) {
                    p++;
                    enclosureAfter = false;
                    isEnclosure = lenEncl > 0 && p + lenEncl < length
                            && line.substring(p, p + lenEncl).equals(enclosure);
                    isEscape = lenEsc > 0 && p + lenEsc < length
                            && line.substring(p, p + lenEsc).equals(escapeCharacter);

                    // Is it really an enclosure? See if it's not repeated twice or escaped!
                    if ((isEnclosure || isEscape) && p < length - 1) // Is
                    {
                        String strnext = line.substring(p + lenEncl, p + 2 * lenEncl);
                        if (strnext.equals(enclosure)) {
                            p++;
                            enclosureAfter = true;
                            dencl = true;

                            // Remember to replace them later on!
                            if (isEscape) {
                                containsEscapedEnclosures = true; // remember
                            }
                        }
                    }
                }

                if (p >= length) {
                    next = p;
                } else {
                    next = p + lenEncl;
                }

                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRowTitle"), //$NON-NLS-1$
                            BaseMessages.getString(PKG, "CsvInput.Log.EndOfEnclosure", "" + p)); //$NON-NLS-1$ //$NON-NLS-2$
                }
            } else {
                enclFound = false;
                boolean found = false;
                int startpoint = from;
                int tries = 1;
                do {
                    next = line.indexOf(delimiter, startpoint);

                    // See if this position is preceded by an escape character.
                    if (lenEsc > 0 && next - lenEsc > 0) {
                        String before = line.substring(next - lenEsc, next);

                        if (escapeCharacter != null && escapeCharacter.equals(before)) {
                            // take the next separator, this one is escaped...
                            startpoint = next + 1;
                            tries++;
                            containsEscapedSeparators = true;
                        } else {
                            found = true;
                        }
                    } else {
                        found = true;
                    }
                } while (!found && next >= 0);
            }
            if (next == -1) {
                next = length;
            }

            if (enclFound) {
                pol = line.substring(from + lenEncl, next - lenEncl);
                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRowTitle"), //$NON-NLS-1$
                            BaseMessages.getString(PKG, "CsvInput.Log.EnclosureFieldFound", "" + pol)); //$NON-NLS-1$ //$NON-NLS-2$
                }
            } else {
                pol = line.substring(from, next);
                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRowTitle"), //$NON-NLS-1$
                            BaseMessages.getString(PKG, "CsvInput.Log.NormalFieldFound", "" + pol)); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }

            if (dencl) {
                StringBuilder sbpol = new StringBuilder(pol);
                int idx = sbpol.indexOf(enclosure + enclosure);
                while (idx >= 0) {
                    sbpol.delete(idx, idx + (enclosure == null ? 0 : enclosure.length()));
                    idx = sbpol.indexOf(enclosure + enclosure);
                }
                pol = sbpol.toString();
            }

            //  replace the escaped enclosures with enclosures... 
            if (containsEscapedEnclosures) {
                String replace = escapeCharacter + enclosure;
                String replaceWith = enclosure;

                pol = Const.replace(pol, replace, replaceWith);
            }

            //replace the escaped separators with separators...
            if (containsEscapedSeparators) {
                String replace = escapeCharacter + delimiter;
                String replaceWith = delimiter;

                pol = Const.replace(pol, replace, replaceWith);
            }

            // Now add pol to the strings found!
            strings.add(pol);

            pos = next + delimiter.length();
            //        fieldnr++;
        }
        if (pos == length) {
            if (log.isRowLevel()) {
                log.logRowlevel(BaseMessages.getString(PKG, "CsvInput.Log.ConvertLineToRowTitle"), //$NON-NLS-1$
                        BaseMessages.getString(PKG, "CsvInput.Log.EndOfEmptyLineFound")); //$NON-NLS-1$
            }
            strings.add(""); //$NON-NLS-1$
            //                  fieldnr++;
        }
    } catch (Exception e) {
        throw new KettleException(
                BaseMessages.getString(PKG, "CsvInput.Log.Error.ErrorConvertingLine", e.toString()), e); //$NON-NLS-1$
    }

    return strings.toArray(new String[strings.size()]);
}

From source file:ca.uhn.fhir.rest.method.HttpGetClientInvocation.java

@Override
public IHttpRequest asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams,
        EncodingEnum theEncoding, Boolean thePrettyPrint) {
    StringBuilder b = new StringBuilder();

    if (!myUrlPath.contains("://")) {
        b.append(theUrlBase);/*from   w  ww  .  java 2s  .  c om*/
        if (!theUrlBase.endsWith("/")) {
            b.append('/');
        }
    }
    b.append(myUrlPath);

    boolean first = b.indexOf("?") == -1;
    for (Entry<String, List<String>> next : myParameters.entrySet()) {
        if (next.getValue() == null || next.getValue().isEmpty()) {
            continue;
        }
        String nextKey = next.getKey();
        for (String nextValue : next.getValue()) {
            first = addQueryParameter(b, first, nextKey, nextValue);
        }
    }

    appendExtraParamsWithQuestionMark(theExtraParams, b, first);

    return super.createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.GET);
}

From source file:edu.ku.brc.af.ui.forms.BrowserLauncherBtn.java

protected void buildAndOpenURL() {
    StringBuilder urlBuilder = new StringBuilder(url);

    if (url.startsWith("[")) {
        // replace the URL prefix ([prefix]) at the start with the value from the DB 
        AppPreferences remotePrefs = AppPreferences.getRemote();

        int endingBracketIndex = urlBuilder.indexOf("]");
        String urlPrefix = urlBuilder.substring(1, endingBracketIndex);
        String urlPrefixValue = remotePrefs.get("URL_Prefix." + urlPrefix, null);

        if (urlPrefixValue == null) {
            String errorMsg = String.format(getResourceString("WLLB_CANNOT_BUILD_URL"),
                    new Object[] { urlPrefix });
            UIRegistry.getStatusBar().setErrorMessage(errorMsg);
            return;
        }/*w  w  w  .  java  2  s.  co  m*/
        urlBuilder.replace(0, endingBracketIndex + 1, urlPrefixValue);
    }

    try {
        AttachmentUtils.openURI(new URI(urlBuilder.toString()));
    } catch (Exception e) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(BrowserLauncherBtn.class, e);
        String errorMsg = String.format(getResourceString("ERROR_CANT_OPEN_URL"),
                new Object[] { urlBuilder.toString() });
        UIRegistry.getStatusBar().setErrorMessage(errorMsg, e);
    }
}

From source file:com.datatorrent.lib.io.fs.FSInputModuleAppTest.java

private void verifyFileContents(File[] files, String expectedData) throws IOException {
    StringBuilder filesData = new StringBuilder();
    for (File file : files) {
        filesData.append(FileUtils.readFileToString(file));
    }//from  ww  w.j  a  v  a2  s .c  o  m
    Assert.assertTrue("File data doesn't contain expected text", filesData.indexOf(expectedData) > -1);
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtilsTest.java

License:asdf

@Test(dataProvider = "userInfoDP")
public void normalizeUserInfo(String inp, String exp) throws URIException {
    StringBuilder sb = new StringBuilder(inp);
    String cmp = URLNormalizerUtils.normalizeUserInfo(sb, 0,
            sb.indexOf("@") >= 0 ? sb.indexOf("@") : sb.length());
    Assert.assertEquals(cmp, exp);/*  w ww.ja v a 2  s. c om*/
}

From source file:de.itsvs.cwtrpc.controller.CacheControlFilter.java

protected PreparedCacheControlUriConfig getPreparedUriConfig(HttpServletRequest request) {
    final String contextPath;
    final StringBuilder sb;
    final String uri;

    contextPath = request.getContextPath();
    sb = new StringBuilder(request.getRequestURI());
    if ((contextPath.length() > 0) && (sb.indexOf(contextPath) == 0)) {
        sb.delete(0, contextPath.length());
    }/*  w  w w . j  ava2  s .  co m*/
    if ((sb.length() == 0) || (sb.charAt(0) != '/')) {
        sb.insert(0, '/');
    }
    uri = sb.toString();

    if (log.isDebugEnabled()) {
        log.debug("Checked URI is '" + uri + "'");
    }
    for (PreparedCacheControlUriConfig config : getUriConfigs()) {
        if ((config.getMethod() == null) || config.getMethod().name().equals(request.getMethod())) {
            if (config.getValue().matches(uri)) {
                if (log.isDebugEnabled()) {
                    log.debug("Matching configuration for URI '" + uri + "' and method '" + request.getMethod()
                            + "' is " + config);
                }
                return config;
            }
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("No matching configuration for URI '" + uri + "' and method '" + request.getMethod() + "'");
    }
    return null;
}

From source file:com.elasticbox.jenkins.k8s.repositories.error.RepositoryException.java

private void appendMessage(StringBuilder msg, Throwable initialCause) {
    if (initialCause instanceof KubernetesClientException) {
        KubernetesClientException kce = (KubernetesClientException) initialCause;
        final Status status = kce.getStatus();
        if (status != null) {
            msg.append(status.getMessage()).append(' ');
        }/*ww w .  j  av a2s.c  o  m*/
    } else if (msg.indexOf(initialCause.toString()) == -1) {
        msg.append(initialCause.toString()).append(' ');
    }
}