Search in sources :

Example 1 with LocationAwareFacesException

use of org.apache.myfaces.view.facelets.LocationAwareFacesException in project myfaces by apache.

the class CreateDynamicCompositeComponentListener method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
    Facelet componentFacelet;
    FaceletFactory faceletFactory = vdl.getFaceletFactory();
    FaceletFactory.setInstance(faceletFactory);
    try {
        componentFacelet = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
    } finally {
        FaceletFactory.setInstance(null);
    }
    UIComponent component = event.getComponent();
    // The execution of this listener activated another call to PostAddToViewEvent, because
    // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
    // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
    // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
    // have any side effect (counts as a refresh).
    Integer step = (Integer) component.getAttributes().get(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW);
    if (step != null && step == 0) {
        component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
    } else {
        return;
    }
    try {
        facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
        // Detect the relationship between parent and child, to ensure the component is properly created
        // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a
        // dynamic component, we need to do it here before apply the handler
        UIComponent parent = component.getParent();
        String facetName = null;
        if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component)) {
            facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
        }
        try {
            if (facetName != null) {
                parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY, facetName);
            }
            // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
            // find the component. Then we reset it to exclude it from facelets refresh algorithm.
            String markId = (String) component.getAttributes().get(FaceletViewDeclarationLanguage.GEN_MARK_ID);
            if (markId == null) {
                ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component, baseKey);
            } else {
                try {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
                    ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component.getParent(), baseKey);
                } finally {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
                }
            }
            if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext)) {
                FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
                if (fcc == null) {
                    FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
                    FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
                    component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                    component.getAttributes().put(DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
                } else {
                    component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                }
            }
        } finally {
            if (facetName != null) {
                parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
            }
        }
    } catch (IOException e) {
        throw new LocationAwareFacesException(e, component);
    } finally {
        facesContext.getAttributes().remove(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) UIComponent(jakarta.faces.component.UIComponent) FaceletViewDeclarationLanguage(org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage) IOException(java.io.IOException) RefreshDynamicComponentListener(org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException)

Example 2 with LocationAwareFacesException

use of org.apache.myfaces.view.facelets.LocationAwareFacesException in project myfaces by apache.

the class UIRepeat method process.

public void process(FacesContext faces, PhaseId phase) {
    // stop if not rendered
    if (!isRendered()) {
        return;
    }
    // validate attributes
    _validateAttributes();
    // reset index
    _captureScopeValues();
    _setIndex(-1);
    try {
        // has children
        if (getChildCount() > 0) {
            int i = getOffset();
            int begin = getBegin();
            int end = getEnd();
            if (begin == -1) {
                end = getSize();
                end = (end >= 0) ? i + end - 1 : Integer.MAX_VALUE - 1;
            }
            if (begin >= 0) {
                i = begin;
            }
            int step = getStep();
            // grab renderer
            String rendererType = getRendererType();
            Renderer renderer = null;
            if (rendererType != null) {
                renderer = getRenderer(faces);
            }
            _count = 0;
            _setIndex(i);
            while (((step > 0 && i <= end) || (step < 0 && i >= end)) && _isIndexAvailable()) {
                if (PhaseId.RENDER_RESPONSE.equals(phase) && renderer != null) {
                    renderer.encodeChildren(faces, this);
                } else {
                    for (int j = 0, childCount = getChildCount(); j < childCount; j++) {
                        UIComponent child = getChildren().get(j);
                        if (PhaseId.APPLY_REQUEST_VALUES.equals(phase)) {
                            child.processDecodes(faces);
                        } else if (PhaseId.PROCESS_VALIDATIONS.equals(phase)) {
                            child.processValidators(faces);
                        } else if (PhaseId.UPDATE_MODEL_VALUES.equals(phase)) {
                            child.processUpdates(faces);
                        } else if (PhaseId.RENDER_RESPONSE.equals(phase)) {
                            child.encodeAll(faces);
                        }
                    }
                }
                ++_count;
                i += step;
                _setIndex(i);
            }
        }
    } catch (IOException e) {
        throw new LocationAwareFacesException(e, this);
    } finally {
        _setIndex(-1);
        _restoreScopeValues();
    }
}
Also used : Renderer(jakarta.faces.render.Renderer) UIComponent(jakarta.faces.component.UIComponent) IOException(java.io.IOException) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException) VisitHint(jakarta.faces.component.visit.VisitHint)

Example 3 with LocationAwareFacesException

use of org.apache.myfaces.view.facelets.LocationAwareFacesException in project myfaces by apache.

the class UIRepeat method _validateAttributes.

private void _validateAttributes() throws FacesException {
    int begin = getBegin();
    int end = getEnd();
    int size = getSize();
    _emptyModel = getDataModel() == EMPTY_MODEL && begin != -1 && end != -1;
    int count = getRowCount();
    int offset = getOffset();
    if (begin == -1) {
        if (size >= 0) {
            end = getOffset() + getSize();
        }
    }
    if (end == -1 && size == -1) {
        if (begin == -1) {
            end = getDataModel().getRowCount();
        } else {
            end = getDataModel().getRowCount() - 1;
        }
    }
    int step = getStep();
    boolean sizeIsEnd = _emptyModel;
    boolean countdown = _emptyModel && end < begin;
    if (size == -1) {
        if (begin == -1) {
            size = end;
            sizeIsEnd = true;
        } else {
            size = countdown ? (begin - end + 1) / step : (end - begin + 1) / step;
        }
    }
    step = countdown && step > 0 ? -step : step;
    if (_emptyModel) {
        if (step > 0 && (end < begin)) {
            throw new LocationAwareFacesException("on empty models, end cannot be less than begin " + "when the step is positive", this);
        } else if (step < 0 && (end > begin)) {
            throw new LocationAwareFacesException("on empty models, end cannot be greater than begin " + "when the step is negative", this);
        }
        setStep(step);
    } else if (end >= 0) {
        if (size < 0) {
            throw new LocationAwareFacesException("iteration size cannot be less than zero", this);
        } else if (!sizeIsEnd && (begin == -1) && (offset + size) > end) {
            throw new LocationAwareFacesException("iteration size cannot be greater than collection size", this);
        } else if (!sizeIsEnd && (begin == -1) && (offset + size) > count) {
            throw new LocationAwareFacesException("iteration size cannot be greater than collection size", this);
        } else if (!sizeIsEnd && (begin >= 0) && (begin + size) > end + 1) {
            throw new LocationAwareFacesException("iteration size cannot be greater than collection size", this);
        } else if (!sizeIsEnd && (begin >= 0) && (end + 1 > count)) {
            throw new LocationAwareFacesException("end cannot be greater than collection size", this);
        } else if (!sizeIsEnd && (begin >= 0) && (begin > count)) {
            throw new LocationAwareFacesException("begin cannot be greater than collection size", this);
        }
    }
    if (begin >= 0 && begin > end) {
        throw new LocationAwareFacesException("begin cannot be greater than end", this);
    }
    if (size > -1 && offset > end) {
        throw new LocationAwareFacesException("iteration offset cannot be greater than collection size", this);
    }
    if (!_emptyModel && step == -1) {
        setStep(1);
    }
    if (!_emptyModel && step < 0) {
        throw new LocationAwareFacesException("iteration step size cannot be less than zero", this);
    } else if (step == 0) {
        throw new LocationAwareFacesException("iteration step size cannot be equal to zero", this);
    }
    _end = end;
}
Also used : LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException) VisitHint(jakarta.faces.component.visit.VisitHint)

Example 4 with LocationAwareFacesException

use of org.apache.myfaces.view.facelets.LocationAwareFacesException in project myfaces by apache.

the class UIRepeat method invokeOnComponent.

@Override
public boolean invokeOnComponent(FacesContext context, String clientId, ContextCallback callback) throws FacesException {
    Assert.notNull(context, "context");
    Assert.notNull(clientId, "clientId");
    Assert.notNull(callback, "callback");
    final String baseClientId = getClientId(context);
    // searching for this component?
    boolean returnValue = baseClientId.equals(clientId);
    boolean isCachedFacesContext = isTemporalFacesContext();
    if (!isCachedFacesContext) {
        setTemporalFacesContext(context);
    }
    pushComponentToEL(context, this);
    try {
        if (returnValue) {
            try {
                callback.invokeContextCallback(context, this);
                return true;
            } catch (Exception e) {
                throw new LocationAwareFacesException(e, this);
            }
        }
        // Now Look throught facets on this UIComponent
        if (this.getFacetCount() > 0) {
            for (Iterator<UIComponent> it = this.getFacets().values().iterator(); !returnValue && it.hasNext(); ) {
                returnValue = it.next().invokeOnComponent(context, clientId, callback);
            }
        }
        if (returnValue) {
            return returnValue;
        }
        // is the component an inner component?
        if (clientId.startsWith(baseClientId)) {
            // Check if the clientId for the component, which we
            // are looking for, has a rowIndex attached
            char separator = context.getNamingContainerSeparatorChar();
            String subId = clientId.substring(baseClientId.length() + 1);
            // the subId matches the regular expression
            if (clientId.charAt(baseClientId.length()) == separator && subId.matches("[0-9]+" + separator + ".*")) {
                String clientRow = subId.substring(0, subId.indexOf(separator));
                // safe the current index, count aside
                final int prevIndex = _index;
                final int prevCount = _count;
                try {
                    int invokeIndex = Integer.parseInt(clientRow);
                    // save the current scope values and set the right index
                    _captureScopeValues();
                    if (invokeIndex != -1) {
                        // calculate count for RepeatStatus
                        _count = _calculateCountForIndex(invokeIndex);
                    }
                    _setIndex(invokeIndex);
                    if (!_isIndexAvailable()) {
                        return false;
                    }
                    for (Iterator<UIComponent> it1 = getChildren().iterator(); !returnValue && it1.hasNext(); ) {
                        // recursive call to find the component
                        returnValue = it1.next().invokeOnComponent(context, clientId, callback);
                    }
                } finally {
                    // restore the previous count, index and scope values
                    _count = prevCount;
                    _setIndex(prevIndex);
                    _restoreScopeValues();
                }
            } else {
                // Searching for this component's children
                if (this.getChildCount() > 0) {
                    // Searching for this component's children/facets
                    for (Iterator<UIComponent> it = this.getChildren().iterator(); !returnValue && it.hasNext(); ) {
                        returnValue = it.next().invokeOnComponent(context, clientId, callback);
                    }
                }
            }
        }
    } finally {
        // all components must call popComponentFromEl after visiting is finished
        popComponentFromEL(context);
        if (!isCachedFacesContext) {
            setTemporalFacesContext(null);
        }
    }
    return returnValue;
}
Also used : UIComponent(jakarta.faces.component.UIComponent) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException) AbortProcessingException(jakarta.faces.event.AbortProcessingException) FacesException(jakarta.faces.FacesException) IOException(java.io.IOException) VisitHint(jakarta.faces.component.visit.VisitHint)

Example 5 with LocationAwareFacesException

use of org.apache.myfaces.view.facelets.LocationAwareFacesException in project myfaces by apache.

the class JsfElementRenderer method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    String elementName = (String) component.getPassThroughAttributes().get(Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY);
    if (elementName == null) {
        throw new LocationAwareFacesException("jsf:element with clientId" + component.getClientId(facesContext) + " requires 'elementName' passthrough attribute", component);
    }
    JsfElement jsfElement = (JsfElement) component;
    Map<String, List<ClientBehavior>> behaviors = jsfElement.getClientBehaviors();
    if (behaviors != null && !behaviors.isEmpty()) {
        ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
    }
    writer.startElement(elementName, component);
    if (!behaviors.isEmpty()) {
        HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
    } else {
        HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
    }
    // Write in the optimized way, because this is a renderer for internal use only
    long commonPropertiesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(component);
    if (behaviors.isEmpty()) {
        CommonHtmlAttributesUtil.renderEventProperties(writer, commonPropertiesMarked, component);
        CommonHtmlAttributesUtil.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
        CommonHtmlAttributesUtil.renderChangeSelectEventProperties(writer, commonPropertiesMarked, component);
    } else {
        long commonEventsMarked = CommonHtmlEventsUtil.getMarkedEvents(component);
        CommonHtmlEventsUtil.renderBehaviorizedEventHandlers(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, behaviors);
        CommonHtmlEventsUtil.renderBehaviorizedFieldEventHandlers(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, component.getClientId(facesContext), behaviors);
    }
    CommonHtmlAttributesUtil.renderStyleProperties(writer, commonPropertiesMarked, component);
    HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONLOAD_ATTR, component, ClientBehaviorEvents.LOAD, behaviors, HTML.ONLOAD_ATTR);
    HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONUNLOAD_ATTR, component, ClientBehaviorEvents.UNLOAD, behaviors, HTML.ONUNLOAD_ATTR);
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) List(java.util.List) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException)

Aggregations

LocationAwareFacesException (org.apache.myfaces.view.facelets.LocationAwareFacesException)6 UIComponent (jakarta.faces.component.UIComponent)4 VisitHint (jakarta.faces.component.visit.VisitHint)3 IOException (java.io.IOException)3 FacesContext (jakarta.faces.context.FacesContext)2 FacesException (jakarta.faces.FacesException)1 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 AbortProcessingException (jakarta.faces.event.AbortProcessingException)1 Renderer (jakarta.faces.render.Renderer)1 Facelet (jakarta.faces.view.facelets.Facelet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AbstractFacelet (org.apache.myfaces.view.facelets.AbstractFacelet)1 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)1 FaceletFactory (org.apache.myfaces.view.facelets.FaceletFactory)1 FaceletViewDeclarationLanguage (org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage)1 RefreshDynamicComponentListener (org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener)1