View Javadoc

1   /*
2    * JaspertReports JSF Plugin Copyright (C) 2011 A. Alonso Dominguez
3    *
4    * This library is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation; either version 2.1 of the License, or (at
7    * your option) any later version. This library is distributed in the hope
8    * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9    * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *
11   * See the GNU Lesser General Public License for more details. You should have
12   * received a copy of the GNU Lesser General Public License along with this
13   * library; if not, write to the Free Software Foundation, Inc., 59 Temple
14   * Place, Suite 330, Boston, MA 02111-1307 USA A.
15   *
16   * Alonso Dominguez
17   * alonsoft@users.sf.net
18   */
19  package net.sf.jasperreports.jsf.resource;
20  
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import javax.faces.context.FacesContext;
26  
27  import net.sf.jasperreports.jsf.context.ExternalContextHelper;
28  import net.sf.jasperreports.jsf.context.JRFacesContext;
29  
30  /**
31   * The Class ContextResourceLoader.
32   */
33  public final class ContextResource implements Resource {
34  
35  	private final String name;
36  	
37      /**
38       * Instantiates a new context resource loader.
39       *
40       * @param name the resource name
41       */
42      protected ContextResource(final String name) {
43      	if (name == null || name.length() == 0) {
44              throw new IllegalArgumentException("'name' can't be empty or null");
45          }
46      	this.name = name;
47      }
48  
49      public String getName() {
50      	return name;
51      }
52      
53      public String getSimpleName() {
54      	int slash = name.lastIndexOf('/');
55      	if (slash > 0) {
56      		return name.substring(slash);
57      	} else {
58      		return name;
59      	}
60      }
61      
62      /*
63       * (non-Javadoc)
64       *
65       * @see
66       * net.sf.jasperreports.jsf.util.ResourceLoader#getResource(java.lang.String
67       * )
68       */
69      public URL getLocation() throws MalformedURLException {
70          return getFacesContext().getExternalContext().getResource(getName());
71      }
72  
73      /*
74       * (non-Javadoc)
75       *
76       * @see
77       * net.sf.jasperreports.jsf.util.ResourceLoader#getResourceAsStream(java
78       * .lang.String)
79       */
80      public InputStream getInputStream() {
81          return getFacesContext()
82                  .getExternalContext().getResourceAsStream(getName());
83      }
84  
85      public String getPath() {
86          ExternalContextHelper helper = getJRFacesContext()
87                  .getExternalContextHelper(getFacesContext());
88          return helper.getResourceRealPath(
89                  getFacesContext().getExternalContext(), getName());
90      }
91  
92      public String toString() {
93      	return name;
94      }
95      
96      protected FacesContext getFacesContext() {
97          FacesContext context = FacesContext.getCurrentInstance();
98          if (context == null) {
99              throw new IllegalStateException("Context reosurces are valid just " +
100                     "inside a Faces' request.");
101         }
102         return context;
103     }
104 
105     protected JRFacesContext getJRFacesContext() {
106         return JRFacesContext.getInstance(getFacesContext());
107     }
108 
109 }