JAL-1807 still testing
[jalviewjs.git] / unused / com / stevesoft / pat / ReplaceRule.java
index 8e969a3..6758e2f 100644 (file)
-//
-// This software is now distributed according to
-// the Lesser Gnu Public License.  Please see
-// http://www.gnu.org/copyleft/lesser.txt for
-// the details.
-//    -- Happy Computing!
-//
-package com.stevesoft.pat;
-
-import java.util.Hashtable;
-
-/**
- * ReplaceRule is a singly linked list of Objects which describe how to replace
- * the matched portion of a String. The only member method that you absolutely
- * need to define to use this class is apply(StringBuffer,RegRes) -- although
- * you may want define toString1() and clone1() (if you are unhappy with the
- * default methods) that are needed by the clone() or toString() methods on this
- * class. During the replacement process, each ReplaceRule tells the replacer
- * what to add to javajs.util.SB and uses the contents of the Regular expression
- * result to get the information it needs to do this. Here is an <a
- * href="http://javaregex.com/code/fancy.java.html">example</a>
- * 
- * @see com.stevesoft.pat.NullRule
- * @see com.stevesoft.pat.AmpersandRule
- * @see com.stevesoft.pat.BackRefRule
- * @see com.stevesoft.pat.LeftRule
- * @see com.stevesoft.pat.RightRule
- * @see com.stevesoft.pat.StringRule
- */
-public abstract class ReplaceRule
-{
-  /** points to the next ReplaceRule in the linked list. */
-  protected ReplaceRule next = null;
-
-  /**
-   * This function appends to the StringBufferLike the text you want to replaced
-   * the portion of the String last matched.
-   */
-  public abstract void apply(StringBufferLike sb, RegRes r);
-
-  /**
-   * A rule describing how to clone only the current ReplaceRule, and none of
-   * the others in this linked list. It is called by clone() for each item in
-   * the list.
-   */
-  public Object clone1()
-  {
-    return new RuleHolder(this);
-  }
-
-  public final Object clone()
-  {
-    ReplaceRule x = (ReplaceRule) clone1();
-    ReplaceRule xsav = x;
-    ReplaceRule y = this;
-    while (y.next != null)
-    {
-      x.next = (ReplaceRule) y.next.clone1();
-      x.name = y.name;
-      x = x.next;
-      y = y.next;
-    }
-    return xsav;
-  }
-
-  static ReplaceRule add(ReplaceRule head, ReplaceRule adding)
-  {
-    if (head == null)
-    {
-      return head = adding;
-    }
-    head.addRule(adding);
-    return head;
-  }
-
-  public ReplaceRule add(ReplaceRule adding)
-  {
-    return add(this, adding);
-  }
-
-  /** Add another ReplaceRule to the linked list. */
-  public void addRule(ReplaceRule r)
-  {
-    if (next == null)
-    {
-      next = r;
-    }
-    else
-    {
-      next.addRule(r);
-    }
-  }
-
-  static Regex getvar = null;
-
-  final static Regex getv()
-  {
-    // Thanks to Michael Jimenez for pointing out the need
-    // to clone getvar rather than simply returning it.
-    // Previously this was not thread safe.
-    // if(getvar != null) return getvar;
-    if (getvar != null)
-    {
-      return (Regex) getvar.clone();
-    }
-    getvar = new Regex("(?:\\\\(\\d+)|" + // ref 1
-            "\\$(?:" + "(\\d+)|" + // ref 2
-            "(\\w+)|" + // ref 3
-            "([&'`])|" + // ref 4
-            "\\{(?:(\\d+)|" + // ref 5
-            "([^\n}\\\\]+))}" + // ref 6
-            ")|" + "\\\\([nrbtaef])|" + // ref 7
-            "\\\\c([\u0000-\uFFFF])|" + // ref 8
-            "\\\\x([A-Fa-f0-9]{2})|" + // ref 9
-            "\\\\([\u0000-\uFFFF])" + // ref 10
-            ")");
-    getvar.optimize();
-    return getvar;
-  }
-
-  static Hashtable defs = new Hashtable();
-
-  public static boolean isDefined(String s)
-  {
-    return defs.get(s) != null;
-  }
-
-  public static void define(String s, Regex r)
-  {
-    defs.put(s, r);
-  }
-
-  public static void define(String s, ReplaceRule r)
-  {
-    defs.put(s, r);
-    r.name = s;
-  }
-
-  String name = getClass().getName();
-
-  public static void define(String s, Transformer t)
-  {
-    defs.put(s, t);
-  }
-
-  public static void undefine(String s)
-  {
-    defs.remove(s);
-  }
-
-  /**
-   * This tells how to convert just the current element (and none of the other
-   * items in the linked list) to a String. This method is called by toString()
-   * for each item in the linked list.
-   */
-  public String toString1()
-  {
-    return "${" + name + "}";
-  }
-
-  /** Convert to a String. */
-  public final String toString()
-  {
-    javajs.util.SB sb = new javajs.util.SB();
-    sb.append(toString1());
-    ReplaceRule rr = this.next;
-    while (rr != null)
-    {
-      sb.append(rr.toString1());
-      rr = rr.next;
-    }
-    return sb.toString();
-  }
-
-  /**
-   * Modified the behavior of a ReplaceRule by supplying an argument. If a
-   * ReplaceRule named "foo" is defined and the pattern "s/x/${foo:5}/" is given
-   * to Regex.perlCode, then the "foo" the definition of "foo" will be retrieved
-   * and arg("5") will be called. If the result is non-null, that is the
-   * ReplaceRule that will be used. If the result is null, then the pattern
-   * works just as if it were "s/x/${foo}/".
-   * 
-   * @see com.stevesoft.pat.Validator#arg(java.lang.String)
-   */
-  public ReplaceRule arg(String s)
-  {
-    return null;
-  }
-
-  static int getHexDigit(char c)
-  {
-    if (c >= '0' && c <= '9')
-    {
-      return c - '0';
-    }
-    if (c >= 'a' && c <= 'f')
-    {
-      return c - 'a' + 10;
-    }
-    return c - 'A' + 10;
-  }
-}
+//\r
+// This software is now distributed according to\r
+// the Lesser Gnu Public License.  Please see\r
+// http://www.gnu.org/copyleft/lesser.txt for\r
+// the details.\r
+//    -- Happy Computing!\r
+//\r
+package com.stevesoft.pat;\r
+\r
+import java.util.Hashtable;\r
+\r
+/**\r
+ * ReplaceRule is a singly linked list of Objects which describe how to replace\r
+ * the matched portion of a String. The only member method that you absolutely\r
+ * need to define to use this class is apply(StringBuffer,RegRes) -- although\r
+ * you may want define toString1() and clone1() (if you are unhappy with the\r
+ * default methods) that are needed by the clone() or toString() methods on this\r
+ * class. During the replacement process, each ReplaceRule tells the replacer\r
+ * what to add to javajs.util.SB and uses the contents of the Regular expression\r
+ * result to get the information it needs to do this. Here is an <a\r
+ * href="http://javaregex.com/code/fancy.java.html">example</a>\r
+ * \r
+ * @see com.stevesoft.pat.NullRule\r
+ * @see com.stevesoft.pat.AmpersandRule\r
+ * @see com.stevesoft.pat.BackRefRule\r
+ * @see com.stevesoft.pat.LeftRule\r
+ * @see com.stevesoft.pat.RightRule\r
+ * @see com.stevesoft.pat.StringRule\r
+ */\r
+public abstract class ReplaceRule\r
+{\r
+  /** points to the next ReplaceRule in the linked list. */\r
+  protected ReplaceRule next = null;\r
+\r
+  /**\r
+   * This function appends to the StringBufferLike the text you want to replaced\r
+   * the portion of the String last matched.\r
+   */\r
+  public abstract void apply(StringBufferLike sb, RegRes r);\r
+\r
+  /**\r
+   * A rule describing how to clone only the current ReplaceRule, and none of\r
+   * the others in this linked list. It is called by clone() for each item in\r
+   * the list.\r
+   */\r
+  public Object clone1()\r
+  {\r
+    return new RuleHolder(this);\r
+  }\r
+\r
+  public final Object clone()\r
+  {\r
+    ReplaceRule x = (ReplaceRule) clone1();\r
+    ReplaceRule xsav = x;\r
+    ReplaceRule y = this;\r
+    while (y.next != null)\r
+    {\r
+      x.next = (ReplaceRule) y.next.clone1();\r
+      x.name = y.name;\r
+      x = x.next;\r
+      y = y.next;\r
+    }\r
+    return xsav;\r
+  }\r
+\r
+  static ReplaceRule add(ReplaceRule head, ReplaceRule adding)\r
+  {\r
+    if (head == null)\r
+    {\r
+      return head = adding;\r
+    }\r
+    head.addRule(adding);\r
+    return head;\r
+  }\r
+\r
+  public ReplaceRule add(ReplaceRule adding)\r
+  {\r
+    return add(this, adding);\r
+  }\r
+\r
+  /** Add another ReplaceRule to the linked list. */\r
+  public void addRule(ReplaceRule r)\r
+  {\r
+    if (next == null)\r
+    {\r
+      next = r;\r
+    }\r
+    else\r
+    {\r
+      next.addRule(r);\r
+    }\r
+  }\r
+\r
+  static Regex getvar = null;\r
+\r
+  final static Regex getv()\r
+  {\r
+    // Thanks to Michael Jimenez for pointing out the need\r
+    // to clone getvar rather than simply returning it.\r
+    // Previously this was not thread safe.\r
+    // if(getvar != null) return getvar;\r
+    if (getvar != null)\r
+    {\r
+      return (Regex) getvar.clone();\r
+    }\r
+    getvar = new Regex("(?:\\\\(\\d+)|" + // ref 1\r
+            "\\$(?:" + "(\\d+)|" + // ref 2\r
+            "(\\w+)|" + // ref 3\r
+            "([&'`])|" + // ref 4\r
+            "\\{(?:(\\d+)|" + // ref 5\r
+            "([^\n}\\\\]+))}" + // ref 6\r
+            ")|" + "\\\\([nrbtaef])|" + // ref 7\r
+            "\\\\c([\u0000-\uFFFF])|" + // ref 8\r
+            "\\\\x([A-Fa-f0-9]{2})|" + // ref 9\r
+            "\\\\([\u0000-\uFFFF])" + // ref 10\r
+            ")");\r
+    getvar.optimize();\r
+    return getvar;\r
+  }\r
+\r
+  static Hashtable defs = new Hashtable();\r
+\r
+  public static boolean isDefined(String s)\r
+  {\r
+    return defs.get(s) != null;\r
+  }\r
+\r
+  public static void define(String s, Regex r)\r
+  {\r
+    defs.put(s, r);\r
+  }\r
+\r
+  public static void define(String s, ReplaceRule r)\r
+  {\r
+    defs.put(s, r);\r
+    r.name = s;\r
+  }\r
+\r
+  String name = getClass().getName();\r
+\r
+  public static void define(String s, Transformer t)\r
+  {\r
+    defs.put(s, t);\r
+  }\r
+\r
+  public static void undefine(String s)\r
+  {\r
+    defs.remove(s);\r
+  }\r
+\r
+  /**\r
+   * This tells how to convert just the current element (and none of the other\r
+   * items in the linked list) to a String. This method is called by toString()\r
+   * for each item in the linked list.\r
+   */\r
+  public String toString1()\r
+  {\r
+    return "${" + name + "}";\r
+  }\r
+\r
+  /** Convert to a String. */\r
+  public final String toString()\r
+  {\r
+    javajs.util.SB sb = new javajs.util.SB();\r
+    sb.append(toString1());\r
+    ReplaceRule rr = this.next;\r
+    while (rr != null)\r
+    {\r
+      sb.append(rr.toString1());\r
+      rr = rr.next;\r
+    }\r
+    return sb.toString();\r
+  }\r
+\r
+  /**\r
+   * Modified the behavior of a ReplaceRule by supplying an argument. If a\r
+   * ReplaceRule named "foo" is defined and the pattern "s/x/${foo:5}/" is given\r
+   * to Regex.perlCode, then the "foo" the definition of "foo" will be retrieved\r
+   * and arg("5") will be called. If the result is non-null, that is the\r
+   * ReplaceRule that will be used. If the result is null, then the pattern\r
+   * works just as if it were "s/x/${foo}/".\r
+   * \r
+   * @see com.stevesoft.pat.Validator#arg(java.lang.String)\r
+   */\r
+  public ReplaceRule arg(String s)\r
+  {\r
+    return null;\r
+  }\r
+\r
+  static int getHexDigit(char c)\r
+  {\r
+    if (c >= '0' && c <= '9')\r
+    {\r
+      return c - '0';\r
+    }\r
+    if (c >= 'a' && c <= 'f')\r
+    {\r
+      return c - 'a' + 10;\r
+    }\r
+    return c - 'A' + 10;\r
+  }\r
+}\r