11
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
12
// - special damages arising in any way out of the use of this software. -
13
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2012 amaury darsch -
14
// - copyright (c) 1999-2015 amaury darsch -
15
15
// ---------------------------------------------------------------------------
17
17
#include "Runnable.hpp"
36
38
static const String URI_HTTP_SCHM = "http";
37
39
// the uri default path
38
40
static const String URI_PATH_XDEF = "/";
39
// the uri default port
40
static const long URI_PORT_XDEF = 80;
42
// get an input file stream
43
static InputStream* get_file_stream (const Uri& uri) {
42
// get a file input stream
43
static InputStream* get_file_istream (const Uri& uri) {
45
45
// get the system path from the uri path
46
46
String path = uri.getsysp ();
47
47
// open a file for input
48
48
return new InputFile (path);
50
throw Exception ("file-error", "cannot access uri", uri.getname ());
54
// get an input http stream
55
static InputStream* get_http_stream (const Uri& uri) {
50
throw Exception ("stream-error", "cannot access uri", uri.getname ());
54
// get a file input stream with a uri path
55
static InputStream* get_file_istream (const String& nuri) {
57
// open a file for input
58
return new InputFile (nuri);
60
throw Exception ("stream-error", "cannot access uri", nuri);
64
// get a file output stream
65
static OutputStream* get_file_ostream (const Uri& uri) {
67
// get the system path from the uri path
68
String path = uri.getsysp ();
69
// open a file for output
70
return new OutputFile (path);
72
throw Exception ("stream-error", "cannot access uri", uri.getname ());
76
// get a file output stream with a uri path
77
static OutputStream* get_file_ostream (const String& nuri) {
79
// open a file for output
80
return new OutputFile (nuri);
82
throw Exception ("stream-error", "cannot access uri", nuri);
86
// get a http input stream
87
static InputStream* get_http_istream (const Uri& uri) {
56
88
// create a http request by uri
57
89
HttpRequest hrq (uri);
58
90
// get the uri host
75
107
Uri huri = hrs->gethloc ();
77
return get_http_stream (huri);
109
return get_http_istream (huri);
79
111
// invalid response
80
112
throw Exception ("http-error", "cannot access uri", uri.getname ());
87
// get the content stream by uri name
89
static InputStream* get_content_stream (const String& unam) {
90
// create a uri by name
93
String sch = uri.getscheme ();
94
// select from the uri scheme
95
if (sch == URI_FILE_SCHM) return get_file_stream (uri);
96
if (sch == URI_HTTP_SCHM) return get_http_stream (uri);
119
// get the content input stream by uri name
121
static InputStream* get_content_istream (const String& unam) {
122
// create a uri by name
124
// get the uri scheme
125
String sch = uri.getscheme ();
126
// select from the uri scheme
127
if (sch == URI_FILE_SCHM) return get_file_istream (uri);
128
if (sch == URI_HTTP_SCHM) return get_http_istream (uri);
129
// invalid uri scheme to process
130
throw Exception ("scheme-error", "invalid xml content uri scheme", sch);
133
// get the content output stream by uri name
135
static OutputStream* get_content_ostream (const String& unam) {
136
// create a uri by name
138
// get the uri scheme
139
String sch = uri.getscheme ();
140
// select from the uri scheme
141
if (sch == URI_FILE_SCHM) return get_file_ostream (uri);
97
142
// invalid uri scheme to process
98
143
throw Exception ("scheme-error", "invalid xml content uri scheme", sch);
102
147
// - class section -
103
148
// -------------------------------------------------------------------------
150
// create a xml content for writing
152
XmlContent::XmlContent (void) {
105
156
// create a xml content by name
107
158
XmlContent::XmlContent (const String& name) {
110
// normalize the document name
111
String unam = Uri::sysname (name);
112
// get the input stream
113
InputStream* is = get_content_stream (unam);
116
// set the document root
162
// normalize the document name
163
d_nuri = Uri::sysname (name);
164
// get the input stream
165
InputStream* is = get_content_istream (d_nuri);
168
// set the document root
169
setroot (d_nuri, is);
178
// create a xml content by name and input stream
180
XmlContent::XmlContent (const String& name, InputStream* is) {
184
// normalize the document name
185
d_nuri = Uri::sysname (name);
186
// set the document root
187
setroot (d_nuri, is);
190
// create a xml content by uri name and path
192
XmlContent::XmlContent (const String& name, const UriPath& puri) {
195
p_puri = new UriPath (puri);
196
// normalize the document name
197
d_nuri = puri.mapruri (name);
198
// get the input stream
199
InputStream* is = get_file_istream (d_nuri);
202
// set the document root
203
setroot (d_nuri, is);
118
204
// clean the stream
119
205
Object::dref (is);
128
214
XmlContent::XmlContent (const String& name, const String& docn) {
131
218
// normalize the document name
132
String unam = Uri::sysname (docn);
219
d_nuri= Uri::sysname (docn);
133
220
// get the input stream
134
InputStream* is = get_content_stream (unam);
221
InputStream* is = get_content_istream (d_nuri);
135
222
Object::iref (is);
137
224
// set the document root
225
setroot (d_nuri, is);
139
226
// clean the stream
140
227
Object::dref (is);
150
237
const String& emod) {
153
241
// normalize the document name
154
String unam = Uri::sysname (docn);
242
d_nuri = Uri::sysname (docn);
155
243
// get the input stream
156
InputStream* is = get_content_stream (unam);
244
InputStream* is = get_content_istream (d_nuri);
157
245
Object::iref (is);
159
247
// set the encoding mode
160
248
is->setemod (emod);
161
249
// set the document root text
250
setrtxt (d_nuri, is);
163
251
// clean the stream
164
252
Object::dref (is);
308
// write a xml content by name
310
void XmlContent::write (const String& name) {
312
OutputStream* os = nilp;
317
// normalize the document name
318
d_nuri = Uri::sysname (name);
319
// get the output stream
320
Object::iref (os = get_content_ostream (d_nuri));
322
throw Exception ("write-error", "nil stream for xml content write");
324
// get the document root
325
XmlRoot* root = newroot (true);
326
// write the document root
328
// close and clean the stream
338
// write a xml content by name and output stream
340
void XmlContent::write (const String& name, OutputStream* os) {
344
throw Exception ("write-error", "nil stream for xml content write");
349
// normalize the document name
350
d_nuri = Uri::sysname (name);
351
// get the document root
352
XmlRoot* root = newroot (true);
353
// write the document root
362
// write a xml content by uri name and path
364
void XmlContent::write (const String& name, const UriPath& puri) {
366
OutputStream* os = nilp;
370
p_puri = new UriPath (puri);
371
// normalize the document name
372
d_nuri = puri.mapruri (name);
373
// get the output stream
374
Object::iref (os = get_file_ostream (d_nuri));
376
throw Exception ("write-error", "nil stream for xml content write");
378
// get the document root
379
XmlRoot* root = newroot (true);
380
// write the document root
382
// close and clean the stream
213
392
// -------------------------------------------------------------------------
214
393
// - object section -
215
394
// -------------------------------------------------------------------------
217
396
// the quark zone
218
static const long QUARK_ZONE_LENGTH = 2;
397
static const long QUARK_ZONE_LENGTH = 3;
219
398
static QuarkZone zone (QUARK_ZONE_LENGTH);
221
400
// the object supported quarks
401
static const long QUARK_WRITE = zone.intern ("write");
222
402
static const long QUARK_GETNURI = zone.intern ("get-document-uri");
223
403
static const long QUARK_GETDOCN = zone.intern ("get-document-name");
227
407
Object* XmlContent::mknew (Vector* argv) {
228
408
long argc = (argv == nilp) ? 0 : argv->length ();
410
// check for 0 argument
411
if (argc == 0) return new XmlContent;
230
412
// check for 1 argument
232
414
String name = argv->getstring (0);
233
415
return new XmlContent (name);
417
// check for 2 arguments
419
String name = argv->getstring (0);
420
Object* obj = argv->get (1);
421
// check for a string
422
String* sobj = dynamic_cast <String*> (obj);
423
if (sobj != nilp) return new XmlContent (name, *sobj);
424
// check for an uri path
425
UriPath* pobj = dynamic_cast <UriPath*> (obj);
426
if (pobj != nilp) return new XmlContent (name, *pobj);
428
throw Exception ("type-error", "invalid oject with xml content",
431
// check for 3 arguments
433
String name = argv->getstring (0);
434
String docn = argv->getstring (1);
435
String emod = argv->getstring (2);
436
return new XmlContent (name, docn, emod);
235
438
// wrong arguments
236
439
throw Exception ("argument-error",
237
440
"too many arguments with xml processor constructor");
262
465
if (quark == QUARK_GETNURI) return new String (getnuri ());
263
466
if (quark == QUARK_GETDOCN) return new String (getdocn ());
468
// check for 1 argument
470
if (quark == QUARK_WRITE) {
471
String name = argv->getstring (0);
476
// check for 2 arguments
478
if (quark == QUARK_WRITE) {
479
String name = argv->getstring (0);
480
Object* obj = argv->get (1);
481
// check for an output stream
482
OutputStream* os = dynamic_cast <OutputStream*> (obj);
487
// check for a uri path
488
UriPath* urip = dynamic_cast <UriPath*> (obj);
493
throw Exception ("type-error", "invalid object with xml content write",
266
497
// call the xml document method
267
498
return XmlDocument::apply (robj, nset, quark, argv);