Example usage for java.lang String replaceFirst

List of usage examples for java.lang String replaceFirst

Introduction

In this page you can find the example usage for java.lang String replaceFirst.

Prototype

public String replaceFirst(String regex, String replacement) 

Source Link

Document

Replaces the first substring of this string that matches the given regular expression with the given replacement.

Usage

From source file:com.norconex.collector.http.robot.impl.DefaultRobotsTxtProvider.java

@Override
public synchronized RobotsTxt getRobotsTxt(DefaultHttpClient httpClient, String url) {
    String baseURL = getBaseURL(url);
    RobotsTxt robotsTxt = robotsTxtCache.get(baseURL);
    if (robotsTxt != null) {
        return robotsTxt;
    }/*w  ww. ja  v a2 s  .  c om*/

    String userAgent = ((String) httpClient.getParams().getParameter(CoreProtocolPNames.USER_AGENT))
            .toLowerCase();
    String robotsURL = baseURL + "/robots.txt";
    HttpGet method = new HttpGet(robotsURL);
    List<String> sitemapLocations = new ArrayList<String>();
    List<IURLFilter> filters = new ArrayList<IURLFilter>();
    MutableFloat crawlDelay = new MutableFloat(RobotsTxt.UNSPECIFIED_CRAWL_DELAY);
    try {
        HttpResponse response = httpClient.execute(method);
        InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
        BufferedReader br = new BufferedReader(isr);
        boolean agentAlreadyMatched = false;
        boolean doneWithAgent = false;
        String line;
        while ((line = br.readLine()) != null) {
            String key = line.replaceFirst("(.*?)(:.*)", "$1").trim();
            String value = line.replaceFirst("(.*?:)(.*)", "$2").trim();
            if ("sitemap".equalsIgnoreCase(key)) {
                sitemapLocations.add(value);
            }
            if (!doneWithAgent) {
                if ("user-agent".equalsIgnoreCase(key)) {
                    if (matchesUserAgent(userAgent, value)) {
                        agentAlreadyMatched = true;
                    } else if (agentAlreadyMatched) {
                        doneWithAgent = true;
                    }
                }
                if (agentAlreadyMatched) {
                    parseAgentLines(baseURL, filters, crawlDelay, key, value);
                }
            }
        }
        isr.close();
    } catch (Exception e) {
        LOG.warn("Not able to obtain robots.txt at: " + robotsURL, e);
    }

    robotsTxt = new RobotsTxt(filters.toArray(new IURLFilter[] {}), crawlDelay.floatValue());
    robotsTxtCache.put(baseURL, robotsTxt);
    return robotsTxt;
}

From source file:com.codepine.api.testrail.model.Case.java

/**
 * Add a custom field.// w w  w.ja va2s .  c  o m
 *
 * @param key   the name of the custom field with or without "custom_" prefix
 * @param value the value of the custom field
 * @return case instance for chaining
 */
public Case addCustomField(String key, Object value) {
    if (customFields == null) {
        customFields = new HashMap<>();
    }
    customFields.put(key.replaceFirst(CUSTOM_FIELD_KEY_PREFIX, ""), value);
    return this;
}

From source file:com.blackducksoftware.integration.build.utils.FilePathGavExtractor.java

public Gav getMavenPathGav(final String filePath, final String localMavenRepoPath) {

    if (filePath == null || localMavenRepoPath == null) {
        return null;
    }// w w w. j  av a  2 s. c  o  m

    final String cleanedFilePath = filePath.replaceFirst(localMavenRepoPath, "");
    final String[] cleanedFilePathSegments = cleanedFilePath.split(File.separator);

    String[] groupIdSegments;
    if (cleanedFilePathSegments[0].equals("")) {
        if (cleanedFilePathSegments.length < 4) {
            return null;
        }
        groupIdSegments = Arrays.copyOfRange(cleanedFilePathSegments, 1, cleanedFilePathSegments.length - 3);
    } else {
        if (cleanedFilePathSegments.length < 3) {
            return null;
        }
        groupIdSegments = Arrays.copyOfRange(cleanedFilePathSegments, 0, cleanedFilePathSegments.length - 3);
    }

    final String groupId = StringUtils.join(groupIdSegments, ".");
    final String artifactId = cleanedFilePathSegments[cleanedFilePathSegments.length - 3];
    final String version = cleanedFilePathSegments[cleanedFilePathSegments.length - 2];

    return new Gav(groupId, artifactId, version);

}

From source file:fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter.java

public static String getArchiveName(File fileToImport) {
    String fileName = fileToImport.getName();
    String extension = FilenameUtils.getExtension(fileName);
    String baseName = FilenameUtils.getBaseName(fileName);
    String shortName = baseName.replaceFirst("_[0-9]*$", "");
    return shortName + "." + extension;
}

From source file:com.norconex.collector.http.robot.impl.StandardRobotsTxtProvider.java

private String cleanLineFromComments(String line) {
    if (line.matches(".*\\s+#.*")) {
        line = line.replaceFirst("\\s+#.*", "");
    }//ww  w. j  a v  a  2 s . c  om
    return line;
}

From source file:gov.nih.nci.caxchange.messaging.RegistrationIntegrationTest.java

private String getUpdateMsg() {
    String msg = getMsg();
    msg = msg.replaceFirst("Create Participant Registration", "Update Participant Registration");
    msg = msg.replaceAll("FirstName", "updFirstName");
    return msg;/*from   w  ww  . ja va 2s  . c o  m*/
}

From source file:com.googlecode.osde.internal.igoogle.IgHostingUtil.java

/**
 * Forms url for previewing an OpenSocial gadget.
 *
 * @return url for previewing/* w  ww.  j a va  2  s. co  m*/
 * @throws IgException
 */
public static String formPreviewOpenSocialGadgetUrl(String hostedFileUrl, boolean useCanvasView, String sid)
        throws IgException {
    String requestUrl = URL_IG_PREVIEW_OPEN_SOCIAL_GADGET + hostedFileUrl;
    logger.fine("requestUrl: " + requestUrl);
    String response = retrieveHttpResponseAsString(requestUrl, sid);
    logger.fine("response:\n" + response);

    // Sample response (all in one line):
    // throw 1; < don't be evil' >
    // {m:[{url:"http://.../osde/preview/gadget.xml",
    // view:[{
    // name:"home",
    // ifr:"http://....ig.ig.gmodules.com/gadgets/ifr?
    // view=home
    // &url=http://.../file/...546/osde/preview/gadget.xml
    // &nocache=0
    // &lang=en&country=us&.lang=en&.country=us
    // &synd=ig&mid=0&ifpctok=-1987853101061147102
    // &exp_split_js=1&exp_track_js=1&exp_new_js_flags=1&exp_ids=...
    // &parent=http://www.google.com&refresh=3600
    // &libs=core:core.io:core.iglegacy:auth-refresh#st=...
    // &gadgetId=...646
    // &gadgetOwner=...450
    // &gadgetViewer=...450
    // &is_signedin=1
    // &is_social=1
    // "}]
    // }]}

    // Form preview-url from response.
    // First, take out unwanted prepending string.
    String previewUrlIdentifier = useCanvasView ? PREVIEW_URL_IDENTIFIER_FOR_CANVAS
            : PREVIEW_URL_IDENTIFIER_FOR_HOME;
    int startIndex = response.indexOf(previewUrlIdentifier) + previewUrlIdentifier.length();
    String previewUrl = response.substring(startIndex);
    logger.fine("previewUrl with appending:\n" + previewUrl);

    // Handle the case when Canvas view is unavailable.
    if (!previewUrl.startsWith("http://")) {
        if (!useCanvasView) {
            throw new IgException("Error: Invalid preview url with response:\n" + response);
        }
        throw new IgException("Canvas view is not available for this gadget.");
    }

    // Take out unwanted appending string.
    int endIndex = previewUrl.indexOf(PREVIEW_URL_END_IDENTIFIER) + PREVIEW_URL_END_IDENTIFIER_LENGTH;
    previewUrl = previewUrl.substring(0, endIndex);
    logger.fine("previewUrl without appending:\n" + previewUrl);

    // Always enable nocache.
    previewUrl = previewUrl.replaceFirst("&nocache=0&", "&nocache=1&");

    // TODO: support lang and country.

    return previewUrl;
}

From source file:consultor.CSVLoader.java

/**
 * Parse CSV file using OpenCSV library and load in 
 * given database table. /*from ww w.  ja  v a 2s  .  c o m*/
 * @param csvFile Input CSV file
 * @param tableName Database table name to import data
 * @param truncateBeforeLoad Truncate the table before inserting 
 *          new records.
 * @throws Exception
 */
public void loadCSV(String csvFile, String tableName, boolean truncateBeforeLoad) throws Exception {

    CSVReader csvReader = null;
    if (null == this.connection) {
        throw new Exception("Not a valid connection.");
    }
    try {

        csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error occured while executing file. " + e.getMessage());
    }

    String[] headerRow = csvReader.readNext();

    if (null == headerRow) {
        throw new FileNotFoundException(
                "No columns defined in given CSV file." + "Please check the CSV file format.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);

    String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
    query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
    query = query.replaceFirst(VALUES_REGEX, questionmarks);

    System.out.println("Query: " + query);

    String[] nextLine;
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = this.connection;
        con.setAutoCommit(false);
        ps = con.prepareStatement(query);

        if (truncateBeforeLoad) {
            //delete data from table before loading csv
            con.createStatement().execute("DELETE FROM " + tableName);
        }

        final int batchSize = 1000;
        int count = 0;
        Date date = null;
        while ((nextLine = csvReader.readNext()) != null) {

            if (null != nextLine) {
                int index = 1;
                for (String string : nextLine) {
                    date = DateUtil.convertToDate(string);
                    if (null != date) {
                        ps.setDate(index++, new java.sql.Date(date.getTime()));
                    } else {
                        ps.setString(index++, string);
                    }
                }
                ps.addBatch();
            }
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch(); // insert remaining records
        con.commit();
    } catch (Exception e) {
        con.rollback();
        e.printStackTrace();
        throw new Exception("Error occured while loading data from file to database." + e.getMessage());
    } finally {
        if (null != ps)
            ps.close();
        if (null != con)
            con.close();

        csvReader.close();
    }
}

From source file:com.indeed.imhotep.index.builder.util.SmartArgs.java

protected void usage() {
    StringWriter helpWriter = new StringWriter();
    PrintWriter helpPrintWriter = new PrintWriter(helpWriter);
    new HelpFormatter().printHelp(helpPrintWriter, 80, " ", "", options, 1, 2, "");
    helpPrintWriter.close();//from   ww w .  j  av  a2  s .c  o  m
    String params = helpWriter.toString();
    params = params.replaceFirst("usage", "Parameters");

    if (!description.isEmpty()) {
        System.err.println("Description:\n" + description + "\n");
    }
    System.err.println(params);
    if (!examples.isEmpty()) {
        System.err.println("Examples:\n" + examples + "\n");
    }
}