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.text.MessageFormat;
044: import javax.faces.application.FacesMessage;
045: import javax.faces.component.StateHolder;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.convert.Converter;
049: import javax.faces.validator.Validator;
050: import javax.faces.validator.ValidatorException;
051: import com.sun.rave.web.ui.theme.Theme;
052: import com.sun.rave.web.ui.util.ThemeUtilities;
053:
054: /**
055: * <p> Use this validator to check the number of characters in a string when
056: * you need to set the validation messages.</p>
057: *
058: * @author avk
059: */
060: public class StringLengthValidator implements Validator, StateHolder {
061:
062: /**
063: * <p>The converter id for this converter.</p>
064: */
065: public static final String VALIDATOR_ID = "com.sun.rave.web.ui.StringLength";
066:
067: /**
068: * The message to use in case the value is too short. May include
069: * {0} for the minimum value.
070: */
071: private String tooShortMessage = null;
072: /**
073: * The message to use in case the value is too long. May include
074: * {0} for the maximum value.
075: */
076: private String tooLongMessage;
077: private int minLength = 0;
078: private int maxLength = 0;
079:
080: private boolean minimumSet = false;
081:
082: private static final boolean DEBUG = false;
083:
084: /** Creates a new instance of StringLengthValidator */
085: public StringLengthValidator() {
086: }
087:
088: /**
089: * Creates a new instance of StringLengthValidator.
090: * @param max The maximum number of characters allowed in the string
091: */
092: public StringLengthValidator(int max) {
093: maxLength = max;
094: }
095:
096: /**
097: * Creates a new instance of StringLengthValidator.
098: * @param max The maximum number of characters allowed in the string
099: * @param min The minimum number of characters allowed in the string
100: */
101: public StringLengthValidator(int max, int min) {
102: this (max);
103: minLength = min;
104: minimumSet = true;
105: }
106:
107: /**
108: * <p> Validate the value with regard to a <code>UIComponent</code> and a
109: * <code>FacesContext</code>.</p>
110: *
111: * @param context The FacesContext
112: * @param component The component to be validated
113: * @param value The submitted value of the component
114: *
115: * @exception ValidatorException if the value is not valid
116: */
117: public void validate(FacesContext context, UIComponent component,
118: Object value) throws ValidatorException {
119:
120: if (DEBUG)
121: log("validate(" + String.valueOf(value) + ")");
122:
123: if ((context == null) || (component == null)) {
124: if (DEBUG)
125: log("\tContext or component is null");
126: throw new NullPointerException();
127: }
128:
129: String string;
130: if (value == null) {
131: if (DEBUG)
132: log("\tValue is null!");
133: string = new String();
134: } else {
135: string = (String) value;
136:
137: }
138: if (DEBUG)
139: log("\tValue is !" + string + "!");
140: if (string.length() > maxLength) {
141: if (DEBUG)
142: log("\tString is longer than maxlength");
143: if (tooLongMessage == null) {
144: Theme theme = ThemeUtilities.getTheme(context);
145: tooLongMessage = theme
146: .getMessage("StringLengthValidator.itemTooLong");
147: }
148: MessageFormat mf = new MessageFormat(tooLongMessage,
149: context.getViewRoot().getLocale());
150: Object[] params = { String.valueOf(maxLength) };
151: FacesMessage msg = new FacesMessage(mf.format(params));
152: throw new ValidatorException(msg);
153:
154: }
155: if (minimumSet && string.length() < minLength) {
156: if (DEBUG)
157: log("\tString is shorter than minlength");
158: if (tooShortMessage == null) {
159: Theme theme = ThemeUtilities.getTheme(context);
160: tooShortMessage = theme
161: .getMessage("StringLengthValidator.itemTooLong");
162: }
163: MessageFormat mf = new MessageFormat(tooShortMessage,
164: context.getViewRoot().getLocale());
165: Object[] params = { String.valueOf(minLength) };
166: FacesMessage msg = new FacesMessage(mf.format(params));
167: throw new ValidatorException(msg);
168: }
169: }
170:
171: private String integerToString(UIComponent component,
172: Integer toConvert) {
173: String result = null;
174: Converter converter = null;
175: FacesContext context = FacesContext.getCurrentInstance();
176:
177: converter = (Converter) context.getApplication()
178: .createConverter("javax.faces.Number");
179: result = converter.getAsString(context, component, toConvert);
180: return result;
181: }
182:
183: /**
184: * Saves the state of the component into an object
185: * @param context the FacesContext
186: * @return the Object representing the state of the component
187: */
188: public Object saveState(FacesContext context) {
189:
190: Object values[] = new Object[5];
191: values[0] = new Integer(maxLength);
192: values[1] = new Integer(minLength);
193: values[2] = minimumSet ? Boolean.TRUE : Boolean.FALSE;
194: values[3] = tooLongMessage;
195: values[4] = tooShortMessage;
196: return (values);
197: }
198:
199: /**
200: * Restore the state of the component.
201: * @param context The FacesContext
202: * @param state the Object representing the state of the component
203: */
204: public void restoreState(FacesContext context, Object state) {
205:
206: Object values[] = (Object[]) state;
207: maxLength = ((Integer) values[0]).intValue();
208: minLength = ((Integer) values[1]).intValue();
209: minimumSet = ((Boolean) values[2]).booleanValue();
210: if (values[3] != null)
211: tooLongMessage = values[3].toString();
212: if (values[4] != null)
213: tooShortMessage = values[4].toString();
214: }
215:
216: private boolean transientValue = false;
217:
218: /**
219: * Returns false, this component needs to save state.
220: * @return false
221: */
222: public boolean isTransient() {
223: return false;
224: }
225:
226: /**
227: * Does nothing
228: */
229: public void setTransient(boolean transientValue) {
230: return;
231: }
232:
233: /**
234: * Get the message to be used if the string is longer than the maxmimum number of characters.
235: * @return the message to be used if the string is longer than the maxmimum number of characters
236: */
237: public String getTooLongMessage() {
238:
239: return this .tooLongMessage;
240: }
241:
242: /**
243: * Set the message to be used if the string is longer than the maximum number of characters.
244: * @param tooLongMessage the message to be used if the string is longer than the maxmimum number of characters
245: */
246: public void setTooLongMessage(String tooLongMessage) {
247:
248: this .tooLongMessage = tooLongMessage;
249: }
250:
251: /**
252: * Get the message to be used if the string is shorter than the minimum number of characters.
253: * @return the message to be used if the string is shorter than the minimum number of characters
254: */
255: public String getTooShortMessage() {
256:
257: return this .tooShortMessage;
258: }
259:
260: /**
261: * Set the message to be used if the string is shorter than the minimum number of characters.
262: * @param tooShortMessage the message to be used if the string is shorter than the minimum number of characters
263: */
264: public void setTooShortMessage(String tooShortMessage) {
265:
266: this .tooShortMessage = tooShortMessage;
267: }
268:
269: private void log(String s) {
270: System.out.println(this .getClass().getName() + "::" + s); //NOI18N
271: }
272:
273: }
|