View Javadoc

1   /*
2    * $Id: AbstractContentAccessor.java,v 1.14 2010/03/19 14:53:48 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.contentAccessor;
5   
6   import java.util.Collections;
7   import java.util.Map;
8   
9   import javax.faces.FacesException;
10  import javax.faces.context.FacesContext;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.rcfaces.core.internal.util.PathTypeTools;
15  import org.rcfaces.core.lang.IContentFamily;
16  
17  /**
18   * 
19   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
20   * @version $Revision: 1.14 $ $Date: 2010/03/19 14:53:48 $
21   */
22  public abstract class AbstractContentAccessor extends AbstractContentPath
23          implements IContentAccessor {
24      private static final Log LOG = LogFactory
25              .getLog(AbstractContentAccessor.class);
26  
27      private final IContentAccessor parentContentAccessor;
28  
29      private final IContentFamily contentFamily;
30  
31      private IContentVersionHandler contentVersionHandler;
32  
33      private IContentProxyHandler contentProxyHandler;
34  
35      protected AbstractContentAccessor(IContentFamily type,
36              IContentVersionHandler contentVersionHandler,
37              IContentProxyHandler contentProxyHandler) {
38          this(type, null, contentVersionHandler, contentProxyHandler);
39      }
40  
41      protected AbstractContentAccessor(IContentAccessor contentAccessor) {
42          this(contentAccessor.getContentFamily(), contentAccessor,
43                  contentAccessor.getContentVersionHandler(), contentAccessor
44                          .getContentProxyHandler());
45      }
46  
47      protected AbstractContentAccessor(IContentFamily type,
48              IContentAccessor contentAccessor,
49              IContentVersionHandler contentVersionHandler,
50              IContentProxyHandler contentProxyHandler) {
51          super(contentAccessor);
52  
53          this.parentContentAccessor = contentAccessor;
54          this.contentFamily = type;
55          this.contentVersionHandler = contentVersionHandler;
56          this.contentProxyHandler = contentProxyHandler;
57      }
58  
59      public IContentAccessor getParentAccessor() {
60          return parentContentAccessor;
61      }
62  
63      public final IContentFamily getContentFamily() {
64          return contentFamily;
65      }
66  
67      public IContentVersionHandler getContentVersionHandler() {
68          return contentVersionHandler;
69      }
70  
71      public void setContentVersionHandler(
72              IContentVersionHandler contentVersionHandler) {
73          this.contentVersionHandler = contentVersionHandler;
74      }
75  
76      public IContentProxyHandler getContentProxyHandler() {
77          return contentProxyHandler;
78      }
79  
80      public void setContentProxyHandler(IContentProxyHandler contentProxyHandler) {
81          this.contentProxyHandler = contentProxyHandler;
82      }
83  
84      public final String resolveURL(FacesContext facesContext,
85              IGeneratedResourceInformation contentInformation,
86              IGenerationResourceInformation generationInformation) {
87  
88          return resolveURL(facesContext, contentInformation,
89                  generationInformation, IContentPath.ABSOLUTE_PATH_TYPE
90                          | IContentPath.RELATIVE_PATH_TYPE
91                          | IContentPath.EXTERNAL_PATH_TYPE);
92      }
93  
94      public final String resolveURL(FacesContext facesContext,
95              IGeneratedResourceInformation contentInformation,
96              IGenerationResourceInformation generationInformation,
97              int pathTypeMask) {
98  
99          IContentAccessor contentAccessor = ContentAccessorEngine.resolveURL(
100                 facesContext, this, contentInformation, generationInformation);
101         if (contentAccessor == null) {
102             return null;
103         }
104 
105         Object resolvedURL = contentAccessor.getContentRef();
106         if ((resolvedURL instanceof String) == false) {
107             if (LOG.isDebugEnabled()) {
108                 LOG.debug("Resolved URL is not a String: "
109                         + resolvedURL
110                         + " (pathType="
111                         + AbstractContentPath.getPathTypeName(contentAccessor
112                                 .getPathType()) + ")");
113             }
114             return null;
115         }
116 
117         String resolvedURLs = (String) resolvedURL;
118 
119         if (LOG.isDebugEnabled()) {
120             LOG.debug("Resolved URL: "
121                     + resolvedURLs
122                     + " (pathType="
123                     + AbstractContentPath.getPathTypeName(contentAccessor
124                             .getPathType()) + ")");
125         }
126 
127         int currentPathType = contentAccessor.getPathType();
128 
129         if ((pathTypeMask & IContentPath.RELATIVE_PATH_TYPE) > 0) {
130             if (currentPathType == IContentPath.RELATIVE_PATH_TYPE) {
131                 return resolvedURLs;
132             }
133         }
134 
135         if ((pathTypeMask & IContentPath.ABSOLUTE_PATH_TYPE) > 0) {
136             if (currentPathType == IContentPath.ABSOLUTE_PATH_TYPE) {
137                 return resolvedURLs;
138             }
139 
140             if (currentPathType == IContentPath.CONTEXT_PATH_TYPE) {
141 
142                 String converted = PathTypeTools
143                         .convertContextPathToAbsoluteType(facesContext,
144                                 resolvedURLs);
145 
146                 return converted;
147             }
148         }
149 
150         if ((pathTypeMask & IContentPath.CONTEXT_PATH_TYPE) > 0) {
151             if (currentPathType == IContentPath.CONTEXT_PATH_TYPE) {
152                 return resolvedURLs;
153             }
154 
155             if (currentPathType == IContentPath.ABSOLUTE_PATH_TYPE) {
156                 String converted = PathTypeTools
157                         .convertAbsolutePathToContextType(facesContext,
158                                 resolvedURLs);
159                 if (converted != null) {
160                     return converted;
161                 }
162 
163                 throw new FacesException(
164                         "Absolute path type is not into the context '"
165                                 + resolvedURLs + "'.");
166 
167             }
168         }
169 
170         if ((pathTypeMask & IContentPath.EXTERNAL_PATH_TYPE) > 0) {
171             if (currentPathType == IContentPath.EXTERNAL_PATH_TYPE) {
172                 return resolvedURLs;
173             }
174         }
175 
176         if (currentPathType == IContentPath.FILTER_PATH_TYPE) {
177             throw new FacesException("Filter '" + resolvedURLs
178                     + "' has not been processed !");
179         }
180 
181         throw new FacesException("Incompatible path type (requested=0x"
182                 + Integer.toHexString(pathTypeMask) + ", pathType=0x"
183                 + Integer.toHexString(currentPathType) + ", url='"
184                 + resolvedURL + "')");
185     }
186 
187     public Object getAttribute(String attributeName) {
188         if (parentContentAccessor != null) {
189             return parentContentAccessor.getAttribute(attributeName);
190         }
191 
192         return null;
193     }
194 
195     public Map getAttributes() {
196         if (parentContentAccessor != null) {
197             return parentContentAccessor.getAttributes();
198         }
199 
200         return Collections.EMPTY_MAP;
201     }
202 
203     public String toString() {
204         return "[AbstractContentAccessor contentType=" + contentFamily
205                 + " pathType="
206                 + AbstractContentPath.getPathTypeName(getPathType())
207                 + " versionHandler=" + contentVersionHandler + " root="
208                 + parentContentAccessor + "]";
209     }
210 }