1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jasperreports.jsf.resource;
20
21 import java.io.File;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29
30 import net.sf.jasperreports.jsf.Constants;
31 import net.sf.jasperreports.jsf.component.UIReport;
32 import net.sf.jasperreports.jsf.context.ExternalContextHelper;
33 import net.sf.jasperreports.jsf.context.JRFacesContext;
34 import net.sf.jasperreports.jsf.util.Util;
35
36 import static org.apache.commons.io.FilenameUtils.*;
37
38
39
40
41
42 public final class DefaultResourceResolver implements ResourceResolver {
43
44 private static final Logger logger = Logger.getLogger(
45 DefaultResourceResolver.class.getPackage().getName(),
46 Constants.LOG_MESSAGES_BUNDLE);
47
48 public Resource resolveResource(FacesContext context, UIComponent component,
49 String name) {
50 if (name == null || name.length() == 0) {
51 throw new IllegalArgumentException(
52 "Resource name must be provided.");
53 }
54
55 Resource resource = null;
56 try {
57 final URL url = new URL(name);
58 resource = new URLResource(url);
59 } catch (MalformedURLException e) {
60 final ClassLoader loader = Util.getClassLoader(this);
61 if (name.startsWith(ClasspathResource.PREFIX)) {
62 resource = new ClasspathResource(name, loader);
63 } else if (name.startsWith("/")) {
64 resource = new ContextResource(name);
65 } else if (loader.getResource(name) != null) {
66 resource = new ClasspathResource(name, loader);
67 } else {
68 resource = resolveRelative(context, component, name);
69 }
70 }
71
72
73
74 return resource;
75 }
76
77 private Resource resolveRelative(FacesContext context,
78 UIComponent component, String name) {
79 if (logger.isLoggable(Level.FINE)) {
80 logger.log(Level.FINE, "JRJSF_0039", name);
81 }
82
83 Resource resource = null;
84 JRFacesContext jrContext = JRFacesContext.getInstance(context);
85 ExternalContextHelper helper =
86 jrContext.getExternalContextHelper(context);
87 String rootPath = null;
88
89 if ((component != null) && (component instanceof UIReport)) {
90
91
92
93 Object value = ((UIReport) component).getValue();
94 if (value != null && (value instanceof String)) {
95 String valueStr = (String) value;
96 if (!valueStr.equals(name)) {
97
98
99
100 rootPath = helper.getResourceRealPath(
101 context.getExternalContext(),
102 "/" + getPath((String) value));
103 }
104 }
105 }
106
107 if (rootPath == null) {
108
109
110
111
112 String viewId = context.getViewRoot().getViewId();
113 rootPath = helper.getResourceRealPath(
114 context.getExternalContext(), "/" + getPath(viewId));
115 }
116
117 if (rootPath != null) {
118 String resourceFileName = rootPath + File.separator + name;
119 File resourceFile = new File(normalize(resourceFileName));
120 if (resourceFile.exists()) {
121 resource = new FileResource(resourceFile);
122 }
123 }
124
125 return resource;
126 }
127
128 }