001: // WARNING: This file was automatically generated. Do not edit it directly,
002: // or you will lose your changes.
003:
004: /*
005: * Licensed to the Apache Software Foundation (ASF) under one
006: * or more contributor license agreements. See the NOTICE file
007: * distributed with this work for additional information
008: * regarding copyright ownership. The ASF licenses this file
009: * to you under the Apache License, Version 2.0 (the
010: * "License"); you may not use this file except in compliance
011: * with the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing,
016: * software distributed under the License is distributed on an
017: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018: * KIND, either express or implied. See the License for the
019: * specific language governing permissions and limitations
020: * under the License.
021: */
022: package javax.faces.component;
023:
024: import java.util.*;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import javax.el.MethodExpression;
030: import javax.el.ValueExpression;
031: import javax.faces.FactoryFinder;
032: import javax.faces.context.ExternalContext;
033: import javax.faces.context.FacesContext;
034: import javax.faces.event.*;
035: import javax.faces.event.FacesEvent;
036: import javax.faces.event.PhaseListener;
037: import javax.faces.lifecycle.Lifecycle;
038: import javax.faces.lifecycle.LifecycleFactory;
039: import javax.faces.webapp.FacesServlet;
040:
041: /**
042: */
043: public class UIViewRoot extends UIComponentBase {
044:
045: static public final String COMPONENT_FAMILY = "javax.faces.ViewRoot";
046: static public final String COMPONENT_TYPE = "javax.faces.ViewRoot";
047:
048: /**
049: * Construct an instance of the UIViewRoot.
050: */
051: public UIViewRoot() {
052: setRendererType(null);
053: }
054:
055: private static final int ANY_PHASE_ORDINAL = PhaseId.ANY_PHASE
056: .getOrdinal();
057: public static final String UNIQUE_ID_PREFIX = "j_id";
058:
059: private final Logger logger = Logger.getLogger(UIViewRoot.class
060: .getName());
061:
062: // todo: is it right to save the state of _events and _phaseListeners?
063:
064: private transient Lifecycle _lifecycle = null;
065:
066: public void queueEvent(FacesEvent event) {
067: checkNull(event, "event");
068: if (_events == null) {
069: _events = new ArrayList<FacesEvent>();
070: }
071: _events.add(event);
072: }
073:
074: public void processDecodes(final FacesContext context) {
075: checkNull(context, "context");
076: process(context, PhaseId.APPLY_REQUEST_VALUES, new Processor() {
077: public void process() {
078: UIViewRoot.super .processDecodes(context);
079: }
080: }, true);
081: }
082:
083: public void processValidators(final FacesContext context) {
084: checkNull(context, "context");
085: process(context, PhaseId.PROCESS_VALIDATIONS, new Processor() {
086: public void process() {
087: UIViewRoot.super .processValidators(context);
088: }
089: }, true);
090: }
091:
092: public void processUpdates(final FacesContext context) {
093: checkNull(context, "context");
094: process(context, PhaseId.UPDATE_MODEL_VALUES, new Processor() {
095: public void process() {
096: UIViewRoot.super .processUpdates(context);
097: }
098: }, true);
099: }
100:
101: public void processApplication(final FacesContext context) {
102: checkNull(context, "context");
103: process(context, PhaseId.INVOKE_APPLICATION, null, true);
104: }
105:
106: public void encodeBegin(FacesContext context)
107: throws java.io.IOException {
108: checkNull(context, "context");
109:
110: boolean skipPhase = false;
111:
112: try {
113: skipPhase = notifyListeners(context,
114: PhaseId.RENDER_RESPONSE, getBeforePhaseListener(),
115: true);
116: } catch (Exception e) {
117: // following the spec we have to swallow the exception
118: logger.log(Level.SEVERE,
119: "Exception while processing phase listener: "
120: + e.getMessage(), e);
121: }
122:
123: if (!skipPhase) {
124: super .encodeBegin(context);
125: }
126: }
127:
128: public void encodeEnd(FacesContext context)
129: throws java.io.IOException {
130: checkNull(context, "context");
131: super .encodeEnd(context);
132: try {
133: notifyListeners(context, PhaseId.RENDER_RESPONSE,
134: getAfterPhaseListener(), false);
135: } catch (Exception e) {
136: // following the spec we have to swallow the exception
137: logger.log(Level.SEVERE,
138: "Exception while processing phase listener: "
139: + e.getMessage(), e);
140: }
141: }
142:
143: /*
144: * Provides a unique id for this component instance.
145: */
146: public String createUniqueId() {
147: ExternalContext extCtx = FacesContext.getCurrentInstance()
148: .getExternalContext();
149: return extCtx.encodeNamespace(UNIQUE_ID_PREFIX
150: + _uniqueIdCounter++);
151: }
152:
153: /**
154: * Gets The locale for this ViewRoot.
155: *
156: * @return the new locale value
157: */
158: public Locale getLocale() {
159: if (_locale != null) {
160: return _locale;
161: }
162: ValueExpression expression = getValueExpression("locale");
163: if (expression != null) {
164: return (Locale) expression.getValue(getFacesContext()
165: .getELContext());
166: } else {
167: Object locale = getFacesContext().getApplication()
168: .getViewHandler()
169: .calculateLocale(getFacesContext());
170:
171: if (locale instanceof Locale) {
172: return (Locale) locale;
173: } else if (locale instanceof String) {
174: return stringToLocale((String) locale);
175: }
176: }
177:
178: return getFacesContext().getApplication().getViewHandler()
179: .calculateLocale(getFacesContext());
180: }
181:
182: private boolean process(FacesContext context, PhaseId phaseId,
183: Processor processor, boolean broadcast) {
184: if (!notifyListeners(context, phaseId,
185: getBeforePhaseListener(), true)) {
186: if (processor != null)
187: processor.process();
188:
189: if (broadcast) {
190: _broadcastForPhase(phaseId);
191: if (context.getRenderResponse()
192: || context.getResponseComplete()) {
193: clearEvents();
194: }
195: }
196: }
197: return notifyListeners(context, phaseId,
198: getAfterPhaseListener(), false);
199: }
200:
201: private boolean notifyListeners(FacesContext context,
202: PhaseId phaseId, MethodExpression listener,
203: boolean beforePhase) {
204: boolean skipPhase = false;
205:
206: if (listener != null
207: || (_phaseListeners != null && !_phaseListeners
208: .isEmpty())) {
209: PhaseEvent event = createEvent(context, phaseId);
210:
211: if (listener != null) {
212: listener.invoke(context.getELContext(),
213: new Object[] { event });
214: skipPhase = context.getResponseComplete()
215: || context.getRenderResponse();
216: }
217:
218: if (_phaseListeners != null && !_phaseListeners.isEmpty()) {
219: for (PhaseListener phaseListener : _phaseListeners) {
220: PhaseId listenerPhaseId = phaseListener
221: .getPhaseId();
222: if (phaseId.equals(listenerPhaseId)
223: || PhaseId.ANY_PHASE
224: .equals(listenerPhaseId)) {
225: if (beforePhase) {
226: phaseListener.beforePhase(event);
227: } else {
228: phaseListener.afterPhase(event);
229: }
230: skipPhase = context.getResponseComplete()
231: || context.getRenderResponse();
232: }
233: }
234: }
235: }
236:
237: return skipPhase;
238: }
239:
240: private PhaseEvent createEvent(FacesContext context, PhaseId phaseId) {
241: if (_lifecycle == null) {
242: LifecycleFactory factory = (LifecycleFactory) FactoryFinder
243: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
244: String id = context.getExternalContext().getInitParameter(
245: FacesServlet.LIFECYCLE_ID_ATTR);
246: if (id == null) {
247: id = LifecycleFactory.DEFAULT_LIFECYCLE;
248: }
249: _lifecycle = factory.getLifecycle(id);
250: }
251: return new PhaseEvent(context, phaseId, _lifecycle);
252: }
253:
254: private void _broadcastForPhase(PhaseId phaseId) {
255: if (_events == null) {
256: return;
257: }
258:
259: boolean abort = false;
260:
261: int phaseIdOrdinal = phaseId.getOrdinal();
262: for (ListIterator<FacesEvent> listiterator = _events
263: .listIterator(); listiterator.hasNext();) {
264: FacesEvent event = listiterator.next();
265: int ordinal = event.getPhaseId().getOrdinal();
266: if (ordinal == ANY_PHASE_ORDINAL
267: || ordinal == phaseIdOrdinal) {
268: UIComponent source = event.getComponent();
269: try {
270: source.broadcast(event);
271: } catch (AbortProcessingException e) {
272: // abort event processing
273: // Page 3-30 of JSF 1.1 spec: "Throw an AbortProcessingException, to tell the JSF implementation
274: // that no further broadcast of this event, or any further events, should take place."
275: abort = true;
276: break;
277: } finally {
278: try {
279: listiterator.remove();
280: } catch (ConcurrentModificationException cme) {
281: int eventIndex = listiterator.previousIndex();
282: _events.remove(eventIndex);
283: listiterator = _events.listIterator();
284: }
285: }
286: }
287: }
288:
289: if (abort) {
290: // TODO: abort processing of any event of any phase or just of any event of the current phase???
291: clearEvents();
292: }
293: }
294:
295: private void clearEvents() {
296: _events = null;
297: }
298:
299: private void checkNull(Object value, String valueLabel) {
300: if (value == null) {
301: throw new NullPointerException(valueLabel + " is null");
302: }
303: }
304:
305: private interface Processor {
306: void process();
307: }
308:
309: private Locale stringToLocale(String localeStr) {
310: // locale expr: \[a-z]{2}((-|_)[A-Z]{2})?
311:
312: if (localeStr.contains("_") || localeStr.contains("-")) {
313: if (localeStr.length() == 2) {
314: // localeStr is the lang
315: return new Locale(localeStr);
316: }
317: } else {
318: if (localeStr.length() == 5) {
319: String lang = localeStr.substring(0, 1);
320: String country = localeStr.substring(3, 4);
321: return new Locale(lang, country);
322: }
323: }
324:
325: return Locale.getDefault();
326: }
327:
328: // Property: locale
329: private Locale _locale;
330:
331: /**
332: * Sets The locale for this ViewRoot.
333: *
334: * @param locale the new locale value
335: */
336: public void setLocale(Locale locale) {
337: this ._locale = locale;
338: }
339:
340: // Property: renderKitId
341: private String _renderKitId;
342:
343: /**
344: * Gets The initial value of this component.
345: *
346: * @return the new renderKitId value
347: */
348: public String getRenderKitId() {
349: if (_renderKitId != null) {
350: return _renderKitId;
351: }
352: ValueExpression expression = getValueExpression("renderKitId");
353: if (expression != null) {
354: return (String) expression.getValue(getFacesContext()
355: .getELContext());
356: }
357: return null;
358: }
359:
360: /**
361: * Sets The initial value of this component.
362: *
363: * @param renderKitId the new renderKitId value
364: */
365: public void setRenderKitId(String renderKitId) {
366: this ._renderKitId = renderKitId;
367: }
368:
369: // Property: viewId
370: private String _viewId;
371:
372: /**
373: * Gets The viewId.
374: *
375: * @return the new viewId value
376: */
377: public String getViewId() {
378: return _viewId;
379: }
380:
381: /**
382: * Sets The viewId.
383: *
384: * @param viewId the new viewId value
385: */
386: public void setViewId(String viewId) {
387: this ._viewId = viewId;
388: }
389:
390: // Property: events
391: private List<FacesEvent> _events;
392:
393: // Property: uniqueIdCounter
394: private long _uniqueIdCounter = 0;
395:
396: // Property: phaseListeners
397: private List<PhaseListener> _phaseListeners;
398:
399: /**
400: * Adds a The phaseListeners attached to ViewRoot.
401: */
402: public void addPhaseListener(PhaseListener phaseListener) {
403: if (phaseListener == null)
404: throw new NullPointerException("phaseListener");
405: if (_phaseListeners == null)
406: _phaseListeners = new ArrayList<PhaseListener>();
407:
408: _phaseListeners.add(phaseListener);
409: }
410:
411: /**
412: * Removes a The phaseListeners attached to ViewRoot.
413: */
414: public void removePhaseListener(PhaseListener phaseListener) {
415: if (phaseListener == null || _phaseListeners == null)
416: return;
417:
418: _phaseListeners.remove(phaseListener);
419: }
420:
421: // Property: beforePhaseListener
422: private MethodExpression _beforePhaseListener;
423:
424: /**
425: * Gets
426: *
427: * @return the new beforePhaseListener value
428: */
429: public MethodExpression getBeforePhaseListener() {
430: if (_beforePhaseListener != null) {
431: return _beforePhaseListener;
432: }
433: ValueExpression expression = getValueExpression("beforePhaseListener");
434: if (expression != null) {
435: return (MethodExpression) expression
436: .getValue(getFacesContext().getELContext());
437: }
438: return null;
439: }
440:
441: /**
442: * Sets
443: *
444: * @param beforePhaseListener the new beforePhaseListener value
445: */
446: public void setBeforePhaseListener(
447: MethodExpression beforePhaseListener) {
448: this ._beforePhaseListener = beforePhaseListener;
449: }
450:
451: // Property: afterPhaseListener
452: private MethodExpression _afterPhaseListener;
453:
454: /**
455: * Gets
456: *
457: * @return the new afterPhaseListener value
458: */
459: public MethodExpression getAfterPhaseListener() {
460: if (_afterPhaseListener != null) {
461: return _afterPhaseListener;
462: }
463: ValueExpression expression = getValueExpression("afterPhaseListener");
464: if (expression != null) {
465: return (MethodExpression) expression
466: .getValue(getFacesContext().getELContext());
467: }
468: return null;
469: }
470:
471: /**
472: * Sets
473: *
474: * @param afterPhaseListener the new afterPhaseListener value
475: */
476: public void setAfterPhaseListener(
477: MethodExpression afterPhaseListener) {
478: this ._afterPhaseListener = afterPhaseListener;
479: }
480:
481: @Override
482: public Object saveState(FacesContext facesContext) {
483: Object[] values = new Object[10];
484: values[0] = super .saveState(facesContext);
485: values[1] = _locale;
486: values[2] = _renderKitId;
487: values[3] = _viewId;
488: values[4] = saveAttachedState(facesContext, _events);
489: values[5] = _uniqueIdCounter;
490: values[7] = saveAttachedState(facesContext, _phaseListeners);
491: values[8] = saveAttachedState(facesContext,
492: _beforePhaseListener);
493: values[9] = saveAttachedState(facesContext, _afterPhaseListener);
494:
495: return values;
496: }
497:
498: @Override
499: public void restoreState(FacesContext facesContext, Object state) {
500: Object[] values = (Object[]) state;
501: super .restoreState(facesContext, values[0]);
502: _locale = (Locale) values[1];
503: _renderKitId = (String) values[2];
504: _viewId = (String) values[3];
505: _events = (List) restoreAttachedState(facesContext, values[4]);
506: _uniqueIdCounter = (Long) values[5];
507: _phaseListeners = (List) restoreAttachedState(facesContext,
508: values[7]);
509: _beforePhaseListener = (MethodExpression) restoreAttachedState(
510: facesContext, values[8]);
511: _afterPhaseListener = (MethodExpression) restoreAttachedState(
512: facesContext, values[9]);
513: }
514:
515: @Override
516: public String getFamily() {
517: return COMPONENT_FAMILY;
518: }
519: }
|