001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv.xs;
019:
020: import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
021: import org.apache.xerces.impl.dv.ValidationContext;
022: import org.apache.xerces.util.XMLChar;
023: import org.apache.xerces.xni.QName;
024: import org.apache.xerces.xs.datatypes.XSQName;
025:
026: /**
027: * Represent the schema type "QName" and "NOTATION"
028: *
029: * @xerces.internal
030: *
031: * @author Neeraj Bajaj, Sun Microsystems, inc.
032: * @author Sandy Gao, IBM
033: *
034: * @version $Id: QNameDV.java 446745 2006-09-15 21:43:58Z mrglavas $
035: */
036: public class QNameDV extends TypeValidator {
037:
038: private static final String EMPTY_STRING = "".intern();
039:
040: public short getAllowedFacets() {
041: return (XSSimpleTypeDecl.FACET_LENGTH
042: | XSSimpleTypeDecl.FACET_MINLENGTH
043: | XSSimpleTypeDecl.FACET_MAXLENGTH
044: | XSSimpleTypeDecl.FACET_PATTERN
045: | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE);
046: }
047:
048: public Object getActualValue(String content,
049: ValidationContext context)
050: throws InvalidDatatypeValueException {
051:
052: // "prefix:localpart" or "localpart"
053: // get prefix and local part out of content
054: String prefix, localpart;
055: int colonptr = content.indexOf(":");
056: if (colonptr > 0) {
057: prefix = context.getSymbol(content.substring(0, colonptr));
058: localpart = content.substring(colonptr + 1);
059: } else {
060: prefix = EMPTY_STRING;
061: localpart = content;
062: }
063:
064: // both prefix (if any) a nd localpart must be valid NCName
065: if (prefix.length() > 0 && !XMLChar.isValidNCName(prefix))
066: throw new InvalidDatatypeValueException(
067: "cvc-datatype-valid.1.2.1", new Object[] { content,
068: "QName" });
069:
070: if (!XMLChar.isValidNCName(localpart))
071: throw new InvalidDatatypeValueException(
072: "cvc-datatype-valid.1.2.1", new Object[] { content,
073: "QName" });
074:
075: // resove prefix to a uri, report an error if failed
076: String uri = context.getURI(prefix);
077: if (prefix.length() > 0 && uri == null)
078: throw new InvalidDatatypeValueException("UndeclaredPrefix",
079: new Object[] { content, prefix });
080:
081: return new XQName(prefix, context.getSymbol(localpart), context
082: .getSymbol(content), uri);
083:
084: }
085:
086: // REVISIT: qname and notation shouldn't support length facets.
087: // now we just return the length of the rawname
088: public int getDataLength(Object value) {
089: return ((XQName) value).rawname.length();
090: }
091:
092: /**
093: * represent QName data
094: */
095: private static final class XQName extends QName implements XSQName {
096: /** Constructs a QName with the specified values. */
097: public XQName(String prefix, String localpart, String rawname,
098: String uri) {
099: setValues(prefix, localpart, rawname, uri);
100: } // <init>(String,String,String,String)
101:
102: /** Returns true if the two objects are equal. */
103: public boolean equals(Object object) {
104: if (object instanceof QName) {
105: QName qname = (QName) object;
106: return uri == qname.uri && localpart == qname.localpart;
107: }
108: return false;
109: } // equals(Object):boolean
110:
111: public synchronized String toString() {
112: return rawname;
113: }
114:
115: public javax.xml.namespace.QName getJAXPQName() {
116: return new javax.xml.namespace.QName(uri, localpart, prefix);
117: }
118:
119: public QName getXNIQName() {
120: return this ;
121: }
122: }
123: } // class QNameDVDV
|