1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 4 5 10 11 public final class AttributeValueTemplate extends Expression { 12 13 private Expression[] components = new Expression[10]; 14 private int numberOfComponents; 15 16 21 22 private AttributeValueTemplate(Expression[] components, int numberOfComponents) { 23 this.components = new Expression[numberOfComponents]; 24 this.numberOfComponents = numberOfComponents; 25 System.arraycopy(components, 0, this.components, 0, numberOfComponents); 26 } 27 28 31 32 public static Expression make(String avt, StaticContext env) throws XPathException { 33 34 if ( avt.indexOf("{") < 0 && avt.indexOf("}") < 0) { return new StringValue(avt); 36 37 } else { 38 int nr = 0; 39 Expression[] components = new Expression[avt.length()]; int i0, i1, i2, i8, i9, len, last; 42 char inquote = ' '; 43 last = 0; 44 len = avt.length(); 45 while (last<len) { 46 i0 = avt.indexOf("{", last); 47 i1 = avt.indexOf("{{", last); 48 i8 = avt.indexOf("}", last); 49 i9 = avt.indexOf("}}", last); 50 52 if (i8>=0 && (i0<0 || i8<i0)) { if (i8 != i9) { throw new XPathException("Closing curly brace in attribute value template \"" + avt + "\" must be doubled"); 55 } 56 components[nr++] = new StringValue(avt.substring(last, i8+1)); 57 last = i8+2; 58 } else if (i1>=0 && i1==i0) { components[nr++] = new StringValue(avt.substring(last, i1+1)); 60 last = i1+2; 61 } else if (i0>=0) { if (i0>last) { 63 components[nr++] = new StringValue(avt.substring(last, i0)); 64 } 65 for (i2=i0+1; i2<len; i2++) { 66 if (avt.charAt(i2)=='\"') inquote = '\"'; 67 if (avt.charAt(i2)=='\'') inquote = '\''; 68 if (inquote!=' ') { 69 i2++; 70 while (i2<len && avt.charAt(i2)!=inquote) i2++; 71 inquote = ' '; 72 } else { 73 if (avt.charAt(i2)=='}') { 74 break; 76 } 77 } 78 } 79 if (i2>=len) { 80 throw new XPathException("No closing \"}\" in attribute value template " + avt); 81 } 82 83 String expr = avt.substring(i0+1, i2); 84 Expression ex = Expression.make(expr, env); 85 components[nr++] = ex; 86 last=i2+1; 87 } else { components[nr++] = new StringValue(avt.substring(last)); 89 last=len; 90 } 91 } 92 return (new AttributeValueTemplate(components, nr)).simplify(); 93 } 94 } 95 96 100 101 public Expression simplify() throws XPathException { 102 103 if (numberOfComponents==0) { 105 return new StringValue(""); 106 } 107 108 if (numberOfComponents==1) { 110 return components[0]; 111 } 112 113 for (int i=0; i<numberOfComponents; i++) { 115 components[i] = components[i].simplify(); 116 } 117 118 return this; 120 } 121 122 127 128 public Value evaluate(Context context) throws XPathException { 129 return new StringValue(evaluateAsString(context)); 130 } 131 132 136 137 public int getDataType() { 138 return Value.STRING; 139 } 140 141 146 147 public String evaluateAsString(Context context) throws XPathException { 148 StringBuffer sb = new StringBuffer (); 149 for (int i=0; i<numberOfComponents; i++) { 150 sb.append(components[i].evaluateAsString(context)); 151 } 152 return sb.toString(); 153 } 154 155 160 161 public int getDependencies() { 162 int dep = 0; 163 for (int i=0; i<numberOfComponents; i++) { 164 dep |= components[i].getDependencies(); 165 } 166 return dep; 167 } 168 169 177 178 public Expression reduce(int dependencies, Context context) throws XPathException { 179 throw new XPathException("Cannot reduce expressions in an attribute value template"); 180 } 181 182 185 186 public void display(int level) { 187 System.err.println(indent(level) + "{<AVT>}"); 188 } 189 } 190 191 | Popular Tags |