1 16 19 20 package org.apache.xalan.xsltc.runtime; 21 22 import java.io.FileWriter ; 23 import java.text.DecimalFormat ; 24 import java.text.DecimalFormatSymbols ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 import javax.xml.transform.Templates ; 29 30 import org.apache.xml.dtm.DTM; 31 32 import org.apache.xalan.xsltc.DOM; 33 import org.apache.xalan.xsltc.DOMCache; 34 import org.apache.xalan.xsltc.DOMEnhancedForDTM; 35 import org.apache.xalan.xsltc.Translet; 36 import org.apache.xalan.xsltc.TransletException; 37 import org.apache.xalan.xsltc.dom.DOMAdapter; 38 import org.apache.xalan.xsltc.dom.KeyIndex; 39 import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory; 40 import org.apache.xml.dtm.DTMAxisIterator; 41 import org.apache.xml.serializer.SerializationHandler; 42 43 50 public abstract class AbstractTranslet implements Translet { 51 52 public String _version = "1.0"; 55 public String _method = null; 56 public String _encoding = "UTF-8"; 57 public boolean _omitHeader = false; 58 public String _standalone = null; 59 public String _doctypePublic = null; 60 public String _doctypeSystem = null; 61 public boolean _indent = false; 62 public String _mediaType = null; 63 public Vector _cdata = null; 64 65 public static final int FIRST_TRANSLET_VERSION = 100; 66 public static final int VER_SPLIT_NAMES_ARRAY = 101; 67 public static final int CURRENT_TRANSLET_VERSION = VER_SPLIT_NAMES_ARRAY; 68 69 protected int transletVersion = FIRST_TRANSLET_VERSION; 75 76 protected String [] namesArray; 78 protected String [] urisArray; 79 protected int[] typesArray; 80 protected String [] namespaceArray; 81 82 protected Templates _templates = null; 84 85 protected boolean _hasIdCall = false; 87 88 protected StringValueHandler stringValueHandler = new StringValueHandler(); 90 91 private final static String EMPTYSTRING = ""; 93 94 private final static String ID_INDEX_NAME = "##id"; 96 97 98 101 public void printInternalState() { 102 System.out.println("-------------------------------------"); 103 System.out.println("AbstractTranslet this = " + this); 104 System.out.println("pbase = " + pbase); 105 System.out.println("vframe = " + pframe); 106 System.out.println("paramsStack.size() = " + paramsStack.size()); 107 System.out.println("namesArray.size = " + namesArray.length); 108 System.out.println("namespaceArray.size = " + namespaceArray.length); 109 System.out.println(""); 110 System.out.println("Total memory = " + Runtime.getRuntime().totalMemory()); 111 } 112 113 118 public final DOMAdapter makeDOMAdapter(DOM dom) 119 throws TransletException { 120 return new DOMAdapter(dom, namesArray, urisArray, typesArray, namespaceArray); 121 } 122 123 126 127 protected int pbase = 0, pframe = 0; 130 protected ArrayList paramsStack = new ArrayList (); 131 132 135 public final void pushParamFrame() { 136 paramsStack.add(pframe, new Integer (pbase)); 137 pbase = ++pframe; 138 } 139 140 143 public final void popParamFrame() { 144 if (pbase > 0) { 145 final int oldpbase = ((Integer )paramsStack.get(--pbase)).intValue(); 146 for (int i = pframe - 1; i >= pbase; i--) { 147 paramsStack.remove(i); 148 } 149 pframe = pbase; pbase = oldpbase; 150 } 151 } 152 153 161 public final Object addParameter(String name, Object value) { 162 name = BasisLibrary.mapQNameToJavaName (name); 163 return addParameter(name, value, false); 164 } 165 166 172 public final Object addParameter(String name, Object value, 173 boolean isDefault) 174 { 175 for (int i = pframe - 1; i >= pbase; i--) { 177 final Parameter param = (Parameter) paramsStack.get(i); 178 179 if (param._name.equals(name)) { 180 if (param._isDefault || !isDefault) { 183 param._value = value; 184 param._isDefault = isDefault; 185 return value; 186 } 187 return param._value; 188 } 189 } 190 191 paramsStack.add(pframe++, new Parameter(name, value, isDefault)); 193 return value; 194 } 195 196 199 public void clearParameters() { 200 pbase = pframe = 0; 201 paramsStack.clear(); 202 } 203 204 208 public final Object getParameter(String name) { 209 210 name = BasisLibrary.mapQNameToJavaName (name); 211 212 for (int i = pframe - 1; i >= pbase; i--) { 213 final Parameter param = (Parameter)paramsStack.get(i); 214 if (param._name.equals(name)) return param._value; 215 } 216 return null; 217 } 218 219 222 223 private MessageHandler _msgHandler = null; 227 228 231 public final void setMessageHandler(MessageHandler handler) { 232 _msgHandler = handler; 233 } 234 235 238 public final void displayMessage(String msg) { 239 if (_msgHandler == null) { 240 System.err.println(msg); 241 } 242 else { 243 _msgHandler.displayMessage(msg); 244 } 245 } 246 247 250 251 public Hashtable _formatSymbols = null; 253 254 258 public void addDecimalFormat(String name, DecimalFormatSymbols symbols) { 259 if (_formatSymbols == null) _formatSymbols = new Hashtable(); 261 262 if (name == null) name = EMPTYSTRING; 264 265 final DecimalFormat df = new DecimalFormat (); 267 if (symbols != null) { 268 df.setDecimalFormatSymbols(symbols); 269 } 270 _formatSymbols.put(name, df); 271 } 272 273 276 public final DecimalFormat getDecimalFormat(String name) { 277 278 if (_formatSymbols != null) { 279 if (name == null) name = EMPTYSTRING; 281 282 DecimalFormat df = (DecimalFormat )_formatSymbols.get(name); 283 if (df == null) df = (DecimalFormat )_formatSymbols.get(EMPTYSTRING); 284 return df; 285 } 286 return(null); 287 } 288 289 295 public final void prepassDocument(DOM document) { 296 setIndexSize(document.getSize()); 297 buildIDIndex(document); 298 } 299 300 305 private final void buildIDIndex(DOM document) { 306 307 if (document instanceof DOMEnhancedForDTM) { 308 DOMEnhancedForDTM enhancedDOM = (DOMEnhancedForDTM)document; 309 310 if (enhancedDOM.hasDOMSource()) { 314 buildKeyIndex(ID_INDEX_NAME, document); 315 return; 316 } 317 else { 318 final Hashtable elementsByID = enhancedDOM.getElementsWithIDs(); 319 320 if (elementsByID == null) { 321 return; 322 } 323 324 final Enumeration idValues = elementsByID.keys(); 328 boolean hasIDValues = false; 329 330 while (idValues.hasMoreElements()) { 331 final Object idValue = idValues.nextElement(); 332 final int element = ((Integer )elementsByID.get(idValue)).intValue(); 333 334 buildKeyIndex(ID_INDEX_NAME, element, idValue); 335 hasIDValues = true; 336 } 337 338 if (hasIDValues) { 339 setKeyIndexDom(ID_INDEX_NAME, document); 340 } 341 } 342 } 343 } 344 345 349 public final void postInitialization() { 350 if (transletVersion < VER_SPLIT_NAMES_ARRAY) { 353 int arraySize = namesArray.length; 354 String [] newURIsArray = new String [arraySize]; 355 String [] newNamesArray = new String [arraySize]; 356 int[] newTypesArray = new int[arraySize]; 357 358 for (int i = 0; i < arraySize; i++) { 359 String name = namesArray[i]; 360 int colonIndex = name.lastIndexOf(':'); 361 int lNameStartIdx = colonIndex+1; 362 363 if (colonIndex > -1) { 364 newURIsArray[i] = name.substring(0, colonIndex); 365 } 366 367 if (name.charAt(lNameStartIdx) == '@') { 370 lNameStartIdx++; 371 newTypesArray[i] = DTM.ATTRIBUTE_NODE; 372 } else if (name.charAt(lNameStartIdx) == '?') { 373 lNameStartIdx++; 374 newTypesArray[i] = DTM.NAMESPACE_NODE; 375 } else { 376 newTypesArray[i] = DTM.ELEMENT_NODE; 377 } 378 newNamesArray[i] = 379 (lNameStartIdx == 0) ? name 380 : name.substring(lNameStartIdx); 381 } 382 383 namesArray = newNamesArray; 384 urisArray = newURIsArray; 385 typesArray = newTypesArray; 386 } 387 388 if (transletVersion > CURRENT_TRANSLET_VERSION) { 392 BasisLibrary.runTimeError(BasisLibrary.UNKNOWN_TRANSLET_VERSION_ERR, 393 this.getClass().getName()); 394 } 395 } 396 397 400 401 private Hashtable _keyIndexes = null; 403 private KeyIndex _emptyKeyIndex = null; 404 private int _indexSize = 0; 405 406 410 public void setIndexSize(int size) { 411 if (size > _indexSize) _indexSize = size; 412 } 413 414 417 public KeyIndex createKeyIndex() { 418 return(new KeyIndex(_indexSize)); 419 } 420 421 427 public void buildKeyIndex(String name, int node, Object value) { 428 if (_keyIndexes == null) _keyIndexes = new Hashtable(); 429 430 KeyIndex index = (KeyIndex)_keyIndexes.get(name); 431 if (index == null) { 432 _keyIndexes.put(name, index = new KeyIndex(_indexSize)); 433 } 434 index.add(value, node); 435 } 436 437 442 public void buildKeyIndex(String name, DOM dom) { 443 if (_keyIndexes == null) _keyIndexes = new Hashtable(); 444 445 KeyIndex index = (KeyIndex)_keyIndexes.get(name); 446 if (index == null) { 447 _keyIndexes.put(name, index = new KeyIndex(_indexSize)); 448 } 449 index.setDom(dom); 450 } 451 452 456 public KeyIndex getKeyIndex(String name) { 457 if (_keyIndexes == null) { 459 return (_emptyKeyIndex != null) 460 ? _emptyKeyIndex 461 : (_emptyKeyIndex = new KeyIndex(1)); 462 } 463 464 final KeyIndex index = (KeyIndex)_keyIndexes.get(name); 466 467 if (index == null) { 469 return (_emptyKeyIndex != null) 470 ? _emptyKeyIndex 471 : (_emptyKeyIndex = new KeyIndex(1)); 472 } 473 474 return(index); 475 } 476 477 481 public void buildKeys(DOM document, DTMAxisIterator iterator, 482 SerializationHandler handler, 483 int root) throws TransletException { 484 485 } 486 487 491 public void setKeyIndexDom(String name, DOM document) { 492 getKeyIndex(name).setDom(document); 493 494 } 495 496 499 500 private DOMCache _domCache = null; 502 503 507 public void setDOMCache(DOMCache cache) { 508 _domCache = cache; 509 } 510 511 515 public DOMCache getDOMCache() { 516 return(_domCache); 517 } 518 519 523 524 public SerializationHandler openOutputHandler(String filename, boolean append) 525 throws TransletException 526 { 527 try { 528 final TransletOutputHandlerFactory factory 529 = TransletOutputHandlerFactory.newInstance(); 530 531 factory.setEncoding(_encoding); 532 factory.setOutputMethod(_method); 533 factory.setWriter(new FileWriter (filename, append)); 534 factory.setOutputType(TransletOutputHandlerFactory.STREAM); 535 536 final SerializationHandler handler 537 = factory.getSerializationHandler(); 538 539 transferOutputSettings(handler); 540 handler.startDocument(); 541 return handler; 542 } 543 catch (Exception e) { 544 throw new TransletException(e); 545 } 546 } 547 548 public SerializationHandler openOutputHandler(String filename) 549 throws TransletException 550 { 551 return openOutputHandler(filename, false); 552 } 553 554 public void closeOutputHandler(SerializationHandler handler) { 555 try { 556 handler.endDocument(); 557 handler.close(); 558 } 559 catch (Exception e) { 560 } 562 } 563 564 567 568 571 public abstract void transform(DOM document, DTMAxisIterator iterator, 572 SerializationHandler handler) 573 throws TransletException; 574 575 578 public final void transform(DOM document, SerializationHandler handler) 579 throws TransletException { 580 transform(document, document.getIterator(), handler); 581 } 582 583 587 public final void characters(final String string, 588 SerializationHandler handler) 589 throws TransletException { 590 if (string != null) { 591 try { 593 handler.characters(string); 594 } catch (Exception e) { 595 throw new TransletException(e); 596 } 597 } 598 } 599 600 603 public void addCdataElement(String name) { 604 if (_cdata == null) { 605 _cdata = new Vector (); 606 } 607 608 int lastColon = name.lastIndexOf(':'); 609 610 if (lastColon > 0) { 611 String uri = name.substring(0, lastColon); 612 String localName = name.substring(lastColon+1); 613 _cdata.addElement(uri); 614 _cdata.addElement(localName); 615 } else { 616 _cdata.addElement(null); 617 _cdata.addElement(name); 618 } 619 } 620 621 624 protected void transferOutputSettings(SerializationHandler handler) { 625 if (_method != null) { 626 if (_method.equals("xml")) { 627 if (_standalone != null) { 628 handler.setStandalone(_standalone); 629 } 630 if (_omitHeader) { 631 handler.setOmitXMLDeclaration(true); 632 } 633 handler.setCdataSectionElements(_cdata); 634 if (_version != null) { 635 handler.setVersion(_version); 636 } 637 handler.setIndent(_indent); 638 if (_doctypeSystem != null) { 639 handler.setDoctype(_doctypeSystem, _doctypePublic); 640 } 641 } 642 else if (_method.equals("html")) { 643 handler.setIndent(_indent); 644 handler.setDoctype(_doctypeSystem, _doctypePublic); 645 if (_mediaType != null) { 646 handler.setMediaType(_mediaType); 647 } 648 } 649 } 650 else { 651 handler.setCdataSectionElements(_cdata); 652 if (_version != null) { 653 handler.setVersion(_version); 654 } 655 if (_standalone != null) { 656 handler.setStandalone(_standalone); 657 } 658 if (_omitHeader) { 659 handler.setOmitXMLDeclaration(true); 660 } 661 handler.setIndent(_indent); 662 handler.setDoctype(_doctypeSystem, _doctypePublic); 663 } 664 } 665 666 private Hashtable _auxClasses = null; 667 668 public void addAuxiliaryClass(Class auxClass) { 669 if (_auxClasses == null) _auxClasses = new Hashtable(); 670 _auxClasses.put(auxClass.getName(), auxClass); 671 } 672 673 public void setAuxiliaryClasses(Hashtable auxClasses) { 674 _auxClasses = auxClasses; 675 } 676 677 public Class getAuxiliaryClass(String className) { 678 if (_auxClasses == null) return null; 679 return((Class )_auxClasses.get(className)); 680 } 681 682 public String [] getNamesArray() { 684 return namesArray; 685 } 686 687 public String [] getUrisArray() { 688 return urisArray; 689 } 690 691 public int[] getTypesArray() { 692 return typesArray; 693 } 694 695 public String [] getNamespaceArray() { 696 return namespaceArray; 697 } 698 699 public boolean hasIdCall() { 700 return _hasIdCall; 701 } 702 703 public Templates getTemplates() { 704 return _templates; 705 } 706 707 public void setTemplates(Templates templates) { 708 _templates = templates; 709 } 710 } 711 | Popular Tags |