dtmmanagerdefault.java

来自「JAVA 所有包」· Java 代码 · 共 858 行 · 第 1/2 页

JAVA
858
字号
              reader.setProperty(                               "http://xml.org/sax/properties/lexical-handler",                               dtm);            } catch (SAXNotRecognizedException e){}              catch (SAXNotSupportedException e){}            try {              reader.parse(xmlSource);            } catch (RuntimeException re) {              dtm.clearCoRoutine();              throw re;            } catch (Exception e) {              dtm.clearCoRoutine();              throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);            }          }          if (DUMPTREE) {            System.out.println("Dumping SAX2DOM");            dtm.dumpDTM(System.err);          }          return dtm;        } finally {          // Reset the ContentHandler, DTDHandler, ErrorHandler to the DefaultHandler          // after creating the DTM.          if (reader != null && !(m_incremental && incremental)) {            reader.setContentHandler(m_defaultHandler);            reader.setDTDHandler(m_defaultHandler);            reader.setErrorHandler(m_defaultHandler);                        // Reset the LexicalHandler to null after creating the DTM.            try {              reader.setProperty("http://xml.org/sax/properties/lexical-handler", null);            }            catch (Exception e) {}          }          releaseXMLReader(reader);        }      } else {        // It should have been handled by a derived class or the caller        // made a mistake.        throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object[]{source})); //"Not supported: " + source);      }    }  }  /**   * Given a W3C DOM node, try and return a DTM handle.   * Note: calling this may be non-optimal, and there is no guarantee that   * the node will be found in any particular DTM.   *   * @param node Non-null reference to a DOM node.   *   * @return a valid DTM handle.   */  synchronized public int getDTMHandleFromNode(org.w3c.dom.Node node)  {    if(null == node)      throw new IllegalArgumentException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NODE_NON_NULL, null)); //"node must be non-null for getDTMHandleFromNode!");    if (node instanceof com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy)      return ((com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy) node).getDTMNodeNumber();		    else    {      // Find the DOM2DTMs wrapped around this Document (if any)      // and check whether they contain the Node in question.      //      // NOTE that since a DOM2DTM may represent a subtree rather      // than a full document, we have to be prepared to check more      // than one -- and there is no guarantee that we will find      // one that contains ancestors or siblings of the node we're      // seeking.      //      // %REVIEW% We could search for the one which contains this      // node at the deepest level, and thus covers the widest      // subtree, but that's going to entail additional work      // checking more DTMs... and getHandleOfNode is not a      // cheap operation in most implementations.			//			// TODO: %REVIEW% If overflow addressing, we may recheck a DTM			// already examined. Ouch. But with the increased number of DTMs,			// scanning back to check this is painful. 			// POSSIBLE SOLUTIONS: 			//   Generate a list of _unique_ DTM objects?			//   Have each DTM cache last DOM node search?			int max = m_dtms.length;      for(int i = 0; i < max; i++)        {          DTM thisDTM=m_dtms[i];          if((null != thisDTM) && thisDTM instanceof DOM2DTM)          {            int handle=((DOM2DTM)thisDTM).getHandleOfNode(node);            if(handle!=DTM.NULL) return handle;          }         }			// Not found; generate a new DTM.			//			// %REVIEW% Is this really desirable, or should we return null			// and make folks explicitly instantiate from a DOMSource? The			// latter is more work but gives the caller the opportunity to			// explicitly add the DTM to a DTMManager... and thus to know when			// it can be discarded again, which is something we need to pay much			// more attention to. (Especially since only DTMs which are assigned			// to a manager can use the overflow addressing scheme.)			//			// %BUG% If the source node was a DOM2DTM$defaultNamespaceDeclarationNode			// and the DTM wasn't registered with this DTMManager, we will create			// a new DTM and _still_ not be able to find the node (since it will			// be resynthesized). Another reason to push hard on making all DTMs			// be managed DTMs.			// Since the real root of our tree may be a DocumentFragment, we need to      // use getParent to find the root, instead of getOwnerDocument.  Otherwise      // DOM2DTM#getHandleOfNode will be very unhappy.      Node root = node;      Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr)root).getOwnerElement() : root.getParentNode();      for (; p != null; p = p.getParentNode())      {        root = p;      }      DOM2DTM dtm = (DOM2DTM) getDTM(new javax.xml.transform.dom.DOMSource(root),																		 false, null, true, true);      int handle;            if(node instanceof com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTMdefaultNamespaceDeclarationNode)      {				// Can't return the same node since it's unique to a specific DTM, 				// but can return the equivalent node -- find the corresponding 				// Document Element, then ask it for the xml: namespace decl.				handle=dtm.getHandleOfNode(((org.w3c.dom.Attr)node).getOwnerElement());				handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName());      }      else				handle = ((DOM2DTM)dtm).getHandleOfNode(node);      if(DTM.NULL == handle)        throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_RESOLVE_NODE, null)); //"Could not resolve the node to a handle!");      return handle;    }  }  /**   * This method returns the SAX2 parser to use with the InputSource   * obtained from this URI.   * It may return null if any SAX2-conformant XML parser can be used,   * or if getInputSource() will also return null. The parser must   * be free for use (i.e., not currently in use for another parse().   * After use of the parser is completed, the releaseXMLReader(XMLReader)   * must be called.   *   * @param inputSource The value returned from the URIResolver.   * @return  a SAX2 XMLReader to use to resolve the inputSource argument.   *   * @return non-null XMLReader reference ready to parse.   */  synchronized public XMLReader getXMLReader(Source inputSource)  {    try    {      XMLReader reader = (inputSource instanceof SAXSource)                         ? ((SAXSource) inputSource).getXMLReader() : null;      // If user did not supply a reader, ask for one from the reader manager      if (null == reader) {        if (m_readerManager == null) {            m_readerManager = XMLReaderManager.getInstance();        }        reader = m_readerManager.getXMLReader();      }      return reader;    } catch (SAXException se) {      throw new DTMException(se.getMessage(), se);    }  }  /**   * Indicates that the XMLReader object is no longer in use for the transform.   *   * Note that the getXMLReader method may return an XMLReader that was   * specified on the SAXSource object by the application code.  Such a   * reader should still be passed to releaseXMLReader, but the reader manager   * will only re-use XMLReaders that it created.   *   * @param reader The XMLReader to be released.   */  synchronized public void releaseXMLReader(XMLReader reader) {    if (m_readerManager != null) {      m_readerManager.releaseXMLReader(reader);    }  }  /**   * Return the DTM object containing a representation of this node.   *   * @param nodeHandle DTM Handle indicating which node to retrieve   *   * @return a reference to the DTM object containing this node.   */  synchronized public DTM getDTM(int nodeHandle)  {    try    {      // Performance critical function.      return m_dtms[nodeHandle >>> IDENT_DTM_NODE_BITS];    }    catch(java.lang.ArrayIndexOutOfBoundsException e)    {      if(nodeHandle==DTM.NULL)				return null;		// Accept as a special case.      else				throw e;		// Programming error; want to know about it.    }      }  /**   * Given a DTM, find the ID number in the DTM tables which addresses   * the start of the document. If overflow addressing is in use, other   * DTM IDs may also be assigned to this DTM.   *   * @param dtm The DTM which (hopefully) contains this node.   *   * @return The DTM ID (as the high bits of a NodeHandle, not as our   * internal index), or -1 if the DTM doesn't belong to this manager.   */  synchronized public int getDTMIdentity(DTM dtm)  {	// Shortcut using DTMDefaultBase's extension hooks	// %REVIEW% Should the lookup be part of the basic DTM API?	if(dtm instanceof DTMDefaultBase)	{		DTMDefaultBase dtmdb=(DTMDefaultBase)dtm;		if(dtmdb.getManager()==this)			return dtmdb.getDTMIDs().elementAt(0);		else			return -1;	}				    int n = m_dtms.length;    for (int i = 0; i < n; i++)    {      DTM tdtm = m_dtms[i];      if (tdtm == dtm && m_dtm_offsets[i]==0)        return i << IDENT_DTM_NODE_BITS;    }    return -1;  }  /**   * Release the DTMManager's reference(s) to a DTM, making it unmanaged.   * This is typically done as part of returning the DTM to the heap after   * we're done with it.   *   * @param dtm the DTM to be released.   *    * @param shouldHardDelete If false, this call is a suggestion rather than an   * order, and we may not actually release the DTM. This is intended to    * support intelligent caching of documents... which is not implemented   * in this version of the DTM manager.   *   * @return true if the DTM was released, false if shouldHardDelete was set   * and we decided not to.   */  synchronized public boolean release(DTM dtm, boolean shouldHardDelete)  {    if(DEBUG)    {      System.out.println("Releasing "+			 (shouldHardDelete ? "HARD" : "soft")+			 " dtm="+			 // Following shouldn't need a nodeHandle, but does...			 // and doesn't seem to report the intended value			 dtm.getDocumentBaseURI()			 );    }    if (dtm instanceof SAX2DTM)    {      ((SAX2DTM) dtm).clearCoRoutine();    }		// Multiple DTM IDs may be assigned to a single DTM. 		// The Right Answer is to ask which (if it supports		// extension, the DTM will need a list anyway). The 		// Wrong Answer, applied if the DTM can't help us,		// is to linearly search them all; this may be very		// painful.		//		// %REVIEW% Should the lookup move up into the basic DTM API?		if(dtm instanceof DTMDefaultBase)		{			com.sun.org.apache.xml.internal.utils.SuballocatedIntVector ids=((DTMDefaultBase)dtm).getDTMIDs();			for(int i=ids.size()-1;i>=0;--i)				m_dtms[ids.elementAt(i)>>>DTMManager.IDENT_DTM_NODE_BITS]=null;		}		else		{			int i = getDTMIdentity(dtm);		    if (i >= 0)			{				m_dtms[i >>> DTMManager.IDENT_DTM_NODE_BITS] = null;			}		}    dtm.documentRelease();    return true;  }  /**   * Method createDocumentFragment   *   *   * NEEDSDOC (createDocumentFragment) @return   */  synchronized public DTM createDocumentFragment()  {    try    {      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();      dbf.setNamespaceAware(true);      DocumentBuilder db = dbf.newDocumentBuilder();      Document doc = db.newDocument();      Node df = doc.createDocumentFragment();      return getDTM(new DOMSource(df), true, null, false, false);    }    catch (Exception e)    {      throw new DTMException(e);    }  }  /**   * NEEDSDOC Method createDTMIterator   *   *   * NEEDSDOC @param whatToShow   * NEEDSDOC @param filter   * NEEDSDOC @param entityReferenceExpansion   *   * NEEDSDOC (createDTMIterator) @return   */  synchronized public DTMIterator createDTMIterator(int whatToShow, DTMFilter filter,                                       boolean entityReferenceExpansion)  {    /** @todo: implement this com.sun.org.apache.xml.internal.dtm.DTMManager abstract method */    return null;  }  /**   * NEEDSDOC Method createDTMIterator   *   *   * NEEDSDOC @param xpathString   * NEEDSDOC @param presolver   *   * NEEDSDOC (createDTMIterator) @return   */  synchronized public DTMIterator createDTMIterator(String xpathString,                                       PrefixResolver presolver)  {    /** @todo: implement this com.sun.org.apache.xml.internal.dtm.DTMManager abstract method */    return null;  }  /**   * NEEDSDOC Method createDTMIterator   *   *   * NEEDSDOC @param node   *   * NEEDSDOC (createDTMIterator) @return   */  synchronized public DTMIterator createDTMIterator(int node)  {    /** @todo: implement this com.sun.org.apache.xml.internal.dtm.DTMManager abstract method */    return null;  }  /**   * NEEDSDOC Method createDTMIterator   *   *   * NEEDSDOC @param xpathCompiler   * NEEDSDOC @param pos   *   * NEEDSDOC (createDTMIterator) @return   */  synchronized public DTMIterator createDTMIterator(Object xpathCompiler, int pos)  {    /** @todo: implement this com.sun.org.apache.xml.internal.dtm.DTMManager abstract method */    return null;  }  /**   * return the expanded name table.   *   * NEEDSDOC @param dtm   *   * NEEDSDOC ($objectName$) @return   */  public ExpandedNameTable getExpandedNameTable(DTM dtm)  {    return m_expandedNameTable;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?