001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.faces.taglib;
018:
019: import org.apache.cocoon.environment.ObjectModelHelper;
020: import org.apache.cocoon.environment.Response;
021: import org.apache.cocoon.i18n.I18nUtils;
022:
023: import org.apache.cocoon.faces.FacesUtils;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026:
027: import javax.faces.application.StateManager;
028: import javax.faces.component.UIComponent;
029: import javax.faces.component.UIViewRoot;
030: import javax.faces.context.ResponseWriter;
031: import javax.faces.el.ValueBinding;
032: import java.io.IOException;
033: import java.util.Locale;
034:
035: /**
036: * @version CVS $Id: ViewTag.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class ViewTag extends UIComponentTag {
039:
040: protected String locale;
041:
042: public void setLocale(String locale) {
043: this .locale = locale;
044: }
045:
046: public int doStartTag(String namespaceURI, String localName,
047: String qName, Attributes atts) throws SAXException {
048: int rc = super .doStartTag(namespaceURI, localName, qName, atts);
049:
050: Response response = ObjectModelHelper.getResponse(objectModel);
051: response.setLocale(getFacesContext().getViewRoot().getLocale());
052:
053: ResponseWriter writer = getFacesContext().getResponseWriter();
054: try {
055: writer.startDocument();
056: } catch (IOException e) {
057: throw new SAXException(e);
058: }
059:
060: return rc;
061: }
062:
063: public int doEndTag(String namespaceURI, String localName,
064: String qName) throws SAXException {
065: int rc = super .doEndTag(namespaceURI, localName, qName);
066:
067: StateManager stateManager = getApplication().getStateManager();
068: StateManager.SerializedView view;
069: try {
070: view = stateManager.saveSerializedView(getFacesContext());
071: } catch (IllegalStateException e) {
072: throw new SAXException(e);
073: } catch (Exception e) {
074: throw new SAXException("Could not save faces view", e);
075: }
076:
077: try {
078: // TODO: Saving state on the client not supported
079: if (view != null) {
080: stateManager.writeState(getFacesContext(), view);
081: }
082: } catch (IOException e) {
083: throw new SAXException("Could not save faces view", e);
084: }
085:
086: ResponseWriter writer = getFacesContext().getResponseWriter();
087: try {
088: writer.endDocument();
089: } catch (IOException e) {
090: throw new SAXException("Exception in endDocument", e);
091: }
092:
093: return rc;
094: }
095:
096: public String getComponentType() {
097: throw new IllegalStateException();
098: }
099:
100: public String getRendererType() {
101: return null;
102: }
103:
104: protected void setProperties(UIComponent component) {
105: super .setProperties(component);
106:
107: if (this .locale != null) {
108: Object localeVal = this .locale;
109: if (FacesUtils.isExpression(this .locale)) {
110: ValueBinding vb = createValueBinding(this .locale);
111: component.setValueBinding("locale", vb);
112: localeVal = vb.getValue(getFacesContext());
113: }
114:
115: Locale value = null;
116: if (localeVal instanceof Locale) {
117: value = (Locale) localeVal;
118: } else if (localeVal instanceof String) {
119: value = I18nUtils.parseLocale((String) localeVal);
120: }
121: ((UIViewRoot) component).setLocale(value);
122: }
123: }
124:
125: public void recycle() {
126: super.recycle();
127: this.locale = null;
128: }
129: }
|