1 22 23 package org.xquark.schema.datatypes; 24 25 import org.apache.xerces.impl.xpath.regex.RegularExpression; 26 import org.xquark.schema.SchemaException; 27 import org.xquark.schema.validation.ValidationContextProvider; 28 29 30 abstract class AbstractQNameType extends MesureableType { 31 private static final String RCSRevision = "$Revision: 1.2 $"; 32 private static final String RCSName = "$Name: $"; 33 34 private RegularExpression ncNameFormat = null; 35 36 AbstractQNameType(String name, int type) { 37 super(name, type); 38 ncNameFormat = new RegularExpression("[\\i-[:]][\\c-[:]]*", "X"); 39 } 40 41 protected Object toValidType(Object data) { 42 return (QName) data; 43 } 44 45 protected abstract QName createQName(String namespaceURI, String localName); 46 47 protected QName toQName(String value, ValidationContextProvider context) throws SchemaException { 48 String prefix = null; 49 String localName = null; 50 int index = value.indexOf(':'); 51 if (index == 0) { 52 super.invalidValue(value); 53 } else if (index == -1) { 54 prefix = ""; 55 localName = value; 56 } else { 57 prefix = value.substring(0, index); 58 if (!ncNameFormat.matches(prefix)) 59 super.invalidValue(value); 60 localName = value.substring(index + 1); 61 } 62 if (!ncNameFormat.matches(localName)) 63 super.invalidValue(value); 64 if (context == null) super.invalidValue(value); 66 String uri = context.getNamespaceURI(prefix); 67 if (uri == null && prefix.length() > 0) 68 super.invalidValue(value); 69 return createQName(uri, localName); 70 } 71 72 public void checkFacets(Object valueSpace) throws SchemaException { 73 super.checkFacets(valueSpace); 74 } 77 78 protected Object toValueSpace(String value, ValidationContextProvider context) throws SchemaException { 79 return toQName(value, context); 80 } 81 82 public String toXMLString(Object data, ValidationContextProvider context) { 83 if (data == null) 84 return null; 85 QName value = (QName) data; 86 if (context == null) 87 return value.toString(); 88 if (value.getNamespaceURI() == null) 89 return value.getLocalName(); 90 String prefix = context.getPrefix(value.getNamespaceURI()); 91 if (prefix == null) 92 return value.toString(); 93 if (prefix.length() == 0) 94 return value.getLocalName(); 95 else 96 return prefix + ":" + value.getLocalName(); 97 } 98 99 } 100 | Popular Tags |