9db8f865b1d5f3e3978b37e7a3480cc4

replaces value between two tags.

private final String DO_NOT_TRANSLATE_TAG = "skip";  

    /**
     * @param input <samp>"No &lt;skip&gt;song&lt;/skip&gt; that I could sing &lt;br/&gt;But I can try for your &lt;skip>heart&lt;/skip&gt;"</samp>
     * @param output <samp>"N &lt;skip&gt; canción &lt;/skip&gt; que yo pudiera cantar &lt;br/&gt; Pero puedo intentar para su &lt;skip&gt; corazón &lt;/skip&gt;"</samp>
     * @return <samp>"N song que yo pudiera cantar &lt;br/&gt; Pero puedo intentar para su heart"</samp>
     */
    private String replaceTranslated(String input, String output)
    {                      
        Pattern pattern = Pattern.compile(format("<%s[^>]*>(.+?)</%ss*>", DO_NOT_TRANSLATE_TAG, DO_NOT_TRANSLATE_TAG));

        List<String> replacements = new ArrayList<String>();
        Matcher inputMatcher = pattern.matcher(input);

        while (inputMatcher.find())
        {
            int start = inputMatcher.start() + (DO_NOT_TRANSLATE_TAG.length() + "<>".length());
            int end = inputMatcher.end() - (DO_NOT_TRANSLATE_TAG.length() + "</>".length());
            String segment = input.substring(start, end).trim();

            replacements.add(segment);
        }

        Matcher outputMatcher = pattern.matcher(output);
        List<String> replaceables = new ArrayList<String>();

        while (outputMatcher.find())
        {
            int start = outputMatcher.start();
            int end = outputMatcher.end();

            String replace = output.substring(start, end);
            replaceables.add(replace);
        }

        if (replacements.size() != replaceables.size())
        {
            if (this.debugFlag)
            {
                // calm down, this is a command line tool.
                System.out.println("[WARN] the count of skipable segments are not equal.");
                System.out.println("[WARN] \tinput replacements: " + replacements.size());
                System.out.println("[WARN] \toutput replaceables: " + replaceables.size());
            }

            // return output untouched
        }
        else
        {
            for (int i = 0; i < replaceables.size(); i++)
            {
                String replaceable = replaceables.get(i);
                String replacment = replacements.get(i);

                output = output.replace(replaceable, replacment);
            }
        }

        return output;
    }

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

bob

April 24, 2009, April 24, 2009 21:19, permalink

No rating. Login to rate!

For starters, use the capturing functionalities of the regular expression:

while (inputMatcher.find())
        {
            replacements.add(inputMatcher.group(1).trim());
        }

        while (outputMatcher.find())
        {
            replaceables.add(outputMatcher.group());
        }
9db8f865b1d5f3e3978b37e7a3480cc4

mikenereson.blogspot.com

April 29, 2009, April 29, 2009 19:57, permalink

No rating. Login to rate!

Thanks Bob. That is the part that I was looking to be refactored.

private final String DO_NOT_TRANSLATE_TAG = "skip";  


    /**
     * @param input  <samp>"No &lt;skip&gt;song&lt;/skip&gt; that I could sing &lt;br/&gt;But I can try for your &lt;skip>heart&lt;/skip&gt;"</samp>
     * @param output <samp>"N &lt;skip&gt; canción &lt;/skip&gt; que yo pudiera cantar &lt;br/&gt; Pero puedo intentar para su &lt;skip&gt; corazón &lt;/skip&gt;"</samp>
     * @return <samp>"N song que yo pudiera cantar &lt;br/&gt; Pero puedo intentar para su heart"</samp>
     */
public String replaceTranslated(String input, String output)
{
    final Pattern pattern = Pattern.compile(format("<%s[^>]*>(.+?)</%ss*>", DO_NOT_TRANSLATE_TAG, DO_NOT_TRANSLATE_TAG));

    final List<String> replacements = new ArrayList<String>();
    final List<String> replaceables = new ArrayList<String>();
    final Matcher inputMatcher = pattern.matcher(input);
    final Matcher outputMatcher = pattern.matcher(output);

    while (inputMatcher.find())
    {
        replacements.add(inputMatcher.group(1).trim());
    }

    while (outputMatcher.find())
    {
        replaceables.add(outputMatcher.group());
    }

    if (replacements.size() == replaceables.size())
    {
        for (int i = 0; i < replaceables.size(); i++)
        {
            String replaceable = replaceables.get(i);
            String replacment = replacements.get(i);

            output = output.replace(replaceable, replacment);
        }
    }

    return output;
}
77af2a7611f8488edae85277498498f7

Jeff Grigg

May 12, 2010, May 12, 2010 00:16, permalink

No rating. Login to rate!

I might use "[^<]*" instead of ".+?" for "everything between the <>s".

Is "s*" near the end of the pattern intentional? (IE: Close tags may end with any number of extra 's' characters.)

77af2a7611f8488edae85277498498f7

Jeff Grigg

May 12, 2010, May 12, 2010 00:16, permalink

No rating. Login to rate!

I might use "[^<]*" instead of ".+?" for "everything between the <>s".

Is "s*" near the end of the pattern intentional? (IE: Close tags may end with any number of extra 's' characters.)

Your refactoring





Format Copy from initial code

or Cancel