Author: bdonlan
Date: 2005-01-08 22:25:14 -0500 (Sat, 08 Jan 2005)
New Revision: 562
Added:
trunk/misc/javer/src/javer/TextFormatter.java
Modified:
/
Log:
[EMAIL PROTECTED]: bdonlan | 2005-01-09T03:24:42.688167Z
Add a text formatter class.
Property changes on:
___________________________________________________________________
Name: svk:merge
- 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local:11299
+ 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local:11303
Added: trunk/misc/javer/src/javer/TextFormatter.java
===================================================================
--- trunk/misc/javer/src/javer/TextFormatter.java 2005-01-09 02:54:41 UTC
(rev 561)
+++ trunk/misc/javer/src/javer/TextFormatter.java 2005-01-09 03:25:14 UTC
(rev 562)
@@ -0,0 +1,139 @@
+/*
+ * TextFormatter.java
+ *
+ * Created on January 8, 2005, 9:45 PM
+ */
+
+package javer;
+import java.util.*;
+import java.util.regex.*;
+
+/**
+ *
+ * @author bdonlan
+ */
+public class TextFormatter implements java.io.Serializable, Cloneable {
+
+ protected static Pattern parsepattern = Pattern.compile("%[^%]*%");
+ protected Vector format;
+ protected String formatstr;
+ protected Set expectedKeys;
+
+ /** Creates a new instance of TextFormatter */
+ public TextFormatter(String formatstr) {
+ this(formatstr, null);
+ }
+
+ public TextFormatter(String formatstr, Set expectedKeys) {
+ this.formatstr = formatstr;
+ this.expectedKeys = new HashSet(expectedKeys);
+ parse(formatstr);
+ }
+
+ protected void parse(String formatStr) {
+ Matcher m = parsepattern.matcher(formatStr);
+ int lastEnd = 0;
+ Vector format = new Vector();
+ Iterator it;
+ while (m.find()) {
+ if (m.start() != lastEnd) {
+ /* grab the literal text between the last replace and now */
+ String literal = formatStr.substring(lastEnd, m.start());
+ LiteralChunk c = new LiteralChunk(formatStr, lastEnd, literal);
+ format.add(c);
+ }
+ Chunk c;
+ String key = formatStr.substring(m.start() + 1, m.end() - 1);
+ lastEnd = m.end();
+ if (key.equals("")) {
+ c = new LiteralChunk(formatStr, m.start(), "%");
+ format.add(c);
+ continue;
+ }
+ if (expectedKeys != null) {
+ if (!expectedKeys.contains(key)) {
+ throw new FormatException(formatStr, m.start(),
+ "Unexpected replacement key: " + key);
+ }
+ }
+ c = new ReplaceChunk(formatStr, m.start(), key);
+ format.add(c);
+ }
+ /* grab any literal data left */
+ String remain = formatStr.substring(lastEnd);
+ if (remain.length() != 0) {
+ format.add(new LiteralChunk(formatStr, lastEnd, remain));
+ }
+ format.trimToSize();
+ this.format = format;
+ }
+
+ public String format(Map replacements) {
+ StringBuffer b = new StringBuffer();
+ Iterator it = format.iterator();
+ while(it.hasNext()) {
+ Chunk c = (Chunk) it.next();
+ b.append(c.generate(replacements));
+ }
+ return b.toString();
+ }
+
+ protected interface Chunk extends java.io.Serializable, Cloneable {
+ public String generate(Map replacements);
+ }
+
+ protected final class LiteralChunk implements Chunk {
+ String literal;
+ public String generate(Map replacements) {
+ return literal;
+ }
+
+ public LiteralChunk(String formatStr, int pos, String literal) {
+ this.literal = literal;
+ }
+ }
+
+ protected final class ReplaceChunk implements Chunk {
+ String key, formatStr;
+ int pos;
+
+ public String generate(Map replacements) {
+ String value = (String) replacements.get(key);
+ if (value == null) {
+ throw new FormatException(formatStr, pos, "Key not found: " +
key);
+ }
+ return value;
+ }
+
+ public ReplaceChunk(String formatStr, int pos, String key) {
+ this.key = key;
+ this.formatStr = formatStr;
+ this.pos = pos;
+ }
+ }
+
+ public class FormatException extends RuntimeException {
+ public FormatException(String formatstr, int pos, String errorDesc) {
+ super(errorDesc);
+ }
+ }
+
+ public static void main(String[] args) throws Throwable {
+ String format;
+ HashMap repl = new HashMap(args.length - 1);
+ java.io.BufferedReader pr = new java.io.BufferedReader(
+ new java.io.InputStreamReader(System.in));
+ int i;
+
+ for (i = 0; i * 2 + 1 < args.length; i++) {
+ repl.put(args[i * 2], args[i * 2 + 1]);
+ }
+ while (true) {
+ TextFormatter f;
+ format = pr.readLine();
+ f = new TextFormatter(format, repl.keySet());
+ System.out.println(f.format(repl));
+ }
+ }
+
+}
Property changes on: trunk/misc/javer/src/javer/TextFormatter.java
___________________________________________________________________
Name: svn:eol-style
+ native