sax2dtm.java
来自「JAVA 所有包」· Java 代码 · 共 2,129 行 · 第 1/5 页
JAVA
2,129 行
{ int dataIndex = _dataOrQName(identity); if (-1 == offset) { offset = m_data.elementAt(dataIndex); } length += m_data.elementAt(dataIndex + 1); } identity = getNextNodeIdentity(identity); } while (DTM.NULL != identity && (_parent(identity) >= startNode)); if (length > 0) { return m_xstrf.newstr(m_chars, offset, length); } } else if(type != DTM.ELEMENT_NODE) { int dataIndex = _dataOrQName(identity); if (dataIndex < 0) { dataIndex = -dataIndex; dataIndex = m_data.elementAt(dataIndex + 1); } return m_xstrf.newstr(m_valuesOrPrefixes.indexToString(dataIndex)); } } return m_xstrf.emptystr(); } /** * Determine if the string-value of a node is whitespace * * @param nodeHandle The node Handle. * * @return Return true if the given node is whitespace. */ public boolean isWhitespace(int nodeHandle) { int identity = makeNodeIdentity(nodeHandle); int type; if(identity==DTM.NULL) // Separate lines because I wanted to breakpoint it type = DTM.NULL; else type= _type(identity); if (isTextType(type)) { int dataIndex = _dataOrQName(identity); int offset = m_data.elementAt(dataIndex); int length = m_data.elementAt(dataIndex + 1); return m_chars.isWhitespace(offset, length); } return false; } /** * 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. */ public 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 (indexOfNSSep > 0) { if (qname.startsWith("xmlns:")) prefix = qname.substring(indexOfNSSep + 1); else prefix = qname.substring(0, indexOfNSSep); } else { if (qname.equals("xmlns")) prefix = ""; else prefix = null; } } else { prefix = null; } return prefix; } /** * Get a prefix either from the uri mapping, or just make * one up! * * @param uri The namespace URI, which may be null. * * @return The prefix if there is one, or null. */ public int getIdForNamespace(String uri) { return m_valuesOrPrefixes.stringToIndex(uri); } /** * Get a prefix either from the qname or from the uri mapping, or just make * one up! * * @return The prefix if there is one, or null. */ public String getNamespaceURI(String prefix) { String uri = ""; int prefixIndex = m_contextIndexes.peek() - 1 ; if(null == prefix) prefix = ""; do { prefixIndex = m_prefixMappings.indexOf(prefix, ++prefixIndex); } while ( (prefixIndex >= 0) && (prefixIndex & 0x01) == 0x01); if (prefixIndex > -1) { uri = (String) m_prefixMappings.elementAt(prefixIndex + 1); } return uri; } /** * 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 { // Guard against characters/ignorableWhitespace events that // contained no characters. They should not result in a node. if (length > 0) { 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; m_systemId = locator.getSystemId(); } /** * 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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?