Search in sources :

Example 11 with Renderer

use of javax.faces.render.Renderer in project liferay-faces-bridge-impl by liferay.

the class HeadRendererPrimeFacesImpl method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    UIViewRoot originalUIViewRoot = facesContext.getViewRoot();
    if (isMobile(facesContext)) {
        List<UIComponent> componentResources = originalUIViewRoot.getComponentResources(facesContext, "head");
        List<UIComponent> resourcesToRemove = new ArrayList<UIComponent>();
        for (UIComponent componentResource : componentResources) {
            // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L96-L104.
            if (isComponentResourceSuppressedWhenMobile(componentResource)) {
                componentResource.setRendered(false);
            } else // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
            if (isMobileComponentResource(componentResource)) {
                resourcesToRemove.add(componentResource);
            }
        }
        for (UIComponent resourceToRemove : resourcesToRemove) {
            originalUIViewRoot.removeComponentResource(facesContext, resourceToRemove, "head");
        }
    }
    // Invoke the PrimeFaces HeadRenderer so that it has the opportunity to add css and/or script resources to the
    // view root. However, the PrimeFaces HeadRenderer must be captured (and thus prevented from actually rendering
    // any resources) so that they can instead be rendered by the superclass.
    FacesContext primeFacesContext = new FacesContextPrimeFacesHeadImpl(facesContext);
    ResponseWriter origResponseWriter = primeFacesContext.getResponseWriter();
    PrimeFacesHeadResponseWriter primeFacesHeadResponseWriter = new PrimeFacesHeadResponseWriter();
    primeFacesContext.setResponseWriter(primeFacesHeadResponseWriter);
    ResourceCapturingUIViewRoot resourceCapturingUIViewRoot = new ResourceCapturingUIViewRoot();
    primeFacesContext.setViewRoot(resourceCapturingUIViewRoot);
    Renderer primeFacesHeadRenderer = getPrimeFacesHeadRenderer(facesContext);
    primeFacesHeadRenderer.encodeBegin(primeFacesContext, uiComponent);
    primeFacesContext.setViewRoot(originalUIViewRoot);
    primeFacesContext.setResponseWriter(origResponseWriter);
    // Get the list of captured resources.
    List<UIComponent> capturedResources = resourceCapturingUIViewRoot.getCapturedComponentResources("head");
    List<UIComponent> capturedMobileResources = new ArrayList<UIComponent>();
    // The PrimeFaces 5.1+ HeadRenderer properly adds resources like "validation/validation.js" to the view root,
    // which makes it possible to easily capture the resources that it wants to add to the head. However, the
    // PrimeFaces 5.0/4.0 HeadRenderer does not add resources to the view root. Instead, it encodes a <script>
    // element to the response writer with a "src" attribute containing a URL (an external script). When this
    // occurs, it is necessary to reverse-engineer the URL of each external script in order to determine the
    // name/library of the corresponding JSF2 resource.
    List<String> externalResourceURLs = primeFacesHeadResponseWriter.getExternalResourceURLs();
    // For each external script URL:
    if (externalResourceURLs.size() > 0) {
        ExternalContext externalContext = facesContext.getExternalContext();
        String resourceNameParam = externalContext.encodeNamespace("javax.faces.resource");
        String libraryNameParam = externalContext.encodeNamespace("ln");
        for (String externalResourceURL : externalResourceURLs) {
            // Determine the value of the "javax.faces.resource" and "ln" parameters from the URL.
            String resourceName = null;
            String libraryName = null;
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            String characterEncoding = responseWriter.getCharacterEncoding();
            Map<String, String[]> parsedParameterMapValuesArray = URLUtil.parseParameterMapValuesArray(externalResourceURL, characterEncoding);
            if (parsedParameterMapValuesArray != null) {
                String[] resourceNameParamValues = parsedParameterMapValuesArray.get(resourceNameParam);
                if ((resourceNameParamValues == null) || (resourceNameParamValues.length < 1)) {
                    resourceNameParamValues = parsedParameterMapValuesArray.get("javax.faces.resource");
                }
                if ((resourceNameParamValues != null) && (resourceNameParamValues.length > 0)) {
                    resourceName = resourceNameParamValues[0];
                }
                if (resourceName == null) {
                    int indexOfResource = externalResourceURL.indexOf("javax.faces.resource/");
                    int indexOfQuery = externalResourceURL.indexOf("?");
                    if (indexOfResource > -1) {
                        int indexOfResourceName = indexOfResource + "javax.faces.resource/".length();
                        if (indexOfQuery > -1) {
                            resourceName = externalResourceURL.substring(indexOfResourceName, indexOfQuery);
                        } else {
                            resourceName = externalResourceURL.substring(indexOfResourceName);
                        }
                    }
                }
                String[] libraryNameParamValues = parsedParameterMapValuesArray.get(libraryNameParam);
                if ((libraryNameParamValues == null) || (libraryNameParamValues.length < 1)) {
                    libraryNameParamValues = parsedParameterMapValuesArray.get("ln");
                }
                if ((libraryNameParamValues != null) && (libraryNameParamValues.length > 0)) {
                    libraryName = libraryNameParamValues[0];
                }
            }
            // resource and add it to the view root.
            if ((resourceName != null) && (libraryName != null)) {
                if (resourceName.equals(PRIMEFACES_THEME_RESOURCE_NAME) && libraryName.startsWith(PRIMEFACES_THEME_PREFIX)) {
                    ResourceComponent primefacesThemeResource = new ResourceComponent(facesContext, resourceName, libraryName, externalContext.encodeNamespace(""));
                    Map<Object, Object> facesContextAttributes = facesContext.getAttributes();
                    facesContextAttributes.put("primefacesTheme", primefacesThemeResource);
                } else {
                    Application application = facesContext.getApplication();
                    ResourceHandler resourceHandler = application.getResourceHandler();
                    UIComponent resource = application.createComponent(UIOutput.COMPONENT_TYPE);
                    String rendererType = resourceHandler.getRendererTypeForResourceName(resourceName);
                    resource.setRendererType(rendererType);
                    resource.setTransient(true);
                    resource.getAttributes().put("name", resourceName);
                    resource.getAttributes().put("library", libraryName);
                    resource.getAttributes().put("target", "head");
                    if (isMobile(facesContext)) {
                        if (isMobileComponentResource(resourceName, libraryName)) {
                            capturedMobileResources.add(resource);
                        } else {
                            if (isComponentResourceSuppressedWhenMobile(resourceName, libraryName)) {
                                resource.setRendered(false);
                            }
                            capturedResources.add(resource);
                        }
                    } else {
                        capturedResources.add(resource);
                    }
                }
            }
        }
    }
    // superclass.
    for (UIComponent componentResource : capturedResources) {
        originalUIViewRoot.addComponentResource(facesContext, componentResource, "head");
    }
    // FACES-2061: If the PrimeFaces HeadRenderer attempted to render an inline script (as is the case when
    // PrimeFaces client side validation is activated) then add a component that can render the script to the view
    // root.
    List<InlineScript> inlineScripts = primeFacesHeadResponseWriter.getInlineScripts();
    if (!inlineScripts.isEmpty()) {
        // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
        if (isMobile(facesContext)) {
            InlineScript mobileInlineScript = inlineScripts.remove(0);
            ListIterator<UIComponent> listIterator = capturedMobileResources.listIterator();
            while (listIterator.hasNext()) {
                UIComponent mobileComponentResource = listIterator.next();
                Map<String, Object> attributes = mobileComponentResource.getAttributes();
                String name = (String) attributes.get("name");
                if (name.equals("jquery/jquery.js")) {
                    listIterator.add(mobileInlineScript);
                    break;
                }
            }
        }
        for (InlineScript primeFacesInlineScript : inlineScripts) {
            originalUIViewRoot.addComponentResource(facesContext, primeFacesInlineScript, "head");
        }
    }
    if (isMobile(facesContext)) {
        // Save captured mobile resources so that they can be rendered in as middle resources before other scripts.
        // For more information, see HeadRendererBridgeImpl.encodeChildren(),
        // http://demos.jquerymobile.com/1.0/docs/api/globalconfig.html, and
        // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
        Map<Object, Object> attributes = facesContext.getAttributes();
        attributes.put(MOBILE_COMPONENT_RESOURCES_KEY, capturedMobileResources);
    }
    // Delegate rendering to the superclass so that it can write resources found in the view root to the head
    // section of the portal page.
    super.encodeBegin(facesContext, uiComponent);
}
Also used : FacesContext(javax.faces.context.FacesContext) ArrayList(java.util.ArrayList) ResourceHandler(javax.faces.application.ResourceHandler) ExternalContext(javax.faces.context.ExternalContext) UIViewRoot(javax.faces.component.UIViewRoot) UIComponent(javax.faces.component.UIComponent) ResponseWriter(javax.faces.context.ResponseWriter) ResourceComponent(com.liferay.faces.bridge.component.internal.ResourceComponent) Renderer(javax.faces.render.Renderer) Application(javax.faces.application.Application) InlineScript(com.liferay.faces.bridge.renderkit.html_basic.internal.InlineScript)

Example 12 with Renderer

use of javax.faces.render.Renderer in project liferay-faces-bridge-impl by liferay.

the class RenderKitBridgeImpl method getRenderer.

@Override
public Renderer getRenderer(String family, String rendererType) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    ProductFactory productFactory = (ProductFactory) FactoryExtensionFinder.getFactory(externalContext, ProductFactory.class);
    final Product PRIMEFACES = productFactory.getProductInfo(Product.Name.PRIMEFACES);
    final boolean PRIMEFACES_DETECTED = PRIMEFACES.isDetected();
    Renderer renderer = super.getRenderer(family, rendererType);
    if (UIOutput.COMPONENT_FAMILY.equals(family)) {
        if (JAVAX_FACES_HEAD.equals(rendererType)) {
            final Product ICEFACES = productFactory.getProductInfo(Product.Name.ICEFACES);
            final boolean ICEFACES_DETECTED = ICEFACES.isDetected();
            if (ICEFACES_DETECTED) {
                renderer = new HeadRendererICEfacesImpl();
            } else if (PRIMEFACES_DETECTED) {
                renderer = new HeadRendererPrimeFacesImpl();
            } else {
                renderer = new HeadRendererBridgeImpl();
            }
        } else if (JAVAX_FACES_BODY.equals(rendererType)) {
            renderer = new BodyRendererBridgeImpl(renderer);
        } else if (RenderKitUtil.SCRIPT_RENDERER_TYPE.equals(rendererType) || RenderKitUtil.STYLESHEET_RENDERER_TYPE.equals(rendererType)) {
            renderer = new ResourceRendererBridgeImpl(renderer);
        }
    } else if (UIForm.COMPONENT_FAMILY.equals(family) && JAVAX_FACES_FORM.equals(rendererType) && PRIMEFACES_DETECTED) {
        renderer = new FormRendererPrimeFacesImpl(PRIMEFACES.getMajorVersion(), PRIMEFACES.getMinorVersion(), renderer);
    } else if (PRIMEFACES_FAMILY.equals(family) && PRIMEFACES_FILE_UPLOAD_RENDERER_TYPE.equals(rendererType)) {
        renderer = new FileUploadRendererPortletImpl(renderer);
    } else if (RICHFACES_FILE_UPLOAD_FAMILY.equals(family) && RICHFACES_FILE_UPLOAD_RENDERER_TYPE.equals(rendererType)) {
        renderer = new FileUploadRendererPortletImpl(new FileUploadRendererRichFacesImpl(renderer));
    }
    return renderer;
}
Also used : FacesContext(javax.faces.context.FacesContext) ProductFactory(com.liferay.faces.util.product.ProductFactory) FileUploadRendererPortletImpl(com.liferay.faces.bridge.renderkit.bridge.internal.FileUploadRendererPortletImpl) Product(com.liferay.faces.util.product.Product) FileUploadRendererRichFacesImpl(com.liferay.faces.bridge.renderkit.richfaces.internal.FileUploadRendererRichFacesImpl) HeadRendererICEfacesImpl(com.liferay.faces.bridge.renderkit.icefaces.internal.HeadRendererICEfacesImpl) FormRendererPrimeFacesImpl(com.liferay.faces.bridge.renderkit.primefaces.internal.FormRendererPrimeFacesImpl) ExternalContext(javax.faces.context.ExternalContext) Renderer(javax.faces.render.Renderer) HeadRendererPrimeFacesImpl(com.liferay.faces.bridge.renderkit.primefaces.internal.HeadRendererPrimeFacesImpl)

Example 13 with Renderer

use of javax.faces.render.Renderer in project liferay-faces-bridge-impl by liferay.

the class ResourceRendererBridgeImplTest method testSaveRestoreStateFACES_3280.

@Test
public void testSaveRestoreStateFACES_3280() {
    Renderer renderer = new RendererMockImpl();
    ResourceRendererBridgeImpl resourceRendererBridgeImpl = new ResourceRendererBridgeImpl(renderer);
    Object state = resourceRendererBridgeImpl.saveState(null);
    Assert.assertEquals(renderer.getClass(), state);
    resourceRendererBridgeImpl = new ResourceRendererBridgeImpl();
    Assert.assertNull(resourceRendererBridgeImpl.getWrapped());
    resourceRendererBridgeImpl.restoreState(null, state);
    Assert.assertNotNull(resourceRendererBridgeImpl.getWrapped());
}
Also used : Renderer(javax.faces.render.Renderer) Test(org.junit.Test)

Example 14 with Renderer

use of javax.faces.render.Renderer in project liferay-faces-alloy by liferay.

the class OutputScriptStylesheetRendererBase method processEvent.

@Override
public void processEvent(ComponentSystemEvent componentSystemEvent) throws AbortProcessingException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Renderer delegateRenderer = getDelegateRenderer(facesContext);
    if (delegateRenderer instanceof ComponentSystemEventListener) {
        ComponentSystemEventListener delegateComponentSystemEventListener = (ComponentSystemEventListener) delegateRenderer;
        delegateComponentSystemEventListener.processEvent(componentSystemEvent);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ComponentSystemEventListener(javax.faces.event.ComponentSystemEventListener) Renderer(javax.faces.render.Renderer)

Example 15 with Renderer

use of javax.faces.render.Renderer in project liferay-faces-alloy by liferay.

the class AbstractButtonRenderer method decode.

@Override
public void decode(FacesContext facesContext, UIComponent uiComponent) {
    if (hasMenu(uiComponent)) {
        UIComponent nodeMenuNav = NodeMenuNavFactory.getNodeMenuNav(uiComponent);
        RenderKit renderKit = facesContext.getRenderKit();
        Renderer nodeMenuNavRenderer = renderKit.getRenderer(nodeMenuNav.getFamily(), nodeMenuNav.getRendererType());
        nodeMenuNavRenderer.decode(facesContext, nodeMenuNav);
    } else {
        super.decode(facesContext, uiComponent);
    }
}
Also used : UIComponent(javax.faces.component.UIComponent) RenderKit(javax.faces.render.RenderKit) Renderer(javax.faces.render.Renderer)

Aggregations

Renderer (javax.faces.render.Renderer)18 UIComponent (javax.faces.component.UIComponent)7 RenderKit (javax.faces.render.RenderKit)7 FacesContext (javax.faces.context.FacesContext)6 ResponseWriter (javax.faces.context.ResponseWriter)4 IOException (java.io.IOException)3 FacesException (javax.faces.FacesException)3 ExternalContext (javax.faces.context.ExternalContext)2 AjaxBehaviorEvent (javax.faces.event.AjaxBehaviorEvent)2 Test (org.junit.Test)2 SelectEvent (org.primefaces.event.SelectEvent)2 ResourceComponent (com.liferay.faces.bridge.component.internal.ResourceComponent)1 FileUploadRendererPortletImpl (com.liferay.faces.bridge.renderkit.bridge.internal.FileUploadRendererPortletImpl)1 InlineScript (com.liferay.faces.bridge.renderkit.html_basic.internal.InlineScript)1 HeadRendererICEfacesImpl (com.liferay.faces.bridge.renderkit.icefaces.internal.HeadRendererICEfacesImpl)1 FormRendererPrimeFacesImpl (com.liferay.faces.bridge.renderkit.primefaces.internal.FormRendererPrimeFacesImpl)1 HeadRendererPrimeFacesImpl (com.liferay.faces.bridge.renderkit.primefaces.internal.HeadRendererPrimeFacesImpl)1 FileUploadRendererRichFacesImpl (com.liferay.faces.bridge.renderkit.richfaces.internal.FileUploadRendererRichFacesImpl)1 Product (com.liferay.faces.util.product.Product)1 ProductFactory (com.liferay.faces.util.product.ProductFactory)1