1 16 17 package org.apache.xerces.impl.dv.xs; 18 19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 20 import org.apache.xerces.util.URI; 21 import org.apache.xerces.impl.dv.ValidationContext; 22 23 33 public class AnyURIDV extends TypeValidator { 34 35 private static final URI BASE_URI; 36 static { 37 URI uri = null; 38 try { 39 uri = new URI("abc://def.ghi.jkl"); 40 } catch (URI.MalformedURIException ex) { 41 } 42 BASE_URI = uri; 43 } 44 45 public short getAllowedFacets(){ 46 return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE ); 47 } 48 49 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 52 try { 54 if( content.length() != 0 ) { 55 final String encoded = encode(content); 57 new URI(BASE_URI, encoded ); 61 } 62 } catch (URI.MalformedURIException ex) { 63 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "anyURI"}); 64 } 65 66 return content; 68 } 69 70 private static boolean gNeedEscaping[] = new boolean[128]; 72 private static char gAfterEscaping1[] = new char[128]; 74 private static char gAfterEscaping2[] = new char[128]; 76 private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', 77 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 78 static { 80 for (int i = 0; i <= 0x1f; i++) { 81 gNeedEscaping[i] = true; 82 gAfterEscaping1[i] = gHexChs[i >> 4]; 83 gAfterEscaping2[i] = gHexChs[i & 0xf]; 84 } 85 gNeedEscaping[0x7f] = true; 86 gAfterEscaping1[0x7f] = '7'; 87 gAfterEscaping2[0x7f] = 'F'; 88 char[] escChs = {' ', '<', '>', '"', '{', '}', 89 '|', '\\', '^', '~', '`'}; 90 int len = escChs.length; 91 char ch; 92 for (int i = 0; i < len; i++) { 93 ch = escChs[i]; 94 gNeedEscaping[ch] = true; 95 gAfterEscaping1[ch] = gHexChs[ch >> 4]; 96 gAfterEscaping2[ch] = gHexChs[ch & 0xf]; 97 } 98 } 99 100 private static String encode(String anyURI){ 104 int len = anyURI.length(), ch; 105 StringBuffer buffer = new StringBuffer (len*3); 106 107 int i = 0; 109 for (; i < len; i++) { 110 ch = anyURI.charAt(i); 111 if (ch >= 128) 113 break; 114 if (gNeedEscaping[ch]) { 115 buffer.append('%'); 116 buffer.append(gAfterEscaping1[ch]); 117 buffer.append(gAfterEscaping2[ch]); 118 } 119 else { 120 buffer.append((char)ch); 121 } 122 } 123 124 if (i < len) { 126 byte[] bytes = null; 128 byte b; 129 try { 130 bytes = anyURI.substring(i).getBytes("UTF-8"); 131 } catch (java.io.UnsupportedEncodingException e) { 132 return anyURI; 134 } 135 len = bytes.length; 136 137 for (i = 0; i < len; i++) { 139 b = bytes[i]; 140 if (b < 0) { 142 ch = b + 256; 143 buffer.append('%'); 144 buffer.append(gHexChs[ch >> 4]); 145 buffer.append(gHexChs[ch & 0xf]); 146 } 147 else if (gNeedEscaping[b]) { 148 buffer.append('%'); 149 buffer.append(gAfterEscaping1[b]); 150 buffer.append(gAfterEscaping2[b]); 151 } 152 else { 153 buffer.append((char)b); 154 } 155 } 156 } 157 158 if (buffer.length() != len) { 161 return buffer.toString(); 162 } 163 else { 164 return anyURI; 165 } 166 } 167 168 } | Popular Tags |