KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > AnyURIDV


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
24  * Represent the schema type "anyURI"
25  *
26  * @xerces.internal
27  *
28  * @author Neeraj Bajaj, Sun Microsystems, inc.
29  * @author Sandy Gao, IBM
30  *
31  * @version $Id: AnyURIDV.java,v 1.9 2005/01/31 03:54:06 mrglavas Exp $
32  */

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     // before we return string we have to make sure it is correct URI as per spec.
50
// for some types (string and derived), they just return the string itself
51
public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
52         // check 3.2.17.c0 must: URI (rfc 2396/2723)
53
try {
54             if( content.length() != 0 ) {
55                 // encode special characters using XLink 5.4 algorithm
56
final String JavaDoc encoded = encode(content);
57                 // Support for relative URLs
58
// According to Java 1.1: URLs may also be specified with a
59
// String and the URL object that it is related to.
60
new URI(BASE_URI, encoded );
61             }
62         } catch (URI.MalformedURIException ex) {
63             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "anyURI"});
64         }
65
66         // REVISIT: do we need to return the new URI object?
67
return content;
68     }
69
70     // which ASCII characters need to be escaped
71
private static boolean gNeedEscaping[] = new boolean[128];
72     // the first hex character if a character needs to be escaped
73
private static char gAfterEscaping1[] = new char[128];
74     // the second hex character if a character needs to be escaped
75
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     // initialize the above 3 arrays
79
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     // To encode special characters in anyURI, by using %HH to represent
101
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', etc.
102
// and non-ASCII characters (whose value >= 128).
103
private static String JavaDoc encode(String JavaDoc anyURI){
104         int len = anyURI.length(), ch;
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(len*3);
106
107         // for each character in the anyURI
108
int i = 0;
109         for (; i < len; i++) {
110             ch = anyURI.charAt(i);
111             // if it's not an ASCII character, break here, and use UTF-8 encoding
112
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         // we saw some non-ascii character
125
if (i < len) {
126             // get UTF-8 bytes for the remaining sub-string
127
byte[] bytes = null;
128             byte b;
129             try {
130                 bytes = anyURI.substring(i).getBytes("UTF-8");
131             } catch (java.io.UnsupportedEncodingException JavaDoc e) {
132                 // should never happen
133
return anyURI;
134             }
135             len = bytes.length;
136
137             // for each byte
138
for (i = 0; i < len; i++) {
139                 b = bytes[i];
140                 // for non-ascii character: make it positive, then escape
141
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 encoding happened, create a new string;
159
// otherwise, return the orginal one.
160
if (buffer.length() != len) {
161             return buffer.toString();
162         }
163         else {
164             return anyURI;
165         }
166     }
167
168 } // class AnyURIDV
169
Popular Tags