~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/srv/xpe/shl/XmlContent.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
// ---------------------------------------------------------------------------
16
16
 
17
17
#include "Runnable.hpp"
19
19
#include "TcpClient.hpp"
20
20
#include "QuarkZone.hpp"
21
21
#include "Exception.hpp"
 
22
#include "InputFile.hpp"
 
23
#include "OutputFile.hpp"
22
24
#include "XmlContent.hpp"
23
25
#include "HttpStream.hpp"
24
26
#include "HttpRequest.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;
41
41
 
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) {
44
44
    try {
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);
49
49
    } catch (...) {
50
 
      throw Exception ("file-error", "cannot access uri", uri.getname ());
51
 
    }
52
 
  }
53
 
 
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 ());
 
51
    }
 
52
  }
 
53
 
 
54
  // get a file input stream with a uri path
 
55
  static InputStream* get_file_istream (const String& nuri) {
 
56
    try {
 
57
      // open a file for input
 
58
      return new InputFile (nuri);
 
59
    } catch (...) {
 
60
      throw Exception ("stream-error", "cannot access uri", nuri);
 
61
    }
 
62
  }
 
63
 
 
64
  // get a file output stream
 
65
  static OutputStream* get_file_ostream (const Uri& uri) {
 
66
    try {
 
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);
 
71
    } catch (...) {
 
72
      throw Exception ("stream-error", "cannot access uri", uri.getname ());
 
73
    }
 
74
  }
 
75
 
 
76
  // get a file output stream with a uri path
 
77
  static OutputStream* get_file_ostream (const String& nuri) {
 
78
    try {
 
79
      // open a file for output
 
80
      return new OutputFile (nuri);
 
81
    } catch (...) {
 
82
      throw Exception ("stream-error", "cannot access uri", nuri);
 
83
    }
 
84
  }
 
85
 
 
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
74
106
      } else {
75
107
        Uri huri = hrs->gethloc ();
76
108
        delete hrs;
77
 
        return get_http_stream (huri);
 
109
        return get_http_istream (huri);
78
110
      }
79
111
      // invalid response
80
112
      throw Exception ("http-error", "cannot access uri", uri.getname ());
84
116
    }
85
117
  }
86
118
 
87
 
  // get the content stream by uri name
88
 
 
89
 
  static InputStream* get_content_stream (const String& unam) {
90
 
    // create a uri by name
91
 
    Uri uri = unam;
92
 
    // get the uri scheme
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
 
120
 
 
121
  static InputStream* get_content_istream (const String& unam) {
 
122
    // create a uri by name
 
123
    Uri uri = unam;
 
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);
 
131
  }
 
132
 
 
133
  // get the content output stream by uri name
 
134
 
 
135
  static OutputStream* get_content_ostream (const String& unam) {
 
136
    // create a uri by name
 
137
    Uri uri = unam;
 
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);
99
144
  }
102
147
  // - class section                                                         -
103
148
  // -------------------------------------------------------------------------
104
149
 
 
150
  // create a xml content for writing
 
151
 
 
152
  XmlContent::XmlContent (void) {
 
153
    p_puri = nilp;
 
154
  }
 
155
 
105
156
  // create a xml content by name
106
157
 
107
158
  XmlContent::XmlContent (const String& name) {
108
159
    // save the name
109
160
    d_name = name;
110
 
    // normalize the document name
111
 
    String unam = Uri::sysname (name);
112
 
    // get the input stream
113
 
    InputStream* is = get_content_stream (unam);
114
 
    Object::iref (is);
115
 
    try {
116
 
      // set the document root
117
 
      setroot (unam, is);
 
161
    p_puri = nilp;
 
162
    // normalize the document name
 
163
    d_nuri = Uri::sysname (name);
 
164
    // get the input stream
 
165
    InputStream* is = get_content_istream (d_nuri);
 
166
    Object::iref (is);
 
167
    try {
 
168
      // set the document root
 
169
      setroot (d_nuri, is);
 
170
      // clean the stream
 
171
      Object::dref (is);
 
172
    } catch (...) {
 
173
      Object::dref (is);
 
174
      throw;
 
175
    }
 
176
  }
 
177
 
 
178
  // create a xml content by name and input stream
 
179
 
 
180
  XmlContent::XmlContent (const String& name, InputStream* is) {
 
181
    // save the name
 
182
    d_name = name;
 
183
    p_puri = nilp;
 
184
    // normalize the document name
 
185
    d_nuri = Uri::sysname (name);
 
186
    // set the document root
 
187
    setroot (d_nuri, is);
 
188
  }
 
189
 
 
190
  // create a xml content by uri name and path
 
191
 
 
192
  XmlContent::XmlContent (const String& name, const UriPath& puri) {
 
193
    // save the name
 
194
    d_name = name;
 
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);
 
200
    Object::iref (is);
 
201
    try {
 
202
      // set the document root
 
203
      setroot (d_nuri, is);
118
204
      // clean the stream
119
205
      Object::dref (is);
120
206
    } catch (...) {
128
214
  XmlContent::XmlContent (const String& name, const String& docn) {
129
215
    // save the name
130
216
    d_name = name;
 
217
    p_puri = nilp;
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);
136
223
    try {
137
224
      // set the document root
138
 
      setroot (unam, is);
 
225
      setroot (d_nuri, is);
139
226
      // clean the stream
140
227
      Object::dref (is);
141
228
    } catch (...) {
150
237
                          const String& emod) {
151
238
    // save the name
152
239
    d_name = name;
 
240
    p_puri = nilp;
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);
158
246
    try {
159
247
      // set the encoding mode
160
248
      is->setemod (emod);
161
249
      // set the document root text
162
 
      setrtxt (unam, is);
 
250
      setrtxt (d_nuri, is);
163
251
      // clean the stream
164
252
      Object::dref (is);
165
253
    } catch (...) {
175
263
    try {
176
264
      XmlDocument::operator = (that);
177
265
      d_nuri = that.d_nuri;
 
266
      Object::iref (p_puri = that.p_puri);
178
267
      that.unlock ();
179
268
    } catch (...) {
180
269
      that.unlock ();
182
271
    }
183
272
  }
184
273
   
 
274
  // destroy this object
 
275
 
 
276
  XmlContent::~XmlContent (void) {
 
277
    Object::dref (p_puri);
 
278
  }
 
279
 
185
280
  // get the document normalized uri
186
281
 
187
282
  String XmlContent::getnuri (void) const {
210
305
    }
211
306
  }
212
307
 
 
308
  // write a xml content by name
 
309
 
 
310
  void XmlContent::write (const String& name) {
 
311
    wrlock ();
 
312
    OutputStream* os = nilp;
 
313
    try {
 
314
      // save the name
 
315
      d_name = name;
 
316
      p_puri = 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));
 
321
      if (os == nilp) {
 
322
        throw Exception ("write-error", "nil stream for xml content write");
 
323
      }
 
324
      // get the document root
 
325
      XmlRoot* root = newroot (true);
 
326
      // write the document root
 
327
      root->write (*os);
 
328
      // close and clean the stream
 
329
      Object::dref (os);
 
330
      unlock ();
 
331
    } catch (...) {
 
332
      Object::dref (os);
 
333
      unlock ();
 
334
      throw;
 
335
    }
 
336
  }
 
337
 
 
338
  // write a xml content by name and output stream
 
339
 
 
340
  void XmlContent::write (const String& name, OutputStream* os) {
 
341
    wrlock ();
 
342
    try {
 
343
      if (os == nilp) {
 
344
        throw Exception ("write-error", "nil stream for xml content write");
 
345
      }
 
346
      // save the name
 
347
      d_name = name;
 
348
      p_puri = nilp;
 
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
 
354
      root->write (*os);
 
355
      unlock ();
 
356
    } catch (...) {
 
357
      unlock ();
 
358
      throw;
 
359
    }
 
360
  }
 
361
 
 
362
  // write a xml content by uri name and path
 
363
 
 
364
  void XmlContent::write (const String& name, const UriPath& puri) {
 
365
    wrlock ();
 
366
    OutputStream* os = nilp;
 
367
    try {
 
368
      // save the name
 
369
      d_name = name;
 
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));
 
375
      if (os == nilp) {
 
376
        throw Exception ("write-error", "nil stream for xml content write");
 
377
      }
 
378
      // get the document root
 
379
      XmlRoot* root = newroot (true);
 
380
      // write the document root
 
381
      root->write (*os);
 
382
      // close and clean the stream
 
383
      Object::dref (os);
 
384
      unlock ();
 
385
    } catch (...) {
 
386
      Object::dref (os);
 
387
      unlock ();
 
388
      throw;
 
389
    }
 
390
  }
 
391
 
213
392
  // -------------------------------------------------------------------------
214
393
  // - object section                                                        -
215
394
  // -------------------------------------------------------------------------
216
395
 
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);
220
399
 
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");
224
404
 
227
407
  Object* XmlContent::mknew (Vector* argv) {
228
408
    long argc = (argv == nilp) ? 0 : argv->length ();
229
409
 
 
410
    // check for 0 argument
 
411
    if (argc == 0) return new XmlContent;
230
412
    // check for 1 argument
231
413
    if (argc == 1) {
232
414
      String name = argv->getstring (0);
233
415
      return new XmlContent (name);
234
416
    }
 
417
    // check for 2 arguments
 
418
    if (argc == 2) {
 
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);
 
427
      // invalid object
 
428
      throw Exception ("type-error", "invalid oject with xml content",
 
429
                       Object::repr (obj));
 
430
    }
 
431
    // check for 3 arguments
 
432
    if (argc == 3) {
 
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);
 
437
    }
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 ());
264
467
    }
265
 
 
 
468
    // check for 1 argument
 
469
    if (argc == 1) {
 
470
      if (quark == QUARK_WRITE) {
 
471
        String name = argv->getstring (0);
 
472
        write (name);
 
473
        return nilp;
 
474
      }
 
475
    }
 
476
    // check for 2 arguments
 
477
    if (argc == 2) {
 
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);
 
483
        if (os != nilp) {
 
484
          write (name, os);
 
485
          return nilp;
 
486
        }
 
487
        // check for a uri path
 
488
        UriPath* urip = dynamic_cast <UriPath*> (obj);
 
489
        if (urip != nilp) {
 
490
          write (name, *urip);
 
491
          return nilp;
 
492
        }
 
493
        throw Exception ("type-error", "invalid object with xml content write",
 
494
                         Object::repr (obj));
 
495
      }
 
496
    }
266
497
    // call the xml document method
267
498
    return XmlDocument::apply (robj, nset, quark, argv);
268
499
  }