1 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 ; 24 import org.xml.sax.SAXException ; 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 ; 32 import java.util.Locale ; 33 34 37 public class ViewTag extends UIComponentTag { 38 39 protected String locale; 40 41 public void setLocale(String locale) { 42 this.locale = locale; 43 } 44 45 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) 46 throws SAXException { 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 e) { 56 throw new SAXException (e); 57 } 58 59 return rc; 60 } 61 62 public int doEndTag(String namespaceURI, String localName, String qName) 63 throws SAXException { 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 e) { 71 throw new SAXException (e); 72 } catch (Exception e) { 73 throw new SAXException ("Could not save faces view", e); 74 } 75 76 try { 77 if (view != null) { 79 stateManager.writeState(getFacesContext(), view); 80 } 81 } catch (IOException e) { 82 throw new SAXException ("Could not save faces view", e); 83 } 84 85 ResponseWriter writer = getFacesContext().getResponseWriter(); 86 try { 87 writer.endDocument(); 88 } catch (IOException e) { 89 throw new SAXException ("Exception in endDocument", e); 90 } 91 92 return rc; 93 } 94 95 public String getComponentType() { 96 throw new IllegalStateException (); 97 } 98 99 public String getRendererType() { 100 return null; 101 } 102 103 protected void setProperties(UIComponent component) { 104 super.setProperties(component); 105 106 if (this.locale != null) { 107 Object 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 value = null; 115 if (localeVal instanceof Locale ) { 116 value = (Locale ) localeVal; 117 } else if (localeVal instanceof String ) { 118 value = I18nUtils.parseLocale((String ) 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 |