📄 icltraversal.java
字号:
// $CALOLSI$
/*
* #=========================================================================
* # Copyright 2004 SRI International. All rights reserved.
* #
* # The material contained in this file is confidential and proprietary to SRI
* # International and may not be reproduced, published, or disclosed to others
* # without authorization from SRI International.
* #
* # DISCLAIMER OF WARRANTIES
* #
* # SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* # SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
* # LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* # PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
* # LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* # OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
* #=========================================================================
* Author : evans
* Date: May 11, 2004
* Time: 6:00:30 PM
*/
package com.sri.oaa2.mapper;
import java.util.LinkedList;
import java.util.List;
import com.sri.oaa2.icl.*;
/**
* Traverses an ICL tree and calls back to an IclVisitor interface.
* This class is meant to be subclassed with the visit() methods overridden.
*/
public class IclTraversal
{
private IclTerm root;
private IclTerm parent;
private int index = -1;
public final void traverse(IclTerm icl)
throws MappingException
{
traverse(icl, null, -1);
}
public final void traverse(IclTerm icl, IclTerm parent, int childIndex)
throws MappingException
{
try
{
root = icl;
this.parent = parent;
index = childIndex;
if(visit(icl)) traverseChildren(icl);
}
finally
{
root = null;
}
}
protected boolean visit(IclTerm icl)
throws MappingException
{
if(icl.isVar())
return visit((IclVar)icl);
else if(icl.isStr())
return visit((IclStr)icl);
else if(icl.isStruct())
return visit((IclStruct)icl);
else if(icl.isInt())
return visit((IclInt)icl);
else if(icl.isFloat())
return visit((IclFloat)icl);
else if(icl.isList())
return visit((IclList)icl);
else if(icl.isGroup())
return visit((IclGroup)icl);
else
return true;
}
protected boolean visit(IclVar icl)
throws MappingException
{
return true;
}
protected boolean visit(IclStr icl)
throws MappingException
{
return true;
}
protected boolean visit(IclStruct icl)
throws MappingException
{
return true;
}
protected boolean visit(IclInt icl)
throws MappingException
{
return true;
}
protected boolean visit(IclFloat icl)
throws MappingException
{
return true;
}
protected boolean visit(IclList icl)
throws MappingException
{
return true;
}
protected boolean visit(IclGroup icl)
throws MappingException
{
return true;
}
/**
* Replaces the currently visited node.
*
* @param icl
*/
protected final void replace(IclTerm icl)
throws MappingException
{
if(parent != null)
parent.replaceElement(index, icl);
else
throw new MappingException("Cannot replace root element of " + root.toString() + " with " + icl);
}
/**
* Remove the currently visited node.
*/
protected final void remove()
throws MappingException
{
if(parent != null)
{
parent.removeElement(index);
index--;
}
else
throw new MappingException("Cannot remove root element of " + root.toString());
}
///////////////////////////////////////////////////////
private void traverseChildren(IclTerm icl)
throws MappingException
{
List traversalQueue = new LinkedList();
traversalQueue.add(icl);
while(traversalQueue.size() > 0)
{
parent = (IclTerm)traversalQueue.remove(0);
for(index = 0; index < parent.getNumChildren(); index++)
{
IclTerm child = parent.getTerm(index);
if(visit(child)) traversalQueue.add(child);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -