KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

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 /**
65  * Represent the schema type "anyURI"
66  *
67  * @author Neeraj Bajaj, Sun Microsystems, inc.
68  * @author Sandy Gao, IBM
69  *
70  * @version $Id: AnyURIDV.java,v 1.5 2004/01/20 17:01:53 sandygao Exp $
71  */

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     // before we return string we have to make sure it is correct URI as per spec.
89
// for some types (string and derived), they just return the string itself
90
public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
91         // check 3.2.17.c0 must: URI (rfc 2396/2723)
92
try {
93             if( content.length() != 0 ) {
94                 // encode special characters using XLink 5.4 algorithm
95
content = encode(content);
96                 // Support for relative URLs
97
// According to Java 1.1: URLs may also be specified with a
98
// String and the URL object that it is related to.
99
new URI(BASE_URI, content );
100             }
101         } catch (URI.MalformedURIException ex) {
102             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "anyURI"});
103         }
104
105         // REVISIT: do we need to return the new URI object?
106
return content;
107     }
108
109     // which ASCII characters need to be escaped
110
private static boolean gNeedEscaping[] = new boolean[128];
111     // the first hex character if a character needs to be escaped
112
private static char gAfterEscaping1[] = new char[128];
113     // the second hex character if a character needs to be escaped
114
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     // initialize the above 3 arrays
118
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     // To encode special characters in anyURI, by using %HH to represent
140
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', etc.
141
// and non-ASCII characters (whose value >= 128).
142
private static String JavaDoc encode(String JavaDoc anyURI){
143         int len = anyURI.length(), ch;
144         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(len*3);
145
146         // for each character in the anyURI
147
int i = 0;
148         for (; i < len; i++) {
149             ch = anyURI.charAt(i);
150             // if it's not an ASCII character, break here, and use UTF-8 encoding
151
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         // we saw some non-ascii character
164
if (i < len) {
165             // get UTF-8 bytes for the remaining sub-string
166
byte[] bytes = null;
167             byte b;
168             try {
169                 bytes = anyURI.substring(i).getBytes("UTF-8");
170             } catch (java.io.UnsupportedEncodingException JavaDoc e) {
171                 // should never happen
172
return anyURI;
173             }
174             len = bytes.length;
175
176             // for each byte
177
for (i = 0; i < len; i++) {
178                 b = bytes[i];
179                 // for non-ascii character: make it positive, then escape
180
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 encoding happened, create a new string;
198
// otherwise, return the orginal one.
199
if (buffer.length() != len)
200             return buffer.toString();
201         else
202             return anyURI;
203     }
204
205 } // class AnyURIDV
206
Popular Tags