View Javadoc

1   /*
2    * $Id: AbstractContentPath.java,v 1.1 2010/03/19 14:53:48 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.contentAccessor;
5   
6   import javax.faces.context.FacesContext;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.rcfaces.core.internal.util.PathTypeTools;
11  
12  /**
13   * 
14   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
15   * @version $Revision: 1.1 $ $Date: 2010/03/19 14:53:48 $
16   */
17  public abstract class AbstractContentPath implements IContentPath {
18      private static final Log LOG = LogFactory.getLog(AbstractContentPath.class);
19  
20      private final IContentPath parentContentPath;
21  
22      private int pathType;
23  
24      public AbstractContentPath(IContentPath parent) {
25          this.parentContentPath = parent;
26      }
27  
28      public int getPathType() {
29          if (pathType > 0) {
30              return pathType;
31          }
32  
33          if (parentContentPath != null) {
34              return parentContentPath.getPathType();
35          }
36  
37          return 0;
38      }
39  
40      public void setPathType(int pathType) {
41          this.pathType = pathType;
42      }
43  
44      public IContentPath getParentContentPath() {
45          return parentContentPath;
46      }
47  
48      public String convertToPathType(FacesContext facesContext,
49              int targetPathType) {
50          switch (targetPathType) {
51          case IContentPath.CONTEXT_PATH_TYPE:
52              return convertToContextPathType(facesContext);
53          }
54  
55          return null;
56      }
57  
58      private String convertToContextPathType(FacesContext facesContext) {
59          String path = getPath();
60  
61          String relativePath;
62  
63          switch (getPathType()) {
64          case IContentPath.EXTERNAL_PATH_TYPE:
65              LOG.error("Can not make operation on an external URL ! (" + path
66                      + ")");
67              return null;
68  
69          case IContentPath.CONTEXT_PATH_TYPE:
70              return path;
71  
72          case IContentPath.ABSOLUTE_PATH_TYPE:
73              relativePath = PathTypeTools.convertAbsolutePathToContextType(
74                      facesContext, path);
75  
76              if (relativePath == null) {
77                  LOG.error("Can not transform Absolute path to Context path ! ("
78                          + path + ")");
79                  return null;
80              }
81  
82              return relativePath;
83  
84          case IContentPath.RELATIVE_PATH_TYPE:
85              relativePath = PathTypeTools.convertRelativePathToContextPath(
86                      facesContext, path, null);
87  
88              if (relativePath == null) {
89                  LOG.error("Can not transform Relative path to Context path ! ("
90                          + path + ")");
91                  return null;
92              }
93  
94              return relativePath;
95          }
96  
97          LOG.error("Invalid state ! (" + path + ")");
98          return null;
99      }
100 
101     public static final String getPathTypeName(int pathType) {
102         switch (pathType) {
103         case IContentPath.UNDEFINED_PATH_TYPE:
104             return "undefined";
105 
106         case IContentPath.ABSOLUTE_PATH_TYPE:
107             return "absolute";
108 
109         case IContentPath.CONTEXT_PATH_TYPE:
110             return "context";
111 
112         case IContentPath.EXTERNAL_PATH_TYPE:
113             return "external";
114 
115         case IContentPath.RELATIVE_PATH_TYPE:
116             return "relative";
117 
118         case IContentPath.FILTER_PATH_TYPE:
119             return "filter";
120         }
121 
122         return "*** Invalid (" + pathType + ") ***";
123     }
124 
125 }