KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > QNameDV


1 /*
2  * Copyright 2001-2005 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.xs;
18
19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
20 import org.apache.xerces.impl.dv.ValidationContext;
21 import org.apache.xerces.util.XMLChar;
22 import org.apache.xerces.xni.QName;
23 import org.apache.xerces.xs.datatypes.XSQName;
24
25 /**
26  * Represent the schema type "QName" and "NOTATION"
27  *
28  * @xerces.internal
29  *
30  * @author Neeraj Bajaj, Sun Microsystems, inc.
31  * @author Sandy Gao, IBM
32  *
33  * @version $Id: QNameDV.java,v 1.9 2005/05/06 15:31:14 ankitp Exp $
34  */

35 public class QNameDV extends TypeValidator {
36
37     private static final String JavaDoc EMPTY_STRING = "".intern();
38
39     public short getAllowedFacets() {
40         return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE);
41     }
42
43     public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context)
44         throws InvalidDatatypeValueException {
45
46         // "prefix:localpart" or "localpart"
47
// get prefix and local part out of content
48
String JavaDoc prefix, localpart;
49         int colonptr = content.indexOf(":");
50         if (colonptr > 0) {
51             prefix = context.getSymbol(content.substring(0,colonptr));
52             localpart = content.substring(colonptr+1);
53         } else {
54             prefix = EMPTY_STRING;
55             localpart = content;
56         }
57
58         // both prefix (if any) a nd localpart must be valid NCName
59
if (prefix.length() > 0 && !XMLChar.isValidNCName(prefix))
60             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "QName"});
61
62         if(!XMLChar.isValidNCName(localpart))
63             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "QName"});
64
65         // resove prefix to a uri, report an error if failed
66
String JavaDoc uri = context.getURI(prefix);
67         if (prefix.length() > 0 && uri == null)
68             throw new InvalidDatatypeValueException("UndeclaredPrefix", new Object JavaDoc[]{content, prefix});
69
70         return new XQName(prefix, context.getSymbol(localpart), context.getSymbol(content), uri);
71
72     }
73
74     // REVISIT: qname and notation shouldn't support length facets.
75
// now we just return the length of the rawname
76
public int getDataLength(Object JavaDoc value) {
77         return ((XQName)value).rawname.length();
78     }
79
80     /**
81      * represent QName data
82      */

83     private static final class XQName extends QName implements XSQName {
84         /** Constructs a QName with the specified values. */
85         public XQName(String JavaDoc prefix, String JavaDoc localpart, String JavaDoc rawname, String JavaDoc uri) {
86             setValues(prefix, localpart, rawname, uri);
87         } // <init>(String,String,String,String)
88

89         /** Returns true if the two objects are equal. */
90         public boolean equals(Object JavaDoc object) {
91             if (object instanceof QName) {
92                 QName qname = (QName)object;
93                 return uri == qname.uri && localpart == qname.localpart;
94             }
95             return false;
96         } // equals(Object):boolean
97

98         public synchronized String JavaDoc toString() {
99             return rawname;
100         }
101         public javax.xml.namespace.QName JavaDoc getJAXPQName() {
102             return new javax.xml.namespace.QName JavaDoc(uri, localpart, prefix);
103         }
104         public QName getXNIQName() {
105             return this;
106         }
107     }
108 } // class QNameDVDV
109
Popular Tags