Example usage for java.util.regex Matcher appendTail

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

Introduction

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

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

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

private void executeBtnPreformed() {
    try {//from   w w w  .ja v 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: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);

    name = buffer.toString();// w w w .  jav  a 2s. c  o m
    if (name.startsWith("_"))
        name = name.substring(1);
    return name;
}

From source file:it.tidalwave.northernwind.core.impl.filter.MacroFilter.java

@Override
@Nonnull/*from w  w w .  j av a2 s  .c  o  m*/
public String filter(final @Nonnull String text, final @Nonnull String mimeType) {
    final Matcher matcher = pattern.matcher(text);
    final StringBuffer buffer = new StringBuffer();

    while (matcher.find()) {
        final String filtered = doFilter(matcher);
        try {
            matcher.appendReplacement(buffer, filtered);
        } catch (IllegalArgumentException e) // better diagnostics
        {
            throw new IllegalArgumentException(
                    String.format("Pattern error: %s regexp: %s%n**** filtered: %s%n**** buffer: %s",
                            e.getMessage(), pattern.pattern(), filtered, buffer));
        }
    }

    matcher.appendTail(buffer);

    return buffer.toString();
}

From source file:com.haulmont.cuba.core.sys.AbstractScripting.java

protected Script createScript(String text) {
    StringBuilder sb = new StringBuilder();
    for (String importItem : imports) {
        sb.append("import ").append(importItem).append("\n");
    }//from   w w w . ja va2  s.  c  o m

    Matcher matcher = IMPORT_PATTERN.matcher(text);
    String result;
    if (matcher.find()) {
        StringBuffer s = new StringBuffer();
        matcher.appendReplacement(s, sb + "$0");
        result = matcher.appendTail(s).toString();
    } else {
        Matcher packageMatcher = PACKAGE_PATTERN.matcher(text);
        if (packageMatcher.find()) {
            StringBuffer s = new StringBuffer();
            packageMatcher.appendReplacement(s, "$0\n" + sb);
            result = packageMatcher.appendTail(s).toString();
        } else {
            result = sb.append(text).toString();
        }
    }

    CompilerConfiguration cc = new CompilerConfiguration();
    cc.setClasspath(groovyClassPath);
    cc.setRecompileGroovySource(true);
    GroovyShell shell = new GroovyShell(javaClassLoader, new Binding(), cc);
    //noinspection UnnecessaryLocalVariable
    Script script = shell.parse(result);
    return script;
}

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  av  a2  s .  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:org.opencms.i18n.CmsEncoder.java

/**
 * Decodes HTML entity references like <code>&amp;#8364;</code> that are contained in the 
 * String to a regular character, but only if that character is contained in the given 
 * encodings charset.<p> //from   w ww. j a v  a2 s. c  om
 * 
 * @param input the input to decode the HTML entities in
 * @param encoding the charset to decode the input for
 * @return the input with the decoded HTML entities
 * 
 * @see #encodeHtmlEntities(String, String)
 */
public static String decodeHtmlEntities(String input, String encoding) {

    Matcher matcher = ENTITIY_PATTERN.matcher(input);
    StringBuffer result = new StringBuffer(input.length());
    Charset charset = Charset.forName(encoding);
    CharsetEncoder encoder = charset.newEncoder();

    while (matcher.find()) {
        String entity = matcher.group();
        String value = entity.substring(2, entity.length() - 1);
        int c = Integer.valueOf(value).intValue();
        if (c < 128) {
            // first 128 chars are contained in almost every charset
            entity = new String(new char[] { (char) c });
            // this is intended as performance improvement since 
            // the canEncode() operation appears quite CPU heavy
        } else if (encoder.canEncode((char) c)) {
            // encoder can encode this char
            entity = new String(new char[] { (char) c });
        }
        matcher.appendReplacement(result, entity);
    }
    matcher.appendTail(result);
    return result.toString();
}

From source file:org.olat.modules.fo.archiver.formatters.ForumRTFFormatter.java

/**
 * @param originalText/*from w w  w.  j a  v  a 2  s . co  m*/
 * @return
 */
private String convertHTMLMarkupToRTF(final String originalText) {
    String htmlText = originalText;

    final Matcher mb = PATTERN_HTML_BOLD.matcher(htmlText);
    final StringBuffer bolds = new StringBuffer();
    while (mb.find()) {
        mb.appendReplacement(bolds, "{\\\\b $1} ");
    }
    mb.appendTail(bolds);
    htmlText = bolds.toString();

    final Matcher mi = PATTERN_HTML_ITALIC.matcher(htmlText);
    final StringBuffer italics = new StringBuffer();
    while (mi.find()) {
        mi.appendReplacement(italics, "{\\\\i $1} ");
    }
    mi.appendTail(italics);
    htmlText = italics.toString();

    final Matcher mbr = PATTERN_HTML_BREAK.matcher(htmlText);
    final StringBuffer breaks = new StringBuffer();
    while (mbr.find()) {
        mbr.appendReplacement(breaks, "\\\\line ");
    }
    mbr.appendTail(breaks);
    htmlText = breaks.toString();

    final Matcher mofo = PATTERN_CSS_O_FOQUOTE.matcher(htmlText);
    final StringBuffer foquotes = new StringBuffer();
    while (mofo.find()) {
        mofo.appendReplacement(foquotes, "\\\\line {\\\\i $1} {\\\\pard $2\\\\par}");
    }
    mofo.appendTail(foquotes);
    htmlText = foquotes.toString();

    final Matcher mp = PATTERN_HTML_PARAGRAPH.matcher(htmlText);
    final StringBuffer paragraphs = new StringBuffer();
    while (mp.find()) {
        mp.appendReplacement(paragraphs, "\\\\line $1 \\\\line");
    }
    mp.appendTail(paragraphs);
    htmlText = paragraphs.toString();

    final Matcher mahref = PATTERN_HTML_AHREF.matcher(htmlText);
    final StringBuffer ahrefs = new StringBuffer();
    while (mahref.find()) {
        mahref.appendReplacement(ahrefs,
                "{\\\\field{\\\\*\\\\fldinst{HYPERLINK\"$1\"}}{\\\\fldrslt{\\\\ul $2}}}");
    }
    mahref.appendTail(ahrefs);
    htmlText = ahrefs.toString();

    final Matcher mli = PATTERN_HTML_LIST.matcher(htmlText);
    final StringBuffer lists = new StringBuffer();
    while (mli.find()) {
        mli.appendReplacement(lists, "$1\\\\line ");
    }
    mli.appendTail(lists);
    htmlText = lists.toString();

    final Matcher mtp = PATTERN_THREEPOINTS.matcher(htmlText);
    final StringBuffer tps = new StringBuffer();
    while (mtp.find()) {
        mtp.appendReplacement(tps, THREEPOINTS);
    }
    mtp.appendTail(tps);
    htmlText = tps.toString();

    // strip all other html-fragments, because not convertable that easy
    htmlText = FilterFactory.getHtmlTagsFilter().filter(htmlText);
    // Remove all &nbsp;
    final Matcher tmp = HTML_SPACE_PATTERN.matcher(htmlText);
    htmlText = tmp.replaceAll(" ");
    htmlText = StringEscapeUtils.unescapeHtml(htmlText);

    return htmlText;
}

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));
    }//  w  w w  . j  av a2  s .  com
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:io.github.swagger2markup.markup.builder.internal.confluenceMarkup.ConfluenceMarkupBuilder.java

private String escapeCellPipes(String cell) {
    Matcher m = ESCAPE_CELL_PIPE_PATTERN.matcher(cell);

    StringBuffer res = new StringBuffer();
    while (m.find()) {
        String repl = m.group(1);
        if (repl.equals(ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString()))
            repl = "\\" + ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString();
        m.appendReplacement(res, Matcher.quoteReplacement(repl));
    }/*  ww w  .  ja  v a 2  s  .  c  om*/
    m.appendTail(res);

    return res.toString();
}

From source file:com.gewara.util.XSSFilter.java

protected String escapeComments(String s) {
    Pattern p = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
    Matcher m = p.matcher(s);
    StringBuffer buf = new StringBuffer();
    if (m.find()) {
        String match = m.group(1); // (.*?)
        m.appendReplacement(buf, "<!--" + htmlSpecialChars(match) + "-->");
    }/*from w  ww  .  j  a va  2 s .c  o m*/
    m.appendTail(buf);

    return buf.toString();
}