1 16 package org.apache.commons.attributes.compiler; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.tools.ant.BuildException; 22 23 26 public class AttributeExpressionParser { 27 28 public static class Argument { 29 public final String field; 30 public final String text; 31 public final int length; 32 33 public Argument (String field, String text, int length) { 34 this.field = field; 35 this.text = text; 36 this.length = length; 37 } 38 39 public boolean equalsOrNull (String a, String b) { 40 if (a == null) { 41 return b == null; 42 } else { 43 return b != null && a.equals (b); 44 } 45 } 46 47 public boolean equals (Object o) { 48 return o instanceof Argument && 49 equalsOrNull (field, ((Argument) o).field) && 50 ((Argument) o).text.equals (text); 51 } 52 53 public String toString () { 54 return "[Argument: " + field + ", " + text + ", " + length + "]"; 55 } 56 } 57 58 public static class ParseResult { 59 public final List arguments = new ArrayList (); 60 public final String className; 61 62 public ParseResult (String className) { 63 this.className = className; 64 } 65 66 public boolean equals (Object o) { 67 return o instanceof ParseResult && 68 className.equals (((ParseResult) o).className) && 69 arguments.equals (((ParseResult) o).arguments); 70 } 71 72 public String toString () { 73 return "[ParseResult: " + className + ", " + arguments + "]"; 74 } 75 } 76 77 protected static Argument nextArgument (String string, int startPos, String filename, int line) { 78 if (string.charAt (startPos) == ')') { 79 return null; 80 } 81 82 StringBuffer sb = new StringBuffer (); 83 84 int i = startPos; 85 int depth = 0; 86 while (!( (string.charAt (i) == ',' || string.charAt (i) == ')') && depth == 0)) { 87 switch (string.charAt (i)) { 88 case '[': 89 case '{': 90 case '(': depth++; break; 91 92 case ']': 93 case '}': 94 case ')': depth--; break; 95 96 case '\'': 97 case '"': { 98 char endChar = string.charAt (i); 100 sb.append (string.charAt (i)); 101 i++; 102 while (true) { 103 char ch = string.charAt (i); 104 if (ch == '\\') { 105 sb.append (ch); 106 i++; 107 ch = string.charAt (i); 108 sb.append (ch); 109 } else { 110 if (ch == endChar) { 111 break; 112 } 113 sb.append (ch); 114 } 115 i++; 116 } 117 } 118 } 119 120 sb.append (string.charAt (i)); 121 i++; 122 if (i == string.length ()) { 123 throw new BuildException (filename + ":" + line + ": Unterminated argument: " + string); 124 } 125 } 126 127 if (string.charAt (i) == ',') { 128 i++; 129 } 130 131 String text = sb.toString (); 132 String field = null; 133 int eqPos = text.indexOf ('='); 134 if (eqPos > -1) { 135 boolean identifier = true; 136 for (int j = 0; j < eqPos; j++) { 137 char ch = text.charAt (j); 138 139 if (Character.isJavaIdentifierPart (ch) || ch == ' ') { 140 } else { 141 identifier = false; 142 } 143 } 144 145 if (identifier) { 146 field = text.substring (0, eqPos).trim (); 147 text = text.substring (eqPos + 1).trim (); 148 } 149 } 150 151 Argument arg = new Argument (field, text, i - startPos); 152 153 return arg; 154 } 155 156 public static ParseResult parse (String string, String filename, int line) { 157 StringBuffer sb = new StringBuffer (); 158 int i = 0; 159 while (i < string.length () && (Character.isJavaIdentifierPart (string.charAt (i)) || string.charAt (i) == '.' || string.charAt (i) == ' ')) { 160 sb.append (string.charAt (i)); 161 i++; 162 } 163 164 if (i == string.length () || string.charAt (i) != '(') { 165 throw new BuildException (filename + ":" + line + ": Illegal expression: " + string); 166 } 167 168 ParseResult result = new ParseResult (sb.toString ()); 169 170 i++; 171 Argument arg = null; 172 boolean seenField = false; 173 while ((arg = nextArgument (string, i, filename, line)) != null) { 174 if (arg.field != null) { 175 seenField = true; 176 } 177 178 if (seenField && arg.field == null) { 179 throw new BuildException (filename + ":" + line + ": Un-named parameters must come before the named parameters: " + string); 180 } 181 182 result.arguments.add (arg); 183 i += arg.length; 184 } 185 186 return result; 187 } 188 } | Popular Tags |