Annotation of parser3/src/main/pa_xml_io.C, revision 1.8
1.1 paf 1: /** @file
2: Parser: plugins to xml library, controlling i/o; implementation
3:
4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
8: #include "pa_xml_io.h"
9:
10: #ifdef XML
11:
1.8 ! paf 12: static const char * const IDENT="$Date: 2003/11/28 09:36:26 $";
1.1 paf 13:
14: #include "libxslt/extensions.h"
15:
16: #include "pa_globals.h"
17: #include "pa_request.h"
18:
1.8 ! paf 19: #ifndef DOXYGEN
! 20: struct MemoryStream {
! 21: const char* m_buf;
! 22: size_t m_size;
! 23: size_t m_position;
! 24:
! 25: int read(char* a_buffer, size_t a_size) {
! 26: size_t left=m_size-m_position;
! 27: if(!left)
! 28: return 0;
! 29:
! 30: size_t to_read=min(a_size, left);
! 31: memcpy(a_buffer, m_buf, to_read);
! 32: m_position+=to_read;
! 33: return to_read;
! 34: }
! 35:
! 36: };
! 37: #endif
! 38:
1.1 paf 39: /**
40: * xmlFileMatchWithLocalhostEqDocumentRoot:
41: * filename: the URI for matching
42: *
43: * check if the URI matches an HTTP one
44: *
45: * Returns 1 if matches, 0 otherwise
46: */
47: static int
48: xmlFileMatchLocalhost(const char* filename) {
49: if (!strncmp(filename, "http://localhost", 16))
50: return(1);
51: return(0);
52: }
53:
54:
55: /**
56: * xmlFileOpenHttpLocalhost :
57: * filename: the URI for matching
58: *
59: * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
60: *
61: * input from FILE *, supports compressed input
62: * if filename is " " then the standard input is used
63: *
64: * Returns an I/O context or NULL in case of error
65: */
66: static void *
67: xmlFileOpenLocalhost (const char* filename) {
1.8 ! paf 68: Request& r=pa_thread_request();
1.1 paf 69: FILE *fd;
1.8 ! paf 70: const char* document_root=r.request_info.document_root;
1.1 paf 71: if(!document_root)
72: document_root=".";
73:
74: char path[MAX_STRING];
75: path[0]=0;
76: strcat(path, document_root);
77: strcat(path, &filename[16]);
78:
1.8 ! paf 79: MemoryStream *stream=new(UseGC) MemoryStream;
! 80: stream->m_buf=file_read_text(r.charsets, *new String(filename), true);
! 81: stream->m_size=strlen(stream->m_buf);
1.1 paf 82:
1.8 ! paf 83: return (void *)stream;
1.1 paf 84: }
85:
1.2 paf 86: static int
87: xmlFileMatchMethod(const char* filename) {
1.3 paf 88: if (!strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/))
1.2 paf 89: return(1);
90: return(0);
91: }
92:
1.5 paf 93: /// parser://method/param/here -> ^MAIN:method[/params/here]
1.2 paf 94: static void *
95: xmlFileOpenMethod (const char* afilename) {
96: Request& r=pa_thread_request();
97:
1.3 paf 98: char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);
1.2 paf 99: const char* method_cstr=lsplit(&s, '/');
100: const String* method=new String(method_cstr);
1.4 paf 101: String::Body param_body("/");
102: if(s)
103: param_body.append_know_length(s, strlen(s));
104:
105: VString* vparam=new VString(*new String(param_body, String::L_TAINTED));
1.2 paf 106: {
107: Temp_lang temp_lang(r, String::L_XML); // default language: XML
1.3 paf 108: Request::Execute_nonvirtual_method_result body=
1.4 paf 109: r.execute_nonvirtual_method(r.main_class, *method, vparam, true);
1.3 paf 110: if(body.string) {
1.2 paf 111: MemoryStream *stream=new(UseGC) MemoryStream;
1.3 paf 112: stream->m_buf=body.string->cstr(String::L_UNSPECIFIED);
1.2 paf 113: stream->m_size=strlen(stream->m_buf);
114: return (void*)stream;
115: }
116: }
117:
118: throw Exception(0,
1.3 paf 119: new String(afilename),
120: "'%s' method not found in %s class",
121: method_cstr, MAIN_CLASS_NAME);
1.2 paf 122: }
123:
124: static int
125: pa_xmlFileReadMethod (void * context, //< MemoryStream actually
126: char * buffer, int len)
127: {
128: MemoryStream& stream=*static_cast<MemoryStream*>(context);
129:
130: return stream.read(buffer, len);
131: }
132:
133: static int
134: pa_xmlFileCloseMethod (void * /*context*/) {
135: return 0;
136: }
137:
138:
1.1 paf 139:
140: void pa_xml_io_init() {
141: // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
142: xmlRegisterInputCallbacks(
143: xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.8 ! paf 144: pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.2 paf 145:
1.5 paf 146: // parser://method/param/here -> ^MAIN:method[/params/here]
1.2 paf 147: xmlRegisterInputCallbacks(
148: xmlFileMatchMethod, xmlFileOpenMethod,
149: pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.1 paf 150: }
151:
152: #endif
E-mail: