public static String injectAfterOption(Message extend, Message by, String extendProto, String byContent)
    {
        Pattern messageRegexp = Pattern.compile("([\\n\\r]?[ \\t]*message\\s+" + extend.getName() +
                "\\s+\\{[^{}]*[\\n\\r][ \\t]*)(option\\s+extends\\s+=\\s+" + by.getName() +
                "\\s*;)", Pattern.MULTILINE);

        Matcher matcher = messageRegexp.matcher(extendProto);

        if (matcher.find())
        {
            // Calculate indentation of option
            Pattern indentRegexp = Pattern.compile("[\\n\\r]([ \\t]+)option\\s+extends\\s+=\\s+" + by.getName() +
                    "\\s*;", Pattern.MULTILINE);
            Matcher indent = indentRegexp.matcher(extendProto.substring(matcher.start(), matcher.end()));
            String indentation = "";
            if (indent.find())
            {
                int is = matcher.start() + indent.start(1), ie = matcher.start() + indent.end(1);
                indentation = generateIndentation(extendProto.substring(is, ie), 0);
            }
            // Make a replace
            StringBuffer sb = new StringBuffer();
            matcher.appendReplacement(sb, "$1" +
                    "// " + generateTimestamp(extend, by) + LINE_SEPARATOR +
                    indentation + "// $2" + LINE_SEPARATOR + insertIndentation(byContent, indentation));
            matcher.appendTail(sb);
            return sb.toString();
        }

        return extendProto;
    }
Kind of preprocessor for proto files. Able to extend one messages with fields from other ones.

@author Ivan Prisyazhniy, Igor Scherbak
@created Mar 9, 2012
protostuff-compiler/src/main/java/io/protostuff/compiler/ProtoToProtoCompiler.java