summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/java-exa.tex
blob: f40f70f84df91316c7037a469e801f8e81dc114b (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
%D \module
%D   [       file=java-exa,
%D        version=2002.??.??,
%D          title=\CONTEXT\ JavaScript Macros,
%D       subtitle=Example Support,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% XFDF versus HTML
% localhost versus remote versus set

% filename | filename-nr => name
% fakename | fakename-nr => file upload, unless localhost
%
% we erase the list because otherwise we end up in browser remembering
% problems; also, it is not possible to set upload fields 'manually'

\startJSpreamble request_1 used now

    var example_method = "HTML" ;
    var example_host   = "" ;
    var example_port   = "" ;
    var example_url    = "" ;
    var example_file   = "" ;

    var example_log    = true ; % false

    function stripped_exa_value( name ) {
        f = this.getField(name) ;
        if (f) {
            str = f.value ;
            if (str.indexOf(name+'-')==0) {
                str = str.substr(name.length+1) ;
            }
            return str ;
        } else {
            return "" ;
        }
    }

    function identify_example_url ( ) {
        if (example_log) {
            console.clear ;
            console.println("base url: "+this.baseURL) ;
            console.println("this url: "+this.URL) ;
        }
        if (this.baseURL != "") {
            example_url = this.baseURL ;
        } else {
            if (this.URL != "") {
                example_url = this.URL
            }
        }
        if (example_url.indexOf("file://")>=0) {
            example_url = "" ;
        }
        str = stripped_exa_value("examplehost") ;
        if (str != "auto") {
            example_host = str ;
            if (str == "localhost") {
                example_port = "8061"
            }
        }
        str = stripped_exa_value("exampleport") ;
        if (str != "auto") {
            example_port = str ;
        }
        if (example_log) {
            console.println("example_url: "+example_url) ;
            console.println("example_host: "+example_host) ;
            console.println("example_port: "+example_port) ;
        }
    }

    function check_example_url ( url ) {
        if (url.indexOf("file://")>=0) {
            url = "http://localhost:8061" ;
            console.println("file url replaced by: "+url) ;
        } else {
            if (url.indexOf("://")<0) {
                url = "http://" + url ;
            }
            if (example_log) {
                console.println("url before check: "+url) ;
            }
%            url = url.replace(/(http:\\\/\\\/.+)(\\\/.*$)/, "$1") ;
            url = url.replace(/(https*:\\\/\\\/.+?)(\\\/.*$)/, "$1/exarequest") ;
            if (example_log) {
                console.println("url after check: "+url) ;
            }
        }
        return url ;
    }

    function set_example_xfdf ( method ) {
        example_method = method ;
    }

    function set_example_file ( file ) {
        example_method = file ;
    }

    function set_example_host ( host ) {
        example_host = host ;
    }

    function set_example_port ( port ) {
        example_host = host ;
    }

    function do_erase_example_file(tag) {
        f = this.getField(tag) ;
        if (f) {
            f.value = "" ;
        }
    }

    function do_erase_example_list(tag) {
        for (i=1;i<=100;i++) {
            f = this.getField(tag+"-"+i) ;
            if (f) {
                f.value = "" ;
            } else {
                return
            }
        }
    }

    function do_submit_example_url ( url ) {
        if ((example_method == "XFDF" ) || (url.indexOf("localhost")>=0)) {
            do_erase_example_file("fakename") ;
            do_erase_example_list("fakename") ;
        }
        url = check_example_url(url) ;
        if (example_log) {
            console.println("submitting form to "+url+" using method "+example_method) ;
        }
        if (example_file != "") {
            url = url + "/" + example_file ;
        }
        % we need the bFDF for acrobat 5
        this.submitForm({cURL : url, bFDF : false, cSubmitAs : example_method}) ;
    }

    function submit_example_form ( ) {
        identify_example_url() ;
        if (example_host != "") {
            if (example_port != "") {
                do_submit_example_url(example_host+":"+example_port) ;
            } else {
                do_submit_example_url(example_host) ;
            }
        } else {
            if (example_url != "") {
                do_submit_example_url(example_url) ;
            } else {
                if (example_port != "") {
                    do_submit_example_url("localhost"+":"+example_port) ;
                } else {
                    do_submit_example_url("localhost:8061") ; % local exampler
                }
            }
        }
        resetfilename () ;
    }

    function submit_form ( host, port) {
        set_example_host(host) ;
        set_example_post(post) ;
        submit_example_form ;
    }

\stopJSpreamble

\startJSpreamble request_2 used now

    var exa_command    = "" ;
    var exa_option     = "" ;
    var exa_filename   = "" ;
    var exa_filelist   = "" ;
    var exa_registered = "" ;

    function set_request (command,option) {
        exa_command = command ;
        if (exa_command == "") {
            exa_option = option ;
        } else { if (option == "") {
            exa_option = "" ;
        } else { if (option.indexOf("--")<0) {
            exa_option = "--action=" + option ;
        } else {
            exa_option = option ;
        } } }
    }

    function assemble_request ( ) {
        v = this.getField('filename') ;
        if (v) {
            exa_filename = v.value ;
        }
        v = this.getField('filelist') ;
        if (v) {
            exa_filelist = v.value ;
        }
%       exa_filename = exa_filename.replace(/\\\\/g,'/') ;
%       exa_filelist = exa_filelist.replace(/\\\\/g,'/') ;
        str = "<?xml version='1.0' ?>\\n" ;
        str = str+"<exa:request xmlns:exa='http://www.pragma-ade.com/schemas/example.rng'><exa:application>" ;
        if (exa_filelist == "") {
            exa_filelist = exa_registered ;
        } else { if (exa_registered != "") {
            exa_filelist = exa_filelist + "\\n" + exa_registered ;
        } }
        if (exa_command != "") {
            str = str+"<exa:command>"+exa_command+"</exa:command>" ;
        }
        if (exa_option != "") {
            str = str+"<exa:option>"+exa_option+"</exa:option>" ;
        }
        if (exa_filename != "") {
            str = str+"<exa:filename>"+exa_filename+"</exa:filename>" ;
        }
        var fls = "" ;
        if (exa_filelist != "") {
            % old method, soon obsolete
            lst = exa_filelist.split(/\\s/) ;
            for (i=0;i<lst.length;i++) {
                fls = fls+"<exa:entry>"+lst[i]+"</exa:entry>" ;
            }
        } else { if (exa_multiple) {
            for (i=1;i<=100;i++) {
                % console.println("file field "+i) ;
                f = this.getField("filename-"+ i) ;
%                 if (f) { if (f.value != "") {
%                     if (g) { if (g.value == "") {
%                         fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
%                     } else {
%                         fls = fls+"<exa:entry label='" + g.value + "'>"+f.value+"</exa:entry>" ;
%                     } } else {
%                         fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
%                     }
%                 } }
                if (f) { if (f.value != "") {
                    fls = fls + "<exa:entry" ;
                    g = this.getField("dataname-"+ i) ;
                    if (g) { if (g.value == "") {
                        % console.println("a") ;
                        fls = fls + ">" ;
                    } else {
                        % console.println("b") ;
                        fls = fls + " label='" + g.value + "'>" ;
                    } } else {
                        % console.println("c") ;
                        fls = fls + ">" ;
                    }
                    fls = fls + f.value + "</exa:entry>" ;
                } }
            }
        } }
        if (fls != "") {
            str = str + "<exa:filelist>" + fls + "</exa:filelist>" ;
        }
        str = str + "</exa:application></exa:request>" ;
        v = this.getField('exa:request') ;
        if (v) {
            v.value = str ;
        }
    }

\stopJSpreamble

\startJSpreamble request_3 used now

    var exa_multiple = false ;

    function setfilename ( suffixes ) {
        if (event.targetName) {
            var name = event.targetName ;
            var fake = name.replace(/filename/,"fakename") ;
        } else {
            var name = 'filename' ;
            var fake = 'fakename' ;
        }
        f = this.getField(fake) ;
        if (f) {
            f.browseForFileToSubmit() ;
            if ((suffixes != "") && (f.value != "")) {
                %
                s = suffixes.replace(/,/g,"|") ;
                r = new RegExp() ;
                s = "\\\\.(" + s + ")$" ;
                r.compile(s, "i") ;
                if (f.value.search(r)<=0) {
                    f.value = "" ;
                    app.alert('This filetype is not permitted.') ;
                %
                % lst = suffixes.split(/,/) ;
                % ok = false ;
                % for (i=0;i<lst.length;i++) {
                %     if (f.value == lst[i]) {
                %         ok = true ;
                %     }
                % }
                % if (! ok) {
                %     f.value = "" ;
                %     app.alert('This filetype is not permitted.') ;
                %
                }
            }
            g = this.getField(name) ;
            if (g) {
                g.value = f.value ;
            }
% we need to set the value of the selector to empty else
% we get some funny preloading of multimeg files in spite
% of the specs that say that xml fields will not get sent
%             if (example_method == "XFDF" ) {
%                 f.value = "" ;
%             } else {
%                 identify_example_url ;
%                 if (example_url.indexOf("localhost")>=0) {
%                     f.value = "" ;
%                 }
%             }
        }
        this.dirty = false ;
    }

    function addfilename () {
        if (exa_multiple) {
            h = this.getField("filelist") ;
            g = this.getField("filename") ;
            if ((g) && (h)) {
                str = g.value ;
                if (h.value == "") {
                    h.value = str ;
                } else {
                    h.value = h.value + "\\n" + str ;
                }
                g.value = "" ;
                this.value = "" ;
            }
        }
        this.dirty = false ;
    }

    % this only works with client that assembles request

    function registerfilename (str) {
        if (str!="") {
            h = this.getField("filelist") ;
            if (h) {
                if (h.value != "") {
                    h.value = h.value + "\\n" ;
                }
                h.value = h.value + str ;
            } else {
                if (exa_registered != "") {
                    exa_registered = exa_registered + "\\n" ;
                }
                exa_registered = exa_registered + str ;
            }
        }
        console.show ;
        console.println('registered files') ;
        console.println("file: "+str) ;
        console.println("list: "+exa_registered) ;
        this.dirty = false ;
    }

    function checkfilename () {
    }

    function getfilename ( suffixes ) {
        setfilename(suffixes) ;
        checkfilename() ;
        addfilename() ;
    }

    function resetfilename () {
        do_erase_example_file("fakename") ;
        do_erase_example_file("filename") ;
        do_erase_example_file("filelist") ;
        do_erase_example_list("filename") ;
        do_erase_example_list("fakename") ;
    }

\stopJSpreamble

\endinput