001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.style;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
038: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
039: import com.icesoft.faces.util.CoreUtils;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.w3c.dom.Element;
044:
045: import javax.faces.component.UIComponent;
046: import javax.faces.context.FacesContext;
047: import javax.servlet.http.HttpServletRequest;
048: import java.beans.Beans;
049: import java.io.IOException;
050:
051: /**
052: * Created by IntelliJ IDEA. User: rmayhew Date: May 30, 2006 Time: 3:59:37 PM
053: * To change this template use File | Settings | File Templates.
054: */
055: public class OutputStyleRenderer extends DomBasicRenderer {
056:
057: private static Log log = LogFactory
058: .getLog(OutputStyleRenderer.class);
059: private static final String IE_EXTENTION = "_ie";
060: private static final String IE_7_EXTENTION = "_ie7";
061: private static final String SAFARI_EXTENTION = "_safari";
062: private static final String CSS_EXTENTION = ".css";
063: private static final String DT_EXTENTION = "_dt";
064:
065: private static final int DEFAULT_TYPE = 0;
066: private static final int IE = 1;
067: private static final int SAFARI = 2;
068: private static final int DT = 3;
069: private static final int IE_7 = 4;
070:
071: public void encodeEnd(FacesContext facesContext,
072: UIComponent uiComponent) throws IOException {
073: validateParameters(facesContext, uiComponent, OutputStyle.class);
074: try {
075: DOMContext domContext = DOMContext.attachDOMContext(
076: facesContext, uiComponent);
077: if (!domContext.isInitialized()) {
078: OutputStyle outputStyle = (OutputStyle) uiComponent;
079: Element styleEle = buildCssElement(domContext);
080: String href = outputStyle.getHref();
081: styleEle.setAttribute(HTML.HREF_ATTR, getResourceURL(
082: facesContext, href));
083: domContext.setRootNode(styleEle);
084: int browserType = browserType(facesContext, uiComponent);
085: if (browserType != DEFAULT_TYPE) {
086: if (href.endsWith(CSS_EXTENTION)) {
087: int i = href.indexOf(CSS_EXTENTION);
088: if (i > 0) {
089: String start = href.substring(0, i);
090: Element ieStyleEle = buildCssElement(domContext);
091: String extention = IE_EXTENTION;
092: if (browserType == SAFARI) {
093: extention = SAFARI_EXTENTION;
094: }
095: if (browserType == DT) {
096: extention = DT_EXTENTION;
097: }
098: if (browserType == IE_7) {
099: extention = IE_7_EXTENTION;
100: }
101: String hrefURL = CoreUtils
102: .resolveResourceURL(facesContext,
103: start + extention
104: + CSS_EXTENTION);
105: ieStyleEle.setAttribute(HTML.HREF_ATTR,
106: hrefURL);
107: styleEle.getParentNode().appendChild(
108: ieStyleEle);
109: } else {
110: throw new RuntimeException(
111: "OutputStyle file attribute is too short. "
112: + "Needs at least one character before .css. Current Value is ["
113: + href + "]");
114: }
115: } else {
116: throw new RuntimeException(
117: "OutputStyle file attribute must end in .css. "
118: + "Current Value is [" + href
119: + "]");
120: }
121: }
122:
123: }
124: domContext.stepOver();
125: domContext.streamWrite(facesContext, uiComponent);
126: } catch (Exception e) {
127: log.error("Error in OutputStyleRenderer", e);
128: }
129: }
130:
131: private Element buildCssElement(DOMContext domContext) {
132: Element styleEle = domContext.createElement("link");
133: styleEle.setAttribute(HTML.REL_ATTR, "stylesheet");
134: styleEle.setAttribute(HTML.TYPE_ATTR, "text/css");
135: return styleEle;
136: }
137:
138: private int browserType(FacesContext facesContext,
139: UIComponent uiComponent) {
140: int result = DEFAULT_TYPE;
141: String useragent = ((OutputStyle) uiComponent).getUserAgent();
142: if (useragent != null) {
143: return _browserType(useragent);
144: }
145:
146: Object o = facesContext.getExternalContext().getRequest();
147: if (o != null) {
148: if (o instanceof HttpServletRequest) {
149: HttpServletRequest request = (HttpServletRequest) o;
150: useragent = request.getHeader("user-agent");
151: if (useragent == null) {
152: useragent = ((OutputStyle) uiComponent)
153: .getUserAgent();
154: }
155: if (useragent == null) {
156: if (log.isDebugEnabled()) {
157: log
158: .debug("Not able to find user agent. Returning default");
159: }
160: return DEFAULT_TYPE;
161: }
162: if (((OutputStyle) uiComponent).getUserAgent() == null) {
163: ((OutputStyle) uiComponent).setUserAgent(useragent
164: .toLowerCase());
165: }
166: String user = useragent.toLowerCase();
167: result = _browserType(user);
168:
169: } else {
170: if (log.isDebugEnabled()) {
171: log
172: .debug("OutputStyleRenderer: Request is not HttpServletRequest. Its ["
173: + o.getClass().getName() + "]");
174: }
175: }
176: } else {
177: if (log.isDebugEnabled()) {
178: log
179: .debug("IceStyleReader: facesContext.getExternalContext().getRequest() is null");
180: }
181: }
182: return result;
183: }
184:
185: private int _browserType(String user) {
186: int result = DEFAULT_TYPE;
187: if (Beans.isDesignTime()) {
188: result = DT;
189: } else {
190: if (user.indexOf("msie") != -1) {
191: result = IE;
192: if (user.indexOf("msie 7") != -1) {
193: result = IE_7;
194: }
195: } else if (user.indexOf("safari") != -1) {
196: result = SAFARI;
197: }
198: }
199: return result;
200: }
201: }
|