@Test public void testReplaceTemplateParameters() throws Exception { TemplateParser templateParser = new TemplateParser("1:{{{1|}}}2", false, true); templateParser.setModel(wikiModel); String result = templateParser.replaceTemplateParameters(null, 0).toString(); assertThat(result).isEqualTo("1:2"); }
@Nullable public StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset) { StringBuilder buffer = null; int bufferStart = 0; try { int level = fWikiModel.incrementRecursionLevel(); if (level > Configuration.PARSER_RECURSION_LIMIT) { return null; } fScannerPosition += curlyBraceOffset; char ch; int parameterStart = -1; StringBuilder recursiveResult; boolean isDefaultValue; while (true) { ch = fSource[fScannerPosition++]; if (ch == '{' && fSource[fScannerPosition] == '{' && fSource[fScannerPosition + 1] == '{' && fSource[fScannerPosition + 2] != '{') { fScannerPosition += 2; parameterStart = fScannerPosition; int temp[] = findNestedParamEnd(fSource, parameterStart); if (temp[0] >= 0) { fScannerPosition = temp[0]; List<String> list = splitByPipe(fSource, parameterStart, fScannerPosition - 3, null); if (list.size() > 0) { String parameterString = list.get(0).trim(); TemplateParser scanner1 = new TemplateParser(parameterString); scanner1.setModel(fWikiModel); recursiveResult = scanner1.replaceTemplateParameters(templateParameters, curlyBraceOffset); if (recursiveResult != null) { parameterString = recursiveResult.toString(); } String value = null; isDefaultValue = false; if (templateParameters != null) { value = templateParameters.get(parameterString); } if (value == null && list.size() > 1) { value = list.get(1); isDefaultValue = true; } if (value != null) { if (value.length() <= Configuration.TEMPLATE_VALUE_LIMIT) { if (buffer == null) { buffer = new StringBuilder(fSource.length + 128); } if (bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, parameterStart - bufferStart - 3); } TemplateParser scanner2 = new TemplateParser(value); scanner2.setModel(fWikiModel); if (isDefaultValue) { recursiveResult = scanner2.replaceTemplateParameters(templateParameters, curlyBraceOffset); } else { recursiveResult = scanner2.replaceTemplateParameters(null, curlyBraceOffset); } if (recursiveResult != null) { buffer.append(recursiveResult); } else { buffer.append(value); } bufferStart = fScannerPosition; } } } fScannerPosition = temp[0]; parameterStart = -1; } } if (buffer != null && buffer.length() > Configuration.TEMPLATE_BUFFER_LIMIT) { return buffer; } } } catch (IndexOutOfBoundsException e) { } finally { fWikiModel.decrementRecursionLevel(); } if (buffer != null && bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, fScannerPosition - bufferStart - 1); } return buffer; }
TemplateParser extends AbstractParser { @Nullable public StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset) { StringBuilder buffer = null; int bufferStart = 0; try { int level = fWikiModel.incrementRecursionLevel(); if (level > Configuration.PARSER_RECURSION_LIMIT) { return null; } fScannerPosition += curlyBraceOffset; char ch; int parameterStart = -1; StringBuilder recursiveResult; boolean isDefaultValue; while (true) { ch = fSource[fScannerPosition++]; if (ch == '{' && fSource[fScannerPosition] == '{' && fSource[fScannerPosition + 1] == '{' && fSource[fScannerPosition + 2] != '{') { fScannerPosition += 2; parameterStart = fScannerPosition; int temp[] = findNestedParamEnd(fSource, parameterStart); if (temp[0] >= 0) { fScannerPosition = temp[0]; List<String> list = splitByPipe(fSource, parameterStart, fScannerPosition - 3, null); if (list.size() > 0) { String parameterString = list.get(0).trim(); TemplateParser scanner1 = new TemplateParser(parameterString); scanner1.setModel(fWikiModel); recursiveResult = scanner1.replaceTemplateParameters(templateParameters, curlyBraceOffset); if (recursiveResult != null) { parameterString = recursiveResult.toString(); } String value = null; isDefaultValue = false; if (templateParameters != null) { value = templateParameters.get(parameterString); } if (value == null && list.size() > 1) { value = list.get(1); isDefaultValue = true; } if (value != null) { if (value.length() <= Configuration.TEMPLATE_VALUE_LIMIT) { if (buffer == null) { buffer = new StringBuilder(fSource.length + 128); } if (bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, parameterStart - bufferStart - 3); } TemplateParser scanner2 = new TemplateParser(value); scanner2.setModel(fWikiModel); if (isDefaultValue) { recursiveResult = scanner2.replaceTemplateParameters(templateParameters, curlyBraceOffset); } else { recursiveResult = scanner2.replaceTemplateParameters(null, curlyBraceOffset); } if (recursiveResult != null) { buffer.append(recursiveResult); } else { buffer.append(value); } bufferStart = fScannerPosition; } } } fScannerPosition = temp[0]; parameterStart = -1; } } if (buffer != null && buffer.length() > Configuration.TEMPLATE_BUFFER_LIMIT) { return buffer; } } } catch (IndexOutOfBoundsException e) { } finally { fWikiModel.decrementRecursionLevel(); } if (buffer != null && bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, fScannerPosition - bufferStart - 1); } return buffer; } }
TemplateParser extends AbstractParser { @Nullable public StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset) { StringBuilder buffer = null; int bufferStart = 0; try { int level = fWikiModel.incrementRecursionLevel(); if (level > Configuration.PARSER_RECURSION_LIMIT) { return null; } fScannerPosition += curlyBraceOffset; char ch; int parameterStart = -1; StringBuilder recursiveResult; boolean isDefaultValue; while (true) { ch = fSource[fScannerPosition++]; if (ch == '{' && fSource[fScannerPosition] == '{' && fSource[fScannerPosition + 1] == '{' && fSource[fScannerPosition + 2] != '{') { fScannerPosition += 2; parameterStart = fScannerPosition; int temp[] = findNestedParamEnd(fSource, parameterStart); if (temp[0] >= 0) { fScannerPosition = temp[0]; List<String> list = splitByPipe(fSource, parameterStart, fScannerPosition - 3, null); if (list.size() > 0) { String parameterString = list.get(0).trim(); TemplateParser scanner1 = new TemplateParser(parameterString); scanner1.setModel(fWikiModel); recursiveResult = scanner1.replaceTemplateParameters(templateParameters, curlyBraceOffset); if (recursiveResult != null) { parameterString = recursiveResult.toString(); } String value = null; isDefaultValue = false; if (templateParameters != null) { value = templateParameters.get(parameterString); } if (value == null && list.size() > 1) { value = list.get(1); isDefaultValue = true; } if (value != null) { if (value.length() <= Configuration.TEMPLATE_VALUE_LIMIT) { if (buffer == null) { buffer = new StringBuilder(fSource.length + 128); } if (bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, parameterStart - bufferStart - 3); } TemplateParser scanner2 = new TemplateParser(value); scanner2.setModel(fWikiModel); if (isDefaultValue) { recursiveResult = scanner2.replaceTemplateParameters(templateParameters, curlyBraceOffset); } else { recursiveResult = scanner2.replaceTemplateParameters(null, curlyBraceOffset); } if (recursiveResult != null) { buffer.append(recursiveResult); } else { buffer.append(value); } bufferStart = fScannerPosition; } } } fScannerPosition = temp[0]; parameterStart = -1; } } if (buffer != null && buffer.length() > Configuration.TEMPLATE_BUFFER_LIMIT) { return buffer; } } } catch (IndexOutOfBoundsException e) { } finally { fWikiModel.decrementRecursionLevel(); } if (buffer != null && bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, fScannerPosition - bufferStart - 1); } return buffer; } TemplateParser(String stringSource);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate, boolean onlyIncludeFlag); }
TemplateParser extends AbstractParser { @Nullable public StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset) { StringBuilder buffer = null; int bufferStart = 0; try { int level = fWikiModel.incrementRecursionLevel(); if (level > Configuration.PARSER_RECURSION_LIMIT) { return null; } fScannerPosition += curlyBraceOffset; char ch; int parameterStart = -1; StringBuilder recursiveResult; boolean isDefaultValue; while (true) { ch = fSource[fScannerPosition++]; if (ch == '{' && fSource[fScannerPosition] == '{' && fSource[fScannerPosition + 1] == '{' && fSource[fScannerPosition + 2] != '{') { fScannerPosition += 2; parameterStart = fScannerPosition; int temp[] = findNestedParamEnd(fSource, parameterStart); if (temp[0] >= 0) { fScannerPosition = temp[0]; List<String> list = splitByPipe(fSource, parameterStart, fScannerPosition - 3, null); if (list.size() > 0) { String parameterString = list.get(0).trim(); TemplateParser scanner1 = new TemplateParser(parameterString); scanner1.setModel(fWikiModel); recursiveResult = scanner1.replaceTemplateParameters(templateParameters, curlyBraceOffset); if (recursiveResult != null) { parameterString = recursiveResult.toString(); } String value = null; isDefaultValue = false; if (templateParameters != null) { value = templateParameters.get(parameterString); } if (value == null && list.size() > 1) { value = list.get(1); isDefaultValue = true; } if (value != null) { if (value.length() <= Configuration.TEMPLATE_VALUE_LIMIT) { if (buffer == null) { buffer = new StringBuilder(fSource.length + 128); } if (bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, parameterStart - bufferStart - 3); } TemplateParser scanner2 = new TemplateParser(value); scanner2.setModel(fWikiModel); if (isDefaultValue) { recursiveResult = scanner2.replaceTemplateParameters(templateParameters, curlyBraceOffset); } else { recursiveResult = scanner2.replaceTemplateParameters(null, curlyBraceOffset); } if (recursiveResult != null) { buffer.append(recursiveResult); } else { buffer.append(value); } bufferStart = fScannerPosition; } } } fScannerPosition = temp[0]; parameterStart = -1; } } if (buffer != null && buffer.length() > Configuration.TEMPLATE_BUFFER_LIMIT) { return buffer; } } } catch (IndexOutOfBoundsException e) { } finally { fWikiModel.decrementRecursionLevel(); } if (buffer != null && bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, fScannerPosition - bufferStart - 1); } return buffer; } TemplateParser(String stringSource);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate, boolean onlyIncludeFlag); static void parse(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean renderTemplate); static void parse(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean parseOnlySignature,
            boolean renderTemplate); static void parsePreprocessRecursive(int startIndex, String rawWikitext, IWikiModel wikiModel, StringBuilder writer,
            boolean renderTemplate, boolean onlyIncludeFlag, Map<String, String> templateParameterMap); static void parseRecursive(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean parseOnlySignature,
                                      boolean renderTemplate, Map<String, String> templateParameterMap); static void mergeParameters(LinkedHashMap<String, String> parameterMap, List<String> unnamedParameters); static Object[] createParameterMap(char[] src, int startOffset, int len); static void createSingleParameter(String srcString, IWikiModel wikiModel, Map<String, String> namedParameterMap,
            List<String> unnamedParams); static int checkParserFunction(CharSequence plainContent); @Nullable StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset); }
TemplateParser extends AbstractParser { @Nullable public StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset) { StringBuilder buffer = null; int bufferStart = 0; try { int level = fWikiModel.incrementRecursionLevel(); if (level > Configuration.PARSER_RECURSION_LIMIT) { return null; } fScannerPosition += curlyBraceOffset; char ch; int parameterStart = -1; StringBuilder recursiveResult; boolean isDefaultValue; while (true) { ch = fSource[fScannerPosition++]; if (ch == '{' && fSource[fScannerPosition] == '{' && fSource[fScannerPosition + 1] == '{' && fSource[fScannerPosition + 2] != '{') { fScannerPosition += 2; parameterStart = fScannerPosition; int temp[] = findNestedParamEnd(fSource, parameterStart); if (temp[0] >= 0) { fScannerPosition = temp[0]; List<String> list = splitByPipe(fSource, parameterStart, fScannerPosition - 3, null); if (list.size() > 0) { String parameterString = list.get(0).trim(); TemplateParser scanner1 = new TemplateParser(parameterString); scanner1.setModel(fWikiModel); recursiveResult = scanner1.replaceTemplateParameters(templateParameters, curlyBraceOffset); if (recursiveResult != null) { parameterString = recursiveResult.toString(); } String value = null; isDefaultValue = false; if (templateParameters != null) { value = templateParameters.get(parameterString); } if (value == null && list.size() > 1) { value = list.get(1); isDefaultValue = true; } if (value != null) { if (value.length() <= Configuration.TEMPLATE_VALUE_LIMIT) { if (buffer == null) { buffer = new StringBuilder(fSource.length + 128); } if (bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, parameterStart - bufferStart - 3); } TemplateParser scanner2 = new TemplateParser(value); scanner2.setModel(fWikiModel); if (isDefaultValue) { recursiveResult = scanner2.replaceTemplateParameters(templateParameters, curlyBraceOffset); } else { recursiveResult = scanner2.replaceTemplateParameters(null, curlyBraceOffset); } if (recursiveResult != null) { buffer.append(recursiveResult); } else { buffer.append(value); } bufferStart = fScannerPosition; } } } fScannerPosition = temp[0]; parameterStart = -1; } } if (buffer != null && buffer.length() > Configuration.TEMPLATE_BUFFER_LIMIT) { return buffer; } } } catch (IndexOutOfBoundsException e) { } finally { fWikiModel.decrementRecursionLevel(); } if (buffer != null && bufferStart < fScannerPosition) { buffer.append(fSource, bufferStart, fScannerPosition - bufferStart - 1); } return buffer; } TemplateParser(String stringSource);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate);  TemplateParser(String stringSource, boolean parseOnlySignature, boolean renderTemplate, boolean onlyIncludeFlag); static void parse(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean renderTemplate); static void parse(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean parseOnlySignature,
            boolean renderTemplate); static void parsePreprocessRecursive(int startIndex, String rawWikitext, IWikiModel wikiModel, StringBuilder writer,
            boolean renderTemplate, boolean onlyIncludeFlag, Map<String, String> templateParameterMap); static void parseRecursive(String rawWikitext, IWikiModel wikiModel, Appendable writer, boolean parseOnlySignature,
                                      boolean renderTemplate, Map<String, String> templateParameterMap); static void mergeParameters(LinkedHashMap<String, String> parameterMap, List<String> unnamedParameters); static Object[] createParameterMap(char[] src, int startOffset, int len); static void createSingleParameter(String srcString, IWikiModel wikiModel, Map<String, String> namedParameterMap,
            List<String> unnamedParams); static int checkParserFunction(CharSequence plainContent); @Nullable StringBuilder replaceTemplateParameters(@Nullable Map<String, String> templateParameters, int curlyBraceOffset); static final String TEMPLATE_PARSER_ERROR; }