KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > EscapeURI


1 package net.sf.saxon.functions;
2 import net.sf.saxon.charcode.UnicodeCharacterSet;
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.FastStringBuffer;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.AtomicValue;
8 import net.sf.saxon.value.BooleanValue;
9 import net.sf.saxon.value.StringValue;
10
11 /**
12  * This class supports the old function escape-uri() and the new replacement functions
13  * encode-for-uri() and iri-to-uri()
14  */

15
16 public class EscapeURI extends SystemFunction {
17
18     public static final int ESCAPE = 0;
19     public static final int ENCODE_FOR_URI = 1;
20     public static final int IRI_TO_URI = 2;
21     public static final int HTML_URI = 3; // not yet used
22

23     /**
24     * Evaluate the function
25     */

26
27     public Item evaluateItem(XPathContext c) throws XPathException {
28         Item item = argument[0].evaluateItem(c);
29         if (item == null) {
30             return StringValue.EMPTY_STRING;
31         }
32         CharSequence JavaDoc s = item.getStringValueCS();
33         boolean escapeReserved = false;
34         switch (operation) {
35             case ESCAPE:
36                 AtomicValue av2 = (AtomicValue)argument[1].evaluateItem(c);
37                 escapeReserved = ((BooleanValue)av2.getPrimitiveValue()).getBooleanValue();
38                 break;
39             case ENCODE_FOR_URI:
40                 escapeReserved = true;
41                 break;
42             case IRI_TO_URI:
43                 escapeReserved = false;
44                 break;
45         }
46         return StringValue.makeStringValue(escape(s, escapeReserved));
47     }
48
49
50 // private static String allHexDigits = "0123456789abcdefABCDEF";
51

52     public static CharSequence JavaDoc escape(CharSequence JavaDoc s, boolean escapeReserved) {
53         FastStringBuffer sb = new FastStringBuffer(s.length());
54         for (int i=0; i<s.length(); i++) {
55             char c = s.charAt(i);
56             if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')) {
57                 sb.append(c);
58             } else if (c<=0x20 || c>=0x7f) {
59                 escapeChar(c, ((i+1)<s.length() ? s.charAt(i+1) : ' '), sb);
60             } else if (escapeReserved) {
61                 if ("-_.!~*'()%".indexOf(c)>=0) {
62                     sb.append(c);
63                 } else {
64                     escapeChar(c, ' ', sb);
65                 }
66             } else {
67                 if ("-_.!~*'()%;/?:@&=+$,#[]".indexOf(c)>=0) {
68                     sb.append(c);
69                 } else {
70                     escapeChar(c, ' ', sb);
71                 }
72             }
73         }
74         return sb;
75     }
76
77     private static final String JavaDoc hex = "0123456789ABCDEF";
78
79     private static void escapeChar(char c, char c2, FastStringBuffer sb) {
80         byte[] array = new byte[4];
81         int used = UnicodeCharacterSet.getUTF8Encoding(c, c2, array);
82         for (int b=0; b<used; b++) {
83             int v = (array[b]>=0 ? array[b] : 256 + array[b]);
84             sb.append('%');
85             sb.append(hex.charAt(v/16));
86             sb.append(hex.charAt(v%16));
87         }
88     }
89
90 }
91
92
93
94 //
95
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
96
// you may not use this file except in compliance with the License. You may obtain a copy of the
97
// License at http://www.mozilla.org/MPL/
98
//
99
// Software distributed under the License is distributed on an "AS IS" basis,
100
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
101
// See the License for the specific language governing rights and limitations under the License.
102
//
103
// The Original Code is: all this file.
104
//
105
// The Initial Developer of the Original Code is Michael H. Kay
106
//
107
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
108
//
109
// Contributor(s): none.
110
//
111
Popular Tags