1 57 58 package com.sun.org.apache.xerces.internal.impl.dv.xs; 59 60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException; 61 import com.sun.org.apache.xerces.internal.util.URI; 62 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext; 63 64 72 public class AnyURIDV extends TypeValidator { 73 74 private static final URI BASE_URI; 75 static { 76 URI uri = null; 77 try { 78 uri = new URI("abc://def.ghi.jkl"); 79 } catch (URI.MalformedURIException ex) { 80 } 81 BASE_URI = uri; 82 } 83 84 public short getAllowedFacets(){ 85 return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE ); 86 } 87 88 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 91 try { 93 if( content.length() != 0 ) { 94 content = encode(content); 96 new URI(BASE_URI, content ); 100 } 101 } catch (URI.MalformedURIException ex) { 102 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "anyURI"}); 103 } 104 105 return content; 107 } 108 109 private static boolean gNeedEscaping[] = new boolean[128]; 111 private static char gAfterEscaping1[] = new char[128]; 113 private static char gAfterEscaping2[] = new char[128]; 115 private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', 116 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 117 static { 119 for (int i = 0; i <= 0x1f; i++) { 120 gNeedEscaping[i] = true; 121 gAfterEscaping1[i] = gHexChs[i >> 4]; 122 gAfterEscaping2[i] = gHexChs[i & 0xf]; 123 } 124 gNeedEscaping[0x7f] = true; 125 gAfterEscaping1[0x7f] = '7'; 126 gAfterEscaping2[0x7f] = 'F'; 127 char[] escChs = {' ', '<', '>', '"', '{', '}', 128 '|', '\\', '^', '~', '`'}; 129 int len = escChs.length; 130 char ch; 131 for (int i = 0; i < len; i++) { 132 ch = escChs[i]; 133 gNeedEscaping[ch] = true; 134 gAfterEscaping1[ch] = gHexChs[ch >> 4]; 135 gAfterEscaping2[ch] = gHexChs[ch & 0xf]; 136 } 137 } 138 139 private static String encode(String anyURI){ 143 int len = anyURI.length(), ch; 144 StringBuffer buffer = new StringBuffer (len*3); 145 146 int i = 0; 148 for (; i < len; i++) { 149 ch = anyURI.charAt(i); 150 if (ch >= 128) 152 break; 153 if (gNeedEscaping[ch]) { 154 buffer.append('%'); 155 buffer.append(gAfterEscaping1[ch]); 156 buffer.append(gAfterEscaping2[ch]); 157 } 158 else { 159 buffer.append((char)ch); 160 } 161 } 162 163 if (i < len) { 165 byte[] bytes = null; 167 byte b; 168 try { 169 bytes = anyURI.substring(i).getBytes("UTF-8"); 170 } catch (java.io.UnsupportedEncodingException e) { 171 return anyURI; 173 } 174 len = bytes.length; 175 176 for (i = 0; i < len; i++) { 178 b = bytes[i]; 179 if (b < 0) { 181 ch = b + 256; 182 buffer.append('%'); 183 buffer.append(gHexChs[ch >> 4]); 184 buffer.append(gHexChs[ch & 0xf]); 185 } 186 else if (gNeedEscaping[b]) { 187 buffer.append('%'); 188 buffer.append(gAfterEscaping1[b]); 189 buffer.append(gAfterEscaping2[b]); 190 } 191 else { 192 buffer.append((char)b); 193 } 194 } 195 } 196 197 if (buffer.length() != len) 200 return buffer.toString(); 201 else 202 return anyURI; 203 } 204 205 } | Popular Tags |