cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From upayav...@apache.org
Subject cvs commit: cocoon-2.1/src/test/org/apache/cocoon/components/treeprocessor/variables PreparedVariableResolverTestCase.java
Date Sat, 03 Apr 2004 20:41:26 GMT
upayavira    2004/04/03 12:41:26

  Modified:    src/java/org/apache/cocoon/components/treeprocessor/variables
                        PreparedVariableResolver.java
               src/test/org/apache/cocoon/components/treeprocessor/variables
                        PreparedVariableResolverTestCase.java
  Log:
  Fixing colon outside of module reference, and enabling escaping of braces
  
  Revision  Changes    Path
  1.5       +70 -38    cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java
  
  Index: PreparedVariableResolver.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PreparedVariableResolver.java	2 Apr 2004 20:22:07 -0000	1.4
  +++ PreparedVariableResolver.java	3 Apr 2004 20:41:26 -0000	1.5
  @@ -77,51 +77,83 @@
           tokens = new ArrayList();
           int pos=0;
           int i;
  +        boolean escape = false;
           for (i=0;i< expr.length();i++) {
               char c = expr.charAt(i);
  -            if (c=='{' || c=='}' || c==':') {
  +            if (escape) {
  +                escape = false;
  +            } else if (c=='\\' && i< expr.length()) {
  +                char nextChar = expr.charAt(i+1);
  +                if (nextChar == '{' || nextChar == '}') {
  +                    expr = expr.substring(0, i) + expr.substring(i+1);
  +                    escape = true;
  +                    i--;
  +                }
  +            } else if (c=='{') {
                   if (i> pos) {
                       tokens.add(new Token(expr.substring(pos, i)));
                   }
  -                if (c=='{') {
  -                    openCount++;
  -                    tokens.add(OPEN_TOKEN);
  -                    int colonPos = indexOf(expr, ":", i);
  -                    int closePos = indexOf(expr, "}", i);
  -                    int openPos  = indexOf(expr, "{", i);
  -                    if (openPos < colonPos && openPos < closePos) {
  -                        throw new PatternException("Invalid '{' at position "+ i + " in
expression " + expr);
  -                    } else if (colonPos < closePos) {
  -                        // we've found a module
  -                        Token token;
  -                        String module = expr.substring(i+1, colonPos);
  -                        
  -                        if (module.equals("sitemap")) {
  -                            // Explicit prefix for sitemap variable
  -                            needsMapStack = true;
  -                            token = new Token(PREFIXED_SITEMAP_VAR);
  -                        } else if (module.startsWith("#")) {
  -                            // anchor syntax refering to a name result level
  -                            needsMapStack = true;
  -                            token = new Token(ANCHOR_VAR, module.substring(1));
  -                        } else {
  -                            // Module used
  -                            token = getNewModuleToken(module);
  -                        }
  -                        tokens.add(token);
  -                        i = colonPos-1;
  -                    } else {
  -                        // Unprefixed name : sitemap variable
  +                openCount++;
  +                tokens.add(OPEN_TOKEN);
  +                int colonPos = indexOf(expr, ":", i);
  +                int closePos = indexOf(expr, "}", i);
  +                int openPos  = indexOf(expr, "{", i);
  +                if (openPos < colonPos && openPos < closePos) {
  +                    throw new PatternException("Invalid '{' at position "+ i + " in expression
" + expr);
  +                } else if (colonPos < closePos) {
  +                    // we've found a module
  +                    Token token;
  +                    String module = expr.substring(i+1, colonPos);
  +                    
  +                    if (module.equals("sitemap")) {
  +                        // Explicit prefix for sitemap variable
                           needsMapStack = true;
  -                        tokens.add(getNewSitemapToken(expr.substring(i+1, closePos)));
  -                        i = closePos-1;
  +                        token = new Token(PREFIXED_SITEMAP_VAR);
  +                    } else if (module.startsWith("#")) {
  +                        // anchor syntax refering to a name result level
  +                        needsMapStack = true;
  +                        token = new Token(ANCHOR_VAR, module.substring(1));
  +                    } else {
  +                        // Module used
  +                        token = getNewModuleToken(module);
  +                    }
  +                    tokens.add(token);
  +                    i = colonPos-1;
  +                } else {
  +                    // Unprefixed name : sitemap variable
  +                    needsMapStack = true;
  +                    tokens.add(getNewSitemapToken(expr.substring(i+1, closePos)));
  +                    i = closePos-1;
  +                }
  +                pos=i+1;
  +            } else if (c=='}') {
  +                if (i>0 && expr.charAt(i-1) == '\\') {
  +                    continue;
  +                }
  +                if (i> pos) {
  +                    tokens.add(new Token(expr.substring(pos, i)));
  +                }
  +                closeCount++;
  +                tokens.add(CLOSE_TOKEN);
  +                pos=i+1;
  +            } else if (c==':') {
  +                if (tokens.size()>0) {
  +                    int lastTokenType = ((Token)tokens.get(tokens.size()-1)).getType();
  +                    if (lastTokenType != PREFIXED_SITEMAP_VAR &&
  +                        lastTokenType != ANCHOR_VAR &&
  +                        lastTokenType != THREADSAFE_MODULE &&
  +                        lastTokenType != STATEFUL_MODULE) {
  +                            continue;
                       }
  -                } else if (c=='}') {
  -                    closeCount++;
  -                    tokens.add(CLOSE_TOKEN);
  -                } else if (c==':') {
  -                    tokens.add(COLON_TOKEN);
                   }
  +                if (i != pos) {
  +                    // this colon isn't part of a module reference 
  +                    continue;
  +                }
  +                if (i> pos) {
  +                    tokens.add(new Token(expr.substring(pos, i)));
  +                }
  +                tokens.add(COLON_TOKEN);
                   pos=i+1;
               }
           }
  
  
  
  1.2       +29 -1     cocoon-2.1/src/test/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolverTestCase.java
  
  Index: PreparedVariableResolverTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolverTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PreparedVariableResolverTestCase.java	2 Apr 2004 20:22:07 -0000	1.1
  +++ PreparedVariableResolverTestCase.java	3 Apr 2004 20:41:26 -0000	1.2
  @@ -158,4 +158,32 @@
           PreparedVariableResolver resolver = new PreparedVariableResolver(expr, manager);
           assertEquals("from juliet to oscar", resolver.resolve(context, getObjectModel()));
       }
  +    
  +    public void testColonInTextContent() throws PatternException {
  +        String expr = "http://cocoon.apache.org";
  +        
  +        InvokeContext context = new InvokeContext(true);
  +        context.enableLogging(new LogKitLogger(getLogger()));
  +
  +        Map sitemapElements;
  +        sitemapElements = new HashMap();
  +        context.pushMap("label", sitemapElements);
  +        
  +        PreparedVariableResolver resolver = new PreparedVariableResolver(expr, manager);
  +        assertEquals("http://cocoon.apache.org", resolver.resolve(context, getObjectModel()));
  +    }
  +    
  +    public void testEscapedBraces() throws PatternException {
  +        String expr = "This is a \\{brace\\}";
  +        
  +        InvokeContext context = new InvokeContext(true);
  +        context.enableLogging(new LogKitLogger(getLogger()));
  +
  +        Map sitemapElements;
  +        sitemapElements = new HashMap();
  +        context.pushMap("label", sitemapElements);
  +
  +        PreparedVariableResolver resolver = new PreparedVariableResolver(expr, manager);
  +        assertEquals("This is a {brace}", resolver.resolve(context, getObjectModel()));
  +    }
   }
  
  
  

Mime
View raw message