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