1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| /**
* XMLLoader
* XMLLoader class loads an external text file and decodes the data with the provided character set.
* If you loads a text file encoded in the euc-kr encoding via an URLLoader, frequently, the source data is currupted.
* To avoid this problem, uses URLStream instead of URLLoader.
*
* XMLLoader is the modified version of the XMLLoaderOld class.
* This code is inspired by Wooyaggo's article(http://wooyaggo.tistory.com/116).
*
*
* @author: Han Sanghun (http://hangunsworld.com, hanguns@gmail.com)
* @created: 2008 03 28
* @last modified: 2008 06 06
*/
/*
Licensed under the MIT License
Copyright (c) 2008 Han Sanghun
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
http://hangunsworld.com/classes/com/hangunsworld/net/
*/
package com.hangunsworld.net{
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLStream;
public class XMLLoader{
private var us:URLStream;
private var ul:URLLoader;
private var ur:URLRequest;
private var encode:String;
public var data:String = "";
/**
* XMLLoader constructor
*/
public function XMLLoader(){
us = new URLStream();
ul = new URLLoader();
}// end constructor
/**
* Adds event listeners to the URLLoader object and the URLStream object.
*/
public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
ul.addEventListener(type, listener, useCapture, priority, useWeakReference);
us.addEventListener(type, agentListener, useCapture, priority, useWeakReference);
}// end addEventListener
/**
* Removes event listeners from the URLLoader object and the URLStream object.
*/
public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
ul.removeEventListener(type, listener, useCapture);
us.removeEventListener(type, agentListener, useCapture);
}// end removeEventListener
/**
* Loads external data via the URLStream object,
* and sets the character set with the provied string.
*
* @param pUr The URLRequest object.
* @param pEncode The string denoting the character set to use to interpret the bytes. [OPTIONAL]
*/
public function load(pUr:URLRequest, pEncode:String="utf-8"):void{
encode = pEncode;
ur = pUr;
us.load(ur);
}// end load
/**
* Dispatches events through the URLLoader object.
*/
private function agentListener(e):void{
if(e.type == Event.COMPLETE){
// If the event's type is Event.COMPLETE, decodes the loaded data using the provided character set.
var str:String = us.readMultiByte(us.bytesAvailable, encode);
// Puts the decoded data into the URLLoader object.
data = str;
ul.data = str;
}
ul.dispatchEvent(e.clone());
}// end agentListener
}// end class
}// end package |