sax2dtm.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,075 行 · 第 1/5 页
JAVA
2,075 行
dataIndex = -dataIndex; dataIndex = m_data.elementAt(dataIndex + 1); } return m_xstrf.newstr(m_valuesOrPrefixes.indexToString(dataIndex)); } } return m_xstrf.emptystr(); } /** * Returns the <code>Element</code> whose <code>ID</code> is given by * <code>elementId</code>. If no such element exists, returns * <code>DTM.NULL</code>. Behavior is not defined if more than one element * has this <code>ID</code>. Attributes (including those * with the name "ID") are not of type ID unless so defined by DTD/Schema * information available to the DTM implementation. * Implementations that do not know whether attributes are of type ID or * not are expected to return <code>DTM.NULL</code>. * * <p>%REVIEW% Presumably IDs are still scoped to a single document, * and this operation searches only within a single document, right? * Wouldn't want collisions between DTMs in the same process.</p> * * @param elementId The unique <code>id</code> value for an element. * @return The handle of the matching element. */ public int getElementById(String elementId) { Integer intObj; boolean isMore = true; do { intObj = (Integer) m_idAttributes.get(elementId); if (null != intObj) return makeNodeHandle(intObj.intValue()); if (!isMore || m_endDocumentOccured) break; isMore = nextNode(); } while (null == intObj); return DTM.NULL; } /** * Get a prefix either from the qname or from the uri mapping, or just make * one up! * * @param qname The qualified name, which may be null. * @param uri The namespace URI, which may be null. * * @return The prefix if there is one, or null. */ private String getPrefix(String qname, String uri) { String prefix; int uriIndex = -1; if (null != uri && uri.length() > 0) { do { uriIndex = m_prefixMappings.indexOf(uri, ++uriIndex); } while ( (uriIndex & 0x01) == 0); if (uriIndex >= 0) { prefix = (String) m_prefixMappings.elementAt(uriIndex - 1); } else if (null != qname) { int indexOfNSSep = qname.indexOf(':'); if (qname.equals("xmlns")) prefix = ""; else if (qname.startsWith("xmlns:")) prefix = qname.substring(indexOfNSSep + 1); else prefix = (indexOfNSSep > 0) ? qname.substring(0, indexOfNSSep) : null; } else { prefix = null; // ?? } } else if (null != qname) { int indexOfNSSep = qname.indexOf(':'); if (qname.equals("xmlns")) prefix = ""; else if (qname.startsWith("xmlns:")) prefix = qname.substring(indexOfNSSep + 1); else prefix = (indexOfNSSep > 0) ? qname.substring(0, indexOfNSSep) : null; } else { prefix = null; } return prefix; } /** * Set an ID string to node association in the ID table. * * @param id The ID string. * @param elem The associated element handle. */ public void setIDAttribute(String id, int elem) { m_idAttributes.put(id, new Integer(elem)); } /** * Check whether accumulated text should be stripped; if not, * append the appropriate flavor of text/cdata node. */ protected void charactersFlush() { if (m_textPendingStart >= 0) // -1 indicates no-text-in-progress { int length = m_chars.size() - m_textPendingStart; boolean doStrip = false; if (getShouldStripWhitespace()) { doStrip = m_chars.isWhitespace(m_textPendingStart, length); } if (doStrip) m_chars.setLength(m_textPendingStart); // Discard accumulated text else { int exName = m_expandedNameTable.getExpandedTypeID(DTM.TEXT_NODE); int dataIndex = m_data.size(); m_previous = addNode(m_coalescedTextType, exName, m_parents.peek(), m_previous, dataIndex, false); m_data.addElement(m_textPendingStart); m_data.addElement(length); } // Reset for next text block m_textPendingStart = -1; m_textType = m_coalescedTextType = DTM.TEXT_NODE; } } //////////////////////////////////////////////////////////////////// // Implementation of the EntityResolver interface. //////////////////////////////////////////////////////////////////// /** * Resolve an external entity. * * <p>Always return null, so that the parser will use the system * identifier provided in the XML document. This method implements * the SAX default behaviour: application writers can override it * in a subclass to do special translations such as catalog lookups * or URI redirection.</p> * * @param publicId The public identifer, or null if none is * available. * @param systemId The system identifier provided in the XML * document. * @return The new input source, or null to require the * default behaviour. * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.EntityResolver#resolveEntity * * @throws SAXException */ public InputSource resolveEntity(String publicId, String systemId) throws SAXException { return null; } //////////////////////////////////////////////////////////////////// // Implementation of DTDHandler interface. //////////////////////////////////////////////////////////////////// /** * Receive notification of a notation declaration. * * <p>By default, do nothing. Application writers may override this * method in a subclass if they wish to keep track of the notations * declared in a document.</p> * * @param name The notation name. * @param publicId The notation public identifier, or null if not * available. * @param systemId The notation system identifier. * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.DTDHandler#notationDecl * * @throws SAXException */ public void notationDecl(String name, String publicId, String systemId) throws SAXException { // no op } /** * Receive notification of an unparsed entity declaration. * * <p>By default, do nothing. Application writers may override this * method in a subclass to keep track of the unparsed entities * declared in a document.</p> * * @param name The entity name. * @param publicId The entity public identifier, or null if not * available. * @param systemId The entity system identifier. * @param notationName The name of the associated notation. * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.DTDHandler#unparsedEntityDecl * * @throws SAXException */ public void unparsedEntityDecl( String name, String publicId, String systemId, String notationName) throws SAXException { if (null == m_entities) { m_entities = new Vector(); } try { systemId = SystemIDResolver.getAbsoluteURI(systemId, getDocumentBaseURI()); } catch (Exception e) { throw new org.xml.sax.SAXException(e); } // private static final int ENTITY_FIELD_PUBLICID = 0; m_entities.addElement(publicId); // private static final int ENTITY_FIELD_SYSTEMID = 1; m_entities.addElement(systemId); // private static final int ENTITY_FIELD_NOTATIONNAME = 2; m_entities.addElement(notationName); // private static final int ENTITY_FIELD_NAME = 3; m_entities.addElement(name); } //////////////////////////////////////////////////////////////////// // Implementation of ContentHandler interface. //////////////////////////////////////////////////////////////////// /** * Receive a Locator object for document events. * * <p>By default, do nothing. Application writers may override this * method in a subclass if they wish to store the locator for use * with other document events.</p> * * @param locator A locator for all SAX document events. * @see org.xml.sax.ContentHandler#setDocumentLocator * @see org.xml.sax.Locator */ public void setDocumentLocator(Locator locator) { m_locator = locator; } /** * Receive notification of the beginning of the document. * * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#startDocument */ public void startDocument() throws SAXException { if (DEBUG) System.out.println("startDocument"); int doc = addNode(DTM.DOCUMENT_NODE, m_expandedNameTable.getExpandedTypeID(DTM.DOCUMENT_NODE), DTM.NULL, DTM.NULL, 0, true); m_parents.push(doc); m_previous = DTM.NULL; m_contextIndexes.push(m_prefixMappings.size()); // for the next element. } /** * Receive notification of the end of the document. * * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#endDocument */ public void endDocument() throws SAXException { if (DEBUG) System.out.println("endDocument"); charactersFlush(); m_nextsib.setElementAt(NULL,0); if (m_firstch.elementAt(0) == NOTPROCESSED) m_firstch.setElementAt(NULL,0); if (DTM.NULL != m_previous) m_nextsib.setElementAt(DTM.NULL,m_previous); m_parents = null; m_prefixMappings = null; m_contextIndexes = null; m_endDocumentOccured = true; } /** * Receive notification of the start of a Namespace mapping. * * <p>By default, do nothing. Application writers may override this * method in a subclass to take specific actions at the start of * each Namespace prefix scope (such as storing the prefix mapping).</p> * * @param prefix The Namespace prefix being declared. * @param uri The Namespace URI mapped to the prefix. * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#startPrefixMapping */ public void startPrefixMapping(String prefix, String uri) throws SAXException { if (DEBUG) System.out.println("startPrefixMapping: prefix: " + prefix + ", uri: " + uri); if(null == prefix) prefix = ""; m_prefixMappings.addElement(prefix); // JDK 1.1.x compat -sc m_prefixMappings.addElement(uri); // JDK 1.1.x compat -sc } /** * Receive notification of the end of a Namespace mapping. * * <p>By default, do nothing. Application writers may override this * method in a subclass to take specific actions at the end of * each prefix mapping.</p> * * @param prefix The Namespace prefix being declared. * @throws SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#endPrefixMapping */ public void endPrefixMapping(String prefix) throws SAXException { if (DEBUG) System.out.println("endPrefixMapping: prefix: " + prefix); if(null == prefix) prefix = ""; int index = m_contextIndexes.peek() - 1; do { index = m_prefixMappings.indexOf(prefix, ++index); } while ( (index >= 0) && ((index & 0x01) == 0x01) ); if (index > -1) { m_prefixMappings.setElementAt("%@$#^@#", index); m_prefixMappings.setElementAt("%@$#^@#", index + 1); } // no op } /** * Check if a declaration has already been made for a given prefix. * * @param prefix non-null prefix string. * * @return true if the declaration has already been declared in the * current context. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?