001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.validator;
042:
043: import java.io.Serializable;
044: import java.lang.reflect.Array;
045: import java.text.MessageFormat;
046: import java.util.Iterator;
047: import java.util.List;
048: import javax.faces.application.FacesMessage;
049:
050: import javax.faces.component.UIComponent;
051: import javax.faces.context.FacesContext;
052: import javax.faces.convert.Converter;
053: import javax.faces.validator.Validator;
054: import javax.faces.validator.ValidatorException;
055: import com.sun.rave.web.ui.component.ListSelector;
056: import com.sun.rave.web.ui.model.list.ListItem;
057: import com.sun.rave.web.ui.theme.Theme;
058: import com.sun.rave.web.ui.util.ThemeUtilities;
059:
060: /**
061: * <p> Use this validator to check the number of characters in a string when
062: * you need to set the validation messages.</p>
063: *
064: * @author avk
065: */
066: public class ValueMatchesOptionsValidator implements Validator,
067: Serializable {
068:
069: /**
070: * <p>The converter id for this converter.</p>
071: */
072: public static final String VALIDATOR_ID = "com.sun.rave.web.ui.ValueMatchesOptions";
073: /**
074: * Error message used if the value is not in the option.
075: */
076: private String message = null;
077:
078: private static final boolean DEBUG = false;
079:
080: /** Creates a new instance of StringLengthValidator */
081: public ValueMatchesOptionsValidator() {
082: }
083:
084: /**
085: * <p> Validate the value with regard to a <code>UIComponent</code> and a
086: * <code>FacesContext</code>.</p>
087: *
088: * @param context The FacesContext
089: * @param component The component to be validated
090: * @param value The submitted value of the component
091: *
092: * @exception ValidatorException if the value is not valid
093: */
094: public void validate(FacesContext context, UIComponent component,
095: Object value) throws ValidatorException {
096:
097: if (DEBUG)
098: log("validate()");
099:
100: if ((context == null) || (component == null)) {
101: String message = "Context or component is null";
102: if (DEBUG)
103: log("\t" + message);
104: throw new NullPointerException(message);
105: }
106:
107: if (!(component instanceof ListSelector)) {
108: String message = this .getClass().getName()
109: + " can only be used with components which subclass "
110: + ListSelector.class.getName();
111: if (DEBUG)
112: log("\t" + message);
113: throw new RuntimeException(message);
114: }
115:
116: ListSelector list = (ListSelector) component;
117: Object valuesAsArray = null;
118:
119: if (value instanceof List) {
120: if (DEBUG)
121: log("\tValue is list");
122: valuesAsArray = ((List) value).toArray();
123: } else if (value.getClass().isArray()) {
124: if (DEBUG)
125: log("\tValue is array");
126: valuesAsArray = value;
127: } else {
128: if (DEBUG)
129: log("\tValue is object");
130: valuesAsArray = new Object[] { value };
131: }
132:
133: int numValues = Array.getLength(valuesAsArray);
134: if (numValues == 0) {
135: if (DEBUG)
136: log("\tArray is empty - values are OK");
137: return;
138: }
139:
140: Object currentValue = null;
141: Iterator itemsIterator = null;
142: ListItem listItem = null;
143: Object listObject = null;
144: boolean foundValue = false;
145: boolean error = false;
146:
147: for (int counter = 0; counter < numValues; ++counter) {
148: currentValue = Array.get(valuesAsArray, counter);
149: itemsIterator = list.getListItems();
150: foundValue = false;
151:
152: if (DEBUG)
153: log("\tChecking: " + String.valueOf(currentValue));
154: while (itemsIterator.hasNext()) {
155: listObject = itemsIterator.next();
156: if (!(listObject instanceof ListItem)) {
157: continue;
158: }
159: listItem = (ListItem) listObject;
160: if (DEBUG)
161: log("ListItem is " + listItem.getLabel());
162: if (currentValue.equals(listItem.getValueObject())) {
163: if (DEBUG)
164: log("Found match");
165: foundValue = true;
166: break;
167: }
168: }
169: if (!foundValue) {
170: if (DEBUG)
171: log("No match found");
172: error = true;
173: break;
174: }
175: }
176:
177: if (error) {
178: if (message == null) {
179: Theme theme = ThemeUtilities.getTheme(context);
180: message = ThemeUtilities.getTheme(context).getMessage(
181: "ListSelector.badValue");
182: }
183: throw new ValidatorException(new FacesMessage(message));
184: }
185: }
186:
187: private void log(String s) {
188: System.out.println(this .getClass().getName() + "::" + s); //NOI18N
189: }
190:
191: /**
192: * Getter for property message.
193: * @return Value of property message.
194: */
195: public String getMessage() {
196:
197: return this .message;
198: }
199:
200: /**
201: * Setter for property message.
202: * @param message New value of property message.
203: */
204: public void setMessage(String message) {
205:
206: this.message = message;
207: }
208: }
|