1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.dv.dtd;
23
24import com.sun.org.apache.xerces.internal.impl.dv.*;
25import com.sun.org.apache.xerces.internal.util.XMLChar;
26
27/**
28 * <P>IDDatatypeValidator - ID represents the ID attribute
29 * type from XML 1.0 Recommendation. The value space
30 * od ID is the set of all strings that match the
31 * NCName production and have been used in an XML
32 * document. The lexical space of ID is the set of all
33 * strings that match the NCName production.</P>
34 * <P>The value space of ID is scoped to a specific
35 * instance document.</P>
36 * <P>The following constraint applies:
37 * An ID must not appear more than once in an XML
38 * document as a value of this type; i.e., ID values
39 * must uniquely identify the elements which bear
40 * them.</P>
41 *
42 * @xerces.internal
43 *
44 * @author Jeffrey Rodriguez, IBM
45 * @author Sandy Gao, IBM
46 *
47 */
48public class IDDatatypeValidator implements DatatypeValidator {
49
50    // construct an ID datatype validator
51    public IDDatatypeValidator() {
52    }
53
54    /**
55     * Checks that "content" string is valid ID value.
56     * If invalid a Datatype validation exception is thrown.
57     *
58     * @param content       the string value that needs to be validated
59     * @param context       the validation context
60     * @throws InvalidDatatypeException if the content is
61     *         invalid according to the rules for the validators
62     * @see InvalidDatatypeValueException
63     */
64    public void validate(String content, ValidationContext context) throws InvalidDatatypeValueException {
65
66        //Check if is valid key-[81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
67        if(context.useNamespaces()) {
68            if (!XMLChar.isValidNCName(content)) {
69                throw new InvalidDatatypeValueException("IDInvalidWithNamespaces", new Object[]{content});
70            }
71        }
72        else {
73            if (!XMLChar.isValidName(content)) {
74                throw new InvalidDatatypeValueException("IDInvalid", new Object[]{content});
75            }
76        }
77
78        if (context.isIdDeclared(content)) {
79            throw new InvalidDatatypeValueException("IDNotUnique", new Object[]{content});
80        }
81
82        context.addId(content);
83    }
84
85}
86