001    package com.thaiopensource.datatype.xsd;
002    
003    import org.relaxng.datatype.ValidationContext;
004    import com.thaiopensource.xml.util.Naming;
005    
006    class QNameDatatype extends DatatypeBase {
007      public boolean lexicallyAllows(String str) {
008        return Naming.isQname(str);
009      }
010    
011      static class QName {
012        private final String namespaceURI;
013        private final String localName;
014        QName(String namespaceURI, String localName) {
015          this.namespaceURI = namespaceURI;
016          this.localName = localName;
017        }
018        public boolean equals(Object obj) {
019          if (obj == null || !(obj instanceof QName))
020            return false;
021          QName other = (QName)obj;
022          return namespaceURI.equals(other.namespaceURI) && localName.equals(other.localName);
023        }
024        public int hashCode() {
025          return localName.hashCode() ^ namespaceURI.hashCode();
026        }
027      }
028    
029      Object getValue(String str, ValidationContext vc) {
030        int i = str.indexOf(':');
031        if (i < 0) {
032          String ns = vc.resolveNamespacePrefix("");
033          if (ns == null)
034            ns = "";
035          return new QName(ns, str);
036        }
037        else {
038          String prefix = str.substring(0, i);
039          String ns = vc.resolveNamespacePrefix(prefix);
040          if (ns == null)
041            return null;
042          return new QName(ns, str.substring(i + 1));
043        }
044      }
045    
046      boolean allowsValue(String str, ValidationContext vc) {
047        int i = str.indexOf(':');
048        return i < 0 || vc.resolveNamespacePrefix(str.substring(0, i)) != null;
049      }
050    
051      public boolean isContextDependent() {
052        return true;
053      }
054    }