1 16 19 package org.apache.xalan.templates; 20 21 import java.util.StringTokenizer ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.TransformerException ; 25 26 import org.apache.xalan.processor.StylesheetHandler; 27 import org.apache.xalan.res.XSLMessages; 28 import org.apache.xalan.res.XSLTErrorResources; 29 import org.apache.xml.utils.FastStringBuffer; 30 import org.apache.xml.utils.StringBufferPool; 31 import org.apache.xpath.XPath; 32 import org.apache.xpath.XPathContext; 33 34 38 public class AVT implements java.io.Serializable , XSLTVisitable 39 { 40 41 45 private String m_simpleString = null; 46 47 51 private Vector m_parts = null; 52 53 57 private String m_rawName; 58 59 64 public String getRawName() 65 { 66 return m_rawName; 67 } 68 69 74 public void setRawName(String rawName) 75 { 76 m_rawName = rawName; 77 } 78 79 83 private String m_name; 84 85 90 public String getName() 91 { 92 return m_name; 93 } 94 95 100 public void setName(String name) 101 { 102 m_name = name; 103 } 104 105 109 private String m_uri; 110 111 116 public String getURI() 117 { 118 return m_uri; 119 } 120 121 126 public void setURI(String uri) 127 { 128 m_uri = uri; 129 } 130 131 144 public AVT(StylesheetHandler handler, String uri, String name, 145 String rawName, String stringedValue, 146 ElemTemplateElement owner) 147 throws javax.xml.transform.TransformerException 148 { 149 150 m_uri = uri; 151 m_name = name; 152 m_rawName = rawName; 153 154 StringTokenizer tokenizer = new StringTokenizer (stringedValue, "{}\"\'", 155 true); 156 int nTokens = tokenizer.countTokens(); 157 158 if (nTokens < 2) 159 { 160 m_simpleString = stringedValue; } 162 else 163 { 164 FastStringBuffer buffer = StringBufferPool.get(); 165 FastStringBuffer exprBuffer = StringBufferPool.get(); 166 167 try 168 { 169 m_parts = new Vector (nTokens + 1); 170 171 String t = null; String lookahead = null; String error = null; 175 while (tokenizer.hasMoreTokens()) 176 { 177 if (lookahead != null) 178 { 179 t = lookahead; 180 lookahead = null; 181 } 182 else 183 t = tokenizer.nextToken(); 184 185 if (t.length() == 1) 186 { 187 switch (t.charAt(0)) 188 { 189 case ('\"') : 190 case ('\'') : 191 { 192 193 buffer.append(t); 195 196 break; 197 } 198 case ('{') : 199 { 200 201 try 202 { 203 lookahead = tokenizer.nextToken(); 205 206 if (lookahead.equals("{")) 207 { 208 209 buffer.append(lookahead); 211 212 lookahead = null; 213 214 break; } 216 217 225 else 226 { 227 if (buffer.length() > 0) 228 { 229 m_parts.addElement(new AVTPartSimple(buffer.toString())); 230 buffer.setLength(0); 231 } 232 233 exprBuffer.setLength(0); 234 235 while (null != lookahead) 236 { 237 if (lookahead.length() == 1) 238 { 239 switch (lookahead.charAt(0)) 240 { 241 case '\'' : 242 case '\"' : 243 { 244 245 exprBuffer.append(lookahead); 247 248 String quote = lookahead; 249 250 lookahead = tokenizer.nextToken(); 252 253 while (!lookahead.equals(quote)) 254 { 255 exprBuffer.append(lookahead); 256 257 lookahead = tokenizer.nextToken(); 258 } 259 260 exprBuffer.append(lookahead); 261 262 lookahead = tokenizer.nextToken(); 263 264 break; 265 } 266 case '{' : 267 { 268 269 error = XSLMessages.createMessage( 271 XSLTErrorResources.ER_NO_CURLYBRACE, null); 273 lookahead = null; 275 break; 276 } 277 case '}' : 278 { 279 280 buffer.setLength(0); 283 284 XPath xpath = 285 handler.createXPath(exprBuffer.toString(), owner); 286 287 m_parts.addElement(new AVTPartXPath(xpath)); 288 289 lookahead = null; 291 break; 292 } 293 default : 294 { 295 296 exprBuffer.append(lookahead); 298 299 lookahead = tokenizer.nextToken(); 300 } 301 } } else 304 { 305 306 exprBuffer.append(lookahead); 308 309 lookahead = tokenizer.nextToken(); 310 } 311 } 313 if (error != null) 314 { 315 break; } 317 } 318 319 break; 320 } 321 catch (java.util.NoSuchElementException ex) 322 { 323 error = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object []{ name, stringedValue }); 324 break; 325 } 326 } 327 case ('}') : 328 { 329 lookahead = tokenizer.nextToken(); 330 331 if (lookahead.equals("}")) 332 { 333 334 buffer.append(lookahead); 336 337 lookahead = null; } 339 else 340 { 341 342 try 344 { 345 handler.warn(XSLTErrorResources.WG_FOUND_CURLYBRACE, null); } 347 catch (org.xml.sax.SAXException se) 348 { 349 throw new TransformerException (se); 350 } 351 352 buffer.append("}"); 353 354 } 356 357 break; 358 } 359 default : 360 { 361 362 buffer.append(t); 364 } 365 } } else 368 { 369 370 buffer.append(t); 372 } 373 374 if (null != error) 375 { 376 try 377 { 378 handler.warn(XSLTErrorResources.WG_ATTR_TEMPLATE, 379 new Object []{ error }); } 381 catch (org.xml.sax.SAXException se) 382 { 383 throw new TransformerException (se); 384 } 385 386 break; 387 } 388 } 390 if (buffer.length() > 0) 391 { 392 m_parts.addElement(new AVTPartSimple(buffer.toString())); 393 buffer.setLength(0); 394 } 395 } 396 finally 397 { 398 StringBufferPool.free(buffer); 399 StringBufferPool.free(exprBuffer); 400 } 401 } 403 if (null == m_parts && (null == m_simpleString)) 404 { 405 406 m_simpleString = ""; 408 } 409 } 410 411 416 public String getSimpleString() 417 { 418 419 if (null != m_simpleString) 420 { 421 return m_simpleString; 422 } 423 else if (null != m_parts) 424 { 425 FastStringBuffer buf = StringBufferPool.get(); 426 String s; 427 428 try 429 { 430 buf.setLength(0); 431 432 int n = m_parts.size(); 433 434 for (int i = 0; i < n; i++) 435 { 436 AVTPart part = (AVTPart) m_parts.elementAt(i); 437 438 buf.append(part.getSimpleString()); 439 } 440 441 s = buf.toString(); 442 } 443 finally 444 { 445 StringBufferPool.free(buf); 446 } 447 448 return s; 449 } 450 else 451 { 452 return ""; 453 } 454 } 455 456 468 public String evaluate( 469 XPathContext xctxt, int context, org.apache.xml.utils.PrefixResolver nsNode) 470 throws javax.xml.transform.TransformerException 471 { 472 473 FastStringBuffer buf = StringBufferPool.get(); 474 475 try 476 { 477 if (null != m_simpleString) 478 { 479 return m_simpleString; 480 } 481 else if (null != m_parts) 482 { 483 buf.setLength(0); 484 485 int n = m_parts.size(); 486 487 for (int i = 0; i < n; i++) 488 { 489 AVTPart part = (AVTPart) m_parts.elementAt(i); 490 491 part.evaluate(xctxt, buf, context, nsNode); 492 } 493 494 return buf.toString(); 495 } 496 else 497 { 498 return ""; 499 } 500 } 501 finally 502 { 503 StringBufferPool.free(buf); 504 } 505 } 506 507 518 public boolean isContextInsensitive() 519 { 520 return null != m_simpleString; 521 } 522 523 529 public boolean canTraverseOutsideSubtree() 530 { 531 532 if (null != m_parts) 533 { 534 int n = m_parts.size(); 535 536 for (int i = 0; i < n; i++) 537 { 538 AVTPart part = (AVTPart) m_parts.elementAt(i); 539 540 if (part.canTraverseOutsideSubtree()) 541 return true; 542 } 543 } 544 545 return false; 546 } 547 548 558 public void fixupVariables(java.util.Vector vars, int globalsSize) 559 { 560 if (null != m_parts) 561 { 562 int n = m_parts.size(); 563 564 for (int i = 0; i < n; i++) 565 { 566 AVTPart part = (AVTPart) m_parts.elementAt(i); 567 568 part.fixupVariables(vars, globalsSize); 569 } 570 } 571 } 572 573 576 public void callVisitors(XSLTVisitor visitor) 577 { 578 if(visitor.visitAVT(this) && (null != m_parts)) 579 { 580 int n = m_parts.size(); 581 582 for (int i = 0; i < n; i++) 583 { 584 AVTPart part = (AVTPart) m_parts.elementAt(i); 585 586 part.callVisitors(visitor); 587 } 588 } 589 } 590 591 592 595 public boolean isSimple() { 596 return m_simpleString != null; 597 } 598 } 599 | Popular Tags |