View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.impl.dv.xs;
22  
23  import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
24  import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
25  import com.sun.org.apache.xerces.internal.util.XMLChar;
26  import com.sun.org.apache.xerces.internal.xni.QName;
27  import com.sun.org.apache.xerces.internal.xs.datatypes.XSQName;
28  
29  /**
30   * Represent the schema type "QName" and "NOTATION"
31   *
32   * @xerces.internal
33   *
34   * @author Neeraj Bajaj, Sun Microsystems, inc.
35   * @author Sandy Gao, IBM
36   *
37   */
38  public class QNameDV extends TypeValidator {
39  
40      private static final String EMPTY_STRING = "".intern();
41  
42      public short getAllowedFacets() {
43          return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE);
44      }
45  
46      public Object getActualValue(String content, ValidationContext context)
47          throws InvalidDatatypeValueException {
48  
49          // "prefix:localpart" or "localpart"
50          // get prefix and local part out of content
51          String prefix, localpart;
52          int colonptr = content.indexOf(":");
53          if (colonptr > 0) {
54              prefix = context.getSymbol(content.substring(0,colonptr));
55              localpart = content.substring(colonptr+1);
56          } else {
57              prefix = EMPTY_STRING;
58              localpart = content;
59          }
60  
61          // both prefix (if any) a nd localpart must be valid NCName
62          if (prefix.length() > 0 && !XMLChar.isValidNCName(prefix))
63              throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "QName"});
64  
65          if(!XMLChar.isValidNCName(localpart))
66              throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "QName"});
67  
68          // resove prefix to a uri, report an error if failed
69          String uri = context.getURI(prefix);
70          if (prefix.length() > 0 && uri == null)
71              throw new InvalidDatatypeValueException("UndeclaredPrefix", new Object[]{content, prefix});
72  
73          return new XQName(prefix, context.getSymbol(localpart), context.getSymbol(content), uri);
74  
75      }
76  
77      // REVISIT: qname and notation shouldn't support length facets.
78      //          now we just return the length of the rawname
79      public int getDataLength(Object value) {
80          return ((XQName)value).rawname.length();
81      }
82  
83      /**
84       * represent QName data
85       */
86      private static final class XQName extends QName implements XSQName {
87          /** Constructs a QName with the specified values. */
88          public XQName(String prefix, String localpart, String rawname, String uri) {
89              setValues(prefix, localpart, rawname, uri);
90          } // <init>(String,String,String,String)
91  
92          /** Returns true if the two objects are equal. */
93          public boolean equals(Object object) {
94              if (object instanceof QName) {
95                  QName qname = (QName)object;
96                  return uri == qname.uri && localpart == qname.localpart;
97              }
98              return false;
99          } // equals(Object):boolean
100 
101         public synchronized String toString() {
102             return rawname;
103         }
104         public javax.xml.namespace.QName getJAXPQName() {
105             return new javax.xml.namespace.QName(uri, localpart, prefix);
106         }
107         public QName getXNIQName() {
108             return this;
109         }
110     }
111 } // class QNameDVDV