KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > AttrString


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.util.Vector JavaDoc;
27
28 /**
29    Class which contains Meta data for all types of attributes which is present in Validation Descriptor
30    * XML File
31    *
32    * Sample
33    * <attribute name=<Name> type="address" />
34    * <attribute name=<Name> type="integer" range="low,high" />
35    * <attribute name=<Name> type="string" max-length="length" />
36     
37    @author Srinivas Krishnan
38    @version 2.0
39 */

40
41 /* Class for attribute type String */
42  
43 public class AttrString extends AttrType {
44     
45     int maxLength;
46     Vector JavaDoc ee;
47     String JavaDoc expr;
48     
49     public AttrString(String JavaDoc name, String JavaDoc type, boolean optional) {
50         super(name,type, optional);
51         this.maxLength = 0;
52         ee = null;
53         expr = null;
54     }
55     
56     public void setRegExpression(String JavaDoc str) {
57         expr = str;
58     }
59     
60     public void setEnumstring(Vector JavaDoc vec) {
61         ee = vec;
62     }
63     
64     public int getMaxLength() {
65         return maxLength;
66     }
67     
68     public void setMaxLength(int max) {
69         maxLength = max;
70     }
71     
72     public void validate(Object JavaDoc o, ValidationContext valCtx) {
73         super.validate(o, valCtx); // call to common validator first
74
if(o == null) {
75             return;
76         }
77         
78         final String JavaDoc str = (String JavaDoc)o;
79         if(0 != maxLength && str.length() > maxLength) {
80             reportAttributeError(valCtx, "invalidStringLength",
81                 "Attribute[{0}={1}] : String {2} length is greather than maximum length {3}",
82                 new Object JavaDoc[] {valCtx.attrName, str, str, String.valueOf(maxLength)});
83         }
84         if(null != ee && !ee.contains(str) ){
85             reportAttributeError(valCtx, "strInvalidEnum",
86                 "Attribute({0}={1}) : Invalid String - Required {2}",
87                 new Object JavaDoc[] {valCtx.attrName, str, ee.toString()});
88         }
89         
90         if(null != expr && !str.matches(expr))
91         {
92             String JavaDoc printOwnerName = GenericValidator.getConfigElementPrintName(
93                 getFutureXPathForValidatingAttribute(valCtx), false, false);
94             String JavaDoc generic_descr = valCtx.smh.getLocalString(
95                 "default_pattern_description",
96                 "Please refer to admin documentation." );
97             String JavaDoc descr = valCtx.smh.getLocalString(
98                 "pattern_description_for_" + (expr.replaceAll("[\\\\=:]", "`")),
99                 generic_descr );
100             
101             reportAttributeError(valCtx, "regexpNotMatch",
102                 "Value \"{1}\" is not valid for attribute \"{0}\" of {2}. {3}",
103                 new Object JavaDoc[] {valCtx.attrName, str, printOwnerName, descr});
104         }
105     }
106 }
107
108
Popular Tags