Author: mck
Date: Wed Nov  9 21:18:21 2011
New Revision: 1199972

URL: http://svn.apache.org/viewvc?rev=1199972&view=rev
Log:
- avoid clash with el expressions: use just {options[..]} rather than 
${options[..]}
- look for either local or cascading option list-attribute
- smaller methods

Modified:
    
tiles/framework/trunk/tiles-parent/tiles-extras/src/main/java/org/apache/tiles/extras/renderer/OptionsRenderer.java

Modified: 
tiles/framework/trunk/tiles-parent/tiles-extras/src/main/java/org/apache/tiles/extras/renderer/OptionsRenderer.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-parent/tiles-extras/src/main/java/org/apache/tiles/extras/renderer/OptionsRenderer.java?rev=1199972&r1=1199971&r2=1199972&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-parent/tiles-extras/src/main/java/org/apache/tiles/extras/renderer/OptionsRenderer.java
 (original)
+++ 
tiles/framework/trunk/tiles-parent/tiles-extras/src/main/java/org/apache/tiles/extras/renderer/OptionsRenderer.java
 Wed Nov  9 21:18:21 2011
@@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory;
 public final class OptionsRenderer implements TypeDetectingRenderer {
 
     private static final Pattern OPTIONS_PATTERN
-            = Pattern.compile(Pattern.quote("${options[") + ".+" + 
Pattern.quote("]}"));
+            = Pattern.compile(Pattern.quote("{options[") + "(.+)" + 
Pattern.quote("]}"));
 
     private static final Logger LOG = 
LoggerFactory.getLogger(OptionsRenderer.class);
 
@@ -68,38 +68,25 @@ public final class OptionsRenderer imple
     @Override
     public void render(final String path, final Request request) throws 
IOException {
 
-        final Matcher matcher =  OPTIONS_PATTERN.matcher((String) path);
+        Matcher matcher =  OPTIONS_PATTERN.matcher((String) path);
 
         if (null != matcher && matcher.find()) {
             boolean done = false;
-            final String match = matcher.group();
+            String match = matcher.group(1);
             ListAttribute fallbacks = (ListAttribute) TilesAccess
                     .getCurrentContainer(request)
                     .getAttributeContext(request)
-                    .getLocalAttribute(match);
+                    .getAttribute(match);
+
+            if(null == fallbacks){
+                throw new IllegalStateException("A matching list-attribute 
name=\"" + match + "\" must be defined.");
+            }else if(fallbacks.getValue().isEmpty()){
+                throw new IllegalStateException("list-attribute name=\"" + 
match + "\" must have minimum one attribute");
+            }
 
             for (Attribute option : (List<Attribute>) fallbacks.getValue()) {
-                final String template = 
path.replaceFirst(Pattern.quote(match), (String)option.getValue());
-                if(!Cache.isTemplateMissing(template)){
-                    try {
-                        if (null != applicationContext.getResource(template)) 
{ // can throw FileNotFoundException !
-                            renderer.render(template, request); // can throw 
FileNotFoundException !
-                            done = true;
-                            Cache.setIfAbsentTemplateFound(template, true);
-                        }
-                    } catch (FileNotFoundException ex) {
-                        if(ex.getMessage().contains(template)){
-                            // expected outcome. continue loop.
-                            LOG.trace(ex.getMessage());
-                        }else{
-                            // comes from an inner 
templateAttribute.render(..) so throw on
-                            throw ex;
-                        }
-                    } catch(IOException ex){ //xxx ???
-                        throw ex;
-                    }
-                    Cache.setIfAbsentTemplateFound(template, false);
-                }
+                String template = 
path.replaceFirst(Pattern.quote(matcher.group()), (String)option.getValue());
+                done = renderAttempt(template, request);
                 if(done){ break; }
             }
             if (!done) {
@@ -110,6 +97,31 @@ public final class OptionsRenderer imple
         }
     }
 
+    private boolean renderAttempt(final String template, final Request 
request) throws IOException{
+        boolean result = false;
+        if(!Cache.isTemplateMissing(template)){
+            try {
+                if (null != applicationContext.getResource(template)) { // can 
throw FileNotFoundException !
+                    renderer.render(template, request); // can throw 
FileNotFoundException !
+                    result = true;
+                    Cache.setIfAbsentTemplateFound(template, true);
+                }
+            } catch (FileNotFoundException ex) {
+                if(ex.getMessage().contains(template)){
+                    // expected outcome. continue loop.
+                    LOG.trace(ex.getMessage());
+                }else{
+                    // comes from an inner templateAttribute.render(..) so 
throw on
+                    throw ex;
+                }
+            } catch(IOException ex){ //xxx ???
+                throw ex;
+            }
+            Cache.setIfAbsentTemplateFound(template, false);
+        }
+        return result;
+    }
+
     private static final class Cache{
 
         /** It takes CACHE_LIFE milliseconds for any hot deployments to 
register.


Reply via email to