Java Code Examples for org.mozilla.javascript.Node#getType()

The following examples show how to use org.mozilla.javascript.Node#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CubeQueryUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param n
 * @param objectName
 * @return
 */
private static String getScriptObjectName( Node n, String objectName )
{
	if ( n == null )
		return null;
	String result = null;
	if ( n.getType( ) == Token.NAME )
	{
		if ( objectName.equals( n.getString( ) ) )
		{
			Node dimNameNode = n.getNext( );
			if ( dimNameNode == null
					|| dimNameNode.getType( ) != Token.STRING )
				return null;

			return dimNameNode.getString( );
		}
	}

	result = getScriptObjectName( n.getFirstChild( ), objectName );
	if ( result == null )
		result = getScriptObjectName( n.getLastChild( ), objectName );

	return result;
}
 
Example 2
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param n
 * @param objectName
 * @return
 */
private static void getScriptObjectName( Node n, String objectName, Set nameSet )
{
	if ( n == null )
		return;
	String result = null;
	if ( n.getType( ) == Token.NAME )
	{
		if ( objectName.equals( n.getString( ) ) )
		{
			Node dimNameNode = n.getNext( );
			if ( dimNameNode == null
					|| dimNameNode.getType( ) != Token.STRING )
				return;

			nameSet.add( dimNameNode.getString( ) );
		}
	}

	getScriptObjectName( n.getFirstChild( ), objectName, nameSet );
	getScriptObjectName( n.getNext( ), objectName, nameSet );
	getScriptObjectName( n.getLastChild( ), objectName, nameSet );
}
 
Example 3
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * compile outer column expression
 * @param refNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileOuterColRef( Node refNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	int level = compileOuterColRefExpr( refNode );
	if ( level == -1 )
	{
		compileComplexExpr( refNode, tree, columnExprList );
	}
	else
	{
		Node nextNode = refNode.getLastChild( );
		if ( nextNode.getType( ) == Token.STRING )
		{
			ColumnBinding info = new ColumnBinding( nextNode.getString( ),
					"",
					level );
			columnExprList.add( info );
		}
	}
	return;
}
 
Example 4
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile column reference expression
 * 
 * @param refNode
 * @throws BirtException
 */
private void compileDirectColRefExpr( Node refNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	assert ( refNode.getType( ) == Token.GETPROP
			|| refNode.getType( ) == Token.GETELEM
			|| refNode.getType( ) == Token.SETELEM || refNode.getType( ) == Token.SETPROP );

	Node rowName = refNode.getFirstChild( );
	assert ( rowName != null );
	if ( rowName.getType( ) != Token.NAME )
	{
		if ( refNode.getType( ) == Token.GETPROP
				|| refNode.getType( ) == Token.GETELEM
				|| refNode.getType( ) == Token.SETELEM
				|| refNode.getType( ) == Token.SETPROP )
		{
			compileOuterColRef( refNode, tree, columnExprList );
			compileRowPositionRef( refNode, tree, columnExprList );
			return;
		}
		compileComplexExpr( refNode, tree, columnExprList );
		return;
	}
	else
		compileSimpleColumnRefExpr( refNode, tree, columnExprList );
}
 
Example 5
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the complex expression
 * 
 * @param complexNode
 * @throws BirtException
 */
private void compileComplexExpr( Node complexNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	Node child = complexNode.getFirstChild( );
	while ( child != null )
	{
		if ( child.getType( ) == Token.FUNCTION )
		{
			int index = getFunctionIndex( child.getString( ), tree );
			compileFunctionNode( tree.getFunctionNode( index ),
					tree,
					columnExprList );
		}
		// keep reference to next child, since subsequent steps could
		// lose
		// the reference to it
		Node nextChild = child.getNext( );

		// do not include constants into the sub-expression list
		if ( child.getType( ) == Token.NUMBER
				|| child.getType( ) == Token.STRING
				|| child.getType( ) == Token.TRUE
				|| child.getType( ) == Token.FALSE
				|| child.getType( ) == Token.NULL )
		{
			processChild( child, tree, columnExprList );
			child = nextChild;
			continue;
		}

		processChild( child, tree, columnExprList );
		child = nextChild;
	}
}
 
Example 6
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * process child node
 * 
 * @param parent
 * @param child
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void processChild( Node child, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	switch ( child.getType( ) )
	{
		case Token.NUMBER :
		case Token.STRING :
		case Token.NULL :
		case Token.TRUE :
		case Token.FALSE :
			break;

		case Token.GETPROP :
		case Token.GETELEM :
		case Token.SETPROP :
		case Token.SETELEM :
		{
			compileDirectColRefExpr( child, tree, columnExprList );
			break;
		}
		case Token.CALL :
			compileAggregateExpr( child, tree, columnExprList );
			break;
		default :
			compileComplexExpr( child, tree, columnExprList );
	}

}
 
Example 7
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * if the Node is row Node, return true
 * 
 * @param refNode
 * @return
 */
private static boolean getDirectColRefExpr( Node refNode )
{
	assert ( refNode.getType( ) == Token.GETPROP || refNode.getType( ) == Token.GETELEM );

	Node rowName = refNode.getFirstChild( );
	assert ( rowName != null );
	if ( rowName.getType( ) != Token.NAME )
		return false;

	String str = rowName.getString( );
	assert ( str != null );
	if ( !str.equals( STRING_ROW ) )
		return false;

	Node rowColumn = rowName.getNext( );
	assert ( rowColumn != null );

	if ( refNode.getType( ) == Token.GETPROP
			&& rowColumn.getType( ) == Token.STRING )
	{
		return true;
	}
	else if ( refNode.getType( ) == Token.GETELEM )
	{
		if ( rowColumn.getType( ) == Token.NUMBER
				|| rowColumn.getType( ) == Token.STRING )
			return true;
	}

	return false;
}
 
Example 8
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * if the Node is row Node, return true
 * 
 * @param refNode
 * @return
 */
private static boolean getDirectColRefExpr( Node refNode, boolean mode )
{
	assert ( refNode.getType( ) == Token.GETPROP || refNode.getType( ) == Token.GETELEM );

	Node rowName = refNode.getFirstChild( );
	assert ( rowName != null );
	if ( rowName.getType( ) != Token.NAME )
		return false;

	String str = rowName.getString( );
	assert ( str != null );
	if ( mode && !str.equals( STRING_ROW ) )
		return false;
	else if ( !mode && !str.equals( STRING_DATASET_ROW ) )
		return false;

	Node rowColumn = rowName.getNext( );
	assert ( rowColumn != null );

	if ( refNode.getType( ) == Token.GETPROP
			&& rowColumn.getType( ) == Token.STRING )
	{
		return true;
	}
	else if ( refNode.getType( ) == Token.GETELEM )
	{
		if ( rowColumn.getType( ) == Token.NUMBER
				|| rowColumn.getType( ) == Token.STRING )
			return true;
	}

	return false;
}
 
Example 9
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * An aggregation expression in the form of Total.xxx for example Total.sum(
 * row.x ) This means the first child is a GETPROP node, and its left child
 * is "Total" and its right child is "sum"
 * 
 * @param callNode
 * @return @throws
 *         DataException
 */
protected IAggrFunction getAggregationFunction( Node callNode )
		throws DataException
{

	Node firstChild = callNode.getFirstChild( );
	if ( firstChild.getType( ) != Token.GETPROP )
		return null;

	Node getPropLeftChild = firstChild.getFirstChild( );
	if ( getPropLeftChild.getType( ) != Token.NAME
			|| !getPropLeftChild.getString( ).equals( TOTAL ) )
		return null;

	Node getPropRightChild = firstChild.getLastChild( );
	if ( getPropRightChild.getType( ) != Token.STRING )
		return null;

	String aggrFuncName = getPropRightChild.getString( );
	IAggrFunction agg = AggregationManager.getInstance( )
			.getAggregation( aggrFuncName );
	if ( agg == null )
	{
		// Aggr function name after Total is invalid; this will eventuall
		// cause
		// an error. Report error now
		throw new DataException( ResourceConstants.INVALID_TOTAL_NAME,
				aggrFuncName );
	}
	return agg;
}
 
Example 10
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setContinue(Node continueTarget)
{
    if (type != Token.LOOP) codeBug();
    if (continueTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = continueTarget;
}
 
Example 11
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setFinally(Node finallyTarget)
{
    if (type != Token.TRY) codeBug();
    if (finallyTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = finallyTarget;
}
 
Example 12
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setDefault(Node defaultTarget)
{
    if (type != Token.SWITCH) codeBug();
    if (defaultTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = defaultTarget;
}
 
Example 13
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param callNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileAggregationFunction( Node callNode,
		ScriptNode tree, List columnExprList ) throws BirtException
{
	Node firstChild = callNode.getFirstChild( );
	if ( firstChild.getType( ) != Token.GETPROP )
		return;

	Node getPropLeftChild = firstChild.getFirstChild( );
	if ( getPropLeftChild.getType( ) == Token.NAME
			&& getPropLeftChild.getString( ).equals( TOTAL ) )
		hasAggregation = true;

	compileComplexExpr( firstChild, tree, columnExprList );
}
 
Example 14
Source File: Jump.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public void setFinally(Node finallyTarget)
{
    if (type != Token.TRY) codeBug();
    if (finallyTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = finallyTarget;
}
 
Example 15
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
	 * 
	 * returns the compiled expression from processing a child node
	 * 
	 * @param context
	 * @param customerChecked
	 * @param parent
	 * @param child
	 * @return
	 * @throws DataException
	 */
protected CompiledExpression processChild( Context context,
		boolean customerChecked, Node parent, Node child, Node grandFather )
		throws DataException
{
	CompiledExpression compiledExpr = null;
	switch ( child.getType( ) )
	{
		case Token.NUMBER :
			compiledExpr = new ConstantExpression( child.getDouble( ) );
			break;

		case Token.STRING :
			compiledExpr = new ConstantExpression( child.getString( ) );
			break;

		case Token.NULL :
			compiledExpr = new ConstantExpression( );
			break;

		case Token.TRUE :
			compiledExpr = new ConstantExpression( true );
			break;

		case Token.FALSE :
			compiledExpr = new ConstantExpression( false );
			break;

		case Token.GETPROP :
		{
			ConstantExpression ce = AggregationConstantsUtil.getConstantExpression( child );
			if ( ce != null )
			{
				compiledExpr = ce;
				break;
			}
		}

		case Token.GETELEM :
			compiledExpr = compileDirectColRefExpr( parent,
					child,
					grandFather,
					customerChecked,
					context );
			break;

		case Token.CALL :
		{
			compiledExpr = compileAggregateExpr( context, parent, child );
			break;
		}
	}
	if ( compiledExpr == null )
		compiledExpr = compileComplexExpr( context, child, customerChecked );
	return compiledExpr;
}
 
Example 16
Source File: ExpressionCompiler.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
protected CompiledExpression compileDirectColRefExpr( Node parent, Node refNode,
		Node grandFather, boolean customerChecked, Context context )
		throws DataException
{
	// if it's a GETPROP or GETELEM with row on the left side,
	// and either a STRING or NUMBER on the right side, then it's
	// a direct column reference
	assert ( refNode.getType( ) == Token.GETPROP || refNode.getType( ) == Token.GETELEM );

	Node rowName = refNode.getFirstChild( );
	assert ( rowName != null );
	if ( rowName.getType( ) != Token.NAME )
		return null;

	String str = rowName.getString( );
	assert ( str != null );
	if ( !str.equals( rowIndicator ) )
		return null;

	Node rowColumn = rowName.getNext( );
	assert ( rowColumn != null );

	if ( refNode.getType( ) == Token.GETPROP
			&& rowColumn.getType( ) == Token.STRING )
	{
		if ( ScriptConstants.OUTER_RESULT_KEYWORD.equals( rowColumn.getString( ) )
				|| ScriptConstants.ROW_NUM_KEYWORD.equals( rowColumn.getString( ) )
				|| "0".equals( rowColumn.getString( ) ) )
			return null;

		return new ColumnReferenceExpression( getDataSetMode( )
				? STRING_ROW : STRING_DATASETROW, rowColumn.getString( ) );
	}
	if ( refNode.getType( ) == Token.GETELEM )
	{
		if ( rowColumn.getType( ) == Token.NUMBER )
		{
			if ( 0 == rowColumn.getDouble( ) )
				return null;
			return new ColumnReferenceExpression( getDataSetMode( )
					? STRING_ROW : STRING_DATASETROW,
					(int) rowColumn.getDouble( ) );
		}
		else if ( rowColumn.getType( ) == Token.STRING )
		{
			if ( "_rownum".equals( rowColumn.getString( ) ) )
				return null;
			return new ColumnReferenceExpression( getDataSetMode( )
					? STRING_ROW : STRING_DATASETROW, rowColumn.getString( ) );
		}
	}

	// right side is not a STRING or a NUMBER, which is what is needed for
	// a direct column reference. so it could be something
	// like row[getColumnIndex()] and that would be a complex expression
	return null;
}
 
Example 17
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 18
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 19
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 
 * @param refNode
 * @return
 */
private int compileOuterColRefExpr( Node refNode )
{
	int count = 0;
	Node rowFirstNode = refNode.getFirstChild( );
	if ( refNode.getType( ) == Token.GETPROP
			|| refNode.getType( ) == Token.GETELEM
			|| refNode.getType( ) == Token.SETPROP
			|| refNode.getType( ) == Token.SETELEM )
	{
		if ( rowFirstNode.getType( ) == Token.NAME
				&& rowFirstNode.getString( ).equals( ROW_INDICATOR ) )
		{
			Node rowColumn = rowFirstNode.getNext( );
			if ( rowColumn.getType( ) == Token.STRING )
			{
				if ( "_outer".equals( rowColumn.getString( ) ) )
					count++;
			}
			return count;
		}
		else if ( rowFirstNode.getType( ) == Token.GETPROP
				|| rowFirstNode.getType( ) == Token.SETPROP )
		{
			if ( compileOuterColRefExpr( rowFirstNode ) == -1 )
				return -1;
			else
				count = count + compileOuterColRefExpr( rowFirstNode );
			Node nextChild = rowFirstNode.getNext( );
			if ( nextChild.getType( ) == Token.STRING )
			{
				if ( "_outer".equals( nextChild.getString( ) ) )
					count++;
			}
		}
		else
			return -1;
		return count;
	}
	else
		return -1;
}
 
Example 20
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * 
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( compiledExprCache.containsKey( expression ) )
		return ( (Boolean) compiledExprCache.get( expression ) ).booleanValue( );
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		compiledExprCache.put( expression, Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}

	compiledExprCache.put( expression, Boolean.valueOf( isColumn ) );
	return isColumn;
}