Package org.apache.cocoon.sitemap

Examples of org.apache.cocoon.sitemap.PatternException


        List mapStack = null; // get the stack only when necessary - lazy inside the loop
        int stackSize = 0;

        if (needsMapStack) {
            if (context == null) {
                throw new PatternException("Need an invoke context to resolve " + this);
            }
            mapStack = context.getMapStack();
            stackSize = mapStack.size();
        }

        Stack stack = new Stack();

        for (Iterator i = tokens.iterator(); i.hasNext();) {
            Token token = (Token) i.next();
            Token last;
            switch (token.getType()){
                case TEXT:
                    if (stack.empty()) {
                        stack.push(new Token(EXPR, token.getStringValue()));
                    } else {
                        last = (Token)stack.peek();
                        if (last.hasType(EXPR)) {
                            last.merge(token);
                        } else {
                            stack.push(new Token(EXPR, token.getStringValue()));
                        }
                    }
                    break;
                case CLOSE:
                    Token expr = (Token)stack.pop();
                    Token lastButOne = (Token)stack.pop();
                    Token result;
                    if (expr.hasType(COLON)) { // i.e. nothing was specified after the colon
                        stack.pop(); // Pop the OPEN
                        result = processModule(lastButOne, EMPTY_TOKEN, objectModel, context, mapStack, stackSize);
                    } else if (lastButOne.hasType(COLON)) {
                        Token module = (Token)stack.pop();
                        stack.pop(); // Pop the OPEN
                        result = processModule(module, expr, objectModel, context, mapStack, stackSize);
                    } else if (lastButOne.hasType(VariableExpressionTokenizer.TokenReciever.NEW_EXPRESSION)) {
                        stack.pop(); // Pop the OPEN
                        ExpressionFactory expressionFactory = null;
                        ObjectModel newObjectModel = null;
                        try {
                            expressionFactory = (ExpressionFactory)manager.lookup(ExpressionFactory.ROLE);
                            newObjectModel = (ObjectModel)manager.lookup(ObjectModel.ROLE);
                            result = processNewExpression(lastButOne, expressionFactory, newObjectModel);
                        } catch (ServiceException e) {
                            throw new PatternException("Cannot obtain necessary components to evaluate new expression '"
                                    + lastButOne.getStringValue() + "' in expression '" + this.originalExpr + "'", e);
                        } finally {
                            if (expressionFactory != null)
                                manager.release(expressionFactory);
                            if (newObjectModel != null)
                                manager.release(newObjectModel);
                        }
                    } else {
                        result = processVariable(expr, mapStack, stackSize);
                    }
                    if (stack.empty()) {
                        stack.push(result);
                    } else {
                        last = (Token)stack.peek();
                        if (last.hasType(EXPR)) {
                            last.merge(result);
                        } else {
                            stack.push(result);
                        }
                    }
                    break;
                case OPEN:
                case COLON:
                case ANCHOR_VAR:
                case THREADSAFE_MODULE:
                case STATEFUL_MODULE:
                case ROOT_SITEMAP_VARIABLE:
                default: {
                    stack.push(token);
                    break;
                }
            }
        }
        if (stack.size() !=1) {
            throw new PatternException("Evaluation error in expression: " + originalExpr);
        }
        return ((Token)stack.pop()).getStringValue();
    }
View Full Code Here


        if (type == ANCHOR_VAR) {
            Map levelResult = context.getMapByAnchor(module.getStringValue());

            if (levelResult == null) {
              throw new PatternException("Error while evaluating '" + this.originalExpr +
                "' : no anchor '" + String.valueOf(module.getStringValue()) + "' found in context");
            }

            Object result = levelResult.get(expr.getStringValue());
            return new Token(EXPR, result==null ? "" : result.toString());
        } else if (type == THREADSAFE_MODULE) {
            try {
                InputModule im = module.getModule();
                Object result = im.getAttribute(expr.getStringValue(), null, objectModel);
                return new Token(EXPR, result==null ? "" : result.toString());

            } catch(ConfigurationException confEx) {
                throw new PatternException("Cannot get variable '" + expr.getStringValue() +
                    "' in expression '" + this.originalExpr + "'", confEx);
            }

        } else if (type == STATEFUL_MODULE) {
            InputModule im = null;
            String moduleName = module.getStringValue();
            try {
                im = (InputModule) this.manager.lookup(InputModule.ROLE + '/' + moduleName);

                Object result = im.getAttribute(expr.getStringValue(), null, objectModel);
                return new Token(EXPR, result==null ? "" : result.toString());

            } catch(ServiceException e) {
                throw new PatternException("Cannot get module '" + moduleName +
                                           "' in expression '" + this.originalExpr + "'", e);

            } catch(ConfigurationException confEx) {
                throw new PatternException("Cannot get variable '" + expr.getStringValue() +
                    "' in expression '" + this.originalExpr + "'", confEx);

            } finally {
                this.manager.release(im);
            }
        } else if (type == SITEMAP_VAR) {
            // Prefixed sitemap variable must be parsed at runtime
            String variable = expr.getStringValue();
            Token token;
            if (variable.startsWith("/")) {
                token = new Token(ROOT_SITEMAP_VARIABLE, variable.substring(1));
            } else {
                // Find level
                int level = 1; // Start at 1 since it will be substracted from list.size()
                int pos = 0;
                while (variable.startsWith("../", pos)) {
                    level++;
                    pos += "../".length();
                }
                token = new Token(level, variable.substring(pos));
            }
            return processVariable(token, mapStack, stackSize);
        } else {
            throw new PatternException("Unknown token type: " + expr.getType());
        }
    }
View Full Code Here

            Object result = ((Map)mapStack.get(0)).get(value);
            return new Token(EXPR, result==null ? "" : result.toString());
        }
        // relative sitemap variable
        if (type > stackSize) {
            throw new PatternException("Error while evaluating '" + this.originalExpr +
                "' : not so many levels");
        }

        Object result = ((Map)mapStack.get(stackSize - type)).get(value);
        return new Token(EXPR, result==null ? "" : result.toString());
View Full Code Here

        Object result;
        try {
            Expression newExpression = expressionFactory.getExpression(expr.getStringValue());
            result = newExpression.evaluate(newObjectModel);
        } catch (ExpressionException e) {
            throw new PatternException("Cannot evaluate new expression '" + expr.getStringValue() + "' in expression "
                                       + "'" + this.originalExpr + "'", e);
        }
        return new Token(EXPR, result == null ? "" : result.toString());
    }
View Full Code Here

                int colonPos = indexOf(expression, ':', i);
                int closePos = indexOf(expression, '}', i);
                int openPos = indexOf(expression, '{', i);

                if (openPos < colonPos && openPos < closePos) {
                    throw new PatternException("Invalid '{' at position " + i +
                                               " in expression \"" + expression + "\"");
                }

                if (colonPos < closePos) {
                    // we've found a module
                    String module = expression.substring(i + 1, colonPos);
                    reciever.addToken(lastTokenType = TokenReciever.MODULE, module);
                    i = colonPos - 1;
                } else {
                    // Unprefixed name: variable
                    reciever.addToken(lastTokenType = TokenReciever.VARIABLE, expression.substring(i + 1, closePos));
                    i = closePos - 1;
                }

                pos = i + 1;
            } else if (c == '}') {
                if (i > 0 && expression.charAt(i - 1) == '\\') {
                    continue;
                }
                if (i > pos) {
                    reciever.addToken(lastTokenType = TokenReciever.TEXT, expression.substring(pos, i));
                }

                closeCount++;
                reciever.addToken(lastTokenType = TokenReciever.CLOSE, null);

                pos = i + 1;
            } else if (c == ':') {
                if (lastTokenType != TokenReciever.MODULE || i != pos) {
                    // this colon isn't part of a module reference
                    continue;
                }

                reciever.addToken(lastTokenType = TokenReciever.COLON, null);
                pos = i + 1;
            }
        }

        if (i > pos) {
            reciever.addToken(lastTokenType = TokenReciever.TEXT, expression.substring(pos, i));
        }

        if (openCount != closeCount) {
            throw new PatternException("Mismatching braces in expression \"" + expression + "\"");
        }
    }
View Full Code Here

            if (stringTemplateParser instanceof LegacySitemapStringTemplateParser)
                this.substitutions = new LegacySubstitutions((LegacySitemapStringTemplateParser) stringTemplateParser, null, expression);
            else
                this.substitutions = new Substitutions(stringTemplateParser, null, expression);
        } catch (Exception e) {
            throw new PatternException(e);
        }
    }
View Full Code Here

            if (this.substitutions instanceof LegacySubstitutions)
                return ((LegacySubstitutions) substitutions).toString(null, this.objectModel, context, objectModel);
            else
                return substitutions.toString(null, this.objectModel);
        } catch (Exception e) {
            throw new PatternException(e);
        }
    }
View Full Code Here

            VariableResolver resolver;
            try {
                resolver = (VariableResolver) manager.lookup(VariableResolver.ROLE);
                resolver.setExpression(expression);
            } catch (ServiceException e) {
                throw new PatternException("Couldn't obtain VariableResolver.", e);
            }

            return resolver;
        }
View Full Code Here

                    pos += "../".length();
                }

                int end = expr.indexOf('}', pos);
                if (end == -1) {
                    throw new PatternException("Unmatched '{' in " + expr);
                }

                stringList.add(expr.substring(pos, end));
                levelList.add(new Integer(level));
View Full Code Here

                if (level == -1) {
                    result.append(this.strings[i]);

                } else {
                    if (level > stackSize) {
                        throw new PatternException("Error while evaluating '" + this.originalExpr +
                            "' : not so many levels");
                    }

                    Object value = ((Map)mapStack.get(stackSize - level)).get(this.strings[i]);
                    if (value != null) {
View Full Code Here

TOP

Related Classes of org.apache.cocoon.sitemap.PatternException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.