View Javadoc

1   /*
2   Copyright (c) 2010, Chin Huang
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7   
8    * Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10   * Redistributions in binary form must reproduce the above copyright notice,
11     this list of conditions and the following disclaimer in the documentation
12     and/or other materials provided with the distribution.
13  
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25  package net.ramapuram.thomas.webapp.jsp;
26  
27  import java.beans.FeatureDescriptor;
28  import java.util.Iterator;
29  import javax.el.ELContext;
30  import javax.el.ELException;
31  import javax.el.ELResolver;
32  import javax.el.PropertyNotFoundException;
33  import javax.el.PropertyNotWritableException;
34  
35  /**
36   * {@link ELResolver} which escapes XML in String values.
37   */
38  public class EscapeXmlELResolver extends ELResolver {
39  
40      private ELResolver originalResolver;
41      private ThreadLocal<Boolean> gettingValue = new ThreadLocal<Boolean>() {
42          @Override
43          protected Boolean initialValue() {
44              return Boolean.FALSE;
45          }
46      };
47      
48      private ELResolver getOriginalResolver(ELContext context) {
49          if (originalResolver == null) {
50              originalResolver = context.getELResolver();
51          }
52          return originalResolver;
53      }
54      
55      @Override
56      public Class<?> getCommonPropertyType(ELContext context, Object base) {
57          return getOriginalResolver(context).getCommonPropertyType(context, base);
58      }
59  
60      @Override
61      public Iterator<FeatureDescriptor> getFeatureDescriptors(
62              ELContext context, Object base)
63      {
64          return getOriginalResolver(context).getFeatureDescriptors(context, base);
65      }
66  
67      @Override
68      public Class<?> getType(ELContext context, Object base, Object property)
69          throws NullPointerException, PropertyNotFoundException, ELException
70      {
71          return getOriginalResolver(context).getType(context, base, property);
72      }
73  
74      @Override
75      public Object getValue(ELContext context, Object base, Object property)
76          throws NullPointerException, PropertyNotFoundException, ELException
77      {
78          if (gettingValue.get()) {
79              return null;
80          }
81          
82          // This resolver is in the original resolver chain.  When this resolver
83          // invokes the original resolver chain, set a flag so when execution
84          // reaches this resolver, act like this resolver is not in the chain.
85          gettingValue.set(true);
86          Object value =
87                  getOriginalResolver(context).getValue(context, base, property);
88          gettingValue.set(false);
89  
90          if (value instanceof String) {
91              value = EscapeXml.escape((String) value);
92          }
93          return value;
94      }
95  
96      @Override
97      public boolean isReadOnly(ELContext context, Object base, Object property)
98          throws NullPointerException, PropertyNotFoundException, ELException
99      {
100         return getOriginalResolver(context).isReadOnly(context, base, property);
101     }
102 
103     @Override
104     public void setValue(
105             ELContext context, Object base, Object property, Object value)
106         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException
107     {
108         getOriginalResolver(context).setValue(context, base, property, value);
109     }
110 }