@Override
  public boolean visitTree(VisitContext visitContext, VisitCallback callback) {

    // First check to see whether we are visitable. If not
    // short-circuit out of this subtree, though allow the
    // visit to proceed through to other subtrees.
    if (!isVisitable(visitContext)) {
      return false;
    }

    // Clear out the row index is one is set so that
    // we start from a clean slate.
    FacesContext facesContext = visitContext.getFacesContext();

    int oldRowIndex = getRowIndex();
    setRowIndex(-1);

    // Push ourselves to EL
    pushComponentToEL(facesContext, null);

    try {

      // Visit ourselves. Note that we delegate to the
      // VisitContext to actually perform the visit.
      VisitResult result = visitContext.invokeVisitCallback(this, callback);

      // If the visit is complete, short-circuit out and end the visit
      if (result == VisitResult.COMPLETE) {
        return true;
      }

      // Visit children, short-circuiting as necessary
      if ((result == VisitResult.ACCEPT) && doVisitChildren(visitContext)) {
        setRowIndex(-1);

        if (visitFixedChildren(visitContext, callback)) {
          return true;
        }

        if (visitDataChildren(visitContext, callback)) {
          return true;
        }
      }
    } finally {

      // Clean up - pop EL and restore old row index
      popComponentFromEL(facesContext);

      try {
        setRowIndex(oldRowIndex);
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
      }
    }

    // Return false to allow the visit to continue
    return false;
  }
  @Override
  public boolean visitTree(VisitContext context, VisitCallback callback) {
    if (!isVisitable(context)) {
      return false;
    }

    FacesContext facesContext = context.getFacesContext();
    pushComponentToEL(facesContext, null);

    try {
      VisitResult result = context.invokeVisitCallback(this, callback);

      if (result == VisitResult.COMPLETE) {
        return true;
      }

      if (result == VisitResult.ACCEPT) {
        if (context instanceof ExtendedVisitContext) {
          ExtendedVisitContext extendedVisitContext = (ExtendedVisitContext) context;
          if (extendedVisitContext.getVisitMode() == ExtendedVisitContextMode.RENDER) {

            result =
                extendedVisitContext.invokeMetaComponentVisitCallback(
                    this, callback, ACTIVE_ITEM_META_COMPONENT);
            if (result == VisitResult.COMPLETE) {
              return true;
            }
          }
        }
      }

      if (result == VisitResult.ACCEPT) {
        Iterator<UIComponent> kids = this.getFacetsAndChildren();

        while (kids.hasNext()) {
          boolean done = kids.next().visitTree(context, callback);

          if (done) {
            return true;
          }
        }
      }
    } finally {
      popComponentFromEL(facesContext);
    }

    return false;
  }
Exemple #3
0
  private void deliverPostRestoreStateEvent(FacesContext facesContext) throws FacesException {
    UIViewRoot root = facesContext.getViewRoot();
    final PostRestoreStateEvent postRestoreStateEvent = new PostRestoreStateEvent(root);
    try {
      Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION);
      VisitContext visitContext = VisitContext.createVisitContext(facesContext, null, hints);
      root.visitTree(
          visitContext,
          new VisitCallback() {

            public VisitResult visit(VisitContext context, UIComponent target) {
              postRestoreStateEvent.setComponent(target);
              target.processEvent(postRestoreStateEvent);
              //noinspection ReturnInsideFinallyBlock
              return VisitResult.ACCEPT;
            }
          });
    } catch (AbortProcessingException e) {
      facesContext
          .getApplication()
          .publishEvent(
              facesContext,
              ExceptionQueuedEvent.class,
              new ExceptionQueuedEventContext(facesContext, e, null, PhaseId.RESTORE_VIEW));
    }
  }
Exemple #4
0
  /** @see UIComponent#visitTree */
  @Override
  public boolean visitTree(VisitContext context, VisitCallback callback) {

    // NamingContainers can optimize partial tree visits by taking advantage
    // of the fact that it is possible to detect whether any ids to visit
    // exist underneath the NamingContainer.  If no such ids exist, there
    // is no need to visit the subtree under the NamingContainer.

    // UIForm is a bit different from other NamingContainers.  It only acts
    // as a NamingContainer when prependId is true.  Note that if it
    // weren't for this, we could push this implementation up in to
    // UIComponent and share it across all NamingContainers.  Instead,
    // we currently duplicate this implementation in UIForm and
    // UINamingContainer, so that we can check isPrependId() here.

    if (!this.isPrependId()) {
      return super.visitTree(context, callback);
    }

    Collection<String> idsToVisit = context.getSubtreeIdsToVisit(this);
    assert (idsToVisit != null);

    // If we have ids to visit, let the superclass implementation
    // handle the visit
    if (!idsToVisit.isEmpty()) {
      return super.visitTree(context, callback);
    }

    // If we have no child ids to visit, just visit ourselves, if
    // we are visitable.
    if (isVisitable(context)) {
      FacesContext facesContext = context.getFacesContext();
      pushComponentToEL(facesContext, null);

      try {
        VisitResult result = context.invokeVisitCallback(this, callback);
        return (result == VisitResult.COMPLETE);
      } finally {
        popComponentFromEL(facesContext);
      }
    }

    // Done visiting this subtree.  Return false to allow
    // visit to continue.
    return false;
  }
  private boolean doVisitChildren(VisitContext context) {
    // TODO optimize for returned IDs
    Collection<String> idsToVisit = context.getSubtreeIdsToVisit(this);

    assert idsToVisit != null;

    // All ids or non-empty collection means we need to visit our children.
    return !idsToVisit.isEmpty();
  }
Exemple #6
0
 @Override
 public boolean visitTree(VisitContext visitContext, VisitCallback callback) {
   FacesContext facesContext = visitContext.getFacesContext();
   AliasVariableMapper alias = getAliasVariableMapper(facesContext);
   try {
     AliasVariableMapper.exposeAliasesToRequest(facesContext, alias);
     return super.visitTree(visitContext, callback);
   } finally {
     if (alias != null) {
       AliasVariableMapper.removeAliasesExposedToRequest(facesContext, alias.getId());
     }
   }
 }
  /** Method process chart tags, it collects chart options and data. */
  @Override
  public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    super.encodeBegin(context, component);

    AbstractChart chart = (AbstractChart) component;

    VisitChart visitCallback = new VisitChart(chart);
    // copy attributes to parent tag and process data
    chart.visitTree(
        VisitContext.createVisitContext(FacesContext.getCurrentInstance()), visitCallback);

    // store data to parent tag
    component.getAttributes().put("chartData", visitCallback.getData());

    if (!visitCallback.isDataEmpty()) {
      component.getAttributes().put("charttype", visitCallback.getChartType());
      component.getAttributes().put("xtype", axisDataTypeToString(visitCallback.getKeyType()));
      component.getAttributes().put("ytype", axisDataTypeToString(visitCallback.getValType()));
    }

    // set flag whether request to server should be sent
    boolean anyServerSideListener = chart.getPlotClickListener() != null ? true : false;
    if (!anyServerSideListener) {
      // check if there is particular series listener
      List<MethodExpression> listeners = visitCallback.getParticularSeriesListeners();
      for (MethodExpression methodExpression : listeners) {
        if (methodExpression != null) {
          anyServerSideListener = true;
          break;
        }
      }
    }
    component.getAttributes().put("serverSideListener", anyServerSideListener);

    // client-side handlers for particular series
    component.getAttributes().put("handlers", visitCallback.getSeriesSpecificHandlers());

    // server-side listeners for particular series
    component
        .getAttributes()
        .put("particularSeriesListeners", visitCallback.getParticularSeriesListeners());
  }
Exemple #8
0
  public void testSpecificIdTraversal() throws Exception {
    buildTree();
    UIViewRoot root = getFacesContext().getViewRoot();
    final StringBuilder builder = new StringBuilder();

    HashSet ids = new HashSet();
    ids.add("form:panel0:data:3:output0");
    ids.add("form:panel1:data:0:output0");
    root.visitTree(
        VisitContext.createVisitContext(getFacesContext(), ids, null),
        new VisitCallback() {
          public VisitResult visit(VisitContext context, UIComponent target) {
            builder.append(target.getClientId(context.getFacesContext()) + " ");
            return VisitResult.ACCEPT;
          }
        });
    System.out.println(builder);
    String result = builder.toString().trim();
    assertEquals(result, "form:panel0:data:3:output0 form:panel1:data:0:output0");
  }
  protected boolean visitDataChildren(VisitContext visitContext, VisitCallback callback) {
    int rowIndex = 0;

    for (setRowIndex(rowIndex); isRowAvailable(); setRowIndex(++rowIndex)) {
      VisitResult result = visitContext.invokeVisitCallback(this, callback);

      if (result == VisitResult.COMPLETE) {
        return true;
      }

      if (result == VisitResult.REJECT) {
        continue;
      }

      for (UIComponent child : getChildren()) {
        if (child.visitTree(visitContext, callback)) {
          return true;
        }
      }
    }

    return false;
  }
Exemple #10
0
  /**
   * PRECONDITION: the necessary factories have been installed in the ServletContext attr set.
   *
   * <p>
   *
   * <p>POSTCONDITION: The facesContext has been initialized with a tree.
   */
  public void execute(FacesContext facesContext) throws FacesException {

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Entering RestoreViewPhase");
    }
    if (null == facesContext) {
      throw new FacesException(
          MessageUtils.getExceptionMessageString(MessageUtils.NULL_CONTEXT_ERROR_MESSAGE_ID));
    }

    // If an app had explicitely set the tree in the context, use that;
    //
    UIViewRoot viewRoot = facesContext.getViewRoot();
    if (viewRoot != null) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Found a pre created view in FacesContext");
      }
      facesContext.getViewRoot().setLocale(facesContext.getExternalContext().getRequestLocale());

      // do per-component actions
      UIViewRoot root = facesContext.getViewRoot();
      final PostRestoreStateEvent event = new PostRestoreStateEvent(root);
      try {
        root.visitTree(
            VisitContext.createVisitContext(facesContext),
            new VisitCallback() {

              public VisitResult visit(VisitContext context, UIComponent target) {
                event.setComponent(target);
                target.processEvent(event);
                return VisitResult.ACCEPT;
              }
            });
      } catch (AbortProcessingException e) {
        facesContext
            .getApplication()
            .publishEvent(
                ExceptionQueuedEvent.class, new ExceptionQueuedEventContext(facesContext, e));
      }

      if (!facesContext.isPostback()) {
        facesContext.renderResponse();
      }
      return;
    }

    // Reconstitute or create the request tree
    Map requestMap = facesContext.getExternalContext().getRequestMap();
    String viewId = (String) requestMap.get("javax.servlet.include.path_info");
    if (viewId == null) {
      viewId = facesContext.getExternalContext().getRequestPathInfo();
    }

    // It could be that this request was mapped using
    // a prefix mapping in which case there would be no
    // path_info.  Query the servlet path.
    if (viewId == null) {
      viewId = (String) requestMap.get("javax.servlet.include.servlet_path");
    }

    if (viewId == null) {
      viewId = facesContext.getExternalContext().getRequestServletPath();
    }

    if (viewId == null) {
      if (LOGGER.isLoggable(Level.WARNING)) {
        LOGGER.warning("viewId is null");
      }
      throw new FacesException(
          MessageUtils.getExceptionMessageString(MessageUtils.NULL_REQUEST_VIEW_ERROR_MESSAGE_ID));
    }

    ViewHandler viewHandler = Util.getViewHandler(facesContext);

    boolean isPostBack = (facesContext.isPostback() && !isErrorPage(facesContext));
    if (isPostBack) {
      // try to restore the view
      viewRoot = viewHandler.restoreView(facesContext, viewId);
      if (viewRoot == null) {
        if (is11CompatEnabled(facesContext)) {
          // 1.1 -> create a new view and flag that the response should
          //        be immediately rendered
          if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Postback: recreating a view for " + viewId);
          }
          viewRoot = viewHandler.createView(facesContext, viewId);
          facesContext.renderResponse();

        } else {
          Object[] params = {viewId};
          throw new ViewExpiredException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.RESTORE_VIEW_ERROR_MESSAGE_ID, params),
              viewId);
        }
      }

      facesContext.setViewRoot(viewRoot);

      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Postback: restored view for " + viewId);
      }
    } else {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("New request: creating a view for " + viewId);
      }

      ViewDeclarationLanguage vdl =
          facesContext
              .getApplication()
              .getViewHandler()
              .getViewDeclarationLanguage(facesContext, viewId);

      if (vdl != null) {
        // If we have one, get the ViewMetadata...
        ViewMetadata metadata = vdl.getViewMetadata(facesContext, viewId);

        if (metadata != null) { // perhaps it's not supported
          // and use it to create the ViewRoot.  This will have, at most
          // the UIViewRoot and its metadata facet.
          viewRoot = metadata.createMetadataView(facesContext);

          // Only skip to render response if there are no view parameters
          Collection<UIViewParameter> params = ViewMetadata.getViewParameters(viewRoot);
          if (params.isEmpty()) {
            facesContext.renderResponse();
          }
        }
      } else {
        facesContext.renderResponse();
      }

      if (null == viewRoot) {
        viewRoot = (Util.getViewHandler(facesContext)).createView(facesContext, viewId);
      }
      facesContext.setViewRoot(viewRoot);
      assert (null != viewRoot);
      facesContext.getApplication().publishEvent(PostAddToViewEvent.class, viewRoot);
    }

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Exiting RestoreViewPhase");
    }
  }
    @Override
    public VisitResult visit(VisitContext context, UIComponent target) {

      if (target instanceof AbstractLegend) {
        copyAttrs(target, chart, "", asList("position", "sorting"));
      } else if (target instanceof AbstractSeries) {
        AbstractSeries s = (AbstractSeries) target;
        ChartDataModel model = s.getData();
        particularSeriesListeners.add(s.getPlotClickListener());

        // Collect Series specific handlers
        Map<String, Object> optMap = new HashMap<String, Object>();
        RenderKitUtils.Attributes seriesEvents =
            attributes()
                .generic("onplothover", "onplothover", "plothover")
                .generic("onplotclick", "onplotclick", "plotclick");

        addToScriptHash(
            optMap,
            context.getFacesContext(),
            target,
            seriesEvents,
            RenderKitUtils.ScriptHashVariableWrapper.eventHandler);

        if (optMap.get("onplotclick") != null) {
          plotClickHandlers.put(new RawJSONString(optMap.get("onplotclick").toString()));
        } else {
          plotClickHandlers.put(s.getOnplotclick());
        }

        if (optMap.get("onplothover") != null) {
          plothoverHandlers.put(new RawJSONString(optMap.get("onplothover").toString()));
        } else {
          plothoverHandlers.put(s.getOnplothover());
        }
        // end collect series specific handler

        if (model == null) {
          /**
           * data model priority: if there is data model passed through data attribute use it.
           * Otherwise nested point tags are expected.
           */
          VisitSeries seriesCallback = new VisitSeries(s.getType());
          s.visitTree(
              VisitContext.createVisitContext(FacesContext.getCurrentInstance()), seriesCallback);
          model = seriesCallback.getModel();

          // if series has no data create empty model
          if (model == null) {
            switch (s.getType()) {
              case line:
                model = new NumberChartDataModel(ChartType.line);
                break;
              case bar:
                model = new NumberChartDataModel(ChartType.bar);
                break;
              case pie:
                model = new StringChartDataModel(ChartType.pie);
                break;
              default:
                break;
            }
          } else {
            nodata = false;
          }
        } else {
          nodata = false;
        }
        model.setAttributes(s.getAttributes());

        try {
          // Check model/series compatibility

          if (chartType == null && (!nodata)) {
            // if series is empty do not set types
            chartType = model.getType();
            keyType = model.getKeyType();
            valType = model.getValueType();
          } else {
            if (chartType == ChartDataModel.ChartType.pie) {
              throw new IllegalArgumentException("Pie chart supports only one series.");
            }
          }
          if (keyType != model.getKeyType() || valType != model.getValueType()) {
            throw new IllegalArgumentException("Data model is not valid for this chart type.");
          }

          data.put(model.export());
        } catch (IOException ex) {
          throw new FacesException(ex);
        }

      } else if (target instanceof AbstractXAxis) {
        copyAttrs(target, chart, "x", asList("min", "max", "pad", "label", "format"));
      } else if (target instanceof AbstractYAxis) {
        copyAttrs(target, chart, "y", asList("min", "max", "pad", "label", "format"));
      }
      return VisitResult.ACCEPT;
    }