KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > style > OutputStyleRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.style;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
38 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
39 import com.icesoft.faces.util.CoreUtils;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.w3c.dom.Element JavaDoc;
44
45 import javax.faces.component.UIComponent;
46 import javax.faces.context.FacesContext;
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import java.beans.Beans JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 /**
52  * Created by IntelliJ IDEA. User: rmayhew Date: May 30, 2006 Time: 3:59:37 PM
53  * To change this template use File | Settings | File Templates.
54  */

55 public class OutputStyleRenderer extends DomBasicRenderer {
56
57     private static Log log = LogFactory.getLog(OutputStyleRenderer.class);
58     private static final String JavaDoc IE_EXTENTION = "_ie";
59     private static final String JavaDoc IE_7_EXTENTION = "_ie7";
60     private static final String JavaDoc SAFARI_EXTENTION = "_safari";
61     private static final String JavaDoc CSS_EXTENTION = ".css";
62     private static final String JavaDoc DT_EXTENTION = "_dt";
63
64     private static final int DEFAULT_TYPE = 0;
65     private static final int IE = 1;
66     private static final int SAFARI = 2;
67     private static final int DT = 3;
68     private static final int IE_7 = 4;
69
70     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
71             throws IOException JavaDoc {
72         validateParameters(facesContext, uiComponent, OutputStyle.class);
73         try {
74             DOMContext domContext =
75                     DOMContext.attachDOMContext(facesContext, uiComponent);
76             if (!domContext.isInitialized()) {
77                 OutputStyle outputStyle = (OutputStyle) uiComponent;
78                 Element JavaDoc styleEle = buildCssElement(domContext);
79                 String JavaDoc href = outputStyle.getHref();
80                 styleEle.setAttribute(HTML.HREF_ATTR, getResourceURL(facesContext,href));
81                 domContext.setRootNode(styleEle);
82                 int browserType = browserType(facesContext, uiComponent);
83                 if (browserType != DEFAULT_TYPE) {
84                     if (href.endsWith(CSS_EXTENTION)) {
85                         int i = href.indexOf(CSS_EXTENTION);
86                         if (i > 0) {
87                             String JavaDoc start = href.substring(0, i);
88                             Element JavaDoc ieStyleEle = buildCssElement(domContext);
89                             String JavaDoc extention = IE_EXTENTION;
90                             if (browserType == SAFARI) {
91                                 extention = SAFARI_EXTENTION;
92                             }
93                             if (browserType == DT) {
94                                 extention = DT_EXTENTION;
95                             }
96                             if(browserType == IE_7){
97                                 extention = IE_7_EXTENTION;
98                             }
99                             String JavaDoc hrefURL = CoreUtils.resolveResourceURL(facesContext, start + extention + CSS_EXTENTION);
100                             ieStyleEle.setAttribute(HTML.HREF_ATTR, hrefURL);
101                             styleEle.getParentNode().appendChild(ieStyleEle);
102                         } else {
103                             throw new RuntimeException JavaDoc(
104                                     "OutputStyle file attribute is too short. " +
105                                     "Needs at least one character before .css. Current Value is [" +
106                                     href + "]");
107                         }
108                     } else {
109                         throw new RuntimeException JavaDoc(
110                                 "OutputStyle file attribute must end in .css. " +
111                                 "Current Value is [" + href + "]");
112                     }
113                 }
114
115             }
116             domContext.stepOver();
117             domContext.streamWrite(facesContext, uiComponent);
118         } catch (Exception JavaDoc e) {
119             log.error("Error in OutputStyleRenderer", e);
120         }
121     }
122
123     private Element JavaDoc buildCssElement(DOMContext domContext) {
124         Element JavaDoc styleEle = domContext.createElement("link");
125         styleEle.setAttribute(HTML.REL_ATTR, "stylesheet");
126         styleEle.setAttribute(HTML.TYPE_ATTR, "text/css");
127         return styleEle;
128     }
129
130     private int browserType(FacesContext facesContext, UIComponent uiComponent) {
131         int result = DEFAULT_TYPE;
132         String JavaDoc useragent = ((OutputStyle)uiComponent).getUserAgent();
133         if(useragent != null){
134             return _browserType(useragent);
135         }
136
137         Object JavaDoc o = facesContext.getExternalContext().getRequest();
138         if (o != null) {
139             if (o instanceof HttpServletRequest JavaDoc) {
140                 HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) o;
141                 useragent = request.getHeader("user-agent");
142                 if(useragent == null){
143                     useragent = ((OutputStyle)uiComponent).getUserAgent();
144                 }
145                 if(useragent == null){
146                     log.warn("Not able to find user agent. Returning default");
147                     return DEFAULT_TYPE;
148                 }
149                 if(((OutputStyle)uiComponent).getUserAgent() == null){
150                     ((OutputStyle)uiComponent).setUserAgent(useragent.toLowerCase());
151                 }
152                 String JavaDoc user = useragent.toLowerCase();
153                 result = _browserType( user);
154
155             } else {
156                 log.error(
157                         "OutputStyleRenderer: Request is not HttpServletRequest. Its [" +
158                         o.getClass().getName() + "]");
159             }
160         } else {
161             log.error(
162                     "IceStyleReader: facesContext.getExternalContext().getRequest() is null");
163         }
164         return result;
165     }
166
167     private int _browserType(String JavaDoc user) {
168         int result = DEFAULT_TYPE;
169         if (Beans.isDesignTime()) {
170             result = DT;
171         } else {
172             if (user.indexOf("msie") != -1) {
173                 result = IE;
174                 if(user.indexOf("msie 7") != -1){
175                     result = IE_7;
176                 }
177             } else if (user.indexOf("safari") != -1) {
178                 result = SAFARI;
179             }
180         }
181         return result;
182     }
183 }
184
Popular Tags