KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > dtd > IDDatatypeValidator


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.xerces.impl.dv.dtd;
18
19 import org.apache.xerces.impl.dv.*;
20 import org.apache.xerces.util.XMLChar;
21
22 /**
23  * <P>IDDatatypeValidator - ID represents the ID attribute
24  * type from XML 1.0 Recommendation. The value space
25  * od ID is the set of all strings that match the
26  * NCName production and have been used in an XML
27  * document. The lexical space of ID is the set of all
28  * strings that match the NCName production.</P>
29  * <P>The value space of ID is scoped to a specific
30  * instance document.</P>
31  * <P>The following constraint applies:
32  * An ID must not appear more than once in an XML
33  * document as a value of this type; i.e., ID values
34  * must uniquely identify the elements which bear
35  * them.</P>
36  *
37  * @xerces.internal
38  *
39  * @author Jeffrey Rodriguez, IBM
40  * @author Sandy Gao, IBM
41  *
42  * @version $Id: IDDatatypeValidator.java,v 1.9 2004/10/06 14:56:51 mrglavas Exp $
43  */

44 public class IDDatatypeValidator implements DatatypeValidator {
45
46     // construct an ID datatype validator
47
public IDDatatypeValidator() {
48     }
49
50     /**
51      * Checks that "content" string is valid ID value.
52      * If invalid a Datatype validation exception is thrown.
53      *
54      * @param content the string value that needs to be validated
55      * @param context the validation context
56      * @throws InvalidDatatypeException if the content is
57      * invalid according to the rules for the validators
58      * @see InvalidDatatypeValueException
59      */

60     public void validate(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
61
62         //Check if is valid key-[81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
63
if(context.useNamespaces()) {
64             if (!XMLChar.isValidNCName(content)) {
65                 throw new InvalidDatatypeValueException("IDInvalidWithNamespaces", new Object JavaDoc[]{content});
66             }
67         }
68         else {
69             if (!XMLChar.isValidName(content)) {
70                 throw new InvalidDatatypeValueException("IDInvalid", new Object JavaDoc[]{content});
71             }
72         }
73
74         if (context.isIdDeclared(content)) {
75             throw new InvalidDatatypeValueException("IDNotUnique", new Object JavaDoc[]{content});
76         }
77         
78         context.addId(content);
79     }
80     
81 }
82
Popular Tags