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.xs;
23
24import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
25import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
26import com.sun.org.apache.xerces.internal.util.XMLChar;
27import com.sun.org.apache.xerces.internal.xni.QName;
28import com.sun.org.apache.xerces.internal.xs.datatypes.XSQName;
29
30/**
31 * Represent the schema type "QName" and "NOTATION"
32 *
33 * @xerces.internal
34 *
35 * @author Neeraj Bajaj, Sun Microsystems, inc.
36 * @author Sandy Gao, IBM
37 *
38 */
39public class QNameDV extends TypeValidator {
40
41    private static final String EMPTY_STRING = "".intern();
42
43    public short getAllowedFacets() {
44        return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE);
45    }
46
47    public Object getActualValue(String content, ValidationContext context)
48        throws InvalidDatatypeValueException {
49
50        // "prefix:localpart" or "localpart"
51        // get prefix and local part out of content
52        String prefix, localpart;
53        int colonptr = content.indexOf(":");
54        if (colonptr > 0) {
55            prefix = context.getSymbol(content.substring(0,colonptr));
56            localpart = content.substring(colonptr+1);
57        } else {
58            prefix = EMPTY_STRING;
59            localpart = content;
60        }
61
62        // both prefix (if any) a nd localpart must be valid NCName
63        if (prefix.length() > 0 && !XMLChar.isValidNCName(prefix))
64            throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "QName"});
65
66        if(!XMLChar.isValidNCName(localpart))
67            throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "QName"});
68
69        // resove prefix to a uri, report an error if failed
70        String uri = context.getURI(prefix);
71        if (prefix.length() > 0 && uri == null)
72            throw new InvalidDatatypeValueException("UndeclaredPrefix", new Object[]{content, prefix});
73
74        return new XQName(prefix, context.getSymbol(localpart), context.getSymbol(content), uri);
75
76    }
77
78    // REVISIT: qname and notation shouldn't support length facets.
79    //          now we just return the length of the rawname
80    public int getDataLength(Object value) {
81        return ((XQName)value).rawname.length();
82    }
83
84    /**
85     * represent QName data
86     */
87    private static final class XQName extends QName implements XSQName {
88        /** Constructs a QName with the specified values. */
89        public XQName(String prefix, String localpart, String rawname, String uri) {
90            setValues(prefix, localpart, rawname, uri);
91        } // <init>(String,String,String,String)
92
93        /** Returns true if the two objects are equal. */
94        public boolean equals(Object object) {
95            if (object instanceof QName) {
96                QName qname = (QName)object;
97                return uri == qname.uri && localpart == qname.localpart;
98            }
99            return false;
100        } // equals(Object):boolean
101
102        public synchronized String toString() {
103            return rawname;
104        }
105        public javax.xml.namespace.QName getJAXPQName() {
106            return new javax.xml.namespace.QName(uri, localpart, prefix);
107        }
108        public QName getXNIQName() {
109            return this;
110        }
111    }
112} // class QNameDVDV
113