1 16 19 package org.apache.xalan.templates; 20 21 import java.util.Enumeration ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.TransformerException ; 25 26 import org.apache.xalan.transformer.TransformerImpl; 27 import org.apache.xml.serializer.SerializationHandler; 28 import org.apache.xml.utils.StringVector; 29 import org.apache.xpath.XPathContext; 30 import org.xml.sax.SAXException ; 31 32 37 public class ElemLiteralResult extends ElemUse 38 { 39 40 46 private boolean isLiteralResultAsStylesheet = false; 47 48 56 public void setIsLiteralResultAsStylesheet(boolean b) 57 { 58 isLiteralResultAsStylesheet = b; 59 } 60 61 69 public boolean getIsLiteralResultAsStylesheet() 70 { 71 return isLiteralResultAsStylesheet; 72 } 73 74 80 public void compose(StylesheetRoot sroot) throws TransformerException 81 { 82 super.compose(sroot); 83 StylesheetRoot.ComposeState cstate = sroot.getComposeState(); 84 java.util.Vector vnames = cstate.getVariableNames(); 85 if (null != m_avts) 86 { 87 int nAttrs = m_avts.size(); 88 89 for (int i = (nAttrs - 1); i >= 0; i--) 90 { 91 AVT avt = (AVT) m_avts.elementAt(i); 92 avt.fixupVariables(vnames, cstate.getGlobalsSize()); 93 } 94 } 95 } 96 97 103 private Vector m_avts = null; 104 105 107 private Vector m_xslAttr = null; 108 109 114 public void addLiteralResultAttribute(AVT avt) 115 { 116 117 if (null == m_avts) 118 m_avts = new Vector (); 119 120 m_avts.addElement(avt); 121 } 122 123 128 public void addLiteralResultAttribute(String att) 129 { 130 131 if (null == m_xslAttr) 132 m_xslAttr = new Vector (); 133 134 m_xslAttr.addElement(att); 135 } 136 137 148 public void setXmlSpace(AVT avt) 149 { 150 addLiteralResultAttribute(avt); 152 String val = avt.getSimpleString(); 153 if(val.equals("default")) 154 { 155 super.setXmlSpace(Constants.ATTRVAL_STRIP); 156 } 157 else if(val.equals("preserve")) 158 { 159 super.setXmlSpace(Constants.ATTRVAL_PRESERVE); 160 } 161 } 163 164 165 172 public AVT getLiteralResultAttribute(String name) 173 { 174 175 if (null != m_avts) 176 { 177 int nAttrs = m_avts.size(); 178 179 for (int i = (nAttrs - 1); i >= 0; i--) 180 { 181 AVT avt = (AVT) m_avts.elementAt(i); 182 183 if (avt.getRawName().equals(name)) 184 { 185 return avt; 186 } 187 } } 189 190 return null; 191 } 192 193 204 public boolean containsExcludeResultPrefix(String prefix, String uri) 205 { 206 if (uri == null || 207 (null == m_excludeResultPrefixes && 208 null == m_ExtensionElementURIs) 209 ) 210 return super.containsExcludeResultPrefix(prefix, uri); 211 212 if (prefix.length() == 0) 213 prefix = Constants.ATTRVAL_DEFAULT_PREFIX; 214 215 if(m_excludeResultPrefixes!=null) 218 for (int i =0; i< m_excludeResultPrefixes.size(); i++) 219 { 220 if (uri.equals(getNamespaceForPrefix(m_excludeResultPrefixes.elementAt(i)))) 221 return true; 222 } 223 224 if(m_ExtensionElementURIs!=null && m_ExtensionElementURIs.contains(uri)) 226 return true; 227 228 return super.containsExcludeResultPrefix(prefix, uri); 229 } 230 231 237 public void resolvePrefixTables() throws TransformerException 238 { 239 240 super.resolvePrefixTables(); 241 242 StylesheetRoot stylesheet = getStylesheetRoot(); 243 244 if ((null != m_namespace) && (m_namespace.length() > 0)) 245 { 246 NamespaceAlias nsa = stylesheet.getNamespaceAliasComposed(m_namespace); 247 248 if (null != nsa) 249 { 250 m_namespace = nsa.getResultNamespace(); 251 252 String resultPrefix = nsa.getStylesheetPrefix(); 255 if ((null != resultPrefix) && (resultPrefix.length() > 0)) 256 m_rawName = resultPrefix + ":" + m_localName; 257 else 258 m_rawName = m_localName; 259 } 260 } 261 262 if (null != m_avts) 263 { 264 int n = m_avts.size(); 265 266 for (int i = 0; i < n; i++) 267 { 268 AVT avt = (AVT) m_avts.elementAt(i); 269 270 String ns = avt.getURI(); 272 273 if ((null != ns) && (ns.length() > 0)) 274 { 275 NamespaceAlias nsa = 276 stylesheet.getNamespaceAliasComposed(m_namespace); 278 if (null != nsa) 279 { 280 String namespace = nsa.getResultNamespace(); 281 282 String resultPrefix = nsa.getStylesheetPrefix(); String rawName = avt.getName(); 285 286 if ((null != resultPrefix) && (resultPrefix.length() > 0)) 287 rawName = resultPrefix + ":" + rawName; 288 289 avt.setURI(namespace); 290 avt.setRawName(rawName); 291 } 292 } 293 } 294 } 295 } 296 297 305 boolean needToCheckExclude() 306 { 307 if (null == m_excludeResultPrefixes && null == m_prefixTable 308 && m_ExtensionElementURIs==null ) 310 return false; 311 else 312 { 313 314 if (null == m_prefixTable) 316 m_prefixTable = new Vector (); 317 318 return true; 319 } 320 } 321 322 326 private String m_namespace; 327 328 337 public void setNamespace(String ns) 338 { 339 if(null == ns) ns = ""; 341 m_namespace = ns; 342 } 343 344 353 public String getNamespace() 354 { 355 return m_namespace; 356 } 357 358 362 private String m_localName; 363 364 370 public void setLocalName(String localName) 371 { 372 m_localName = localName; 373 } 374 375 384 public String getLocalName() 385 { 386 return m_localName; 387 } 388 389 393 private String m_rawName; 394 395 401 public void setRawName(String rawName) 402 { 403 m_rawName = rawName; 404 } 405 406 412 public String getRawName() 413 { 414 return m_rawName; 415 } 416 417 422 public String getPrefix() 423 { 424 int len=m_rawName.length()-m_localName.length()-1; 425 return (len>0) 426 ? m_rawName.substring(0,len) 427 : ""; 428 } 429 430 431 435 private StringVector m_ExtensionElementURIs; 436 437 443 public void setExtensionElementPrefixes(StringVector v) 444 { 445 m_ExtensionElementURIs = v; 446 } 447 448 458 public String getExtensionElementPrefix(int i) 459 throws ArrayIndexOutOfBoundsException 460 { 461 462 if (null == m_ExtensionElementURIs) 463 throw new ArrayIndexOutOfBoundsException (); 464 465 return m_ExtensionElementURIs.elementAt(i); 466 } 467 468 474 public int getExtensionElementPrefixCount() 475 { 476 return (null != m_ExtensionElementURIs) 477 ? m_ExtensionElementURIs.size() : 0; 478 } 479 480 488 public boolean containsExtensionElementURI(String uri) 489 { 490 491 if (null == m_ExtensionElementURIs) 492 return false; 493 494 return m_ExtensionElementURIs.contains(uri); 495 } 496 497 503 public int getXSLToken() 504 { 505 return Constants.ELEMNAME_LITERALRESULT; 506 } 507 508 513 public String getNodeName() 514 { 515 516 return m_rawName; 518 } 519 520 524 private String m_version; 525 526 532 public void setVersion(String v) 533 { 534 m_version = v; 535 } 536 537 543 public String getVersion() 544 { 545 return m_version; 546 } 547 548 552 private StringVector m_excludeResultPrefixes; 553 554 566 public void setExcludeResultPrefixes(StringVector v) 567 { 568 m_excludeResultPrefixes = v; 569 } 570 571 582 private boolean excludeResultNSDecl(String prefix, String uri) 583 throws TransformerException 584 { 585 586 if (null != m_excludeResultPrefixes) 587 { 588 return containsExcludeResultPrefix(prefix, uri); 589 } 590 591 return false; 592 } 593 594 606 public void execute(TransformerImpl transformer) 607 throws TransformerException 608 { 609 SerializationHandler rhandler = transformer.getSerializationHandler(); 610 611 try 612 { 613 if (TransformerImpl.S_DEBUG) { 614 rhandler.flushPending(); 617 transformer.getTraceManager().fireTraceEvent(this); 618 } 619 620 rhandler.startPrefixMapping(getPrefix(), getNamespace()); 624 625 executeNSDecls(transformer); 627 rhandler.startElement(getNamespace(), getLocalName(), getRawName()); 628 } 629 catch (SAXException se) 630 { 631 throw new TransformerException (se); 632 } 633 634 639 640 TransformerException tException = null; 642 try 643 { 644 645 super.execute(transformer); 647 648 if (null != m_avts) 651 { 652 int nAttrs = m_avts.size(); 653 654 for (int i = (nAttrs - 1); i >= 0; i--) 655 { 656 AVT avt = (AVT) m_avts.elementAt(i); 657 XPathContext xctxt = transformer.getXPathContext(); 658 int sourceNode = xctxt.getCurrentNode(); 659 String stringedValue = 660 avt.evaluate(xctxt, sourceNode, this); 661 662 if (null != stringedValue) 663 { 664 665 670 rhandler.addAttribute( 671 avt.getURI(), 672 avt.getName(), 673 avt.getRawName(), 674 "CDATA", 675 stringedValue); 676 } 677 } } 679 680 transformer.executeChildTemplates(this, true); 683 } 684 catch (TransformerException te) 685 { 686 tException = te; 688 } 689 catch (SAXException se) 690 { 691 tException = new TransformerException (se); 692 } 693 694 try 695 { 696 701 if (TransformerImpl.S_DEBUG) { 702 transformer.getTraceManager().fireTraceEndEvent(this); 706 } 707 rhandler.endElement(getNamespace(), getLocalName(), getRawName()); 708 } 709 catch (SAXException se) 710 { 711 715 if (tException != null) 716 throw tException; 717 else 718 throw new TransformerException (se); 719 } 720 721 724 if (tException != null) 725 throw tException; 726 727 unexecuteNSDecls(transformer); 728 729 try 731 { 732 rhandler.endPrefixMapping(getPrefix()); 733 } 734 catch (SAXException se) 735 { 736 throw new TransformerException (se); 737 } 738 } 739 740 747 public Enumeration enumerateLiteralResultAttributes() 748 { 749 return (null == m_avts) ? null : m_avts.elements(); 750 } 751 752 759 protected boolean accept(XSLTVisitor visitor) 760 { 761 return visitor.visitLiteralResultElement(this); 762 } 763 764 768 protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs) 769 { 770 if (callAttrs && null != m_avts) 771 { 772 int nAttrs = m_avts.size(); 773 774 for (int i = (nAttrs - 1); i >= 0; i--) 775 { 776 AVT avt = (AVT) m_avts.elementAt(i); 777 avt.callVisitors(visitor); 778 } 779 } 780 super.callChildVisitors(visitor, callAttrs); 781 } 782 783 } 784 | Popular Tags |