mas01mj@732
|
1 /*
|
mas01mj@732
|
2 Copyright (c) 2008, Adobe Systems Incorporated
|
mas01mj@732
|
3 All rights reserved.
|
mas01mj@732
|
4
|
mas01mj@732
|
5 Redistribution and use in source and binary forms, with or without
|
mas01mj@732
|
6 modification, are permitted provided that the following conditions are
|
mas01mj@732
|
7 met:
|
mas01mj@732
|
8
|
mas01mj@732
|
9 * Redistributions of source code must retain the above copyright notice,
|
mas01mj@732
|
10 this list of conditions and the following disclaimer.
|
mas01mj@732
|
11
|
mas01mj@732
|
12 * Redistributions in binary form must reproduce the above copyright
|
mas01mj@732
|
13 notice, this list of conditions and the following disclaimer in the
|
mas01mj@732
|
14 documentation and/or other materials provided with the distribution.
|
mas01mj@732
|
15
|
mas01mj@732
|
16 * Neither the name of Adobe Systems Incorporated nor the names of its
|
mas01mj@732
|
17 contributors may be used to endorse or promote products derived from
|
mas01mj@732
|
18 this software without specific prior written permission.
|
mas01mj@732
|
19
|
mas01mj@732
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
mas01mj@732
|
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
mas01mj@732
|
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
mas01mj@732
|
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mas01mj@732
|
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
mas01mj@732
|
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
mas01mj@732
|
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
mas01mj@732
|
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
mas01mj@732
|
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
mas01mj@732
|
31 */
|
mas01mj@732
|
32
|
mas01mj@732
|
33 package com.adobe.net
|
mas01mj@732
|
34 {
|
mas01mj@732
|
35 import flash.utils.ByteArray;
|
mas01mj@732
|
36
|
mas01mj@732
|
37 /**
|
mas01mj@732
|
38 * This class implements an efficient lookup table for URI
|
mas01mj@732
|
39 * character escaping. This class is only needed if you
|
mas01mj@732
|
40 * create a derived class of URI to handle custom URI
|
mas01mj@732
|
41 * syntax. This class is used internally by URI.
|
mas01mj@732
|
42 *
|
mas01mj@732
|
43 * @langversion ActionScript 3.0
|
mas01mj@732
|
44 * @playerversion Flash 9.0*
|
mas01mj@732
|
45 */
|
mas01mj@732
|
46 public class URIEncodingBitmap extends ByteArray
|
mas01mj@732
|
47 {
|
mas01mj@732
|
48 /**
|
mas01mj@732
|
49 * Constructor. Creates an encoding bitmap using the given
|
mas01mj@732
|
50 * string of characters as the set of characters that need
|
mas01mj@732
|
51 * to be URI escaped.
|
mas01mj@732
|
52 *
|
mas01mj@732
|
53 * @langversion ActionScript 3.0
|
mas01mj@732
|
54 * @playerversion Flash 9.0
|
mas01mj@732
|
55 */
|
mas01mj@732
|
56 public function URIEncodingBitmap(charsToEscape:String) : void
|
mas01mj@732
|
57 {
|
mas01mj@732
|
58 var i:int;
|
mas01mj@732
|
59 var data:ByteArray = new ByteArray();
|
mas01mj@732
|
60
|
mas01mj@732
|
61 // Initialize our 128 bits (16 bytes) to zero
|
mas01mj@732
|
62 for (i = 0; i < 16; i++)
|
mas01mj@732
|
63 this.writeByte(0);
|
mas01mj@732
|
64
|
mas01mj@732
|
65 data.writeUTFBytes(charsToEscape);
|
mas01mj@732
|
66 data.position = 0;
|
mas01mj@732
|
67
|
mas01mj@732
|
68 while (data.bytesAvailable)
|
mas01mj@732
|
69 {
|
mas01mj@732
|
70 var c:int = data.readByte();
|
mas01mj@732
|
71
|
mas01mj@732
|
72 if (c > 0x7f)
|
mas01mj@732
|
73 continue; // only escape low bytes
|
mas01mj@732
|
74
|
mas01mj@732
|
75 var enc:int;
|
mas01mj@732
|
76 this.position = (c >> 3);
|
mas01mj@732
|
77 enc = this.readByte();
|
mas01mj@732
|
78 enc |= 1 << (c & 0x7);
|
mas01mj@732
|
79 this.position = (c >> 3);
|
mas01mj@732
|
80 this.writeByte(enc);
|
mas01mj@732
|
81 }
|
mas01mj@732
|
82 }
|
mas01mj@732
|
83
|
mas01mj@732
|
84 /**
|
mas01mj@732
|
85 * Based on the data table contained in this object, check
|
mas01mj@732
|
86 * if the given character should be escaped.
|
mas01mj@732
|
87 *
|
mas01mj@732
|
88 * @param char the character to be escaped. Only the first
|
mas01mj@732
|
89 * character in the string is used. Any other characters
|
mas01mj@732
|
90 * are ignored.
|
mas01mj@732
|
91 *
|
mas01mj@732
|
92 * @return the integer value of the raw UTF8 character. For
|
mas01mj@732
|
93 * example, if '%' is given, the return value is 37 (0x25).
|
mas01mj@732
|
94 * If the character given does not need to be escaped, the
|
mas01mj@732
|
95 * return value is zero.
|
mas01mj@732
|
96 *
|
mas01mj@732
|
97 * @langversion ActionScript 3.0
|
mas01mj@732
|
98 * @playerversion Flash 9.0
|
mas01mj@732
|
99 */
|
mas01mj@732
|
100 public function ShouldEscape(char:String) : int
|
mas01mj@732
|
101 {
|
mas01mj@732
|
102 var data:ByteArray = new ByteArray();
|
mas01mj@732
|
103 var c:int, mask:int;
|
mas01mj@732
|
104
|
mas01mj@732
|
105 // write the character into a ByteArray so
|
mas01mj@732
|
106 // we can pull it out as a raw byte value.
|
mas01mj@732
|
107 data.writeUTFBytes(char);
|
mas01mj@732
|
108 data.position = 0;
|
mas01mj@732
|
109 c = data.readByte();
|
mas01mj@732
|
110
|
mas01mj@732
|
111 if (c & 0x80)
|
mas01mj@732
|
112 {
|
mas01mj@732
|
113 // don't escape high byte characters. It can make international
|
mas01mj@732
|
114 // URI's unreadable. We just want to escape characters that would
|
mas01mj@732
|
115 // make URI syntax ambiguous.
|
mas01mj@732
|
116 return 0;
|
mas01mj@732
|
117 }
|
mas01mj@732
|
118 else if ((c < 0x1f) || (c == 0x7f))
|
mas01mj@732
|
119 {
|
mas01mj@732
|
120 // control characters must be escaped.
|
mas01mj@732
|
121 return c;
|
mas01mj@732
|
122 }
|
mas01mj@732
|
123
|
mas01mj@732
|
124 this.position = (c >> 3);
|
mas01mj@732
|
125 mask = this.readByte();
|
mas01mj@732
|
126
|
mas01mj@732
|
127 if (mask & (1 << (c & 0x7)))
|
mas01mj@732
|
128 {
|
mas01mj@732
|
129 // we need to escape this, return the numeric value
|
mas01mj@732
|
130 // of the character
|
mas01mj@732
|
131 return c;
|
mas01mj@732
|
132 }
|
mas01mj@732
|
133 else
|
mas01mj@732
|
134 {
|
mas01mj@732
|
135 return 0;
|
mas01mj@732
|
136 }
|
mas01mj@732
|
137 }
|
mas01mj@732
|
138 }
|
mas01mj@732
|
139 } |