Author: alexsmirnov
Date: 2007-06-29 13:25:00 -0400 (Fri, 29 Jun 2007)
New Revision: 1416
Added:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContext.java
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java
Removed:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java
branches/refactor1/framework/api/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java
branches/refactor1/framework/api/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java
Modified:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/resource/InternetResourceBuilder.java
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/skin/SkinFactory.java
Log:
continue refactoring
Added:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContext.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContext.java
(rev 0)
+++
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContext.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -0,0 +1,114 @@
+package org.ajax4jsf.framework.ajax;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.el.VariableResolver;
+
+public abstract class AjaxContext {
+
+ /**
+ * Key for keep request state information in request-scope attributes.
+ */
+ public static final String AJAX_CONTEXT_KEY = "ajaxContext";
+
+ public abstract Map getCommonAjaxParameters();
+
+ public abstract String getAjaxActionURL(FacesContext context);
+
+ public abstract String getAjaxActionURL();
+
+ public abstract void setResponseData(Object responseData);
+
+ public abstract Object getResponseData();
+
+ public abstract void setViewIdHolder(ViewIdHolder viewIdHolder);
+
+ public abstract ViewIdHolder getViewIdHolder();
+
+ public abstract boolean removeRenderedArea(String id);
+
+ public abstract void addRenderedArea(String id);
+
+ public abstract Set getAjaxRenderedAreas();
+
+ public abstract void addComponentToAjaxRender(UIComponent base, String id);
+
+ public abstract void addComponentToAjaxRender(UIComponent component);
+
+ public abstract void addRegionsFromComponent(UIComponent component);
+
+ public abstract Set getAjaxAreasToRender();
+
+ public abstract boolean isAjaxRequest(FacesContext context);
+
+ public abstract boolean isAjaxRequest();
+
+ public abstract void processHeadResources(FacesContext context)
+ throws FacesException;
+
+ public abstract void encodeAjaxEnd(FacesContext context, UIComponent component)
+ throws IOException;
+
+ public abstract void encodeAjaxBegin(FacesContext context, UIComponent component)
+ throws IOException;
+
+ public abstract void renderAjaxRegion(FacesContext context, UIComponent component,
+ boolean useFilterWriter) throws FacesException;
+
+ public abstract void renderSubmittedAjaxRegion(FacesContext context, final boolean
useFilterWriter);
+
+ public abstract void renderSubmittedAjaxRegion(FacesContext context);
+
+ public static final String SCRIPTS_PARAMETER =
"org.ajax4jsf.framework.HEADER_SCRIPTS";
+ public static final String STYLES_PARAMETER =
"org.ajax4jsf.framework.HEADER_STYLES";
+ public static final String RESPONSE_DATA_KEY = "_ajax:data";
+
+ /**
+ * Get instance of current AJAX Context. Instance get by
+ * {@link VariableResolver#resolveVariable(FacesContext, String)} for
+ * variable {@link AjaxContext#AJAX_CONTEXT_KEY}
+ *
+ * @return memento instance for current request
+ */
+ public static AjaxContext getCurrentInstance() {
+ FacesContext context = FacesContext.getCurrentInstance();
+ return getCurrentInstance(context);
+ }
+
+ /**
+ * Get instance of current AJAX Context. Instance get by
+ * {@link VariableResolver#resolveVariable(FacesContext, String)} for
+ * variable {@link AjaxContext#AJAX_CONTEXT_KEY}
+ *
+ * @param context
+ * current FacesContext
+ * @return instance of AjaxContext.
+ */
+ public static AjaxContext getCurrentInstance(FacesContext context) {
+ if (null == context) {
+ throw new NullPointerException("FacesContext is null");
+ }
+ AjaxContext ajaxContext = (AjaxContext) context.getApplication()
+ .getVariableResolver().resolveVariable(context,
+ AJAX_CONTEXT_KEY);
+ if (null == ajaxContext) {
+ // TODO Create default implementation.
+// ajaxContext = new AjaxContext();
+// context.getExternalContext().getRequestMap().put(AJAX_CONTEXT_KEY,
+// ajaxContext);
+ }
+ return ajaxContext;
+ }
+
+ public AjaxContext() {
+ super();
+ }
+
+ public abstract Map getResponseDataMap();
+
+}
\ No newline at end of file
Deleted:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -1,806 +0,0 @@
-/**
- * License Agreement.
- *
- * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.ajax4jsf.framework.ajax;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
-
-import javax.faces.FacesException;
-import javax.faces.FactoryFinder;
-import javax.faces.application.Application;
-import javax.faces.application.StateManager;
-import javax.faces.application.ViewHandler;
-import javax.faces.application.StateManager.SerializedView;
-import javax.faces.component.NamingContainer;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-import javax.faces.context.ResponseWriter;
-import javax.faces.el.VariableResolver;
-import javax.faces.event.AbortProcessingException;
-import javax.faces.render.RenderKit;
-import javax.faces.render.RenderKitFactory;
-import javax.faces.render.Renderer;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-
-import org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter;
-import org.ajax4jsf.framework.ajax.xmlfilter.FilterServletResponseWrapper;
-import org.ajax4jsf.framework.renderer.AjaxContainerRenderer;
-import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
-import org.ajax4jsf.framework.renderer.HeaderResourceProducer;
-import org.ajax4jsf.framework.renderer.RendererUtils.HTML;
-import org.ajax4jsf.framework.skin.Skin;
-import org.ajax4jsf.framework.skin.SkinFactory;
-import org.ajax4jsf.framework.skin.SkinNotFoundException;
-import org.ajax4jsf.framework.util.config.WebXml;
-import org.ajax4jsf.framework.util.message.Messages;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- * This class incapsulated
- *
- * @author asmirnov(a)exadel.com (latest modification by $Author: alexsmirnov $)
- * @version $Revision: 1.1.2.7 $ $Date: 2007/02/08 19:07:16 $
- *
- */
-public class AjaxContext {
- /**
- * Key for keep request state information in request-scope attributes.
- */
- public static final String AJAX_CONTEXT_KEY = "ajaxContext";
-
- public static final String SCRIPTS_PARAMETER =
"org.ajax4jsf.framework.HEADER_SCRIPTS";
-
- public static final String STYLES_PARAMETER =
"org.ajax4jsf.framework.HEADER_STYLES";
-
- public static final String RESOURCES_PROCESSED =
"org.ajax4jsf.framework.HEADER_PROCESSED";
-
- public static final String RESPONSE_DATA_KEY = "_ajax:data";
-
- private static final Log log = LogFactory.getLog(AjaxContext.class);
-
- private static ComponentInvoker invoker;
-
- private static Map contextClasses = new HashMap();
-
- Set ajaxAreasToRender = new HashSet();
-
- Set ajaxRenderedAreas = new HashSet();
-
- boolean ajaxRequest = false;
-
- boolean ajaxRequestSet = false;
-
- boolean selfRender = false;
-
- Integer viewSequence = new Integer(1);
-
- String submittedRegionClientId = null;
-
- boolean submittedRegionSet = false;
-
- ViewIdHolder viewIdHolder = null;
-
- Map responseDataMap = new HashMap();
-
- Map commonAjaxParameters = new HashMap();
-
- static {
- try {
- // Attempt to create JSF1.2 specific invoker.
- invoker = new JsfOneOneInvoker();
- } catch (Exception e) {
- invoker = new JsfOneOneInvoker();
- }
- }
-
- /**
- * Get instance of current AJAX Context. Instance get by
- * {@link VariableResolver#resolveVariable(FacesContext, String)} for
- * variable {@link AjaxContext#AJAX_CONTEXT_KEY}
- *
- * @return memento instance for current request
- */
- public static AjaxContext getCurrentInstance() {
- FacesContext context = FacesContext.getCurrentInstance();
- return getCurrentInstance(context);
- }
-
- /**
- * Get instance of current AJAX Context. Instance get by
- * {@link VariableResolver#resolveVariable(FacesContext, String)} for
- * variable {@link AjaxContext#AJAX_CONTEXT_KEY}
- *
- * @param context
- * current FacesContext
- * @return instance of AjaxContext.
- */
- public static AjaxContext getCurrentInstance(FacesContext context) {
- if (null == context) {
- throw new NullPointerException("FacesContext is null");
- }
- AjaxContext ajaxContext = (AjaxContext) context.getApplication()
- .getVariableResolver().resolveVariable(context,
- AJAX_CONTEXT_KEY);
- if (null == ajaxContext) {
- // Create default implementation.
- ajaxContext = new AjaxContext();
- context.getExternalContext().getRequestMap().put(AJAX_CONTEXT_KEY,
- ajaxContext);
- }
- return ajaxContext;
- }
-
- /**
- * @param root
- * @param context
- * @param callback
- * @param regionId
- * @return
- * @see
org.ajax4jsf.framework.ajax.JsfOneOneInvoker#invokeOnComponent(javax.faces.component.UIComponent,
- * javax.faces.context.FacesContext,
- * org.ajax4jsf.framework.ajax.InvokerCallback, java.lang.String)
- */
- public static boolean invokeOnComponent(UIComponent root,
- FacesContext context, InvokerCallback callback, String regionId) {
- return invoker.invokeOnComponent(root, context, callback, regionId);
- }
-
- /**
- * @param viewRoot
- * @param context
- * @param callback
- * @see
org.ajax4jsf.framework.ajax.JsfOneOneInvoker#invokeOnRegionOrRoot(org.ajax4jsf.framework.ajax.AjaxViewRoot,
- * javax.faces.context.FacesContext,
- * org.ajax4jsf.framework.ajax.InvokerCallback)
- */
- public static void invokeOnRegionOrRoot(AjaxViewRoot viewRoot,
- FacesContext context, InvokerCallback callback) {
- invoker.invokeOnRegionOrRoot(viewRoot, context, callback);
- }
-
- private InvokerCallback _ajaxInvoker = new InvokerCallback() {
-
- public void invoke(FacesContext context, UIComponent component) {
- if (component instanceof AjaxContainer) {
- AjaxContainer ajax = (AjaxContainer) component;
- renderAjaxRegion(context, component, true);
- } else {
- // Container not found, use Root for encode.
- renderAjaxRegion(context, context.getViewRoot(), true);
- }
- }
-
- public void invokeRoot(FacesContext context) {
- renderAjaxRegion(context, context.getViewRoot(), true);
- }
-
- };
-
- public void renderSubmittedAjaxRegion(FacesContext context) {
- renderSubmittedAjaxRegion(context, true);
- }
-
- public void renderSubmittedAjaxRegion(FacesContext context,
- final boolean useFilterWriter) {
- InvokerCallback ajaxInvoker = new InvokerCallback() {
-
- public void invoke(FacesContext context, UIComponent component) {
- if (component instanceof AjaxContainer) {
- renderAjaxRegion(context, component, useFilterWriter);
- } else {
- // Container not found, use Root for encode.
- renderAjaxRegion(context, context.getViewRoot(),
- useFilterWriter);
- }
- }
-
- public void invokeRoot(FacesContext context) {
- renderAjaxRegion(context, context.getViewRoot(),
- useFilterWriter);
- }
-
- };
- if (!invokeOnComponent(context.getViewRoot(), context, ajaxInvoker,
- getSubmittedRegionClientId(context))) {
- renderAjaxRegion(context, context.getViewRoot(), useFilterWriter);
- }
-
- }
-
- /**
- * @param context
- * @param useFilterWriter
- * TODO
- * @throws AbortProcessingException
- */
- public void renderAjaxRegion(FacesContext context, UIComponent component,
- boolean useFilterWriter) throws FacesException {
- if (log.isDebugEnabled()) {
- log.debug(Messages.getMessage(Messages.RENDER_AJAX_REQUEST,
- component.getId()));
- }
- try {
- setSelfRender(true);
- // create response writer.
- ExternalContext extContext = context.getExternalContext();
- RenderKit renderKit = context.getRenderKit();
- String encoding;
- // Depends if we talk about servlets, portlets, ...
- if (extContext.getRequest() instanceof ServletRequest) {
- ServletRequest request = (ServletRequest) extContext
- .getRequest();
- ServletResponse response = (ServletResponse) extContext
- .getResponse();
- // HACK - bypass MyFaces ( and other ) extensions filter.
-
- // Setup encoding and content type
- String contentType = "text/xml";
- // get the encoding - must be setup by faces context or filter.
- encoding = request.getCharacterEncoding();
- if (encoding == null) {
- encoding = "UTF-8";
- }
- response.setContentType(contentType + ";charset=" + encoding);
- } else
- encoding = "UTF-8";
-
- PrintWriter servletWriter;
- if (useFilterWriter
- && extContext.getRequestMap().containsKey(
- BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE)) {
- // HACK - Special case for MyFaces, since <f:view don't call
- // encode methods,
- // encode response as for self-rendered region directly to
- // filter response wrpper.
- // to avoid exceptions, inform wrapper to ignore illegal states
- // for getWriter/Stream.
- ServletResponse servletResponse = (ServletResponse) extContext
- .getRequestMap().get(
- BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE);
- servletResponse.resetBuffer();
- servletWriter = servletResponse.getWriter();
- ((FilterServletResponseWrapper) servletResponse)
- .setUseNullStream(true);
- } else {
- servletWriter = getWriter(extContext);
- }
- ResponseWriter writer = renderKit.createResponseWriter(
- servletWriter, null, encoding);
- context.setResponseWriter(writer);
- // make response
- writer.startDocument();
- encodeAjaxBegin(context, component);
- component.encodeBegin(context);
- ((AjaxContainer) component).encodeAjax(context);
- component.encodeEnd(context);
- saveViewState(context);
- encodeAjaxEnd(context, component);
- writer.endDocument();
- writer.flush();
- writer.close();
- servletWriter.close();
- // Save tree state.
- } catch (IOException e) {
- throw new FacesException(Messages.getMessage(
- Messages.RENDERING_AJAX_REGION_ERROR, component
- .getClientId(context)), e);
- } finally {
- context.responseComplete();
- // component.setRendererType(defaultRenderer);
- }
- }
-
- /**
- * Encode declaration for AJAX response. Render
<html><body>
- *
- * @param context
- * @param component
- * @throws IOException
- */
- public void encodeAjaxBegin(FacesContext context, UIComponent component)
- throws IOException {
- // AjaxContainer ajax = (AjaxContainer) component;
- ResponseWriter out = context.getResponseWriter();
- // DebugUtils.traceView("ViewRoot in AJAX Page encode begin");
- out.startElement(HTML.HTML_ELEMENT, component);
- // TODO - html attributes. lang - from current locale ?
- Locale locale = context.getViewRoot().getLocale();
- out.writeAttribute(HTML.lang_ATTRIBUTE, locale.toString(), "lang");
- out.startElement(HTML.BODY_ELEMENT, component);
- }
-
- /**
- * End encoding of AJAX response. Render tag with included areas and
- * close </body></html>
- *
- * @param context
- * @param component
- * @throws IOException
- */
- public void encodeAjaxEnd(FacesContext context, UIComponent component)
- throws IOException {
- // AjaxContainer ajax = (AjaxContainer) component;
- ResponseWriter out = context.getResponseWriter();
- // DebugUtils.traceView("ViewRoot in AJAX Page encode begin");
- out.endElement(HTML.BODY_ELEMENT);
- out.endElement(HTML.HTML_ELEMENT);
- }
-
- /**
- * @param context
- * @param root
- * @throws FacesException
- */
- public void processHeadResources(FacesContext context)
- throws FacesException {
- ExternalContext externalContext = context.getExternalContext();
- Map requestMap = externalContext.getRequestMap();
- if (!Boolean.TRUE.equals(requestMap.get(RESOURCES_PROCESSED))) {
- if (null != requestMap.get(BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE)) {
- if (log.isDebugEnabled()) {
- log
- .debug("Process component tree for collect used scripts and styles");
- }
- UIViewRoot root = context.getViewRoot();
- Set scripts = new LinkedHashSet();
- Set styles = new LinkedHashSet();
- RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder
- .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
- RenderKit renderKit = rkFactory.getRenderKit(context, context
- .getViewRoot().getRenderKitId());
- processHeadResources(context, root, scripts, styles, renderKit);
- if (scripts.size() > 0) {
- if (log.isDebugEnabled()) {
- StringBuffer buff = new StringBuffer(
- "Scripts for insert into head : \n");
- for (Iterator iter = scripts.iterator(); iter.hasNext();) {
- String script = (String) iter.next();
- buff.append(script).append("\n");
- }
- log.debug(buff.toString());
- }
- requestMap.put(SCRIPTS_PARAMETER, scripts);
- }
- // Set default style sheet for current skin.
- String styleSheetUri = null;
- try {
- styleSheetUri = (String) SkinFactory.getInstance().getSkin(
- context).getParameter(context,
- Skin.generalStyleSheet);
- } catch (SkinNotFoundException e) {
- log.warn("Current Skin is not found", e);
- }
- if (null != styleSheetUri) {
- String resourceURL = context.getApplication()
- .getViewHandler().getResourceURL(context,
- styleSheetUri);
- // TODO - some resources can be non-session aware, we
- // must
- // skip encoding for this case ?
- // But, in common case - static links not need session
- // info,
- // and dynamic resources perform encodings if nessesary
- // resourceURL =
- // context.getExternalContext().encodeResourceURL(resourceURL);
- styles.add(resourceURL);
- }
- if (styles.size() > 0) {
- if (log.isDebugEnabled()) {
- StringBuffer buff = new StringBuffer(
- "Styles for insert into head : \n");
- for (Iterator iter = styles.iterator(); iter.hasNext();) {
- String style = (String) iter.next();
- buff.append(style).append("\n");
- }
- log.debug(buff.toString());
- }
- requestMap.put(STYLES_PARAMETER, styles);
- }
- // Mark as processed.
- requestMap.put(RESOURCES_PROCESSED, Boolean.TRUE);
- }
-
- }
- }
-
- /**
- * Append nessesary scripts and styles from component ( if renderer
- * implements {@link HeaderResourceProducer}) and recursive process all
- * facets and childrens.
- *
- * @param context
- * TODO
- * @param root
- * @param scripts
- * @param styles
- * @param renderKit
- * TODO
- */
- private void processHeadResources(FacesContext context, UIComponent root,
- Set scripts, Set styles, RenderKit renderKit) {
- Renderer renderer = getRenderer(context, root, renderKit);
- if (null != renderer) {
- if (renderer instanceof HeaderResourceProducer) {
- HeaderResourceProducer producer = (HeaderResourceProducer) renderer;
- Set set = producer.getHeaderScripts(context, root);
- if (null != set) {
- scripts.addAll(set);
- }
- set = producer.getHeaderStyles(context, root);
- if (null != set) {
- styles.addAll(set);
- }
- }
- }
- for (Iterator iter = root.getFacets().values().iterator(); iter
- .hasNext();) {
- UIComponent child = (UIComponent) iter.next();
- processHeadResources(context, child, scripts, styles, renderKit);
- }
- for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
- UIComponent child = (UIComponent) iter.next();
- processHeadResources(context, child, scripts, styles, renderKit);
- }
- }
-
- /**
- * Find renderer for given component.
- *
- * @param context
- * @param comp
- * @param renderKit
- * @return
- */
- private Renderer getRenderer(FacesContext context, UIComponent comp,
- RenderKit renderKit) {
-
- String rendererType = comp.getRendererType();
- if (rendererType != null) {
- return (renderKit.getRenderer(comp.getFamily(), rendererType));
- } else {
- return (null);
- }
-
- }
-
- public void saveViewState(FacesContext context) throws IOException {
- // TODO - for facelets environment, we need to remove transient
- // components.
- try {
- Application.class.getMethod("getExpressionFactory", null);
- } catch (NoSuchMethodException e) {
- // JSF 1.1 !
- }
- ResponseWriter writer = context.getResponseWriter();
- StateManager stateManager = context.getApplication().getStateManager();
- SerializedView serializedView = stateManager
- .saveSerializedView(context);
- if (null != serializedView) {
- StringWriter bufWriter = new StringWriter();
- ResponseWriter cloneWithWriter = writer.cloneWithWriter(bufWriter);
- context.setResponseWriter(cloneWithWriter);
- stateManager.writeState(context, serializedView);
- cloneWithWriter.flush();
- if (bufWriter.getBuffer().length() > 0) {
- context.getExternalContext().getRequestMap().put(
- AjaxViewHandler.SERIALIZED_STATE_KEY,
- bufWriter.toString());
- }
- // Restore original writer.
- context.setResponseWriter(writer);
- }
- }
-
- /**
- * @return Returns the ajaxRequest.
- */
- public boolean isAjaxRequest() {
- return isAjaxRequest(FacesContext.getCurrentInstance());
- }
-
- /**
- * @return Returns the ajaxRequest.
- */
- public boolean isAjaxRequest(FacesContext context) {
- if (!this.ajaxRequestSet) {
- ajaxRequest = null != getSubmittedRegionClientId(context);
- ajaxRequestSet = true;
- }
- return ajaxRequest;
- }
-
- /**
- * @param ajaxRequest
- * The ajaxRequest to set.
- */
- public void setAjaxRequest(boolean ajaxRequest) {
- this.ajaxRequest = ajaxRequest;
- this.ajaxRequestSet = true;
- }
-
- /**
- * @return Returns the ajaxAreasToRender.
- */
- public Set getAjaxAreasToRender() {
- return this.ajaxAreasToRender;
- }
-
- /**
- * Add affected regions's ID to ajaxView component.
- *
- * @param component
- */
- public void addRegionsFromComponent(UIComponent component) {
- // First step - find parent ajax view
- Set ajaxRegions = AjaxRendererUtils.getAjaxAreas(component);
- // if (ajaxRegions == null){
- // FacesContext context = FacesContext.getCurrentInstance();
- // ajaxRegions = AjaxRendererUtils.getAbsoluteId(context,component);
- // }
- if (log.isDebugEnabled()) {
- log.debug(Messages.getMessage(Messages.INVOKE_AJAX_REGION_LISTENER,
- component.getId()));
- }
- if (ajaxRegions != null) {
- for (Iterator iter = ajaxRegions.iterator(); iter.hasNext();) {
- String id = iter.next().toString();
- ajaxAreasToRender.add(convertId(component, id));
- }
- }
- }
-
- public void addComponentToAjaxRender(UIComponent component) {
- this.ajaxAreasToRender.add(AjaxRendererUtils.getAbsoluteId(component));
- }
-
- public void addComponentToAjaxRender(UIComponent base, String id) {
- this.ajaxAreasToRender.add(convertId(base, id));
- }
-
- /**
- * Test for relative id of target components. Attempt convert to
- * absolute. For use as argument for
- * {@link UIComponent#findComponent(java.lang.String)}
- *
- * @param component
- * @param id
- * @return
- */
- private String convertId(UIComponent component, String id) {
- if (id.charAt(0) == NamingContainer.SEPARATOR_CHAR) {
- return id;
- }
- if (null == component) {
- throw new NullPointerException(
- "Base component for search re-rendered compnnent is null");
- }
- UIComponent target = null;
- UIComponent parent = component;
- UIComponent root = component;
- while (null == target && null != parent) {
- target = parent.findComponent(id);
- root = parent;
- parent = parent.getParent();
- }
- if (null == target) {
- target = findUIComponentBelow(root, id);
- }
- if (null != target) {
- return AjaxRendererUtils.getAbsoluteId(target);
- }
- log.warn("Target component for id "+id+" not found");
- return id;
- }
-
- private UIComponent findUIComponentBelow(UIComponent root,
- String id) {
-
- UIComponent target = null;
- for (Iterator iter = root.getFacetsAndChildren(); iter.hasNext();) {
- UIComponent child = (UIComponent) iter.next();
- if (child instanceof NamingContainer) {
- try {
- target = child.findComponent(id);
- } catch (IllegalArgumentException iae) {
- continue;
- }
- }
- if (target == null) {
- if (child.getChildCount() > 0) {
- target = findUIComponentBelow(child, id);
- }
- }
-
- if (target != null) {
- break;
- }
-
- }
- return target;
- }
-
- /**
- * @return Returns the ajaxRenderedAreas.
- */
- public Set getAjaxRenderedAreas() {
- return ajaxRenderedAreas;
- }
-
- public void addRenderedArea(String id) {
- ajaxRenderedAreas.add(id);
- }
-
- public boolean removeRenderedArea(String id) {
- return ajaxRenderedAreas.remove(id);
- }
-
- /**
- * @return Returns the submittedClientId.
- */
- public String getSubmittedRegionClientId(FacesContext context) {
- if (!this.submittedRegionSet) {
- this.submittedRegionClientId = (String) context
- .getExternalContext().getRequestParameterMap().get(
- AjaxContainerRenderer.AJAX_PARAMETER_NAME);
- this.submittedRegionSet = true;
- if (!this.ajaxRequestSet) {
- setAjaxRequest(this.submittedRegionClientId != null);
- }
- }
- return this.submittedRegionClientId;
- }
-
- /**
- * @param submittedClientId
- * The submittedClientId to set.
- */
- public void setSubmittedRegionClientId(String submittedClientId) {
- this.submittedRegionClientId = submittedClientId;
- this.submittedRegionSet = true;
- }
-
- /**
- * @return Returns the selfRender.
- */
- public boolean isSelfRender() {
- return selfRender;
- }
-
- /**
- * @param selfRender
- * The selfRender to set.
- */
- public void setSelfRender(boolean selfRender) {
- this.selfRender = selfRender;
- }
-
- /**
- * @return the vievIdHolder
- */
- public ViewIdHolder getViewIdHolder() {
- return viewIdHolder;
- }
-
- /**
- * @param viewIdHolder
- * the vievIdHolder to set
- */
- public void setViewIdHolder(ViewIdHolder viewIdHolder) {
- this.viewIdHolder = viewIdHolder;
- }
-
- /**
- * @return the responseData
- */
- public Object getResponseData() {
- return responseDataMap.get(RESPONSE_DATA_KEY);
- }
-
- /**
- * @param responseData
- * the responseData to set
- */
- public void setResponseData(Object responseData) {
- this.responseDataMap.put(RESPONSE_DATA_KEY, responseData);
- }
-
- /**
- * @return the responseDataMap
- */
- public Map getResponseDataMap() {
- return responseDataMap;
- }
-
- /**
- * Gives back the writer of a Response object.
- *
- * @param extContext
- * The external context.
- * @return The writer of the response.
- * @throws FacesException
- * If the response object has no getWriter() method.
- */
- protected PrintWriter getWriter(ExternalContext extContext)
- throws FacesException {
- PrintWriter writer = null;
- Object response = extContext.getResponse();
- try {
- Method gW = response.getClass()
- .getMethod("getWriter", new Class[0]);
- writer = (PrintWriter) gW.invoke(response, new Object[0]);
- } catch (Exception e) {
- throw new FacesException(e);
- }
- return writer;
- }
-
- public String getAjaxActionURL() {
- return getAjaxActionURL(FacesContext.getCurrentInstance());
- }
-
- public String getAjaxActionURL(FacesContext context) {
- // Check arguments
- if (null == context) {
- throw new NullPointerException(
- "Faces context for build AJAX Action URL is null");
- }
- UIViewRoot viewRoot = context.getViewRoot();
- if (null == viewRoot) {
- throw new NullPointerException(
- "Faces view tree for build AJAX Action URL is null");
- }
- String viewId = viewRoot.getViewId();
- if (null == viewId) {
- throw new NullPointerException(
- "View id for build AJAX Action URL is null");
- }
- if (!viewId.startsWith("/")) {
- throw new IllegalArgumentException(
- "Illegal view Id for build AJAX Action URL: " + viewId);
- }
- ViewHandler viewHandler = context.getApplication().getViewHandler();
- return context.getExternalContext().encodeActionURL(
- viewHandler.getActionURL(context, viewId));
- }
-
- /**
- * @return the commonAjaxParameters
- */
- public Map getCommonAjaxParameters() {
- return commonAjaxParameters;
- }
-
-}
\ No newline at end of file
Copied:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java
(from rev 1393,
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java)
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java
(rev 0)
+++
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -0,0 +1,37 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.ajax4jsf.framework.ajax;
+
+/**
+ * Interface to skip navigation cases in ViewHandler and , instead, store new ViewId.
+ * @author shura
+ *
+ */
+public interface ViewIdHolder {
+
+ public boolean skipNavigation(String ViewId);
+
+ public String getViewId();
+
+ public void setViewId(String newViewId);
+
+}
Modified:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/resource/InternetResourceBuilder.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/resource/InternetResourceBuilder.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/resource/InternetResourceBuilder.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -148,7 +148,8 @@
log
.debug("Create default implementation instance of InternetBuilder");
}
- instance = new ResourceBuilderImpl();
+ // TODO - detect default instance.
+// instance = new ResourceBuilderImpl();
}
instances.put(loader, instance);
}
Modified:
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/skin/SkinFactory.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/skin/SkinFactory.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/skin/SkinFactory.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -113,7 +113,8 @@
}
}
if (instance == null) {
- instance = new SkinFactoryImpl();
+ // TODO - create default instance
+// instance = new SkinFactoryImpl();
}
instances.put(loader, instance);
}
Deleted:
branches/refactor1/framework/api/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/api/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -1,60 +0,0 @@
-/**
- * License Agreement.
- *
- * JBoss RichFaces 3.0 - Ajax4jsf Component Library
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.component.state;
-
-import org.richfaces.component.UITree;
-
-
-/**
- * @author Nick Belaevski - mailto:nbelaevski@exadel.com
- * created 19.06.2007
- *
- * This interface is intended to "advising" changes to tree state. Tree
component should queue user-provided
- * instance of the interface for changes during RENDER_RESPONSE phase. Changes advised by
user should be
- * immediately applied. Advisor methods are provided with {@link UITree} component
instance to queue current
- * tree state.
- */
-public interface TreeStateAdvisor {
- /**
- * Advises new node opened/closed state
- * @param tree {@link UITree} component state to queue state
- * @return
- * <ul>
- * <li><code>null</code> if changes are not needed</li>
- * <li>{@link Boolean#TRUE} to advise node to be opened</li>
- * <li>{@link Boolean#FALSE} to advise node to be closed</li>
- * </ul>
- */
- public Boolean adviseNodeOpened(UITree tree);
-
- /**
- * Advises new node selection
- * @param tree {@link UITree} component state to queue state
- * @return
- * <ul>
- * <li><code>null</code> if changes are not needed</li>
- * <li>{@link Boolean#TRUE} to advise current node to be selected</li>
- * <li>{@link Boolean#FALSE} to advise current node to be
unselected</li>
- * </ul>
- */
- public Boolean adviseNodeSelected(UITree tree);
-}
Deleted:
branches/refactor1/framework/api/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java
===================================================================
---
branches/refactor1/framework/api/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/api/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -1,164 +0,0 @@
-/**
- * License Agreement.
- *
- * JBoss RichFaces 3.0 - Ajax4jsf Component Library
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.component.xml;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.commons.digester.Digester;
-import org.apache.commons.digester.RulesBase;
-import org.richfaces.component.TreeNode;
-import org.richfaces.component.TreeNodeImpl;
-import org.xml.sax.Attributes;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-
-/**
- * @author Nick Belaevski - nbelaevski(a)exadel.com
- * created 16.11.2006
- *
- */
-public class XmlTreeDataBuilder {
-
- private final static class Rule extends org.apache.commons.digester.Rule {
- private int level = -1;
- private List idsList = new ArrayList();
- private List treeNodesList = new ArrayList();
- private List exclusionSets = new ArrayList();
- private TreeNode treeNode = new TreeNodeImpl(); //add empty node to serve as root
-
- public void begin(String namespace, String name, Attributes attributes)
- throws Exception {
- super.begin(namespace, name, attributes);
-
- level++;
-
- XmlNodeData xmlNodeData = new XmlNodeData();
- xmlNodeData.setName(name);
- xmlNodeData.setNamespace(namespace);
-
- String id = null;
-
- if (attributes != null) {
- int length = attributes.getLength();
- for (int i = 0; i < length; i++) {
- xmlNodeData.setAttribute(attributes.getQName(i),
- attributes.getValue(i));
-
- }
-
- id = attributes.getValue("id");
- }
-
- if (exclusionSets.size() == level) {
- exclusionSets.add(null);
- }
-
- if (id == null || id.length() == 0) {
- int currentId = 0;
-
- if (idsList.size() <= level) {
- for (int i = idsList.size(); i <= level; i++) {
- idsList.add(null);
- }
- } else {
- Integer integer = (Integer) idsList.get(level);
- currentId = integer.intValue() + 1;
- }
-
- Set exclusions = (Set) exclusionSets.get(level);
-
- while (exclusions != null &&
exclusions.contains(Integer.toString(currentId))) {
- currentId++;
- }
-
- idsList.set(level, new Integer(currentId));
-
- id = Integer.toString(currentId);
- } else {
- Set exclusions = (Set) exclusionSets.get(level);
- if (exclusions == null) {
- exclusions = new HashSet();
-
- exclusionSets.set(level, exclusions);
- }
-
- exclusions.add(id);
- }
-
- TreeNode node = new TreeNodeImpl();
- node.setData(xmlNodeData);
-
- this.treeNode.addChild(id, node);
- this.treeNodesList.add(this.treeNode);
- this.treeNode = node;
- }
-
- public void body(String namespace, String name, String text)
- throws Exception {
- super.body(namespace, name, text);
-
- if (text != null) {
- ((XmlNodeData) this.treeNode.getData()).setText(text.trim());
- }
- }
-
- public void end(String namespace, String name) throws Exception {
- super.end(namespace, name);
-
- level--;
-
- if (idsList.size() - 1 > level + 1) {
- //idsList grew larger than we really need
- idsList.remove(idsList.size() - 1);
- }
-
- if (exclusionSets.size() - 1 > level + 1) {
- //the same condition as above
- exclusionSets.remove(exclusionSets.size() - 1);
- }
-
- this.treeNode = (TreeNode) this.treeNodesList.remove(this.treeNodesList.size() - 1);
- }
- }
-
- public static TreeNode build(InputSource inputSource) throws SAXException, IOException
{
- Digester digester = new Digester();
- Rule rule = new Rule();
- final List rulesList = new ArrayList(1);
- rulesList.add(rule);
-
- RulesBase rulesBase = new RulesBase() {
- protected List lookup(String namespace, String name) {
- return rulesList;
- }
- };
- digester.setRules(rulesBase);
- digester.setNamespaceAware(true);
- digester.parse(inputSource);
-
- return rule.treeNode;
- }
-}
Copied:
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java
(from rev 1415,
branches/refactor1/framework/api/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java)
===================================================================
---
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java
(rev 0)
+++
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/AjaxContextImpl.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -0,0 +1,757 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.ajax4jsf.framework.ajax;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
+import javax.faces.application.Application;
+import javax.faces.application.StateManager;
+import javax.faces.application.ViewHandler;
+import javax.faces.application.StateManager.SerializedView;
+import javax.faces.component.NamingContainer;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.render.RenderKit;
+import javax.faces.render.RenderKitFactory;
+import javax.faces.render.Renderer;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter;
+import org.ajax4jsf.framework.ajax.xmlfilter.FilterServletResponseWrapper;
+import org.ajax4jsf.framework.renderer.AjaxContainerRenderer;
+import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
+import org.ajax4jsf.framework.renderer.HeaderResourceProducer;
+import org.ajax4jsf.framework.renderer.RendererUtils.HTML;
+import org.ajax4jsf.framework.skin.Skin;
+import org.ajax4jsf.framework.skin.SkinFactory;
+import org.ajax4jsf.framework.skin.SkinNotFoundException;
+import org.ajax4jsf.framework.util.config.WebXml;
+import org.ajax4jsf.framework.util.message.Messages;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * This class incapsulated
+ *
+ * @author asmirnov(a)exadel.com (latest modification by $Author: alexsmirnov $)
+ * @version $Revision: 1.1.2.7 $ $Date: 2007/02/08 19:07:16 $
+ *
+ */
+public class AjaxContextImpl extends AjaxContext {
+ public static final String RESOURCES_PROCESSED =
"org.ajax4jsf.framework.HEADER_PROCESSED";
+
+ private static final Log log = LogFactory.getLog(AjaxContext.class);
+
+ private static ComponentInvoker invoker;
+
+ private static Map contextClasses = new HashMap();
+
+ Set ajaxAreasToRender = new HashSet();
+
+ Set ajaxRenderedAreas = new HashSet();
+
+ boolean ajaxRequest = false;
+
+ boolean ajaxRequestSet = false;
+
+ boolean selfRender = false;
+
+ Integer viewSequence = new Integer(1);
+
+ String submittedRegionClientId = null;
+
+ boolean submittedRegionSet = false;
+
+ ViewIdHolder viewIdHolder = null;
+
+ Map responseDataMap = new HashMap();
+
+ Map commonAjaxParameters = new HashMap();
+
+ static {
+ try {
+ // Attempt to create JSF1.2 specific invoker.
+ invoker = new JsfOneOneInvoker();
+ } catch (Exception e) {
+ invoker = new JsfOneOneInvoker();
+ }
+ }
+
+ /**
+ * @param root
+ * @param context
+ * @param callback
+ * @param regionId
+ * @return
+ * @see
org.ajax4jsf.framework.ajax.JsfOneOneInvoker#invokeOnComponent(javax.faces.component.UIComponent,
+ * javax.faces.context.FacesContext,
+ * org.ajax4jsf.framework.ajax.InvokerCallback, java.lang.String)
+ */
+ public static boolean invokeOnComponent(UIComponent root,
+ FacesContext context, InvokerCallback callback, String regionId) {
+ return invoker.invokeOnComponent(root, context, callback, regionId);
+ }
+
+ /**
+ * @param viewRoot
+ * @param context
+ * @param callback
+ * @see
org.ajax4jsf.framework.ajax.JsfOneOneInvoker#invokeOnRegionOrRoot(org.ajax4jsf.framework.ajax.AjaxViewRoot,
+ * javax.faces.context.FacesContext,
+ * org.ajax4jsf.framework.ajax.InvokerCallback)
+ */
+ public static void invokeOnRegionOrRoot(AjaxViewRoot viewRoot,
+ FacesContext context, InvokerCallback callback) {
+ invoker.invokeOnRegionOrRoot(viewRoot, context, callback);
+ }
+
+ private InvokerCallback _ajaxInvoker = new InvokerCallback() {
+
+ public void invoke(FacesContext context, UIComponent component) {
+ if (component instanceof AjaxContainer) {
+ AjaxContainer ajax = (AjaxContainer) component;
+ renderAjaxRegion(context, component, true);
+ } else {
+ // Container not found, use Root for encode.
+ renderAjaxRegion(context, context.getViewRoot(), true);
+ }
+ }
+
+ public void invokeRoot(FacesContext context) {
+ renderAjaxRegion(context, context.getViewRoot(), true);
+ }
+
+ };
+
+ public void renderSubmittedAjaxRegion(FacesContext context) {
+ renderSubmittedAjaxRegion(context, true);
+ }
+
+ public void renderSubmittedAjaxRegion(FacesContext context,
+ final boolean useFilterWriter) {
+ InvokerCallback ajaxInvoker = new InvokerCallback() {
+
+ public void invoke(FacesContext context, UIComponent component) {
+ if (component instanceof AjaxContainer) {
+ renderAjaxRegion(context, component, useFilterWriter);
+ } else {
+ // Container not found, use Root for encode.
+ renderAjaxRegion(context, context.getViewRoot(),
+ useFilterWriter);
+ }
+ }
+
+ public void invokeRoot(FacesContext context) {
+ renderAjaxRegion(context, context.getViewRoot(),
+ useFilterWriter);
+ }
+
+ };
+ if (!invokeOnComponent(context.getViewRoot(), context, ajaxInvoker,
+ getSubmittedRegionClientId(context))) {
+ renderAjaxRegion(context, context.getViewRoot(), useFilterWriter);
+ }
+
+ }
+
+ /**
+ * @param context
+ * @param useFilterWriter
+ * TODO
+ * @throws AbortProcessingException
+ */
+ public void renderAjaxRegion(FacesContext context, UIComponent component,
+ boolean useFilterWriter) throws FacesException {
+ if (log.isDebugEnabled()) {
+ log.debug(Messages.getMessage(Messages.RENDER_AJAX_REQUEST,
+ component.getId()));
+ }
+ try {
+ setSelfRender(true);
+ // create response writer.
+ ExternalContext extContext = context.getExternalContext();
+ RenderKit renderKit = context.getRenderKit();
+ String encoding;
+ // Depends if we talk about servlets, portlets, ...
+ if (extContext.getRequest() instanceof ServletRequest) {
+ ServletRequest request = (ServletRequest) extContext
+ .getRequest();
+ ServletResponse response = (ServletResponse) extContext
+ .getResponse();
+ // HACK - bypass MyFaces ( and other ) extensions filter.
+
+ // Setup encoding and content type
+ String contentType = "text/xml";
+ // get the encoding - must be setup by faces context or filter.
+ encoding = request.getCharacterEncoding();
+ if (encoding == null) {
+ encoding = "UTF-8";
+ }
+ response.setContentType(contentType + ";charset=" + encoding);
+ } else
+ encoding = "UTF-8";
+
+ PrintWriter servletWriter;
+ if (useFilterWriter
+ && extContext.getRequestMap().containsKey(
+ BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE)) {
+ // HACK - Special case for MyFaces, since <f:view don't call
+ // encode methods,
+ // encode response as for self-rendered region directly to
+ // filter response wrpper.
+ // to avoid exceptions, inform wrapper to ignore illegal states
+ // for getWriter/Stream.
+ ServletResponse servletResponse = (ServletResponse) extContext
+ .getRequestMap().get(
+ BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE);
+ servletResponse.resetBuffer();
+ servletWriter = servletResponse.getWriter();
+ ((FilterServletResponseWrapper) servletResponse)
+ .setUseNullStream(true);
+ } else {
+ servletWriter = getWriter(extContext);
+ }
+ ResponseWriter writer = renderKit.createResponseWriter(
+ servletWriter, null, encoding);
+ context.setResponseWriter(writer);
+ // make response
+ writer.startDocument();
+ encodeAjaxBegin(context, component);
+ component.encodeBegin(context);
+ ((AjaxContainer) component).encodeAjax(context);
+ component.encodeEnd(context);
+ saveViewState(context);
+ encodeAjaxEnd(context, component);
+ writer.endDocument();
+ writer.flush();
+ writer.close();
+ servletWriter.close();
+ // Save tree state.
+ } catch (IOException e) {
+ throw new FacesException(Messages.getMessage(
+ Messages.RENDERING_AJAX_REGION_ERROR, component
+ .getClientId(context)), e);
+ } finally {
+ context.responseComplete();
+ // component.setRendererType(defaultRenderer);
+ }
+ }
+
+ /**
+ * Encode declaration for AJAX response. Render
<html><body>
+ *
+ * @param context
+ * @param component
+ * @throws IOException
+ */
+ public void encodeAjaxBegin(FacesContext context, UIComponent component)
+ throws IOException {
+ // AjaxContainer ajax = (AjaxContainer) component;
+ ResponseWriter out = context.getResponseWriter();
+ // DebugUtils.traceView("ViewRoot in AJAX Page encode begin");
+ out.startElement(HTML.HTML_ELEMENT, component);
+ // TODO - html attributes. lang - from current locale ?
+ Locale locale = context.getViewRoot().getLocale();
+ out.writeAttribute(HTML.lang_ATTRIBUTE, locale.toString(), "lang");
+ out.startElement(HTML.BODY_ELEMENT, component);
+ }
+
+ /**
+ * End encoding of AJAX response. Render tag with included areas and
+ * close </body></html>
+ *
+ * @param context
+ * @param component
+ * @throws IOException
+ */
+ public void encodeAjaxEnd(FacesContext context, UIComponent component)
+ throws IOException {
+ // AjaxContainer ajax = (AjaxContainer) component;
+ ResponseWriter out = context.getResponseWriter();
+ // DebugUtils.traceView("ViewRoot in AJAX Page encode begin");
+ out.endElement(HTML.BODY_ELEMENT);
+ out.endElement(HTML.HTML_ELEMENT);
+ }
+
+ /**
+ * @param context
+ * @param root
+ * @throws FacesException
+ */
+ public void processHeadResources(FacesContext context)
+ throws FacesException {
+ ExternalContext externalContext = context.getExternalContext();
+ Map requestMap = externalContext.getRequestMap();
+ if (!Boolean.TRUE.equals(requestMap.get(RESOURCES_PROCESSED))) {
+ if (null != requestMap.get(BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE)) {
+ if (log.isDebugEnabled()) {
+ log
+ .debug("Process component tree for collect used scripts and styles");
+ }
+ UIViewRoot root = context.getViewRoot();
+ Set scripts = new LinkedHashSet();
+ Set styles = new LinkedHashSet();
+ RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder
+ .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+ RenderKit renderKit = rkFactory.getRenderKit(context, context
+ .getViewRoot().getRenderKitId());
+ processHeadResources(context, root, scripts, styles, renderKit);
+ if (scripts.size() > 0) {
+ if (log.isDebugEnabled()) {
+ StringBuffer buff = new StringBuffer(
+ "Scripts for insert into head : \n");
+ for (Iterator iter = scripts.iterator(); iter.hasNext();) {
+ String script = (String) iter.next();
+ buff.append(script).append("\n");
+ }
+ log.debug(buff.toString());
+ }
+ requestMap.put(SCRIPTS_PARAMETER, scripts);
+ }
+ // Set default style sheet for current skin.
+ String styleSheetUri = null;
+ try {
+ styleSheetUri = (String) SkinFactory.getInstance().getSkin(
+ context).getParameter(context,
+ Skin.generalStyleSheet);
+ } catch (SkinNotFoundException e) {
+ log.warn("Current Skin is not found", e);
+ }
+ if (null != styleSheetUri) {
+ String resourceURL = context.getApplication()
+ .getViewHandler().getResourceURL(context,
+ styleSheetUri);
+ // TODO - some resources can be non-session aware, we
+ // must
+ // skip encoding for this case ?
+ // But, in common case - static links not need session
+ // info,
+ // and dynamic resources perform encodings if nessesary
+ // resourceURL =
+ // context.getExternalContext().encodeResourceURL(resourceURL);
+ styles.add(resourceURL);
+ }
+ if (styles.size() > 0) {
+ if (log.isDebugEnabled()) {
+ StringBuffer buff = new StringBuffer(
+ "Styles for insert into head : \n");
+ for (Iterator iter = styles.iterator(); iter.hasNext();) {
+ String style = (String) iter.next();
+ buff.append(style).append("\n");
+ }
+ log.debug(buff.toString());
+ }
+ requestMap.put(STYLES_PARAMETER, styles);
+ }
+ // Mark as processed.
+ requestMap.put(RESOURCES_PROCESSED, Boolean.TRUE);
+ }
+
+ }
+ }
+
+ /**
+ * Append nessesary scripts and styles from component ( if renderer
+ * implements {@link HeaderResourceProducer}) and recursive process all
+ * facets and childrens.
+ *
+ * @param context
+ * TODO
+ * @param root
+ * @param scripts
+ * @param styles
+ * @param renderKit
+ * TODO
+ */
+ private void processHeadResources(FacesContext context, UIComponent root,
+ Set scripts, Set styles, RenderKit renderKit) {
+ Renderer renderer = getRenderer(context, root, renderKit);
+ if (null != renderer) {
+ if (renderer instanceof HeaderResourceProducer) {
+ HeaderResourceProducer producer = (HeaderResourceProducer) renderer;
+ Set set = producer.getHeaderScripts(context, root);
+ if (null != set) {
+ scripts.addAll(set);
+ }
+ set = producer.getHeaderStyles(context, root);
+ if (null != set) {
+ styles.addAll(set);
+ }
+ }
+ }
+ for (Iterator iter = root.getFacets().values().iterator(); iter
+ .hasNext();) {
+ UIComponent child = (UIComponent) iter.next();
+ processHeadResources(context, child, scripts, styles, renderKit);
+ }
+ for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
+ UIComponent child = (UIComponent) iter.next();
+ processHeadResources(context, child, scripts, styles, renderKit);
+ }
+ }
+
+ /**
+ * Find renderer for given component.
+ *
+ * @param context
+ * @param comp
+ * @param renderKit
+ * @return
+ */
+ private Renderer getRenderer(FacesContext context, UIComponent comp,
+ RenderKit renderKit) {
+
+ String rendererType = comp.getRendererType();
+ if (rendererType != null) {
+ return (renderKit.getRenderer(comp.getFamily(), rendererType));
+ } else {
+ return (null);
+ }
+
+ }
+
+ public void saveViewState(FacesContext context) throws IOException {
+ // TODO - for facelets environment, we need to remove transient
+ // components.
+ try {
+ Application.class.getMethod("getExpressionFactory", null);
+ } catch (NoSuchMethodException e) {
+ // JSF 1.1 !
+ }
+ ResponseWriter writer = context.getResponseWriter();
+ StateManager stateManager = context.getApplication().getStateManager();
+ SerializedView serializedView = stateManager
+ .saveSerializedView(context);
+ if (null != serializedView) {
+ StringWriter bufWriter = new StringWriter();
+ ResponseWriter cloneWithWriter = writer.cloneWithWriter(bufWriter);
+ context.setResponseWriter(cloneWithWriter);
+ stateManager.writeState(context, serializedView);
+ cloneWithWriter.flush();
+ if (bufWriter.getBuffer().length() > 0) {
+ context.getExternalContext().getRequestMap().put(
+ AjaxViewHandler.SERIALIZED_STATE_KEY,
+ bufWriter.toString());
+ }
+ // Restore original writer.
+ context.setResponseWriter(writer);
+ }
+ }
+
+ /**
+ * @return Returns the ajaxRequest.
+ */
+ public boolean isAjaxRequest() {
+ return isAjaxRequest(FacesContext.getCurrentInstance());
+ }
+
+ /**
+ * @return Returns the ajaxRequest.
+ */
+ public boolean isAjaxRequest(FacesContext context) {
+ if (!this.ajaxRequestSet) {
+ ajaxRequest = null != getSubmittedRegionClientId(context);
+ ajaxRequestSet = true;
+ }
+ return ajaxRequest;
+ }
+
+ /**
+ * @param ajaxRequest
+ * The ajaxRequest to set.
+ */
+ public void setAjaxRequest(boolean ajaxRequest) {
+ this.ajaxRequest = ajaxRequest;
+ this.ajaxRequestSet = true;
+ }
+
+ /**
+ * @return Returns the ajaxAreasToRender.
+ */
+ public Set getAjaxAreasToRender() {
+ return this.ajaxAreasToRender;
+ }
+
+ /**
+ * Add affected regions's ID to ajaxView component.
+ *
+ * @param component
+ */
+ public void addRegionsFromComponent(UIComponent component) {
+ // First step - find parent ajax view
+ Set ajaxRegions = AjaxRendererUtils.getAjaxAreas(component);
+ // if (ajaxRegions == null){
+ // FacesContext context = FacesContext.getCurrentInstance();
+ // ajaxRegions = AjaxRendererUtils.getAbsoluteId(context,component);
+ // }
+ if (log.isDebugEnabled()) {
+ log.debug(Messages.getMessage(Messages.INVOKE_AJAX_REGION_LISTENER,
+ component.getId()));
+ }
+ if (ajaxRegions != null) {
+ for (Iterator iter = ajaxRegions.iterator(); iter.hasNext();) {
+ String id = iter.next().toString();
+ ajaxAreasToRender.add(convertId(component, id));
+ }
+ }
+ }
+
+ public void addComponentToAjaxRender(UIComponent component) {
+ this.ajaxAreasToRender.add(AjaxRendererUtils.getAbsoluteId(component));
+ }
+
+ public void addComponentToAjaxRender(UIComponent base, String id) {
+ this.ajaxAreasToRender.add(convertId(base, id));
+ }
+
+ /**
+ * Test for relative id of target components. Attempt convert to
+ * absolute. For use as argument for
+ * {@link UIComponent#findComponent(java.lang.String)}
+ *
+ * @param component
+ * @param id
+ * @return
+ */
+ private String convertId(UIComponent component, String id) {
+ if (id.charAt(0) == NamingContainer.SEPARATOR_CHAR) {
+ return id;
+ }
+ if (null == component) {
+ throw new NullPointerException(
+ "Base component for search re-rendered compnnent is null");
+ }
+ UIComponent target = null;
+ UIComponent parent = component;
+ UIComponent root = component;
+ while (null == target && null != parent) {
+ target = parent.findComponent(id);
+ root = parent;
+ parent = parent.getParent();
+ }
+ if (null == target) {
+ target = findUIComponentBelow(root, id);
+ }
+ if (null != target) {
+ return AjaxRendererUtils.getAbsoluteId(target);
+ }
+ log.warn("Target component for id "+id+" not found");
+ return id;
+ }
+
+ private UIComponent findUIComponentBelow(UIComponent root,
+ String id) {
+
+ UIComponent target = null;
+ for (Iterator iter = root.getFacetsAndChildren(); iter.hasNext();) {
+ UIComponent child = (UIComponent) iter.next();
+ if (child instanceof NamingContainer) {
+ try {
+ target = child.findComponent(id);
+ } catch (IllegalArgumentException iae) {
+ continue;
+ }
+ }
+ if (target == null) {
+ if (child.getChildCount() > 0) {
+ target = findUIComponentBelow(child, id);
+ }
+ }
+
+ if (target != null) {
+ break;
+ }
+
+ }
+ return target;
+ }
+
+ /**
+ * @return Returns the ajaxRenderedAreas.
+ */
+ public Set getAjaxRenderedAreas() {
+ return ajaxRenderedAreas;
+ }
+
+ public void addRenderedArea(String id) {
+ ajaxRenderedAreas.add(id);
+ }
+
+ public boolean removeRenderedArea(String id) {
+ return ajaxRenderedAreas.remove(id);
+ }
+
+ /**
+ * @return Returns the submittedClientId.
+ */
+ public String getSubmittedRegionClientId(FacesContext context) {
+ if (!this.submittedRegionSet) {
+ this.submittedRegionClientId = (String) context
+ .getExternalContext().getRequestParameterMap().get(
+ AjaxContainerRenderer.AJAX_PARAMETER_NAME);
+ this.submittedRegionSet = true;
+ if (!this.ajaxRequestSet) {
+ setAjaxRequest(this.submittedRegionClientId != null);
+ }
+ }
+ return this.submittedRegionClientId;
+ }
+
+ /**
+ * @param submittedClientId
+ * The submittedClientId to set.
+ */
+ public void setSubmittedRegionClientId(String submittedClientId) {
+ this.submittedRegionClientId = submittedClientId;
+ this.submittedRegionSet = true;
+ }
+
+ /**
+ * @return Returns the selfRender.
+ */
+ public boolean isSelfRender() {
+ return selfRender;
+ }
+
+ /**
+ * @param selfRender
+ * The selfRender to set.
+ */
+ public void setSelfRender(boolean selfRender) {
+ this.selfRender = selfRender;
+ }
+
+ /**
+ * @return the vievIdHolder
+ */
+ public ViewIdHolder getViewIdHolder() {
+ return viewIdHolder;
+ }
+
+ /**
+ * @param viewIdHolder
+ * the vievIdHolder to set
+ */
+ public void setViewIdHolder(ViewIdHolder viewIdHolder) {
+ this.viewIdHolder = viewIdHolder;
+ }
+
+ /**
+ * @return the responseData
+ */
+ public Object getResponseData() {
+ return responseDataMap.get(RESPONSE_DATA_KEY);
+ }
+
+ /**
+ * @param responseData
+ * the responseData to set
+ */
+ public void setResponseData(Object responseData) {
+ this.responseDataMap.put(RESPONSE_DATA_KEY, responseData);
+ }
+
+ /**
+ * @return the responseDataMap
+ */
+ public Map getResponseDataMap() {
+ return responseDataMap;
+ }
+
+ /**
+ * Gives back the writer of a Response object.
+ *
+ * @param extContext
+ * The external context.
+ * @return The writer of the response.
+ * @throws FacesException
+ * If the response object has no getWriter() method.
+ */
+ protected PrintWriter getWriter(ExternalContext extContext)
+ throws FacesException {
+ PrintWriter writer = null;
+ Object response = extContext.getResponse();
+ try {
+ Method gW = response.getClass()
+ .getMethod("getWriter", new Class[0]);
+ writer = (PrintWriter) gW.invoke(response, new Object[0]);
+ } catch (Exception e) {
+ throw new FacesException(e);
+ }
+ return writer;
+ }
+
+ public String getAjaxActionURL() {
+ return getAjaxActionURL(FacesContext.getCurrentInstance());
+ }
+
+ public String getAjaxActionURL(FacesContext context) {
+ // Check arguments
+ if (null == context) {
+ throw new NullPointerException(
+ "Faces context for build AJAX Action URL is null");
+ }
+ UIViewRoot viewRoot = context.getViewRoot();
+ if (null == viewRoot) {
+ throw new NullPointerException(
+ "Faces view tree for build AJAX Action URL is null");
+ }
+ String viewId = viewRoot.getViewId();
+ if (null == viewId) {
+ throw new NullPointerException(
+ "View id for build AJAX Action URL is null");
+ }
+ if (!viewId.startsWith("/")) {
+ throw new IllegalArgumentException(
+ "Illegal view Id for build AJAX Action URL: " + viewId);
+ }
+ ViewHandler viewHandler = context.getApplication().getViewHandler();
+ return context.getExternalContext().encodeActionURL(
+ viewHandler.getActionURL(context, viewId));
+ }
+
+ /**
+ * @return the commonAjaxParameters
+ */
+ public Map getCommonAjaxParameters() {
+ return commonAjaxParameters;
+ }
+
+}
\ No newline at end of file
Deleted:
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java
===================================================================
---
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java 2007-06-29
17:10:57 UTC (rev 1415)
+++
branches/refactor1/framework/impl/src/main/java/org/ajax4jsf/framework/ajax/ViewIdHolder.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -1,37 +0,0 @@
-/**
- * License Agreement.
- *
- * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.ajax4jsf.framework.ajax;
-
-/**
- * Interface to skip navigation cases in ViewHandler and , instead, store new ViewId.
- * @author shura
- *
- */
-public interface ViewIdHolder {
-
- public boolean skipNavigation(String ViewId);
-
- public String getViewId();
-
- public void setViewId(String newViewId);
-
-}
Copied:
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java
(from rev 1393,
branches/refactor1/framework/api/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java)
===================================================================
---
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java
(rev 0)
+++
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/state/TreeStateAdvisor.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -0,0 +1,60 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces 3.0 - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component.state;
+
+import org.richfaces.component.UITree;
+
+
+/**
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com
+ * created 19.06.2007
+ *
+ * This interface is intended to "advising" changes to tree state. Tree
component should queue user-provided
+ * instance of the interface for changes during RENDER_RESPONSE phase. Changes advised by
user should be
+ * immediately applied. Advisor methods are provided with {@link UITree} component
instance to queue current
+ * tree state.
+ */
+public interface TreeStateAdvisor {
+ /**
+ * Advises new node opened/closed state
+ * @param tree {@link UITree} component state to queue state
+ * @return
+ * <ul>
+ * <li><code>null</code> if changes are not needed</li>
+ * <li>{@link Boolean#TRUE} to advise node to be opened</li>
+ * <li>{@link Boolean#FALSE} to advise node to be closed</li>
+ * </ul>
+ */
+ public Boolean adviseNodeOpened(UITree tree);
+
+ /**
+ * Advises new node selection
+ * @param tree {@link UITree} component state to queue state
+ * @return
+ * <ul>
+ * <li><code>null</code> if changes are not needed</li>
+ * <li>{@link Boolean#TRUE} to advise current node to be selected</li>
+ * <li>{@link Boolean#FALSE} to advise current node to be
unselected</li>
+ * </ul>
+ */
+ public Boolean adviseNodeSelected(UITree tree);
+}
Copied:
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java
(from rev 1393,
branches/refactor1/framework/api/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java)
===================================================================
---
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java
(rev 0)
+++
branches/refactor1/ui/tree/src/main/java/org/richfaces/component/xml/XmlTreeDataBuilder.java 2007-06-29
17:25:00 UTC (rev 1416)
@@ -0,0 +1,164 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces 3.0 - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component.xml;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.digester.Digester;
+import org.apache.commons.digester.RulesBase;
+import org.richfaces.component.TreeNode;
+import org.richfaces.component.TreeNodeImpl;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * @author Nick Belaevski - nbelaevski(a)exadel.com
+ * created 16.11.2006
+ *
+ */
+public class XmlTreeDataBuilder {
+
+ private final static class Rule extends org.apache.commons.digester.Rule {
+ private int level = -1;
+ private List idsList = new ArrayList();
+ private List treeNodesList = new ArrayList();
+ private List exclusionSets = new ArrayList();
+ private TreeNode treeNode = new TreeNodeImpl(); //add empty node to serve as root
+
+ public void begin(String namespace, String name, Attributes attributes)
+ throws Exception {
+ super.begin(namespace, name, attributes);
+
+ level++;
+
+ XmlNodeData xmlNodeData = new XmlNodeData();
+ xmlNodeData.setName(name);
+ xmlNodeData.setNamespace(namespace);
+
+ String id = null;
+
+ if (attributes != null) {
+ int length = attributes.getLength();
+ for (int i = 0; i < length; i++) {
+ xmlNodeData.setAttribute(attributes.getQName(i),
+ attributes.getValue(i));
+
+ }
+
+ id = attributes.getValue("id");
+ }
+
+ if (exclusionSets.size() == level) {
+ exclusionSets.add(null);
+ }
+
+ if (id == null || id.length() == 0) {
+ int currentId = 0;
+
+ if (idsList.size() <= level) {
+ for (int i = idsList.size(); i <= level; i++) {
+ idsList.add(null);
+ }
+ } else {
+ Integer integer = (Integer) idsList.get(level);
+ currentId = integer.intValue() + 1;
+ }
+
+ Set exclusions = (Set) exclusionSets.get(level);
+
+ while (exclusions != null &&
exclusions.contains(Integer.toString(currentId))) {
+ currentId++;
+ }
+
+ idsList.set(level, new Integer(currentId));
+
+ id = Integer.toString(currentId);
+ } else {
+ Set exclusions = (Set) exclusionSets.get(level);
+ if (exclusions == null) {
+ exclusions = new HashSet();
+
+ exclusionSets.set(level, exclusions);
+ }
+
+ exclusions.add(id);
+ }
+
+ TreeNode node = new TreeNodeImpl();
+ node.setData(xmlNodeData);
+
+ this.treeNode.addChild(id, node);
+ this.treeNodesList.add(this.treeNode);
+ this.treeNode = node;
+ }
+
+ public void body(String namespace, String name, String text)
+ throws Exception {
+ super.body(namespace, name, text);
+
+ if (text != null) {
+ ((XmlNodeData) this.treeNode.getData()).setText(text.trim());
+ }
+ }
+
+ public void end(String namespace, String name) throws Exception {
+ super.end(namespace, name);
+
+ level--;
+
+ if (idsList.size() - 1 > level + 1) {
+ //idsList grew larger than we really need
+ idsList.remove(idsList.size() - 1);
+ }
+
+ if (exclusionSets.size() - 1 > level + 1) {
+ //the same condition as above
+ exclusionSets.remove(exclusionSets.size() - 1);
+ }
+
+ this.treeNode = (TreeNode) this.treeNodesList.remove(this.treeNodesList.size() - 1);
+ }
+ }
+
+ public static TreeNode build(InputSource inputSource) throws SAXException, IOException
{
+ Digester digester = new Digester();
+ Rule rule = new Rule();
+ final List rulesList = new ArrayList(1);
+ rulesList.add(rule);
+
+ RulesBase rulesBase = new RulesBase() {
+ protected List lookup(String namespace, String name) {
+ return rulesList;
+ }
+ };
+ digester.setRules(rulesBase);
+ digester.setNamespaceAware(true);
+ digester.parse(inputSource);
+
+ return rule.treeNode;
+ }
+}