📄 abstracttranslet.java
字号:
// Distinguish attribute and element names. Attribute has // @ before local part of name. if (name.charAt(lNameStartIdx) == '@') { lNameStartIdx++; newTypesArray[i] = DTM.ATTRIBUTE_NODE; } else if (name.charAt(lNameStartIdx) == '?') { lNameStartIdx++; newTypesArray[i] = DTM.NAMESPACE_NODE; } else { newTypesArray[i] = DTM.ELEMENT_NODE; } newNamesArray[i] = (lNameStartIdx == 0) ? name : name.substring(lNameStartIdx); } namesArray = newNamesArray; urisArray = newURIsArray; typesArray = newTypesArray; } // Was translet compiled using a more recent version of the XSLTC // compiler than is known by the AbstractTranslet class? If, so // and we've made it this far (which is doubtful), we should give up. if (transletVersion > CURRENT_TRANSLET_VERSION) { BasisLibrary.runTimeError(BasisLibrary.UNKNOWN_TRANSLET_VERSION_ERR, this.getClass().getName()); } } /************************************************************************ * Index(es) for <xsl:key> / key() / id() ************************************************************************/ // Container for all indexes for xsl:key elements private Hashtable _keyIndexes = null; private KeyIndex _emptyKeyIndex = null; private int _indexSize = 0; private int _currentRootForKeys = 0; /** * This method is used to pass the largest DOM size to the translet. * Needed to make sure that the translet can index the whole DOM. */ public void setIndexSize(int size) { if (size > _indexSize) _indexSize = size; } /** * Creates a KeyIndex object of the desired size - don't want to resize!!! */ public KeyIndex createKeyIndex() { return(new KeyIndex(_indexSize)); } /** * Adds a value to a key/id index * @param name is the name of the index (the key or ##id) * @param node is the node handle of the node to insert * @param value is the value that will look up the node in the given index */ public void buildKeyIndex(String name, int node, Object value) { if (_keyIndexes == null) _keyIndexes = new Hashtable(); KeyIndex index = (KeyIndex)_keyIndexes.get(name); if (index == null) { _keyIndexes.put(name, index = new KeyIndex(_indexSize)); } index.add(value, node, _currentRootForKeys); } /** * Create an empty KeyIndex in the DOM case * @param name is the name of the index (the key or ##id) * @param dom is the DOM */ public void buildKeyIndex(String name, DOM dom) { if (_keyIndexes == null) _keyIndexes = new Hashtable(); KeyIndex index = (KeyIndex)_keyIndexes.get(name); if (index == null) { _keyIndexes.put(name, index = new KeyIndex(_indexSize)); } index.setDom(dom, dom.getDocument()); } /** * Returns the index for a given key (or id). * The index implements our internal iterator interface */ public KeyIndex getKeyIndex(String name) { // Return an empty key index iterator if none are defined if (_keyIndexes == null) { return (_emptyKeyIndex != null) ? _emptyKeyIndex : (_emptyKeyIndex = new KeyIndex(1)); } // Look up the requested key index final KeyIndex index = (KeyIndex)_keyIndexes.get(name); // Return an empty key index iterator if the requested index not found if (index == null) { return (_emptyKeyIndex != null) ? _emptyKeyIndex : (_emptyKeyIndex = new KeyIndex(1)); } return(index); } private void setRootForKeys(int root) { _currentRootForKeys = root; } /** * This method builds key indexes - it is overridden in the compiled * translet in cases where the <xsl:key> element is used */ public void buildKeys(DOM document, DTMAxisIterator iterator, SerializationHandler handler, int root) throws TransletException { } /** * This method builds key indexes - it is overridden in the compiled * translet in cases where the <xsl:key> element is used */ public void setKeyIndexDom(String name, DOM document) { getKeyIndex(name).setDom(document, document.getDocument()); } /************************************************************************ * DOM cache handling ************************************************************************/ // Hold the DOM cache (if any) used with this translet private DOMCache _domCache = null; /** * Sets the DOM cache used for additional documents loaded using the * document() function. */ public void setDOMCache(DOMCache cache) { _domCache = cache; } /** * Returns the DOM cache used for this translet. Used by the LoadDocument * class (if present) when the document() function is used. */ public DOMCache getDOMCache() { return(_domCache); } /************************************************************************ * Multiple output document extension. * See compiler/TransletOutput for actual implementation. ************************************************************************/ public SerializationHandler openOutputHandler(String filename, boolean append) throws TransletException { try { final TransletOutputHandlerFactory factory = TransletOutputHandlerFactory.newInstance(); String dirStr = new File(filename).getParent(); if ((null != dirStr) && (dirStr.length() > 0)) { File dir = new File(dirStr); dir.mkdirs(); } factory.setEncoding(_encoding); factory.setOutputMethod(_method); factory.setWriter(new FileWriter(filename, append)); factory.setOutputType(TransletOutputHandlerFactory.STREAM); final SerializationHandler handler = factory.getSerializationHandler(); transferOutputSettings(handler); handler.startDocument(); return handler; } catch (Exception e) { throw new TransletException(e); } } public SerializationHandler openOutputHandler(String filename) throws TransletException { return openOutputHandler(filename, false); } public void closeOutputHandler(SerializationHandler handler) { try { handler.endDocument(); handler.close(); } catch (Exception e) { // what can you do? } } /************************************************************************ * Native API transformation methods - _NOT_ JAXP/TrAX ************************************************************************/ /** * Main transform() method - this is overridden by the compiled translet */ public abstract void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler) throws TransletException; /** * Calls transform() with a given output handler */ public final void transform(DOM document, SerializationHandler handler) throws TransletException { try { transform(document, document.getIterator(), handler); } finally { _keyIndexes = null; } } /** * Used by some compiled code as a shortcut for passing strings to the * output handler */ public final void characters(final String string, SerializationHandler handler) throws TransletException { if (string != null) { //final int length = string.length(); try { handler.characters(string); } catch (Exception e) { throw new TransletException(e); } } } /** * Add's a name of an element whose text contents should be output as CDATA */ public void addCdataElement(String name) { if (_cdata == null) { _cdata = new Vector(); } int lastColon = name.lastIndexOf(':'); if (lastColon > 0) { String uri = name.substring(0, lastColon); String localName = name.substring(lastColon+1); _cdata.addElement(uri); _cdata.addElement(localName); } else { _cdata.addElement(null); _cdata.addElement(name); } } /** * Transfer the output settings to the output post-processor */ protected void transferOutputSettings(SerializationHandler handler) { if (_method != null) { if (_method.equals("xml")) { if (_standalone != null) { handler.setStandalone(_standalone); } if (_omitHeader) { handler.setOmitXMLDeclaration(true); } handler.setCdataSectionElements(_cdata); if (_version != null) { handler.setVersion(_version); } handler.setIndent(_indent); handler.setIndentAmount(_indentamount); if (_doctypeSystem != null) { handler.setDoctype(_doctypeSystem, _doctypePublic); } } else if (_method.equals("html")) { handler.setIndent(_indent); handler.setDoctype(_doctypeSystem, _doctypePublic); if (_mediaType != null) { handler.setMediaType(_mediaType); } } } else { handler.setCdataSectionElements(_cdata); if (_version != null) { handler.setVersion(_version); } if (_standalone != null) { handler.setStandalone(_standalone); } if (_omitHeader) { handler.setOmitXMLDeclaration(true); } handler.setIndent(_indent); handler.setDoctype(_doctypeSystem, _doctypePublic); } } private Hashtable _auxClasses = null; public void addAuxiliaryClass(Class auxClass) { if (_auxClasses == null) _auxClasses = new Hashtable(); _auxClasses.put(auxClass.getName(), auxClass); } public void setAuxiliaryClasses(Hashtable auxClasses) { _auxClasses = auxClasses; } public Class getAuxiliaryClass(String className) { if (_auxClasses == null) return null; return((Class)_auxClasses.get(className)); } // GTM added (see pg 110) public String[] getNamesArray() { return namesArray; } public String[] getUrisArray() { return urisArray; } public int[] getTypesArray() { return typesArray; } public String[] getNamespaceArray() { return namespaceArray; } public boolean hasIdCall() { return _hasIdCall; } public Templates getTemplates() { return _templates; } public void setTemplates(Templates templates) { _templates = templates; } /************************************************************************ * DOMImplementation caching for basis library ************************************************************************/ protected DOMImplementation _domImplementation = null; public Document newDocument(String uri, String qname) throws ParserConfigurationException { if (_domImplementation == null) { _domImplementation = DocumentBuilderFactory.newInstance() .newDocumentBuilder().getDOMImplementation(); } return _domImplementation.createDocument(uri, qname, null); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -