1
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
1 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
2 // jkl-parsexml.js ---- JavaScript Kantan Library for Parsing XML
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
3 // Copyright 2005-2007 Kawasaki Yusuke <u-suke@kawa.net>
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
4 // http://www.kawa.net/works/js/jkl/parsexml.html
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
5 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
6 // v0.01 2005/05/18 first release
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
7 // v0.02 2005/05/20 Opera 8.0beta may be abailable but somtimes crashed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
8 // v0.03 2005/05/20 overrideMimeType( "text/xml" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
9 // v0.04 2005/05/21 class variables: REQUEST_TYPE, RESPONSE_TYPE
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
10 // v0.05 2005/05/22 use Msxml2.DOMDocument.5.0 for GET method on IE6
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
11 // v0.06 2005/05/22 CDATA_SECTION_NODE
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
12 // v0.07 2005/05/23 use Microsoft.XMLDOM for GET method on IE6
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
13 // v0.10 2005/10/11 new function: JKL.ParseXML.HTTP.responseText()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
14 // v0.11 2005/10/13 new sub class: JKL.ParseXML.Text, JSON and DOM.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
15 // v0.12 2005/10/14 new sub class: JKL.ParseXML.CSV and CSVmap.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
16 // v0.13 2005/10/28 bug fixed: TEXT_NODE regexp for white spaces
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
17 // v0.14 2005/11/06 bug fixed: TEXT_NODE regexp at Safari
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
18 // v0.15 2005/11/08 bug fixed: JKL.ParseXML.CSV.async() method
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
19 // v0.16 2005/11/15 new sub class: LoadVars, and UTF-8 text on Safari
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
20 // v0.18 2005/11/16 improve: UTF-8 text file on Safari
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
21 // v0.19 2006/02/03 use XMLHTTPRequest instead of ActiveX on IE7,iCab
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
22 // v0.20 2006/03/22 (skipped)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
23 // v0.21 2006/11/30 use ActiveX again on IE7
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
24 // v0.22 2007/01/04 JKL.ParseXML.JSON.parseResponse() updated
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
25 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
26
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
27 if ( typeof(JKL) == 'undefined' ) JKL = function() {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
28
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
29 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
30 // class: JKL.ParseXML
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
31
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
32 JKL.ParseXML = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
33 // debug.print( "new JKL.ParseXML( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
34 this.http = new JKL.ParseXML.HTTP( url, query, method, false );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
35 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
36 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
37
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
38 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
39 // class variables
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
40
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
41 JKL.ParseXML.VERSION = "0.22";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
42 JKL.ParseXML.MIME_TYPE_XML = "text/xml";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
43 JKL.ParseXML.MAP_NODETYPE = [
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
44 "",
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
45 "ELEMENT_NODE", // 1
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
46 "ATTRIBUTE_NODE", // 2
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
47 "TEXT_NODE", // 3
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
48 "CDATA_SECTION_NODE", // 4
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
49 "ENTITY_REFERENCE_NODE", // 5
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
50 "ENTITY_NODE", // 6
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
51 "PROCESSING_INSTRUCTION_NODE", // 7
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
52 "COMMENT_NODE", // 8
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
53 "DOCUMENT_NODE", // 9
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
54 "DOCUMENT_TYPE_NODE", // 10
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
55 "DOCUMENT_FRAGMENT_NODE", // 11
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
56 "NOTATION_NODE" // 12
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
57 ];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
58
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
59 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
60 // define callback function (ajax)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
61
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
62 JKL.ParseXML.prototype.async = function ( func, args ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
63 this.callback_func = func; // callback function
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
64 this.callback_arg = args; // first argument
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
65 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
66
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
67 JKL.ParseXML.prototype.onerror = function ( func, args ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
68 this.onerror_func = func; // callback function
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
69 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
70
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
71 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
72 // method: parse()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
73 // return: parsed object
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
74 // Download a file from remote server and parse it.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
75
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
76 JKL.ParseXML.prototype.parse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
77 if ( ! this.http ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
78
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
79 // set onerror call back
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
80 if ( this.onerror_func ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
81 this.http.onerror( this.onerror_func );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
82 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
83
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
84 if ( this.callback_func ) { // async mode
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
85 var copy = this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
86 var proc = function() {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
87 if ( ! copy.http ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
88 var data = copy.parseResponse();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
89 copy.callback_func( data, copy.callback_arg ); // call back
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
90 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
91 this.http.async( proc );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
92 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
93
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
94 this.http.load();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
95
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
96 if ( ! this.callback_func ) { // sync mode
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
97 var data = this.parseResponse();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
98 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
99 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
100 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
101
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
102 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
103 // every child/children into array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
104 JKL.ParseXML.prototype.setOutputArrayAll = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
105 this.setOutputArray( true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
106 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
107 // a child into scalar, children into array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
108 JKL.ParseXML.prototype.setOutputArrayAuto = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
109 this.setOutputArray( null );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
110 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
111 // every child/children into scalar (first sibiling only)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
112 JKL.ParseXML.prototype.setOutputArrayNever = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
113 this.setOutputArray( false );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
114 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
115 // specified child/children into array, other child/children into scalar
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
116 JKL.ParseXML.prototype.setOutputArrayElements = function ( list ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
117 this.setOutputArray( list );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
118 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
119 // specify how to treate child/children into scalar/array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
120 JKL.ParseXML.prototype.setOutputArray = function ( mode ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
121 if ( typeof(mode) == "string" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
122 mode = [ mode ]; // string into array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
123 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
124 if ( mode && typeof(mode) == "object" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
125 if ( mode.length < 0 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
126 mode = false; // false when array == []
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
127 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
128 var hash = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
129 for( var i=0; i<mode.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
130 hash[mode[i]] = true;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
131 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
132 mode = hash; // array into hashed array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
133 if ( mode["*"] ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
134 mode = true; // true when includes "*"
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
135 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
136 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
137 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
138 this.usearray = mode;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
139 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
140
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
141 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
142 // method: parseResponse()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
143
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
144 JKL.ParseXML.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
145 var root = this.http.documentElement();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
146 var data = this.parseDocument( root );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
147 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
148 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
149
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
150 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
151 // convert from DOM root node to JavaScript Object
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
152 // method: parseElement( rootElement )
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
153
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
154 JKL.ParseXML.prototype.parseDocument = function ( root ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
155 // debug.print( "parseDocument: "+root );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
156 if ( ! root ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
157
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
158 var ret = this.parseElement( root ); // parse root node
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
159 // debug.print( "parsed: "+ret );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
160
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
161 if ( this.usearray == true ) { // always into array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
162 ret = [ ret ];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
163 } else if ( this.usearray == false ) { // always into scalar
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
164 //
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
165 } else if ( this.usearray == null ) { // automatic
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
166 //
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
167 } else if ( this.usearray[root.nodeName] ) { // specified tag
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
168 ret = [ ret ];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
169 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
170
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
171 var json = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
172 json[root.nodeName] = ret; // root nodeName
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
173 return json;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
174 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
175
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
176 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
177 // convert from DOM Element to JavaScript Object
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
178 // method: parseElement( element )
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
179
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
180 JKL.ParseXML.prototype.parseElement = function ( elem ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
181 // debug.print( "nodeType: "+JKL.ParseXML.MAP_NODETYPE[elem.nodeType]+" <"+elem.nodeName+">" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
182
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
183 // COMMENT_NODE
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
184
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
185 if ( elem.nodeType == 7 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
186 return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
187 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
188
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
189 // TEXT_NODE CDATA_SECTION_NODE
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
190
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
191 if ( elem.nodeType == 3 || elem.nodeType == 4 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
192 // var bool = elem.nodeValue.match( /[^\u0000-\u0020]/ );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
193 var bool = elem.nodeValue.match( /[^\x00-\x20]/ ); // for Safari
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
194 if ( bool == null ) return; // ignore white spaces
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
195 // debug.print( "TEXT_NODE: "+elem.nodeValue.length+ " "+bool );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
196 return elem.nodeValue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
197 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
198
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
199 var retval;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
200 var cnt = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
201
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
202 // parse attributes
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
203
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
204 if ( elem.attributes && elem.attributes.length ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
205 retval = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
206 for ( var i=0; i<elem.attributes.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
207 var key = elem.attributes[i].nodeName;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
208 if ( typeof(key) != "string" ) continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
209 var val = elem.attributes[i].nodeValue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
210 if ( ! val ) continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
211 if ( typeof(cnt[key]) == "undefined" ) cnt[key] = 0;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
212 cnt[key] ++;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
213 this.addNode( retval, key, cnt[key], val );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
214 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
215 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
216
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
217 // parse child nodes (recursive)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
218
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
219 if ( elem.childNodes && elem.childNodes.length ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
220 var textonly = true;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
221 if ( retval ) textonly = false; // some attributes exists
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
222 for ( var i=0; i<elem.childNodes.length && textonly; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
223 var ntype = elem.childNodes[i].nodeType;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
224 if ( ntype == 3 || ntype == 4 ) continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
225 textonly = false;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
226 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
227 if ( textonly ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
228 if ( ! retval ) retval = "";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
229 for ( var i=0; i<elem.childNodes.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
230 retval += elem.childNodes[i].nodeValue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
231 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
232 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
233 if ( ! retval ) retval = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
234 for ( var i=0; i<elem.childNodes.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
235 var key = elem.childNodes[i].nodeName;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
236 if ( typeof(key) != "string" ) continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
237 var val = this.parseElement( elem.childNodes[i] );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
238 if ( ! val ) continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
239 if ( typeof(cnt[key]) == "undefined" ) cnt[key] = 0;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
240 cnt[key] ++;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
241 this.addNode( retval, key, cnt[key], val );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
242 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
243 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
244 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
245 return retval;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
246 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
247
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
248 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
249 // method: addNode( hash, key, count, value )
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
250
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
251 JKL.ParseXML.prototype.addNode = function ( hash, key, cnts, val ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
252 if ( this.usearray == true ) { // into array
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
253 if ( cnts == 1 ) hash[key] = [];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
254 hash[key][hash[key].length] = val; // push
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
255 } else if ( this.usearray == false ) { // into scalar
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
256 if ( cnts == 1 ) hash[key] = val; // only 1st sibling
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
257 } else if ( this.usearray == null ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
258 if ( cnts == 1 ) { // 1st sibling
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
259 hash[key] = val;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
260 } else if ( cnts == 2 ) { // 2nd sibling
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
261 hash[key] = [ hash[key], val ];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
262 } else { // 3rd sibling and more
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
263 hash[key][hash[key].length] = val;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
264 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
265 } else if ( this.usearray[key] ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
266 if ( cnts == 1 ) hash[key] = [];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
267 hash[key][hash[key].length] = val; // push
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
268 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
269 if ( cnts == 1 ) hash[key] = val; // only 1st sibling
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
270 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
271 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
272
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
273 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
274 // class: JKL.ParseXML.Text
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
275
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
276 JKL.ParseXML.Text = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
277 // debug.print( "new JKL.ParseXML.Text( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
278 this.http = new JKL.ParseXML.HTTP( url, query, method, true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
279 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
280 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
281
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
282 JKL.ParseXML.Text.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
283 JKL.ParseXML.Text.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
284 JKL.ParseXML.Text.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
285
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
286 JKL.ParseXML.Text.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
287 var data = this.http.responseText();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
288 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
289 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
290
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
291 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
292 // class: JKL.ParseXML.JSON
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
293
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
294 JKL.ParseXML.JSON = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
295 // debug.print( "new JKL.ParseXML.JSON( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
296 this.http = new JKL.ParseXML.HTTP( url, query, method, true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
297 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
298 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
299
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
300 JKL.ParseXML.JSON.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
301 JKL.ParseXML.JSON.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
302 JKL.ParseXML.JSON.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
303
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
304 JKL.ParseXML.JSON.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
305 var text = this.http.responseText();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
306 // http://www.antimon2.atnifty.com/2007/01/jklparsexmljson.html
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
307 if ( typeof(text) == 'undefined' ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
308 if ( ! text.length ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
309 var data = eval( "("+text+")" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
310 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
311 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
312
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
313 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
314 // class: JKL.ParseXML.DOM
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
315
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
316 JKL.ParseXML.DOM = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
317 // debug.print( "new JKL.ParseXML.DOM( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
318 this.http = new JKL.ParseXML.HTTP( url, query, method, false );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
319 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
320 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
321
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
322 JKL.ParseXML.DOM.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
323 JKL.ParseXML.DOM.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
324 JKL.ParseXML.DOM.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
325
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
326 JKL.ParseXML.DOM.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
327 var data = this.http.documentElement();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
328 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
329 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
330
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
331 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
332 // class: JKL.ParseXML.CSV
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
333
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
334 JKL.ParseXML.CSV = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
335 // debug.print( "new JKL.ParseXML.CSV( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
336 this.http = new JKL.ParseXML.HTTP( url, query, method, true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
337 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
338 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
339
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
340 JKL.ParseXML.CSV.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
341 JKL.ParseXML.CSV.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
342 JKL.ParseXML.CSV.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
343
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
344 JKL.ParseXML.CSV.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
345 var text = this.http.responseText();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
346 var data = this.parseCSV( text );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
347 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
348 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
349
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
350 JKL.ParseXML.CSV.prototype.parseCSV = function ( text ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
351 text = text.replace( /\r\n?/g, "\n" ); // new line character
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
352 var pos = 0;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
353 var len = text.length;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
354 var table = [];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
355 while( pos<len ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
356 var line = [];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
357 while( pos<len ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
358 if ( text.charAt(pos) == '"' ) { // "..." quoted column
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
359 var nextquote = text.indexOf( '"', pos+1 );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
360 while ( nextquote<len && nextquote > -1 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
361 if ( text.charAt(nextquote+1) != '"' ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
362 break; // end of column
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
363 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
364 nextquote = text.indexOf( '"', nextquote+2 );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
365 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
366 if ( nextquote < 0 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
367 // unclosed quote
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
368 } else if ( text.charAt(nextquote+1) == "," ) { // end of column
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
369 var quoted = text.substr( pos+1, nextquote-pos-1 );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
370 quoted = quoted.replace(/""/g,'"');
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
371 line[line.length] = quoted;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
372 pos = nextquote+2;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
373 continue;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
374 } else if ( text.charAt(nextquote+1) == "\n" || // end of line
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
375 len==nextquote+1 ) { // end of file
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
376 var quoted = text.substr( pos+1, nextquote-pos-1 );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
377 quoted = quoted.replace(/""/g,'"');
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
378 line[line.length] = quoted;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
379 pos = nextquote+2;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
380 break;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
381 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
382 // invalid column
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
383 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
384 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
385 var nextcomma = text.indexOf( ",", pos );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
386 var nextnline = text.indexOf( "\n", pos );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
387 if ( nextnline < 0 ) nextnline = len;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
388 if ( nextcomma > -1 && nextcomma < nextnline ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
389 line[line.length] = text.substr( pos, nextcomma-pos );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
390 pos = nextcomma+1;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
391 } else { // end of line
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
392 line[line.length] = text.substr( pos, nextnline-pos );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
393 pos = nextnline+1;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
394 break;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
395 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
396 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
397 if ( line.length >= 0 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
398 table[table.length] = line; // push line
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
399 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
400 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
401 if ( table.length < 0 ) return; // null data
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
402 return table;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
403 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
404
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
405 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
406 // class: JKL.ParseXML.CSVmap
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
407
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
408 JKL.ParseXML.CSVmap = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
409 // debug.print( "new JKL.ParseXML.CSVmap( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
410 this.http = new JKL.ParseXML.HTTP( url, query, method, true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
411 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
412 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
413
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
414 JKL.ParseXML.CSVmap.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
415 JKL.ParseXML.CSVmap.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
416 JKL.ParseXML.CSVmap.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
417 JKL.ParseXML.CSVmap.prototype.parseCSV = JKL.ParseXML.CSV.prototype.parseCSV;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
418
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
419 JKL.ParseXML.CSVmap.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
420 var text = this.http.responseText();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
421 var source = this.parseCSV( text );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
422 if ( ! source ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
423 if ( source.length < 0 ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
424
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
425 var title = source.shift(); // first line as title
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
426 var data = [];
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
427 for( var i=0; i<source.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
428 var hash = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
429 for( var j=0; j<title.length && j<source[i].length; j++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
430 hash[title[j]] = source[i][j]; // array to map
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
431 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
432 data[data.length] = hash; // push line
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
433 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
434 return data;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
435 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
436
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
437 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
438 // class: JKL.ParseXML.LoadVars
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
439
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
440 JKL.ParseXML.LoadVars = function ( url, query, method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
441 // debug.print( "new JKL.ParseXML.LoadVars( '"+url+"', '"+query+"', '"+method+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
442 this.http = new JKL.ParseXML.HTTP( url, query, method, true );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
443 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
444 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
445
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
446 JKL.ParseXML.LoadVars.prototype.parse = JKL.ParseXML.prototype.parse;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
447 JKL.ParseXML.LoadVars.prototype.async = JKL.ParseXML.prototype.async;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
448 JKL.ParseXML.LoadVars.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
449
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
450 JKL.ParseXML.LoadVars.prototype.parseResponse = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
451 var text = this.http.responseText();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
452 text = text.replace( /\r\n?/g, "\n" ); // new line character
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
453 var hash = {};
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
454 var list = text.split( "&" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
455 for( var i=0; i<list.length; i++ ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
456 var eq = list[i].indexOf( "=" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
457 if ( eq > -1 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
458 var key = decodeURIComponent(list[i].substr(0,eq).replace("+","%20"));
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
459 var val = decodeURIComponent(list[i].substr(eq+1).replace("+","%20"));
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
460 hash[key] = val;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
461 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
462 hash[list[i]] = "";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
463 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
464 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
465 return hash;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
466 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
467
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
468 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
469 // class: JKL.ParseXML.HTTP
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
470 // constructer: new JKL.ParseXML.HTTP()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
471
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
472 JKL.ParseXML.HTTP = function( url, query, method, textmode ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
473 // debug.print( "new JKL.ParseXML.HTTP( '"+url+"', '"+query+"', '"+method+"', '"+textmode+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
474 this.url = url;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
475 if ( typeof(query) == "string" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
476 this.query = query;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
477 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
478 this.query = "";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
479 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
480 if ( method ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
481 this.method = method;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
482 } else if ( typeof(query) == "string" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
483 this.method = "POST";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
484 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
485 this.method = "GET";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
486 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
487 this.textmode = textmode ? true : false;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
488 this.req = null;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
489 this.xmldom_flag = false;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
490 this.onerror_func = null;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
491 this.callback_func = null;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
492 this.already_done = null;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
493 return this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
494 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
495
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
496 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
497 // class variables
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
498
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
499 JKL.ParseXML.HTTP.REQUEST_TYPE = "application/x-www-form-urlencoded";
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
500 JKL.ParseXML.HTTP.ACTIVEX_XMLDOM = "Microsoft.XMLDOM"; // Msxml2.DOMDocument.5.0
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
501 JKL.ParseXML.HTTP.ACTIVEX_XMLHTTP = "Microsoft.XMLHTTP"; // Msxml2.XMLHTTP.3.0
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
502 JKL.ParseXML.HTTP.EPOCH_TIMESTAMP = "Thu, 01 Jun 1970 00:00:00 GMT"
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
503
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
504 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
505
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
506 JKL.ParseXML.HTTP.prototype.onerror = JKL.ParseXML.prototype.onerror;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
507 JKL.ParseXML.HTTP.prototype.async = function( func ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
508 this.async_func = func;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
509 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
510
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
511 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
512 // [IE+IXMLDOMElement]
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
513 // XML text/xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
514 // XML application/rdf+xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
515 // TEXT text/plain NG
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
516 // TEXT others NG
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
517 // [IE+IXMLHttpRequest]
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
518 // XML text/xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
519 // XML application/rdf+xml NG
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
520 // TEXT text/plain OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
521 // TEXT others OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
522 // [Firefox+XMLHttpRequest]
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
523 // XML text/xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
524 // XML application/rdf+xml OK (overrideMimeType)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
525 // TEXT text/plain OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
526 // TEXT others OK (overrideMimeType)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
527 // [Opera+XMLHttpRequest]
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
528 // XML text/xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
529 // XML application/rdf+xml OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
530 // TEXT text/plain OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
531 // TEXT others OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
532 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
533
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
534 JKL.ParseXML.HTTP.prototype.load = function() {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
535 // create XMLHttpRequest object
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
536 if ( window.ActiveXObject ) { // IE5.5,6,7
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
537 var activex = JKL.ParseXML.HTTP.ACTIVEX_XMLHTTP; // IXMLHttpRequest
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
538 if ( this.method == "GET" && ! this.textmode ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
539 // use IXMLDOMElement to accept any mime types
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
540 // because overrideMimeType() is not available on IE6
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
541 activex = JKL.ParseXML.HTTP.ACTIVEX_XMLDOM; // IXMLDOMElement
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
542 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
543 // debug.print( "new ActiveXObject( '"+activex+"' )" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
544 this.req = new ActiveXObject( activex );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
545 } else if ( window.XMLHttpRequest ) { // Firefox, Opera, iCab
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
546 // debug.print( "new XMLHttpRequest()" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
547 this.req = new XMLHttpRequest();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
548 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
549
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
550 // async mode when call back function is specified
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
551 var async_flag = this.async_func ? true : false;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
552 // debug.print( "async: "+ async_flag );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
553
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
554 // open for XMLHTTPRequest (not for IXMLDOMElement)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
555 if ( typeof(this.req.send) != "undefined" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
556 // debug.print( "open( '"+this.method+"', '"+this.url+"', "+async_flag+" );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
557 this.req.open( this.method, this.url, async_flag );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
558 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
559
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
560 // // If-Modified-Since: Thu, 01 Jun 1970 00:00:00 GMT
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
561 // if ( typeof(this.req.setRequestHeader) != "undefined" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
562 // // debug.print( "If-Modified-Since"+JKL.ParseXML.HTTP.EPOCH_TIMESTAMP );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
563 // this.req.setRequestHeader( "If-Modified-Since", JKL.ParseXML.HTTP.EPOCH_TIMESTAMP );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
564 // }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
565
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
566 // Content-Type: application/x-www-form-urlencoded (request header)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
567 // Some server does not accept without request content-type.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
568 if ( typeof(this.req.setRequestHeader) != "undefined" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
569 // debug.print( "Content-Type: "+JKL.ParseXML.HTTP.REQUEST_TYPE+" (request)" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
570 this.req.setRequestHeader( "Content-Type", JKL.ParseXML.HTTP.REQUEST_TYPE );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
571 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
572
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
573 // Content-Type: text/xml (response header)
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
574 // FireFox does not accept other mime types like application/rdf+xml etc.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
575 if ( typeof(this.req.overrideMimeType) != "undefined" && ! this.textmode ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
576 // debug.print( "Content-Type: "+JKL.ParseXML.MIME_TYPE_XML+" (response)" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
577 this.req.overrideMimeType( JKL.ParseXML.MIME_TYPE_XML );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
578 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
579
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
580 // set call back handler when async mode
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
581 if ( async_flag ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
582 var copy = this;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
583 copy.already_done = false; // not parsed yet
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
584 var check_func = function () {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
585 if ( copy.req.readyState != 4 ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
586 // debug.print( "readyState(async): "+copy.req.readyState );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
587 var succeed = copy.checkResponse();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
588 // debug.print( "checkResponse(async): "+succeed );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
589 if ( ! succeed ) return; // failed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
590 if ( copy.already_done ) return; // parse only once
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
591 copy.already_done = true; // already parsed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
592 copy.async_func(); // call back async
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
593 };
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
594 this.req.onreadystatechange = check_func;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
595 // for document.implementation.createDocument
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
596 // this.req.onload = check_func;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
597 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
598
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
599 // send the request and query string
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
600 if ( typeof(this.req.send) != "undefined" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
601 // debug.print( "XMLHTTPRequest: send( '"+this.query+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
602 this.req.send( this.query ); // XMLHTTPRequest
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
603 } else if ( typeof(this.req.load) != "undefined" ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
604 // debug.print( "IXMLDOMElement: load( '"+this.url+"' );" );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
605 this.req.async = async_flag;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
606 this.req.load( this.url ); // IXMLDOMElement
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
607 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
608
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
609 // just return when async mode
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
610 if ( async_flag ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
611
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
612 var succeed = this.checkResponse();
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
613 // debug.print( "checkResponse(sync): "+succeed );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
614 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
615
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
616 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
617 // method: checkResponse()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
618
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
619 JKL.ParseXML.HTTP.prototype.checkResponse = function() {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
620 // parseError on IXMLDOMElement
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
621 if ( this.req.parseError && this.req.parseError.errorCode != 0 ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
622 // debug.print( "parseError: "+this.req.parseError.reason );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
623 if ( this.onerror_func ) this.onerror_func( this.req.parseError.reason );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
624 return false; // failed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
625 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
626
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
627 // HTTP response code
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
628 if ( this.req.status-0 > 0 &&
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
629 this.req.status != 200 && // OK
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
630 this.req.status != 206 && // Partial Content
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
631 this.req.status != 304 ) { // Not Modified
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
632 // debug.print( "status: "+this.req.status );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
633 if ( this.onerror_func ) this.onerror_func( this.req.status );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
634 return false; // failed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
635 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
636
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
637 return true; // succeed
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
638 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
639
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
640 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
641 // method: documentElement()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
642 // return: XML DOM in response body
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
643
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
644 JKL.ParseXML.HTTP.prototype.documentElement = function() {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
645 // debug.print( "documentElement: "+this.req );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
646 if ( ! this.req ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
647 if ( this.req.responseXML ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
648 return this.req.responseXML.documentElement; // XMLHTTPRequest
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
649 } else {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
650 return this.req.documentElement; // IXMLDOMDocument
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
651 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
652 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
653
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
654 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
655 // method: responseText()
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
656 // return: text string in response body
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
657
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
658 JKL.ParseXML.HTTP.prototype.responseText = function() {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
659 // debug.print( "responseText: "+this.req );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
660 if ( ! this.req ) return;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
661
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
662 // Safari and Konqueror cannot understand the encoding of text files.
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
663 if ( navigator.appVersion.match( "KHTML" ) ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
664 var esc = escape( this.req.responseText );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
665 // debug.print( "escape: "+esc );
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
666 if ( ! esc.match("%u") && esc.match("%") ) {
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
667 return decodeURIComponent(esc);
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
668 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
669 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
670
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
671 return this.req.responseText;
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
672 }
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
673
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
674 // ================================================================
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
675 // http://msdn.microsoft.com/library/en-us/xmlsdk/html/d051f7c5-e882-42e8-a5b6-d1ce67af275c.asp
|
e085737@tamayose-syuuichi-no-macbook.local
parents:
diff
changeset
|
676 // ================================================================
|