KickJava   Java API By Example, From Geeks To Geeks.

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


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.XML11Char;
21
22 /**
23  * <P>IDDatatypeValidator - ID represents the ID attribute
24  * type from XML 1.1 Recommendation. The value space
25  * of 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  * @author Neil Graham, IBM
42  *
43  * @version $Id: XML11IDDatatypeValidator.java,v 1.4 2004/10/06 14:56:51 mrglavas Exp $
44  */

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

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