redundentexpreliminator.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,474 行 · 第 1/4 页

JAVA
1,474
字号
  			for(int j = 0; j < numStepCorrection; j++)  			{  				elems[i] = elems[i].getParentElem();  			}  		}  	}  	  	// Now everyone has an equal number of ancestors.  Walk up from here   	// equally until all are equal.  	ElemTemplateElement first = null;  	while(shortestAncestorCount-- >= 0)  	{  		boolean areEqual = true;  		first = elems[0];  		for(int i = 1; i < numExprs; i++)  		{  			if(first != elems[i])  			{  				areEqual = false;  				break;  			}  		}  		// This second check is to make sure we have a common ancestor that is not the same   		// as the expression owner... i.e. the var decl has to go above the expression owner.  		if(areEqual && isNotSameAsOwner(head, first) && first.canAcceptVariables())  		{  			if(DIAGNOSE_MULTISTEPLIST)  			{  				System.err.print(first.getClass().getName());  				System.err.println(" at   " + first.getSystemId() + " Line " + first.getLineNumber());  			}  			return first;  		}   			  		for(int i = 0; i < numExprs; i++)  		{  			elems[i] = elems[i].getParentElem();  		}  	}  	  	assertion(false, "Could not find common ancestor!!!");  	return null;  }    /**   * Find out if the given ElemTemplateElement is not the same as one of    * the ElemTemplateElement owners of the expressions.   *    * @param head Head of linked list of expression owners.   * @param ete The ElemTemplateElement that is a candidate for a psuedo    * variable parent.   * @return true if the given ElemTemplateElement is not the same as one of    * the ElemTemplateElement owners of the expressions.  This is to make sure    * we find an ElemTemplateElement that is in a viable position to hold    * psuedo variables that are visible to the references.   */  protected boolean isNotSameAsOwner(MultistepExprHolder head, ElemTemplateElement ete)  {  	MultistepExprHolder next = head;  	while(null != next)  	{  		ElemTemplateElement elemOwner = getElemFromExpression(next.m_exprOwner.getExpression());  		if(elemOwner == ete)  			return false;  		next = next.m_next;  	}  	return true;  }    /**   * Count the number of ancestors that a ElemTemplateElement has.   *    * @param elem An representation of an element in an XSLT stylesheet.   * @return The number of ancestors of elem (including the element itself).   */  protected int countAncestors(ElemTemplateElement elem)  {  	int count = 0;  	while(null != elem)  	{  		count++;  		elem = elem.getParentElem();  	}  	return count;  }  /**   * Print out diagnostics about partial multistep evaluation.   */  protected void diagnoseMultistepList(      int matchCount,      int lengthToTest,      boolean isGlobal)  {      if (matchCount > 0)        {        System.err.print(          "Found multistep matches: " + matchCount + ", " + lengthToTest + " length");        if (isGlobal)              System.err.println(" (global)");        else              System.err.println();      }  }    /**   * Change a given number of steps to a single variable reference.   *    * @param uniquePsuedoVarName The name of the variable reference.   * @param wi The walking iterator that is to be changed.   * @param numSteps The number of steps to be changed.   * @param isGlobal true if this will be a global reference.   */  protected LocPathIterator changePartToRef(final QName uniquePsuedoVarName, WalkingIterator wi,                                  final int numSteps, final boolean isGlobal)  {  	Variable var = new Variable();  	var.setQName(uniquePsuedoVarName);  	var.setIsGlobal(isGlobal);  	if(isGlobal)  	{	ElemTemplateElement elem = getElemFromExpression(wi);  		StylesheetRoot root = elem.getStylesheetRoot();  		Vector vars = root.getVariablesAndParamsComposed();  		var.setIndex(vars.size()-1);  	}  	  	// Walk to the first walker after the one's we are replacing.  	AxesWalker walker = wi.getFirstWalker();  	for(int i = 0; i < numSteps; i++)  	{  		assertion(null != walker, "Walker should not be null!");  		walker = walker.getNextWalker();  	}  	  	if(null != walker)  	{  	  	  FilterExprWalker few = new FilterExprWalker(wi);  	  few.setInnerExpression(var);  	  few.exprSetParent(wi);  	  few.setNextWalker(walker);  	  walker.setPrevWalker(few);  	  wi.setFirstWalker(few);  	  return wi;  	}  	else  	{  	  FilterExprIteratorSimple feis = new FilterExprIteratorSimple(var);  	  feis.exprSetParent(wi.exprGetParent());  	  return feis;  	}  }    /**   * Create a new WalkingIterator from the steps in another WalkingIterator.   *    * @param wi The iterator from where the steps will be taken.   * @param numSteps The number of steps from the first to copy into the new    *                 iterator.   * @return The new iterator.   */  protected WalkingIterator createIteratorFromSteps(final WalkingIterator wi, int numSteps)  {  	WalkingIterator newIter = new WalkingIterator(wi.getPrefixResolver());  	try  	{  		AxesWalker walker = (AxesWalker)wi.getFirstWalker().clone();  		newIter.setFirstWalker(walker);  		walker.setLocPathIterator(newIter);  		for(int i = 1; i < numSteps; i++)  		{  			AxesWalker next = (AxesWalker)walker.getNextWalker().clone();  			walker.setNextWalker(next);  			next.setLocPathIterator(newIter);  			walker = next;  		}  		walker.setNextWalker(null);  	}  	catch(CloneNotSupportedException cnse)  	{  		throw new WrappedRuntimeException(cnse);  	}  	return newIter;  }      /**   * Compare a given number of steps between two iterators, to see if they are equal.   *    * @param iter1 The first iterator to compare.   * @param iter2 The second iterator to compare.   * @param numSteps The number of steps to compare.   * @return true If the given number of steps are equal.   *    */  protected boolean stepsEqual(WalkingIterator iter1, WalkingIterator iter2,                                          int numSteps)  {  	AxesWalker aw1 = iter1.getFirstWalker();  	AxesWalker aw2 = iter2.getFirstWalker();  	  	for(int i = 0; (i < numSteps); i++)  	{  		if((null == aw1) || (null == aw2))  		 	return false;  		 	  		if(!aw1.deepEquals(aw2))  			return false;  		  		aw1 = aw1.getNextWalker();  		aw2 = aw2.getNextWalker();  	}  	  	assertion((null != aw1) || (null != aw2), "Total match is incorrect!");  	  	return true;  }    /**   * For the reduction of location path parts, create a list of all    * the multistep paths with more than one step, sorted by the    * number of steps, with the most steps occuring earlier in the list.   * If the list is only one member, don't bother returning it.   *    * @param paths Vector of ExpressionOwner objects, which may contain null entries.    *              The ExpressionOwner objects must own LocPathIterator objects.   * @return null if no multipart paths are found or the list is only of length 1,    * otherwise the first MultistepExprHolder in a linked list of these objects.   */  protected MultistepExprHolder createMultistepExprList(Vector paths)  {  	MultistepExprHolder first = null;  	int n = paths.size();  	for(int i = 0; i < n; i++)  	{  		ExpressionOwner eo = (ExpressionOwner)paths.elementAt(i);  		if(null == eo)  			continue;  			  		// Assuming location path iterators should be OK.  		LocPathIterator lpi = (LocPathIterator)eo.getExpression();  		int numPaths = countSteps(lpi);  		if(numPaths > 1)  		{  			if(null == first)  				first = new MultistepExprHolder(eo, numPaths, null);  			else  				first = first.addInSortedOrder(eo, numPaths);  		}  	}  	  	if((null == first) || (first.getLength() <= 1))  		return null;  	else  		return first;  }    /**   * Look through the vector from start point, looking for redundant occurances.   * When one or more are found, create a psuedo variable declaration, insert    * it into the stylesheet, and replace the occurance with a reference to    * the psuedo variable.  When a redundent variable is found, it's slot in    * the vector will be replaced by null.   *    * @param start The position to start looking in the vector.   * @param targetIndex The position of firstOccuranceOwner.   * @param firstOccuranceOwner The owner of the expression we are looking for.   * @param psuedoVarRecipient Where to put the psuedo variables.   *    * @return The number of expression occurances that were modified.   */  protected int findAndEliminateRedundant(int start, int firstOccuranceIndex,                         ExpressionOwner firstOccuranceOwner,                          ElemTemplateElement psuedoVarRecipient,                         Vector paths)                  throws org.w3c.dom.DOMException   {	MultistepExprHolder head = null;	MultistepExprHolder tail = null;	int numPathsFound = 0;	int n = paths.size();		Expression expr1 = firstOccuranceOwner.getExpression();	if(DEBUG)		assertIsLocPathIterator(expr1, firstOccuranceOwner);	boolean isGlobal = (paths == m_absPaths);	LocPathIterator lpi = (LocPathIterator)expr1;	int stepCount = countSteps(lpi);	for(int j = start; j < n; j++)	{		ExpressionOwner owner2 = (ExpressionOwner)paths.elementAt(j);		if(null != owner2)		{			Expression expr2 = owner2.getExpression();			boolean isEqual = expr2.deepEquals(lpi);			if(isEqual)			{  						LocPathIterator lpi2  = (LocPathIterator)expr2;								if(null == head)				{					head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);					tail = head;					numPathsFound++;				}				tail.m_next = new MultistepExprHolder(owner2, stepCount, null);				tail = tail.m_next;					// Null out the occurance, so we don't have to test it again.				paths.setElementAt(null, j);								// foundFirst = true;				numPathsFound++;			}		}	}		// Change all globals in xsl:templates, etc, to global vars no matter what.	if((0 == numPathsFound) && isGlobal)	{      head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);      numPathsFound++;	}		if(null != head)	{		ElemTemplateElement root = isGlobal ? psuedoVarRecipient : findCommonAncestor(head);		LocPathIterator sharedIter = (LocPathIterator)head.m_exprOwner.getExpression();		ElemVariable var = createPsuedoVarDecl(root, sharedIter, isGlobal);		if(DIAGNOSE_MULTISTEPLIST)			System.err.println("Created var: "+var.getName()+(isGlobal ? "(Global)" : ""));		QName uniquePsuedoVarName = var.getName();		while(null != head)		{			ExpressionOwner owner = head.m_exprOwner;				if(DIAGNOSE_MULTISTEPLIST)				diagnoseLineNumber(owner.getExpression());			changeToVarRef(uniquePsuedoVarName, owner, paths, root);			head = head.m_next;		}		// Replace the first occurance with the variable's XPath, so  		// that further reduction may take place if needed.		paths.setElementAt(var.getSelect(), firstOccuranceIndex);	}		return numPathsFound;  }     /**   * To be removed.   */  protected int oldFindAndEliminateRedundant(int start, int firstOccuranceIndex,                         ExpressionOwner firstOccuranceOwner,                          ElemTemplateElement psuedoVarRecipient,                         Vector paths)                  throws org.w3c.dom.DOMException   {	QName uniquePsuedoVarName = null;	boolean foundFirst = false;	int numPathsFound = 0;	int n = paths.size();	Expression expr1 = firstOccuranceOwner.getExpression();	if(DEBUG)		assertIsLocPathIterator(expr1, firstOccuranceOwner);	boolean isGlobal = (paths == m_absPaths);	LocPathIterator lpi = (LocPathIterator)expr1;	for(int j = start; j < n; j++)	{		ExpressionOwner owner2 = (ExpressionOwner)paths.elementAt(j);		if(null != owner2)		{

⌨️ 快捷键说明

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