[CVS snippet] Factored rendering of snippet to Snippet class
Aslak Hellesoy <rinkrank-yCVjj/[email protected]> Wed, 25 Feb 2004 19:03:38 -0600
| Newsgroups | gmane.comp.java.nanocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit in snippet/src on MAIN
main/snippet/Snippet.java +53 added 1.1
/SnippetMacro.java +33 -38 1.4 -> 1.5
/SnippetReader.java +14 -29 1.3 -> 1.4
test/snippet/SnippetTest.java +88 added 1.1
/SnippetMacroTest.java +26 -29 1.4 -> 1.5
/SnippetReaderTest.java +10 -75 1.4 -> 1.5
+224 -171
2 added + 4 modified, total 6 files
Factored rendering of snippet to Snippet class
Always use \n
----------
snippet /src /main /snippet
Snippet.java added at 1.1
diff -N Snippet.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Snippet.java 26 Feb 2004 01:03:37 -0000 1.1
@@ -0,0 +1,53 @@
+package snippet;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author Aslak Hellesøy
+ * @author Jon Tirs;eacuten
+ * @author Carlos Villela
+ * @version $Revision: 1.1 $
+ */
+public class Snippet {
+ private final List lines;
+
+ public Snippet(List lines) {
+ this.lines = lines;
+ }
+
+ public void writeContent(Writer writer, boolean withLineNumbers) throws IOException {
+ NumberFormat intFormat = new DecimalFormat(getFormat(lines.size()));
+ int counter = 1;
+ for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
+ String line = (String) iterator.next();
+ if (withLineNumbers) {
+ String lineNumber = intFormat.format(counter);
+ writer.write(lineNumber);
+ writer.write(". ");
+ counter++;
+ }
+ writer.write(stripEOL(line));
+ writer.write("\n");
+ }
+ }
+
+ private String stripEOL(String line) {
+ line = line.replaceAll("\r", "");
+ line = line.replaceAll("\n", "");
+ return line;
+ }
+
+ private String getFormat(int numberOfLines) {
+ int numberOfDigits = (numberOfLines / 10) + 1;
+ StringBuffer format = new StringBuffer();
+ for(int i = 0; i < numberOfDigits; i++) {
+ format.append('0');
+ }
+ return format.toString();
+ }
+}
\ No newline at end of file
----------
snippet /src /main /snippet
SnippetMacro.java 1.4 -> 1.5
diff -u -r1.4 -r1.5
--- SnippetMacro.java 25 Feb 2004 22:49:25 -0000 1.4
+++ SnippetMacro.java 26 Feb 2004 01:03:37 -0000 1.5
@@ -5,17 +5,22 @@
import java.io.IOException;
import java.io.Writer;
-import java.net.URL;
import java.net.MalformedURLException;
-import java.util.Map;
+import java.net.URL;
import java.util.HashMap;
+import java.util.Map;
+/**
+ * @author Jon Tirs;eacuten
+ * @author Aslak Hellesøy
+ * @author Carlos Villela
+ * @version $Revision: 1.5 $
+ */
public class SnippetMacro extends BaseMacro {
- static final String EOL = System.getProperty("line.separator");
- private Map cache = new HashMap();
- private long timeout = 60 * 60 * 1000; // one hour default cache
- private Map timeCached = new HashMap();
- private boolean debug = false;
+ private static final int MINUTE = 60 * 1000;
+ private final Map cache = new HashMap();
+ private final Map timeCached = new HashMap();
+ private long timeout = 60 * MINUTE; // one hour default cache
public String getName() {
return "snippet";
@@ -35,42 +40,32 @@
}
String lang = parameter.get("lang");
- boolean useLines = "true".equals(linesParam);
- StringBuffer snippet = getSnippet(url, id, useLines);
+ boolean withLineNumbers = "true".equals(linesParam);
+ Snippet snippet = getSnippet(url, id);
+
+ if (lang != null) {
+ writer.write("{code:lang=" + lang + "}\n");
+ }
+ snippet.writeContent(writer, withLineNumbers);
+ if (lang != null) {
+ writer.write("{code}\n");
+ }
+ }
- if(lang != null) {
- snippet.insert(0, EOL);
- snippet.insert(0, "{code:lang=" + lang + "}");
- snippet.append("{code}");
- }
-
- writer.write(snippet.toString());
- }
-
- StringBuffer getSnippet(URL url, String id, boolean useLines) throws IOException {
- StringBuffer result;
- String cachedSnippet = (String) getCachedSnippet(url, id);
- if(cachedSnippet != null) {
- result = new StringBuffer(cachedSnippet);
- if (debug) {
- result.append("(Served from cache)");
- }
- }
- else {
- result = new SnippetReader(url).readSnippet(id, useLines);
- cacheSnippet(url, id, result.toString());
- if (debug) {
- result.append("(Fetched from url, cache content " + cache + ")");
- }
+ Snippet getSnippet(URL url, String id) throws IOException {
+ Snippet result = getCachedSnippet(url, id);
+ if (result == null) {
+ result = new SnippetReader(url).readSnippet(id);
+ cacheSnippet(url, id, result);
}
return result;
}
- private Object getCachedSnippet(URL url, String id) {
- if(isCacheTimedout(url, id)) {
+ private Snippet getCachedSnippet(URL url, String id) {
+ if (isCacheTimedout(url, id)) {
removeFromCache(url, id);
}
- return cache.get(globalSnippetId(url, id));
+ return (Snippet) cache.get(globalSnippetId(url, id));
}
boolean isCacheTimedout(URL url, String id) {
@@ -102,8 +97,8 @@
}
}
- public void cacheSnippet(URL url, String id, String content) {
- cache.put(globalSnippetId(url, id), content);
+ public void cacheSnippet(URL url, String id, Snippet snippet) {
+ cache.put(globalSnippetId(url, id), snippet);
timeCached.put(globalSnippetId(url, id), new Long(System.currentTimeMillis()));
}
----------
snippet /src /main /snippet
SnippetReader.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- SnippetReader.java 25 Feb 2004 22:49:25 -0000 1.3
+++ SnippetReader.java 26 Feb 2004 01:03:37 -0000 1.4
@@ -1,52 +1,37 @@
package snippet;
-import java.net.URL;
-import java.io.IOException;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
-import java.util.List;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
-import java.text.NumberFormat;
-import java.text.DecimalFormat;
+import java.util.List;
+/**
+ * @author Jon Tirs;eacuten
+ * @author Aslak Hellesøy
+ * @author Carlos Villela
+ * @version $Revision: 1.4 $
+ */
public class SnippetReader {
- private static final String EOL = System.getProperty("line.separator");
-
- private URL source;
+ private final URL source;
public SnippetReader(URL source) {
this.source = source;
}
- public StringBuffer readSnippet(String snippetId, boolean useLineNumbers) throws IOException {
+ public Snippet readSnippet(String snippetId) throws IOException {
List lines = readLines(snippetId);
+ List snippetLines = new ArrayList();
int minIndent = minIndent(lines);
- StringBuffer result = new StringBuffer();
- NumberFormat intFormat = new DecimalFormat(getFormat(lines.size()));
- int lineNumber = 1;
for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
String line = (String) iterator.next();
if (!isDemarcator(line)) {
- if(useLineNumbers) {
- String l = intFormat.format(lineNumber);
- result.append(l).append(". ");
- lineNumber++;
- }
- result.append(line.substring(minIndent));
- result.append(EOL);
+ snippetLines.add(line.substring(minIndent));
}
}
- return result;
- }
-
- private String getFormat(int numberOfLines) {
- int numberOfDigits = (numberOfLines / 10) + 1;
- StringBuffer format = new StringBuffer();
- for(int i = 0; i < numberOfDigits; i++) {
- format.append('0');
- }
- return format.toString();
+ return new Snippet(snippetLines);
}
int minIndent(List lines) {
----------
snippet /src /test /snippet
SnippetTest.java added at 1.1
diff -N SnippetTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SnippetTest.java 26 Feb 2004 01:03:37 -0000 1.1
@@ -0,0 +1,88 @@
+package snippet;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Arrays;
+
+/**
+ * @author Aslak Hellesøy
+ * @version $Revision: 1.1 $
+ */
+public class SnippetTest extends TestCase {
+ private final Snippet nineLines = new Snippet(Arrays.asList(new String[]{
+ "one\n",
+ " two\n",
+ "three\n",
+ "four\n",
+ "five\n",
+ "six\n",
+ "seven\n",
+ "eight\n",
+ "nine\n"
+ }));
+
+ private final Snippet tenLines = new Snippet(Arrays.asList(new String[]{
+ "one\n",
+ " two\n",
+ "three\n",
+ "four\n",
+ "five\n",
+ "six\n",
+ "seven\n",
+ "eight\n",
+ "nine\n",
+ "ten\n"
+ }));
+
+ public void testNineLineSnippetCanBeWrittenWithOneDigitLineNumbers() throws IOException {
+ StringWriter written = new StringWriter();
+ nineLines.writeContent(written, true);
+ assertEquals("" +
+ "1. one\n" +
+ "2. two\n" +
+ "3. three\n" +
+ "4. four\n" +
+ "5. five\n" +
+ "6. six\n" +
+ "7. seven\n" +
+ "8. eight\n" +
+ "9. nine\n"
+ , written.getBuffer().toString());
+ }
+
+ public void testTenLineSnippetCanBeWrittenWithTwoDigitLineNumbers() throws IOException {
+ StringWriter written = new StringWriter();
+ tenLines.writeContent(written, true);
+ assertEquals("" +
+ "01. one\n" +
+ "02. two\n" +
+ "03. three\n" +
+ "04. four\n" +
+ "05. five\n" +
+ "06. six\n" +
+ "07. seven\n" +
+ "08. eight\n" +
+ "09. nine\n" +
+ "10. ten\n"
+ , written.getBuffer().toString());
+ }
+
+ public void testSnippetCanBeWrittenWithTwoDigitLineNumbers() throws IOException {
+ StringWriter written = new StringWriter();
+ tenLines.writeContent(written, false);
+ assertEquals("" +
+ "one\n" +
+ " two\n" +
+ "three\n" +
+ "four\n" +
+ "five\n" +
+ "six\n" +
+ "seven\n" +
+ "eight\n" +
+ "nine\n" +
+ "ten\n"
+ , written.getBuffer().toString());
+ }
+}
\ No newline at end of file
----------
snippet /src /test /snippet
SnippetMacroTest.java 1.4 -> 1.5
diff -u -r1.4 -r1.5
--- SnippetMacroTest.java 25 Feb 2004 22:49:26 -0000 1.4
+++ SnippetMacroTest.java 26 Feb 2004 01:03:37 -0000 1.5
@@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
+import java.util.Arrays;
public class SnippetMacroTest extends TestCase {
static final String EOL = System.getProperty("line.separator");
@@ -24,7 +25,7 @@
public void testExecuteWritesSnippetFromParameter() throws IOException {
parameter.setParams("id=mySnippetId|url=" + snippetUrl().toExternalForm());
- assertEquals("assertEquals(2, 1 + 1);" + EOL, executeMacro());
+ assertEquals("assertEquals(2, 1 + 1);\n", executeMacro());
}
private URL snippetUrl() {
@@ -35,18 +36,18 @@
private String executeMacro() throws IOException {
StringWriter writer = new StringWriter();
snippetMacro.execute(writer, parameter);
- return writer.toString();
+ return writer.getBuffer().toString();
}
public void testIfYouPassInLangItGetsWrappedInCodeMacro() throws IOException {
parameter.setParams("lang=java|id=multilineSnippet|url=" + snippetUrl().toExternalForm());
- assertEquals("{code:lang=java}" + EOL +
- "assertEquals(2, 1 + 1);" + EOL +
- "if(true) {" + EOL +
- " assertEquals(6, 2 * 3);" + EOL +
- "}" + EOL +
- "assertEquals(2, 4 / 2);" + EOL +
- "{code}",
+ assertEquals("{code:lang=java}\n" +
+ "assertEquals(2, 1 + 1);\n" +
+ "if(true) {\n" +
+ " assertEquals(6, 2 * 3);\n" +
+ "}\n" +
+ "assertEquals(2, 4 / 2);\n" +
+ "{code}\n",
executeMacro());
}
@@ -57,23 +58,19 @@
public void testReturnsCachedSnippetIfSnippetIsInCache() throws IOException {
URL url = new URL("file:cachedUrl");
String id = "snippet id";
- String content = "cached content";
- snippetMacro.cacheSnippet(url, id, content);
- assertEquals(content, snippetMacro.getSnippet(url, id, true).toString());
+ Snippet snippet = new Snippet(Arrays.asList(new String[]{"cached content"}));
+ snippetMacro.cacheSnippet(url, id, snippet);
+ assertSame(snippet, snippetMacro.getSnippet(url, id));
}
public void testRemovesSnippetFromCacheWhenTimedOut() throws IOException {
- assertEquals("assertEquals(2, 1 + 1);" + EOL,
- snippetMacro.getSnippet(snippetUrl(), "mySnippetId", false).toString());
- snippetMacro.cacheSnippet(snippetUrl(), "mySnippetId", "different cache content");
- assertEquals(System.currentTimeMillis(), snippetMacro.getTimeCached(snippetUrl(), "mySnippetId"));
- assertFalse(snippetMacro.isCacheTimedout(snippetUrl(), "mySnippetId"));
- assertEquals("different cache content",
- snippetMacro.getSnippet(snippetUrl(), "mySnippetId", true).toString());
+ Snippet snippetOne = snippetMacro.getSnippet(snippetUrl(), "mySnippetId");
+ Snippet snippetTwo = snippetMacro.getSnippet(snippetUrl(), "mySnippetId");
+ assertSame(snippetOne, snippetTwo);
+
snippetMacro.setCacheTimeout(0);
- assertTrue(snippetMacro.isCacheTimedout(snippetUrl(), "mySnippetId"));
- assertEquals("assertEquals(2, 1 + 1);" + EOL,
- snippetMacro.getSnippet(snippetUrl(), "mySnippetId", false).toString());
+ Snippet snippetThree = snippetMacro.getSnippet(snippetUrl(), "mySnippetId");
+ assertNotSame(snippetOne, snippetThree);
}
@@ -83,13 +80,13 @@
public void testPutsInLineNumbersIfSpecified() throws IOException {
parameter.setParams("lang=java|linenumbers=true|id=multilineSnippet|url=" + snippetUrl().toExternalForm());
- assertEquals("{code:lang=java}" + EOL +
- "1. assertEquals(2, 1 + 1);" + EOL +
- "2. if(true) {" + EOL +
- "3. assertEquals(6, 2 * 3);" + EOL +
- "4. }" + EOL +
- "5. assertEquals(2, 4 / 2);" + EOL +
- "{code}",
+ assertEquals("{code:lang=java}\n" +
+ "1. assertEquals(2, 1 + 1);\n" +
+ "2. if(true) {\n" +
+ "3. assertEquals(6, 2 * 3);\n" +
+ "4. }\n" +
+ "5. assertEquals(2, 4 / 2);\n" +
+ "{code}\n",
executeMacro());
}
----------
snippet /src /test /snippet
SnippetReaderTest.java 1.4 -> 1.5
diff -u -r1.4 -r1.5
--- SnippetReaderTest.java 25 Feb 2004 22:49:26 -0000 1.4
+++ SnippetReaderTest.java 26 Feb 2004 01:03:37 -0000 1.5
@@ -3,6 +3,7 @@
import junit.framework.TestCase;
import java.io.IOException;
+import java.io.StringWriter;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
@@ -51,12 +52,6 @@
// END SNIPPET: mySnippetId
}
- public void testCanReadSnippetFromThisJavaFile() throws IOException {
- SnippetReader snippetReader = new SnippetReader(getResource("SnippetReaderTest.java"));
- String snippet = snippetReader.readSnippet("mySnippetId", false).toString();
- assertEquals("assertEquals(2, 1 + 1);" + EOL, snippet);
- }
-
public void testTheObviousIsTrueSeveralTimes() {
// START SNIPPET: multilineSnippet
assertEquals(2, 1 + 1);
@@ -69,78 +64,18 @@
// END SNIPPET: multilineSnippet
}
- public void testTheObviousIsTrueNineLines() {
- // START SNIPPET: ninelineSnippet
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- if (true) {
- assertEquals(2, 1 + 1);
- }
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- // END SNIPPET: ninelineSnippet
- }
-
- public void testTheObviousIsTrueTenLines() {
- // START SNIPPET: tenlineSnippet
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- if (true) {
- assertEquals(2, 1 + 1);
- }
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- assertEquals(2, 1 + 1);
- // END SNIPPET: tenlineSnippet
- }
-
- public void testSnippetsWithLessThanTenLinesHaveOneDigitLineNumbers() throws IOException {
- SnippetReader snippetReader = new SnippetReader(getResource("SnippetReaderTest.java"));
- String snippet = snippetReader.readSnippet("ninelineSnippet", true).toString();
- assertEquals(
- "1. assertEquals(2, 1 + 1);" + EOL +
- "2. assertEquals(2, 1 + 1);" + EOL +
- "3. if (true) {" + EOL +
- "4. assertEquals(2, 1 + 1);" + EOL +
- "5. }" + EOL +
- "6. assertEquals(2, 1 + 1);" + EOL +
- "7. assertEquals(2, 1 + 1);" + EOL +
- "8. assertEquals(2, 1 + 1);" + EOL +
- "9. assertEquals(2, 1 + 1);" + EOL,
- snippet);
- }
-
- public void testSnippetsWithTenLinesOrMoreHaveTwoDigitLineNumbers() throws IOException {
- SnippetReader snippetReader = new SnippetReader(getResource("SnippetReaderTest.java"));
- String snippet = snippetReader.readSnippet("tenlineSnippet", true).toString();
- assertEquals(
- "01. assertEquals(2, 1 + 1);" + EOL +
- "02. assertEquals(2, 1 + 1);" + EOL +
- "03. if (true) {" + EOL +
- "04. assertEquals(2, 1 + 1);" + EOL +
- "05. }" + EOL +
- "06. assertEquals(2, 1 + 1);" + EOL +
- "07. assertEquals(2, 1 + 1);" + EOL +
- "08. assertEquals(2, 1 + 1);" + EOL +
- "09. assertEquals(2, 1 + 1);" + EOL +
- "10. assertEquals(2, 1 + 1);" + EOL,
- snippet);
- }
-
public void testCanReadSnippetsWithSeveralLinesAndNestedSnippet() throws IOException {
SnippetReader snippetReader = new SnippetReader(getResource("SnippetReaderTest.java"));
- String snippet = snippetReader.readSnippet("multilineSnippet", false).toString();
+ Snippet snippet = snippetReader.readSnippet("multilineSnippet");
+ StringWriter written = new StringWriter();
+ snippet.writeContent(written, false);
assertEquals(
- "assertEquals(2, 1 + 1);" + EOL +
- "if(true) {" + EOL +
- " assertEquals(6, 2 * 3);" + EOL +
- "}" + EOL +
- "assertEquals(2, 4 / 2);" + EOL,
- snippet);
+ "assertEquals(2, 1 + 1);\n" +
+ "if(true) {\n" +
+ " assertEquals(6, 2 * 3);\n" +
+ "}\n" +
+ "assertEquals(2, 4 / 2);\n",
+ written.getBuffer().toString());
}
public void testMinIndentFindsTheNumberOfSpacesOfTheLeastIndentedLine() {