summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremie Bresson2012-07-05 12:51:10 (EDT)
committerDavid Green2012-07-09 09:54:54 (EDT)
commit6d61fcabeee824df898541d6d18da9b63a755230 (patch)
treee5fc8458fbf4053fe241fad0bcfb7b5e3dcf007b
parent349cb9dd3d43ef9296596c584ee62792d1c8a288 (diff)
downloadorg.eclipse.mylyn.docs-6d61fcabeee824df898541d6d18da9b63a755230.zip
org.eclipse.mylyn.docs-6d61fcabeee824df898541d6d18da9b63a755230.tar.gz
org.eclipse.mylyn.docs-6d61fcabeee824df898541d6d18da9b63a755230.tar.bz2
bug 379783: [WikiText] Expand MediaWiki template recursively.
Add the possibility to handle resolutions of templates recursively. Templates included in other templates are resolved. (with a loop detection). - Modification of TemplateProcessor. - Unit tests for the cases proposed in Bugzilla. - Add Message support for the Loop Detection Error Bugzilla: https://bugs.eclipse.org/bugs/show_bug.cgi?id=379783 Change-Id: I84f8079275f090c662186f9cf47311f688347b0b
-rw-r--r--org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/Messages.java32
-rw-r--r--org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessor.java29
-rw-r--r--org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/messages.properties11
-rw-r--r--org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessorTest.java59
4 files changed, 127 insertions, 4 deletions
diff --git a/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/Messages.java b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/Messages.java
new file mode 100644
index 0000000..dcfb963
--- /dev/null
+++ b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/Messages.java
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Jeremie Bresson and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Jeremie Bresson - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.wikitext.mediawiki.core;
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+class Messages {
+ private static final String BUNDLE_NAME = "org.eclipse.mylyn.internal.wikitext.mediawiki.core.messages"; //$NON-NLS-1$
+
+ private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
+
+ private Messages() {
+ }
+
+ public static String getString(String key) {
+ try {
+ return RESOURCE_BUNDLE.getString(key);
+ } catch (MissingResourceException e) {
+ return '!' + key + '!';
+ }
+ }
+}
diff --git a/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessor.java b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessor.java
index a4cd0a0..a4789aa 100644
--- a/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessor.java
+++ b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010 David Green and others.
+ * Copyright (c) 2010, 2012 David Green and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,14 +7,19 @@
*
* Contributors:
* David Green - initial API and implementation
+ * Jeremie Bresson - Bug 379783
*******************************************************************************/
package org.eclipse.mylyn.internal.wikitext.mediawiki.core;
+import java.text.MessageFormat;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -57,7 +62,10 @@ public class TemplateProcessor {
}
public String processTemplates(String markupContent) {
+ return processTemplates(markupContent, Collections.<String> emptySet());
+ }
+ private String processTemplates(String markupContent, Set<String> usedTemplates) {
StringBuilder processedMarkup = new StringBuilder();
int lastIndex = 0;
@@ -70,8 +78,23 @@ public class TemplateProcessor {
String templateName = matcher.group(2);
Template template = resolveTemplate(templateName);
if (template != null) {
- String parameters = matcher.group(3);
- String replacementText = processTemplate(template, parameters);
+ String replacementText;
+ if (usedTemplates.contains(templateName)) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("<span class=\"error\">"); //$NON-NLS-1$
+ sb.append(MessageFormat.format(
+ Messages.getString("TemplateProcessor_loopDetected"), template.getName())); //$NON-NLS-1$
+ sb.append("</span>"); //$NON-NLS-1$
+ replacementText = sb.toString();
+ } else {
+ String parameters = matcher.group(3);
+ replacementText = processTemplate(template, parameters);
+ //The replacementText might contain other templates. Add the current template to the set of used template and call recursively this function again:
+ Set<String> templates = new HashSet<String>(usedTemplates);
+ templates.add(templateName);
+ replacementText = processTemplates(replacementText, templates);
+ }
+ replacementText = processTemplates(replacementText);
processedMarkup.append(replacementText);
}
lastIndex = matcher.end();
diff --git a/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/messages.properties b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/messages.properties
new file mode 100644
index 0000000..891afe1
--- /dev/null
+++ b/org.eclipse.mylyn.wikitext.mediawiki.core/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/messages.properties
@@ -0,0 +1,11 @@
+###############################################################################
+# Copyright (c) 2012 Jeremie Bresson and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# Jeremie Bresson - initial API and implementation
+###############################################################################
+TemplateProcessor_loopDetected=Template loop detected:{0}
diff --git a/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessorTest.java b/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessorTest.java
index 2f8d218..cf5dd6a 100644
--- a/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessorTest.java
+++ b/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/mediawiki/core/TemplateProcessorTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2010 David Green and others.
+ * Copyright (c) 2007, 2010, 2012 David Green and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* David Green - initial API and implementation
+ * Jeremie Bresson - Bug 379783
*******************************************************************************/
package org.eclipse.mylyn.internal.wikitext.mediawiki.core;
@@ -202,6 +203,62 @@ public class TemplateProcessorTest extends TestCase {
}
}
+ public void testBasicTemplatesNoParametersRec() {
+ //Bug 379783
+ Template templateFoo = new Template();
+ templateFoo.setName("foo");
+ templateFoo.setTemplateMarkup("_{{bar}}expanded_");
+ markupLanguage.getTemplates().add(templateFoo);
+
+ Template templateBar = new Template();
+ templateBar.setName("bar");
+ templateBar.setTemplateMarkup("+exp+");
+ markupLanguage.getTemplates().add(templateBar);
+
+ TemplateProcessor templateProcessor = new TemplateProcessor(markupLanguage);
+
+ String markup = templateProcessor.processTemplates("one {{foo}} two");
+ assertEquals("one _+exp+expanded_ two", markup);
+ }
+
+ public void testBasicTemplateNoParametersRecLoopDetection() {
+ //Bug 379783
+ Template templateMer = new Template();
+ templateMer.setName("mer");
+ templateMer.setTemplateMarkup("Ątest{{mer}}testĄ");
+ markupLanguage.getTemplates().add(templateMer);
+
+ TemplateProcessor templateProcessor = new TemplateProcessor(markupLanguage);
+
+ String markup = templateProcessor.processTemplates("{{mer}}");
+ assertEquals("Ątest<span class=\"error\">Template loop detected:mer</span>testĄ", markup);
+ }
+
+ public void testBasicTemplatesNoParametersRecLoopDetection() {
+ //Bug 379783
+ Template template1 = new Template();
+ template1.setName("rec_a");
+ template1.setTemplateMarkup("+ rec_a {{rec_b}} rec_a +");
+ markupLanguage.getTemplates().add(template1);
+
+ Template template2 = new Template();
+ template2.setName("rec_b");
+ template2.setTemplateMarkup("+ rec_b {{rec_c}} rec_b +");
+ markupLanguage.getTemplates().add(template2);
+
+ Template template3 = new Template();
+ template3.setName("rec_c");
+ template3.setTemplateMarkup("+ rec_c {{rec_a}} rec_c +");
+ markupLanguage.getTemplates().add(template3);
+
+ TemplateProcessor templateProcessor = new TemplateProcessor(markupLanguage);
+
+ String markup = templateProcessor.processTemplates("{{rec_a}}");
+ assertEquals(
+ "+ rec_a + rec_b + rec_c <span class=\"error\">Template loop detected:rec_a</span> rec_c + rec_b + rec_a +",
+ markup);
+ }
+
private void assertContains(Set<String> strings, String string) {
assertTrue(String.format("Expected %s but got %s", string, strings), strings.contains(string));
}