KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > types > Token


1 /*
2  * Copyright 2001-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 package org.apache.axis.types;
17
18 import org.apache.axis.utils.Messages;
19
20 /**
21  * Custom class for supporting primitive XSD data type Token.
22  * token represents tokenized strings.
23  * The base type of token is normalizedString.
24  *
25  * @author Chris Haddad <chaddad@cobia.net>
26  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#token">XML Schema 3.3.2</a>
27  */

28 public class Token extends NormalizedString {
29
30     public Token() {
31         super();
32     }
33
34     /**
35      * ctor for Token
36      * @exception IllegalArgumentException will be thrown if validation fails
37      */

38     public Token(String JavaDoc stValue) throws IllegalArgumentException JavaDoc {
39         try {
40             setValue(stValue);
41         }
42         catch (IllegalArgumentException JavaDoc e) {
43             // recast normalizedString exception as token exception
44
throw new IllegalArgumentException JavaDoc(
45                     Messages.getMessage("badToken00") + "data=[" +
46                     stValue + "]");
47         }
48     }
49
50     /**
51      *
52      * validate the value against the xsd definition
53      *
54      * The value space of token is the set of strings that do not
55      * contain the line feed (#xA) nor tab (#x9) characters, that
56      * have no leading or trailing spaces (#x20) and that have no
57      * internal sequences of two or more spaces. The lexical space
58      * of token is the set of strings that do not contain the line
59      * feed (#xA) nor tab (#x9) characters, that have no leading or
60      * trailing spaces (#x20) and that have no internal sequences of two
61      * or more spaces.
62      */

63     public static boolean isValid(String JavaDoc stValue) {
64         int scan;
65         // check to see if we have a string to review
66
if ( (stValue == null) || (stValue.length() == 0) )
67             return true;
68             
69         // no leading space
70
if (stValue.charAt(0) == 0x20)
71             return false;
72
73         // no trail space
74
if (stValue.charAt(stValue.length() - 1) == 0x20)
75             return false;
76
77         for (scan=0; scan < stValue.length(); scan++) {
78             char cDigit = stValue.charAt(scan);
79             switch (cDigit) {
80                 case 0x09:
81                 case 0x0A:
82                     return false;
83                 case 0x20:
84                    // no doublspace
85
if (scan+1 < stValue.length())
86                         if (stValue.charAt(scan + 1) == 0x20) {
87                             return false;
88                         }
89                 default:
90                     break;
91             }
92         }
93         return true;
94     }
95     
96     /**
97      *
98      * validates the data and sets the value for the object.
99      * @param stValue String value
100      * @throws IllegalArgumentException if invalid format
101      */

102     public void setValue(String JavaDoc stValue) throws IllegalArgumentException JavaDoc {
103         if (Token.isValid(stValue) == false)
104             throw new IllegalArgumentException JavaDoc(
105                Messages.getMessage("badToken00") +
106                " data=[" + stValue + "]");
107         m_value = stValue;
108     }
109
110 }
111
Popular Tags