KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > validators > ByteLengthValidator


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.latka.validators;
18
19 import org.apache.commons.latka.Validator;
20 import org.apache.commons.latka.ValidationException;
21
22 import org.apache.commons.latka.http.Response;
23
24 import java.text.MessageFormat JavaDoc;
25 /**
26  * FIXME: Docs
27  *
28  * @author Morgan Delagrange
29  * @author dIon Gillard
30  * @version $Id: ByteLengthValidator.java 155424 2005-02-26 13:09:29Z dirkv $
31  */

32 public class ByteLengthValidator extends BaseValidator implements Validator {
33
34   // --------------------------------------------------------------- Attributes
35

36   protected int _minLength = 0;
37   protected int _maxLength = -1;
38
39   protected static final MessageFormat JavaDoc MAX_MESSAGE = new MessageFormat JavaDoc("Expected at most {0,number,integer} bytes. Found {1,number,integer}.");
40   protected static final MessageFormat JavaDoc MIN_MESSAGE = new MessageFormat JavaDoc("Expected at least {0,number,integer} bytes. Found {1,number,integer}.");
41
42   // ------------------------------------------------------------- Constructors
43

44   public ByteLengthValidator() {
45       this(null,0,-1);
46   }
47
48   public ByteLengthValidator(String JavaDoc label) {
49       this(label,0,-1);
50   }
51
52   public ByteLengthValidator(int min, int max) {
53       this(null,min,max);
54   }
55
56   public ByteLengthValidator(String JavaDoc label, int min, int max) {
57       super(label);
58       _minLength = min;
59       _maxLength = max;
60   }
61
62   // ------------------------------------------------------------------ Methods
63

64   public void setMinLength(int length) {
65     _minLength = length;
66   }
67
68   public void setMaxLength(int length) {
69     _maxLength = length;
70   }
71
72   public void validate(Response response)
73     throws ValidationException {
74
75     int byteLength = response.getByteLength();
76
77     if (_maxLength > -1) {
78       if (byteLength > _maxLength) {
79         fail(MAX_MESSAGE.format(new Object JavaDoc[] { new Integer JavaDoc(_maxLength), new Integer JavaDoc(byteLength) }).toString());
80       }
81     }
82
83     if (byteLength < _minLength) {
84       fail(MIN_MESSAGE.format(new Object JavaDoc[] { new Integer JavaDoc(_minLength), new Integer JavaDoc(byteLength) }).toString());
85     }
86   }
87
88 }
89
Popular Tags