KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > ViewTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces.taglib;
17
18 import org.apache.cocoon.environment.ObjectModelHelper;
19 import org.apache.cocoon.environment.Response;
20 import org.apache.cocoon.i18n.I18nUtils;
21
22 import org.apache.cocoon.faces.FacesUtils;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import javax.faces.application.StateManager;
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.context.ResponseWriter;
30 import javax.faces.el.ValueBinding;
31 import java.io.IOException JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 /**
35  * @version CVS $Id: ViewTag.java 46253 2004-09-17 14:36:29Z vgritsenko $
36  */

37 public class ViewTag extends UIComponentTag {
38
39     protected String JavaDoc locale;
40
41     public void setLocale(String JavaDoc locale) {
42         this.locale = locale;
43     }
44
45     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
46     throws SAXException JavaDoc {
47         int rc = super.doStartTag(namespaceURI, localName, qName, atts);
48
49         Response response = ObjectModelHelper.getResponse(objectModel);
50         response.setLocale(getFacesContext().getViewRoot().getLocale());
51
52         ResponseWriter writer = getFacesContext().getResponseWriter();
53         try {
54             writer.startDocument();
55         } catch (IOException JavaDoc e) {
56             throw new SAXException JavaDoc(e);
57         }
58
59         return rc;
60     }
61
62     public int doEndTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
63     throws SAXException JavaDoc {
64         int rc = super.doEndTag(namespaceURI, localName, qName);
65
66         StateManager stateManager = getApplication().getStateManager();
67         StateManager.SerializedView view;
68         try {
69             view = stateManager.saveSerializedView(getFacesContext());
70         } catch (IllegalStateException JavaDoc e) {
71             throw new SAXException JavaDoc(e);
72         } catch (Exception JavaDoc e) {
73             throw new SAXException JavaDoc("Could not save faces view", e);
74         }
75
76         try {
77             // TODO: Saving state on the client not supported
78
if (view != null) {
79                 stateManager.writeState(getFacesContext(), view);
80             }
81         } catch (IOException JavaDoc e) {
82             throw new SAXException JavaDoc("Could not save faces view", e);
83         }
84
85         ResponseWriter writer = getFacesContext().getResponseWriter();
86         try {
87             writer.endDocument();
88         } catch (IOException JavaDoc e) {
89             throw new SAXException JavaDoc("Exception in endDocument", e);
90         }
91
92         return rc;
93     }
94
95     public String JavaDoc getComponentType() {
96         throw new IllegalStateException JavaDoc();
97     }
98
99     public String JavaDoc getRendererType() {
100         return null;
101     }
102
103     protected void setProperties(UIComponent component) {
104         super.setProperties(component);
105
106         if (this.locale != null) {
107             Object JavaDoc localeVal = this.locale;
108             if (FacesUtils.isExpression(this.locale)) {
109                 ValueBinding vb = createValueBinding(this.locale);
110                 component.setValueBinding("locale", vb);
111                 localeVal = vb.getValue(getFacesContext());
112             }
113
114             Locale JavaDoc value = null;
115             if (localeVal instanceof Locale JavaDoc) {
116                 value = (Locale JavaDoc) localeVal;
117             } else if (localeVal instanceof String JavaDoc) {
118                 value = I18nUtils.parseLocale((String JavaDoc) localeVal);
119             }
120             ((UIViewRoot) component).setLocale(value);
121         }
122     }
123
124     public void recycle() {
125         super.recycle();
126         this.locale = null;
127     }
128 }
129
Popular Tags