001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.language.simple.ast;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.language.simple.types.SimpleIllegalSyntaxException;
022    import org.apache.camel.language.simple.types.SimpleParserException;
023    import org.apache.camel.language.simple.types.SimpleToken;
024    
025    /**
026     * Starts a function
027     */
028    public class SimpleFunctionStart extends BaseSimpleNode implements BlockStart {
029    
030        private CompositeNodes block;
031    
032        public SimpleFunctionStart(SimpleToken token) {
033            super(token);
034            this.block = new CompositeNodes(token);
035        }
036    
037        @Override
038        public String toString() {
039            // output a nice toString so it makes debugging easier as we can see the entire block
040            return "${" + block + "}";
041        }
042    
043        @Override
044        public Expression createExpression(String expression) {
045            // a function can either be a simple literal function, or contain nested functions
046            if (block.getChildren().size() == 1 && block.getChildren().get(0) instanceof LiteralNode) {
047                return doCreateLiteralExpression(expression);
048            } else {
049                return doCreateCompositeExpression(expression);
050            }
051        }
052    
053        private Expression doCreateLiteralExpression(final String expression) {
054            SimpleFunctionExpression function = new SimpleFunctionExpression(this.getToken());
055            LiteralNode literal = (LiteralNode) block.getChildren().get(0);
056            function.addText(literal.getText());
057            return function.createExpression(expression);
058        }
059    
060        private Expression doCreateCompositeExpression(final String expression) {
061            final SimpleToken token = getToken();
062            return new Expression() {
063                @Override
064                public <T> T evaluate(Exchange exchange, Class<T> type) {
065                    StringBuilder sb = new StringBuilder();
066    
067                    // we need to concat the block so we have the expression
068                    for (SimpleNode child : block.getChildren()) {
069                        if (child instanceof LiteralNode) {
070                            String text = ((LiteralNode) child).getText();
071                            sb.append(text);
072                        } else if (child instanceof SimpleFunctionStart) {
073                            try {
074                                // pass in null when we evaluate the nested expressions
075                                Expression nested = child.createExpression(null);
076                                String text = nested.evaluate(exchange, String.class);
077                                if (text != null) {
078                                    sb.append(text);
079                                }
080                            } catch (SimpleParserException e) {
081                                // must rethrow parser exception as illegal syntax with details about the location
082                                throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
083                            }
084                        }
085                    }
086    
087                    // we have now concat the block as a String which contains the function expression
088                    // which we then need to evaluate as a function
089                    String exp = sb.toString();
090                    SimpleFunctionExpression function = new SimpleFunctionExpression(token);
091                    function.addText(exp);
092                    try {
093                        return function.createExpression(exp).evaluate(exchange, type);
094                    } catch (SimpleParserException e) {
095                        // must rethrow parser exception as illegal syntax with details about the location
096                        throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
097                    }
098                }
099    
100                @Override
101                public String toString() {
102                    return expression;
103                }
104            };
105        }
106    
107        @Override
108        public boolean acceptAndAddNode(SimpleNode node) {
109            // only accept literals or embedded functions
110            if (node instanceof LiteralNode || node instanceof SimpleFunctionStart) {
111                block.addChild(node);
112                return true;
113            } else {
114                return false;
115            }
116        }
117    
118    }