Example usage for java.util.regex Matcher appendReplacement

List of usage examples for java.util.regex Matcher appendReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher appendReplacement.

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:com.ejushang.steward.common.genericdao.dao.hibernate.GeneralDAO.java

/**
 * ql?order by?//from   ww  w. j  a  v  a2s. c  o  m
 * @param ql ?
 * @return ??
 */
private String removeOrderBy(String ql) {
    if (ql != null && !"".equals(ql)) {
        Matcher m = removeOrderByPattern.matcher(ql);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, "");
        }
        m.appendTail(sb);
        return sb.toString();
    }
    return "";
}

From source file:com.iana.boesc.utility.BOESCUtil.java

/**
   * this method change first character in upper case & return modified string
   * //from ww w  .ja  v  a2 s. com
   * @param str
   * @return modified str
   */
public final String initCap(String rowStr) {
    StringBuffer stringbf = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(rowStr);
    while (m.find()) {
        m.appendReplacement(stringbf, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }
    return stringbf.toString();
}

From source file:gtu._work.ui.SqlCreaterUI.java

private void executeBtnPreformed() {
    try {//from w  ww  .  j  av a 2s. c o  m
        logArea.setText("");
        File srcFile = JCommonUtil.filePathCheck(excelFilePathText.getText(), "?", false);
        if (srcFile == null) {
            return;
        }
        if (!srcFile.getName().endsWith(".xlsx")) {
            JCommonUtil._jOptionPane_showMessageDialog_error("excel");
            return;
        }
        if (StringUtils.isBlank(sqlArea.getText())) {
            return;
        }
        File saveFile = JCommonUtil._jFileChooser_selectFileOnly_saveFile();
        if (saveFile == null) {
            JCommonUtil._jOptionPane_showMessageDialog_error("?");
            return;
        }

        String sqlText = sqlArea.getText();

        StringBuffer sb = new StringBuffer();
        Map<Integer, String> refMap = new HashMap<Integer, String>();
        Pattern sqlPattern = Pattern.compile("\\$\\{(\\w+)\\}", Pattern.MULTILINE);
        Matcher matcher = sqlPattern.matcher(sqlText);
        while (matcher.find()) {
            String val = StringUtils.trim(matcher.group(1)).toUpperCase();
            refMap.put(ExcelUtil.cellEnglishToPos(val), val);
            matcher.appendReplacement(sb, "\\$\\{" + val + "\\}");
        }
        matcher.appendTail(sb);
        appendLog(refMap.toString());

        sqlText = sb.toString();
        sqlArea.setText(sqlText);

        Configuration cfg = new Configuration();
        StringTemplateLoader stringTemplatge = new StringTemplateLoader();
        stringTemplatge.putTemplate("aaa", sqlText);
        cfg.setTemplateLoader(stringTemplatge);
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        Template temp = cfg.getTemplate("aaa");

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(saveFile), "utf8"));

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bis);
        Sheet sheet = xssfWorkbook.getSheetAt(0);
        for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
            Row row = sheet.getRow(j);
            if (row == null) {
                continue;
            }
            Map<String, Object> root = new HashMap<String, Object>();
            for (int index : refMap.keySet()) {
                root.put(refMap.get(index), formatCellType(row.getCell(index)));
            }
            appendLog(root.toString());

            StringWriter out = new StringWriter();
            temp.process(root, out);
            out.flush();
            String writeStr = out.getBuffer().toString();
            appendLog(writeStr);

            writer.write(writeStr);
            writer.newLine();
        }
        bis.close();

        writer.flush();
        writer.close();

        JCommonUtil._jOptionPane_showMessageDialog_info("? : \n" + saveFile);
    } catch (Exception ex) {
        JCommonUtil.handleException(ex);
    }
}

From source file:com.modelsolv.kaboom.model.resource.nativeImpl.ObjectResourceDefinitionImpl.java

private URI bindParameters(Object canonicalObject, CanonicalObjectReader reader) {

    Pattern pattern = Pattern.compile("\\{(.+?)\\}");
    Matcher matcher = pattern.matcher(getURITemplate());
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        String param = matcher.group(1);
        CDMProperty boundProperty = parameterBindings.get(param);
        if (boundProperty == null) {
            throw new RuntimeException("Could not find a binding for the parameter " + param);
        }//  w w  w . j a  v  a2s .  c  o  m
        String replacement = (String) reader.getPropertyValue(canonicalObject, boundProperty.getName());
        matcher.appendReplacement(buffer, "");
        buffer.append(replacement);
    }
    matcher.appendTail(buffer);
    try {
        return new URI(buffer.toString());
    } catch (Exception e) {
        throw new RuntimeException("Could not create resource URI using the provided URI template, "
                + "canonical object and reader.", e);
    }
}

From source file:com.qumoon.commons.web.HTMLInputChecker.java

private Matcher appendReplacement(Matcher matcher, StringBuffer buf, String replacement) {
    return matcher.appendReplacement(buf, quoteReplacement(replacement));
}

From source file:net.firejack.platform.core.utils.Factory.java

private String fixPath(String name) {
    Matcher matcher = pattern.matcher(name);
    StringBuffer buffer = new StringBuffer(name.length());
    while (matcher.find())
        matcher.appendReplacement(buffer, StringUtils.capitalize(matcher.group()));
    matcher.appendTail(buffer);/*from   w  ww  .j  a  v  a2s.c om*/

    name = buffer.toString();
    if (name.startsWith("_"))
        name = name.substring(1);
    return name;
}

From source file:org.apache.bval.jsr.DefaultMessageInterpolator.java

private String replaceVariables(String message, ResourceBundle bundle, Locale locale, boolean recurse) {
    final Matcher matcher = messageParameterPattern.matcher(message);
    final StringBuffer sb = new StringBuffer(64);
    String resolvedParameterValue;
    while (matcher.find()) {
        final String parameter = matcher.group(1);
        resolvedParameterValue = resolveParameter(parameter, bundle, locale, recurse);

        matcher.appendReplacement(sb, sanitizeForAppendReplacement(resolvedParameterValue));
    }//from   w ww  .  j a  va2  s .  co m
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:net.sf.j2ep.UrlRewritingResponseWrapper.java

/**
 * Rewrites the header Set-Cookie so that path and domain is correct.
 * //from  w  w w .j a va  2 s .  c  om
 * @param value
 *            The original header
 * @return The rewritten header
 */
private String rewriteSetCookie(String value) {
    StringBuffer header = new StringBuffer();
    Matcher matcher = pathAndDomainPattern.matcher(value);
    while (matcher.find()) {
        if (matcher.group(1).equalsIgnoreCase("path=")) {
            String path = server.getRule().revert(matcher.group(2).replace(server.getPath(), contextPath)); // server.getRule().revert(matcher.group(2));
            path = "/";
            matcher.appendReplacement(header, "$1" + path + ";");
        } else {
            matcher.appendReplacement(header, "");
        }

    }
    matcher.appendTail(header);
    log.debug("Set-Cookie header rewritten \"" + value + "\" >> " + header.toString());
    return header.toString();
}

From source file:CssCompressor.java

public void compress(Writer out, int linebreakpos) throws IOException {

    Pattern p;//  www . j  a v a 2 s .com
    Matcher m;
    String css;
    StringBuffer sb;
    int startIndex, endIndex;

    // Remove all comment blocks...
    startIndex = 0;
    boolean iemac = false;
    boolean preserve = false;
    sb = new StringBuffer(srcsb.toString());
    while ((startIndex = sb.indexOf("/*", startIndex)) >= 0) {
        preserve = sb.length() > startIndex + 2 && sb.charAt(startIndex + 2) == '!';
        endIndex = sb.indexOf("*/", startIndex + 2);
        if (endIndex < 0) {
            if (!preserve) {
                sb.delete(startIndex, sb.length());
            }
        } else if (endIndex >= startIndex + 2) {
            if (sb.charAt(endIndex - 1) == '\\') {
                // Looks like a comment to hide rules from IE Mac.
                // Leave this comment, and the following one, alone...
                startIndex = endIndex + 2;
                iemac = true;
            } else if (iemac) {
                startIndex = endIndex + 2;
                iemac = false;
            } else if (!preserve) {
                sb.delete(startIndex, endIndex + 2);
            } else {
                startIndex = endIndex + 2;
            }
        }
    }

    css = sb.toString();

    // Normalize all whitespace strings to single spaces. Easier to work
    // with that way.
    css = css.replaceAll("\\s+", " ");

    // Make a pseudo class for the Box Model Hack
    css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___");

    // ---------where zk modify it
    sb = new StringBuffer();
    p = Pattern.compile("\\$\\{([^\\}]+)\\}");
    Matcher m1 = p.matcher(css);
    while (m1.find()) {
        String s1 = m1.group();
        s1 = s1.replaceAll("\\$\\{", "__EL__");
        s1 = s1.replaceAll(":", "__ELSP__");
        s1 = s1.replaceAll("\\}", "__ELEND__");

        m1.appendReplacement(sb, s1);
    }
    m1.appendTail(sb);
    css = sb.toString();

    // ---------where zk modify it----end

    // Remove the spaces before the things that should not have spaces
    // before them.
    // But, be careful not to turn "p :link {...}" into "p:link{...}"
    // Swap out any pseudo-class colons with the token, and then swap back.
    sb = new StringBuffer();
    p = Pattern.compile("(^|\\})(([^\\{:])+:)+([^\\{]*\\{)");
    m = p.matcher(css);
    while (m.find()) {
        String s = m.group();
        s = s.replaceAll(":", "___PSEUDOCLASSCOLON___");
        m.appendReplacement(sb, s);
    }
    m.appendTail(sb);
    css = sb.toString();
    css = css.replaceAll("\\s+([!{};:>+\\(\\)\\],])", "$1");
    css = css.replaceAll("___PSEUDOCLASSCOLON___", ":");

    // Remove the spaces after the things that should not have spaces after
    // them.
    css = css.replaceAll("([!{}:;>+\\(\\[,])\\s+", "$1");

    // Add the semicolon where it's missing.
    css = css.replaceAll("([^;\\}])}", "$1;}");

    // Replace 0(px,em,%) with 0.
    css = css.replaceAll("([\\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)", "$1$2");

    // Replace 0 0 0 0; with 0.
    css = css.replaceAll(":0 0 0 0;", ":0;");
    css = css.replaceAll(":0 0 0;", ":0;");
    css = css.replaceAll(":0 0;", ":0;");
    // Replace background-position:0; with background-position:0 0;
    css = css.replaceAll("background-position:0;", "background-position:0 0;");

    // Replace 0.6 to .6, but only when preceded by : or a white-space
    css = css.replaceAll("(:|\\s)0+\\.(\\d+)", "$1.$2");

    // Shorten colors from rgb(51,102,153) to #336699
    // This makes it more likely that it'll get further compressed in the
    // next step.
    p = Pattern.compile("rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)");
    m = p.matcher(css);
    sb = new StringBuffer();
    while (m.find()) {
        String[] rgbcolors = m.group(1).split(",");
        StringBuffer hexcolor = new StringBuffer("#");
        for (int i = 0; i < rgbcolors.length; i++) {
            int val = Integer.parseInt(rgbcolors[i]);
            if (val < 16) {
                hexcolor.append("0");
            }
            hexcolor.append(Integer.toHexString(val));
        }
        m.appendReplacement(sb, hexcolor.toString());
    }
    m.appendTail(sb);
    css = sb.toString();

    // Shorten colors from #AABBCC to #ABC. Note that we want to make sure
    // the color is not preceded by either ", " or =. Indeed, the property
    // filter: chroma(color="#FFFFFF");
    // would become
    // filter: chroma(color="#FFF");
    // which makes the filter break in IE.
    p = Pattern.compile(
            "([^\"'=\\s])(\\s*)#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])");
    m = p.matcher(css);
    sb = new StringBuffer();
    while (m.find()) {
        // Test for AABBCC pattern
        if (m.group(3).equalsIgnoreCase(m.group(4)) && m.group(5).equalsIgnoreCase(m.group(6))
                && m.group(7).equalsIgnoreCase(m.group(8))) {
            m.appendReplacement(sb, m.group(1) + m.group(2) + "#" + m.group(3) + m.group(5) + m.group(7));
        } else {
            m.appendReplacement(sb, m.group());
        }
    }
    m.appendTail(sb);
    css = sb.toString();

    // Remove empty rules.
    css = css.replaceAll("[^\\}]+\\{;\\}", "");

    if (linebreakpos >= 0) {
        // Some source control tools don't like it when files containing
        // lines longer
        // than, say 8000 characters, are checked in. The linebreak option
        // is used in
        // that case to split long lines after a specific column.
        int i = 0;
        int linestartpos = 0;
        sb = new StringBuffer(css);
        while (i < sb.length()) {
            char c = sb.charAt(i++);
            if (c == '}' && i - linestartpos > linebreakpos) {
                sb.insert(i, '\n');
                linestartpos = i;
            }
        }

        css = sb.toString();
    }

    // Replace the pseudo class for the Box Model Hack
    css = css.replaceAll("___PSEUDOCLASSBMH___", "\"\\\\\"}\\\\\"\"");

    // Replace multiple semi-colons in a row by a single one
    // See SF bug #1980989
    css = css.replaceAll(";;+", ";");

    // ---------where zk modify it
    css = css.replaceAll("__EL__", "\\$\\{");
    css = css.replaceAll("__ELSP__", ":");
    css = css.replaceAll("__ELEND__", "\\}");

    // ---------where zk modify it----end

    // Trim the final string (for any leading or trailing white spaces)
    css = css.trim();

    // Write the output...
    out.write(css);
}

From source file:org.cesecore.configuration.ConfigurationTestHolder.java

private String interpolate(final String orderString) {
    final Pattern PATTERN = Pattern.compile("\\$\\{(.+?)\\}");
    final Matcher m = PATTERN.matcher(orderString);
    final StringBuffer sb = new StringBuffer(orderString.length());
    m.reset();//from w w  w.ja  v  a2s. c  om
    while (m.find()) {
        // when the pattern is ${identifier}, group 0 is 'identifier'
        final String key = m.group(1);
        final String value = getExpandedString(key, "");

        // if the pattern does exists, replace it by its value
        // otherwise keep the pattern ( it is group(0) )
        if (value != null) {
            m.appendReplacement(sb, value);
        } else {
            // I'm doing this to avoid the backreference problem as there will be a $
            // if I replace directly with the group 0 (which is also a pattern)
            m.appendReplacement(sb, "");
            final String unknown = m.group(0);
            sb.append(unknown);
        }
    }
    m.appendTail(sb);
    return sb.toString();
}