All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.faces.component.validator.ComponentValidators Maven / Gradle / Ivy

Go to download

This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.

There is a newer version: 2.2.20
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.faces.component.validator;


import com.sun.faces.util.RequestStateManager;

import javax.faces.context.FacesContext;
import javax.faces.component.EditableValueHolder;
import javax.faces.validator.Validator;
import javax.faces.application.Application;
import javax.faces.view.facelets.ValidatorHandler;
import javax.faces.view.facelets.FaceletContext;
import java.util.*;

/**
 * 

* This class is responsible for adding default validators and/or validators * that wrap multiple EditableValueHolder instances within the view. *

*/ public class ComponentValidators { /** * Key within the FacesContext's attribute map under which * a single ComponentValidators instance will be stored. */ private static final String COMPONENT_VALIDATORS = "javax.faces.component.ComponentValidators"; /** * Stack of ValidatorInfo instances. Each instance represents * a particular nesting level within the view. As a nesting level is encountered, * a ValidatorInfo will be pushed to the stack and all * EditableValueHolder instances will be configured based on * all ValidatorInfos on the stack. When the current nesting level * is closed, the ValidatorInfo instance will be popped and thus * have no impact on other EditableValueHolders. */ private LinkedList validatorStack = null; // ------------------------------------------------------------ Constructors public ComponentValidators() { validatorStack = new LinkedList(); } // ---------------------------------------------------------- Public Methods /** * @param context the FacesContext for the current request * @param createIfNull flag indicating whether or not a * ComponentValidators instance should be created or not * @return a ComponentValidators instance for processing * a view request. If createIfNull is false * and no ComponentValidators has been created, this method * will return null */ public static ComponentValidators getValidators(FacesContext context, boolean createIfNull) { Map attrs = context.getAttributes(); ComponentValidators componentValidators = (ComponentValidators) attrs .get(COMPONENT_VALIDATORS); if ((componentValidators == null) && createIfNull) { componentValidators = new ComponentValidators(); attrs.put(COMPONENT_VALIDATORS, componentValidators); } return componentValidators; } /** *

* Creates and installs default validators, if any, into the argument * EditableValueHolder. This method is merely a utility * method to be called when there is no ComponentValidators * available, or there are no ValidatorInfo instances on the * stack. *

* * @param ctx the FacesContext for the current request * @param editableValueHolder the component receiving the Validators */ @SuppressWarnings({"unchecked"}) public static void addDefaultValidatorsToComponent(FacesContext ctx, EditableValueHolder editableValueHolder) { Set keySet = ctx.getApplication().getDefaultValidatorInfo().keySet(); List validatorIds = new ArrayList(keySet.size()); Set disabledValidatorIds = (Set) RequestStateManager.remove(ctx, RequestStateManager.DISABLED_VALIDATORS); for (String key : keySet) { if (disabledValidatorIds != null && disabledValidatorIds.contains(key)) { continue; } validatorIds.add(key); } addValidatorsToComponent(ctx, validatorIds, editableValueHolder, null); } /** *

* Based on the ValidatorInfo instances present on the stack, * configure the argument EditableValueHolder with Validators * created from the available info. *

* * @param ctx the FacesContext for the current request * @param editableValueHolder the component receiving the Validators */ @SuppressWarnings({"unchecked"}) public void addValidators(FacesContext ctx, EditableValueHolder editableValueHolder) { if ((validatorStack == null) || validatorStack.isEmpty()) { addDefaultValidatorsToComponent(ctx, editableValueHolder); return; } Application application = ctx.getApplication(); Map defaultValidatorInfo = application.getDefaultValidatorInfo(); Set keySet = defaultValidatorInfo.keySet(); List validatorIds = new ArrayList(keySet.size()); for (String key : keySet) { validatorIds.add(key); } Set disabledIds = (Set) RequestStateManager.remove(ctx, RequestStateManager.DISABLED_VALIDATORS); int count = validatorStack.size(); for (int i = count - 1; i >= 0; i--) { ValidatorInfo info = validatorStack.get(i); if (!info.isEnabled() || (disabledIds != null && disabledIds.contains(info.getValidatorId()))) { if (validatorIds.contains(info.getValidatorId())) { validatorIds.remove(info.getValidatorId()); } } else { if (!validatorIds.contains(info.getValidatorId())) { validatorIds.add(info.getValidatorId()); } } } // add the validators to the EditableValueHolder. addValidatorsToComponent(ctx, validatorIds, editableValueHolder, ((validatorStack == null || validatorStack.isEmpty()) ? null : validatorStack)); } /** *

* Pushes the provided ValidatorInfo onto the stack. *

* * @param info */ public void pushValidatorInfo(ValidatorInfo info) { validatorStack.add(info); } /** *

* Pops the last ValidatorInfo instance from the stack. *

*/ public void popValidatorInfo() { if (validatorStack.size() > 0) { validatorStack.removeLast(); } } // --------------------------------------------------------- Private Methods /** *

* Install the validators, if not already present on the component, * using the IDs included in validatorIds. *

* * @param ctx the FacesContext for the current request * @param validatorIds the validator IDs to be added to the * EditableValueHolder * @param editableValueHolder the target component to which the validators * installed * @param validatorStack current stack of ValidatorInfo instances */ private static void addValidatorsToComponent(FacesContext ctx, Collection validatorIds, EditableValueHolder editableValueHolder, LinkedList validatorStack) { if (validatorIds == null || validatorIds.isEmpty()) { return; } Application application = ctx.getApplication(); Map defaultValidatorInfo = application.getDefaultValidatorInfo(); Validator[] validators = editableValueHolder.getValidators(); // check to make sure that Validator instances haven't already // been added. for (Map.Entry defaultValidator : defaultValidatorInfo.entrySet()) { for (Validator validator : validators) { if (defaultValidator.getValue().equals(validator.getClass().getName())) { validatorIds.remove(defaultValidator.getKey()); break; } } } // we now have the complete List of Validator IDs to add to the // target EditablValueHolder for (String id : validatorIds) { Validator v = application.createValidator(id); // work backwards up the stack of ValidatorInfo to find the // nearest matching ValidatorInfo to apply attributes if (validatorStack != null) { for (int i = validatorStack.size() - 1; i >= 0; i--) { ValidatorInfo info = validatorStack.get(i); if (id.equals(info.getValidatorId())) { info.applyAttributes(v); break; } } } editableValueHolder.addValidator(v); } } // ---------------------------------------------------------- Nested Classes /** * Generic information container for a validator at a particular * nesting Level. */ public static class ValidatorInfo { private String validatorId; private boolean enabled; private ValidatorHandler owner; private FaceletContext ctx; // ------------------------------------------------------------ Constructors public ValidatorInfo(FaceletContext ctx, ValidatorHandler owner) { this.owner = owner; this.ctx = ctx; this.validatorId = owner.getValidatorId(ctx); this.enabled = !owner.isDisabled(ctx); } // ------------------------------------------------------------------------- public String getValidatorId() { return validatorId; } public boolean isEnabled() { return enabled; } public void applyAttributes(Validator v) { owner.setAttributes(ctx, v); } } // END ValidatorInfo }