1 2 17 20 21 package com.sun.org.apache.xalan.internal.xsltc.runtime; 22 23 import java.io.FileWriter ; 24 import java.io.File ; 25 import java.text.DecimalFormat ; 26 import java.text.DecimalFormatSymbols ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Vector ; 30 import javax.xml.transform.Templates ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.DOMImplementation ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 36 37 import com.sun.org.apache.xml.internal.dtm.DTM; 38 39 import com.sun.org.apache.xalan.internal.xsltc.DOM; 40 import com.sun.org.apache.xalan.internal.xsltc.DOMCache; 41 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM; 42 import com.sun.org.apache.xalan.internal.xsltc.Translet; 43 import com.sun.org.apache.xalan.internal.xsltc.TransletException; 44 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter; 45 import com.sun.org.apache.xalan.internal.xsltc.dom.KeyIndex; 46 import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory; 47 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator; 48 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 49 50 57 public abstract class AbstractTranslet implements Translet { 58 59 public String _version = "1.0"; 62 public String _method = null; 63 public String _encoding = "UTF-8"; 64 public boolean _omitHeader = false; 65 public String _standalone = null; 66 public String _doctypePublic = null; 67 public String _doctypeSystem = null; 68 public boolean _indent = false; 69 public String _mediaType = null; 70 public Vector _cdata = null; 71 public int _indentamount = -1; 72 73 public static final int FIRST_TRANSLET_VERSION = 100; 74 public static final int VER_SPLIT_NAMES_ARRAY = 101; 75 public static final int CURRENT_TRANSLET_VERSION = VER_SPLIT_NAMES_ARRAY; 76 77 protected int transletVersion = FIRST_TRANSLET_VERSION; 83 84 protected String [] namesArray; 86 protected String [] urisArray; 87 protected int[] typesArray; 88 protected String [] namespaceArray; 89 90 protected Templates _templates = null; 92 93 protected boolean _hasIdCall = false; 95 96 protected StringValueHandler stringValueHandler = new StringValueHandler(); 98 99 private final static String EMPTYSTRING = ""; 101 102 private final static String ID_INDEX_NAME = "##id"; 104 105 106 109 public void printInternalState() { 110 System.out.println("-------------------------------------"); 111 System.out.println("AbstractTranslet this = " + this); 112 System.out.println("pbase = " + pbase); 113 System.out.println("vframe = " + pframe); 114 System.out.println("paramsStack.size() = " + paramsStack.size()); 115 System.out.println("namesArray.size = " + namesArray.length); 116 System.out.println("namespaceArray.size = " + namespaceArray.length); 117 System.out.println(""); 118 System.out.println("Total memory = " + Runtime.getRuntime().totalMemory()); 119 } 120 121 126 public final DOMAdapter makeDOMAdapter(DOM dom) 127 throws TransletException { 128 return new DOMAdapter(dom, namesArray, urisArray, typesArray, namespaceArray); 129 } 130 131 134 135 protected int pbase = 0, pframe = 0; 138 protected ArrayList paramsStack = new ArrayList (); 139 140 143 public final void pushParamFrame() { 144 paramsStack.add(pframe, new Integer (pbase)); 145 pbase = ++pframe; 146 } 147 148 151 public final void popParamFrame() { 152 if (pbase > 0) { 153 final int oldpbase = ((Integer )paramsStack.get(--pbase)).intValue(); 154 for (int i = pframe - 1; i >= pbase; i--) { 155 paramsStack.remove(i); 156 } 157 pframe = pbase; pbase = oldpbase; 158 } 159 } 160 161 169 public final Object addParameter(String name, Object value) { 170 name = BasisLibrary.mapQNameToJavaName (name); 171 return addParameter(name, value, false); 172 } 173 174 180 public final Object addParameter(String name, Object value, 181 boolean isDefault) 182 { 183 for (int i = pframe - 1; i >= pbase; i--) { 185 final Parameter param = (Parameter) paramsStack.get(i); 186 187 if (param._name.equals(name)) { 188 if (param._isDefault || !isDefault) { 191 param._value = value; 192 param._isDefault = isDefault; 193 return value; 194 } 195 return param._value; 196 } 197 } 198 199 paramsStack.add(pframe++, new Parameter(name, value, isDefault)); 201 return value; 202 } 203 204 207 public void clearParameters() { 208 pbase = pframe = 0; 209 paramsStack.clear(); 210 } 211 212 216 public final Object getParameter(String name) { 217 218 name = BasisLibrary.mapQNameToJavaName (name); 219 220 for (int i = pframe - 1; i >= pbase; i--) { 221 final Parameter param = (Parameter)paramsStack.get(i); 222 if (param._name.equals(name)) return param._value; 223 } 224 return null; 225 } 226 227 230 231 private MessageHandler _msgHandler = null; 235 236 239 public final void setMessageHandler(MessageHandler handler) { 240 _msgHandler = handler; 241 } 242 243 246 public final void displayMessage(String msg) { 247 if (_msgHandler == null) { 248 System.err.println(msg); 249 } 250 else { 251 _msgHandler.displayMessage(msg); 252 } 253 } 254 255 258 259 public Hashtable _formatSymbols = null; 261 262 266 public void addDecimalFormat(String name, DecimalFormatSymbols symbols) { 267 if (_formatSymbols == null) _formatSymbols = new Hashtable(); 269 270 if (name == null) name = EMPTYSTRING; 272 273 final DecimalFormat df = new DecimalFormat (); 275 if (symbols != null) { 276 df.setDecimalFormatSymbols(symbols); 277 } 278 _formatSymbols.put(name, df); 279 } 280 281 284 public final DecimalFormat getDecimalFormat(String name) { 285 286 if (_formatSymbols != null) { 287 if (name == null) name = EMPTYSTRING; 289 290 DecimalFormat df = (DecimalFormat )_formatSymbols.get(name); 291 if (df == null) df = (DecimalFormat )_formatSymbols.get(EMPTYSTRING); 292 return df; 293 } 294 return(null); 295 } 296 297 303 public final void prepassDocument(DOM document) { 304 setIndexSize(document.getSize()); 305 buildIDIndex(document); 306 } 307 308 313 private final void buildIDIndex(DOM document) { 314 315 if (document instanceof DOMEnhancedForDTM) { 316 DOMEnhancedForDTM enhancedDOM = (DOMEnhancedForDTM)document; 317 318 if (enhancedDOM.hasDOMSource()) { 322 buildKeyIndex(ID_INDEX_NAME, document); 323 return; 324 } 325 else { 326 final Hashtable elementsByID = enhancedDOM.getElementsWithIDs(); 327 328 if (elementsByID == null) { 329 return; 330 } 331 332 final Enumeration idValues = elementsByID.keys(); 336 boolean hasIDValues = false; 337 338 while (idValues.hasMoreElements()) { 339 final Object idValue = idValues.nextElement(); 340 final int element = ((Integer )elementsByID.get(idValue)).intValue(); 341 342 buildKeyIndex(ID_INDEX_NAME, element, idValue); 343 hasIDValues = true; 344 } 345 346 if (hasIDValues) { 347 setKeyIndexDom(ID_INDEX_NAME, document); 348 } 349 } 350 } 351 } 352 353 357 public final void postInitialization() { 358 if (transletVersion < VER_SPLIT_NAMES_ARRAY) { 361 int arraySize = namesArray.length; 362 String [] newURIsArray = new String [arraySize]; 363 String [] newNamesArray = new String [arraySize]; 364 int[] newTypesArray = new int[arraySize]; 365 366 for (int i = 0; i < arraySize; i++) { 367 String name = namesArray[i]; 368 int colonIndex = name.lastIndexOf(':'); 369 int lNameStartIdx = colonIndex+1; 370 371 if (colonIndex > -1) { 372 newURIsArray[i] = name.substring(0, colonIndex); 373 } 374 375 if (name.charAt(lNameStartIdx) == '@') { 378 lNameStartIdx++; 379 newTypesArray[i] = DTM.ATTRIBUTE_NODE; 380 } else if (name.charAt(lNameStartIdx) == '?') { 381 lNameStartIdx++; 382 newTypesArray[i] = DTM.NAMESPACE_NODE; 383 } else { 384 newTypesArray[i] = DTM.ELEMENT_NODE; 385 } 386 newNamesArray[i] = 387 (lNameStartIdx == 0) ? name 388 : name.substring(lNameStartIdx); 389 } 390 391 namesArray = newNamesArray; 392 urisArray = newURIsArray; 393 typesArray = newTypesArray; 394 } 395 396 if (transletVersion > CURRENT_TRANSLET_VERSION) { 400 BasisLibrary.runTimeError(BasisLibrary.UNKNOWN_TRANSLET_VERSION_ERR, 401 this.getClass().getName()); 402 } 403 } 404 405 408 409 private Hashtable _keyIndexes = null; 411 private KeyIndex _emptyKeyIndex = null; 412 private int _indexSize = 0; 413 414 418 public void setIndexSize(int size) { 419 if (size > _indexSize) _indexSize = size; 420 } 421 422 425 public KeyIndex createKeyIndex() { 426 return(new KeyIndex(_indexSize)); 427 } 428 429 435 public void buildKeyIndex(String name, int node, Object value) { 436 if (_keyIndexes == null) _keyIndexes = new Hashtable(); 437 438 KeyIndex index = (KeyIndex)_keyIndexes.get(name); 439 if (index == null) { 440 _keyIndexes.put(name, index = new KeyIndex(_indexSize)); 441 } 442 index.add(value, node); 443 } 444 445 450 public void buildKeyIndex(String name, DOM dom) { 451 if (_keyIndexes == null) _keyIndexes = new Hashtable(); 452 453 KeyIndex index = (KeyIndex)_keyIndexes.get(name); 454 if (index == null) { 455 _keyIndexes.put(name, index = new KeyIndex(_indexSize)); 456 } 457 index.setDom(dom); 458 } 459 460 464 public KeyIndex getKeyIndex(String name) { 465 if (_keyIndexes == null) { 467 return (_emptyKeyIndex != null) 468 ? _emptyKeyIndex 469 : (_emptyKeyIndex = new KeyIndex(1)); 470 } 471 472 final KeyIndex index = (KeyIndex)_keyIndexes.get(name); 474 475 if (index == null) { 477 return (_emptyKeyIndex != null) 478 ? _emptyKeyIndex 479 : (_emptyKeyIndex = new KeyIndex(1)); 480 } 481 482 return(index); 483 } 484 485 489 public void buildKeys(DOM document, DTMAxisIterator iterator, 490 SerializationHandler handler, 491 int root) throws TransletException { 492 493 } 494 495 499 public void setKeyIndexDom(String name, DOM document) { 500 getKeyIndex(name).setDom(document); 501 502 } 503 504 507 508 private DOMCache _domCache = null; 510 511 515 public void setDOMCache(DOMCache cache) { 516 _domCache = cache; 517 } 518 519 523 public DOMCache getDOMCache() { 524 return(_domCache); 525 } 526 527 531 532 public SerializationHandler openOutputHandler(String filename, boolean append) 533 throws TransletException 534 { 535 try { 536 final TransletOutputHandlerFactory factory 537 = TransletOutputHandlerFactory.newInstance(); 538 539 String dirStr = new File (filename).getParent(); 540 if ((null != dirStr) && (dirStr.length() > 0)) { 541 File dir = new File (dirStr); 542 dir.mkdirs(); 543 } 544 545 factory.setEncoding(_encoding); 546 factory.setOutputMethod(_method); 547 factory.setWriter(new FileWriter (filename, append)); 548 factory.setOutputType(TransletOutputHandlerFactory.STREAM); 549 550 final SerializationHandler handler 551 = factory.getSerializationHandler(); 552 553 transferOutputSettings(handler); 554 handler.startDocument(); 555 return handler; 556 } 557 catch (Exception e) { 558 throw new TransletException(e); 559 } 560 } 561 562 public SerializationHandler openOutputHandler(String filename) 563 throws TransletException 564 { 565 return openOutputHandler(filename, false); 566 } 567 568 public void closeOutputHandler(SerializationHandler handler) { 569 try { 570 handler.endDocument(); 571 handler.close(); 572 } 573 catch (Exception e) { 574 } 576 } 577 578 581 582 585 public abstract void transform(DOM document, DTMAxisIterator iterator, 586 SerializationHandler handler) 587 throws TransletException; 588 589 592 public final void transform(DOM document, SerializationHandler handler) 593 throws TransletException { 594 transform(document, document.getIterator(), handler); 595 } 596 597 601 public final void characters(final String string, 602 SerializationHandler handler) 603 throws TransletException { 604 if (string != null) { 605 try { 607 handler.characters(string); 608 } catch (Exception e) { 609 throw new TransletException(e); 610 } 611 } 612 } 613 614 617 public void addCdataElement(String name) { 618 if (_cdata == null) { 619 _cdata = new Vector (); 620 } 621 622 int lastColon = name.lastIndexOf(':'); 623 624 if (lastColon > 0) { 625 String uri = name.substring(0, lastColon); 626 String localName = name.substring(lastColon+1); 627 _cdata.addElement(uri); 628 _cdata.addElement(localName); 629 } else { 630 _cdata.addElement(null); 631 _cdata.addElement(name); 632 } 633 } 634 635 638 protected void transferOutputSettings(SerializationHandler handler) { 639 if (_method != null) { 640 if (_method.equals("xml")) { 641 if (_standalone != null) { 642 handler.setStandalone(_standalone); 643 } 644 if (_omitHeader) { 645 handler.setOmitXMLDeclaration(true); 646 } 647 handler.setCdataSectionElements(_cdata); 648 if (_version != null) { 649 handler.setVersion(_version); 650 } 651 handler.setIndent(_indent); 652 handler.setIndentAmount(_indentamount); 653 if (_doctypeSystem != null) { 654 handler.setDoctype(_doctypeSystem, _doctypePublic); 655 } 656 } 657 else if (_method.equals("html")) { 658 handler.setIndent(_indent); 659 handler.setDoctype(_doctypeSystem, _doctypePublic); 660 if (_mediaType != null) { 661 handler.setMediaType(_mediaType); 662 } 663 } 664 } 665 else { 666 handler.setCdataSectionElements(_cdata); 667 if (_version != null) { 668 handler.setVersion(_version); 669 } 670 if (_standalone != null) { 671 handler.setStandalone(_standalone); 672 } 673 if (_omitHeader) { 674 handler.setOmitXMLDeclaration(true); 675 } 676 handler.setIndent(_indent); 677 handler.setDoctype(_doctypeSystem, _doctypePublic); 678 } 679 } 680 681 private Hashtable _auxClasses = null; 682 683 public void addAuxiliaryClass(Class auxClass) { 684 if (_auxClasses == null) _auxClasses = new Hashtable(); 685 _auxClasses.put(auxClass.getName(), auxClass); 686 } 687 688 public void setAuxiliaryClasses(Hashtable auxClasses) { 689 _auxClasses = auxClasses; 690 } 691 692 public Class getAuxiliaryClass(String className) { 693 if (_auxClasses == null) return null; 694 return((Class )_auxClasses.get(className)); 695 } 696 697 public String [] getNamesArray() { 699 return namesArray; 700 } 701 702 public String [] getUrisArray() { 703 return urisArray; 704 } 705 706 public int[] getTypesArray() { 707 return typesArray; 708 } 709 710 public String [] getNamespaceArray() { 711 return namespaceArray; 712 } 713 714 public boolean hasIdCall() { 715 return _hasIdCall; 716 } 717 718 public Templates getTemplates() { 719 return _templates; 720 } 721 722 public void setTemplates(Templates templates) { 723 _templates = templates; 724 } 725 726 729 protected DOMImplementation _domImplementation = null; 730 731 public Document newDocument(String uri, String qname) 732 throws ParserConfigurationException 733 { 734 if (_domImplementation == null) { 735 _domImplementation = DocumentBuilderFactory.newInstance() 736 .newDocumentBuilder().getDOMImplementation(); 737 } 738 return _domImplementation.createDocument(uri, qname, null); 739 } 740 } 741 | Popular Tags |