2
* Copyright 2006-2008 The FLWOR Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
#ifndef ZORBA_JSONMODULE_JSON_H
17
#define ZORBA_JSONMODULE_JSON_H
21
#include <zorba/zorba.h>
22
#include <zorba/iterator.h>
23
#include <zorba/function.h>
24
#include <zorba/external_module.h>
26
#include "stringiterator_streambuf.h"
32
//*****************************************************************************
33
//*****************************************************************************
34
class JsonModule : public ExternalModule
37
static ItemFactory* theFactory;
43
bool operator()(const String& s1, const String& s2) const
45
return s1.compare(s2) < 0;
49
typedef std::map<String, ExternalFunction*, ltstr> FuncMap_t;
51
FuncMap_t theFunctions;
54
virtual ~JsonModule();
57
getURI() const { return "http://www.zorba-xquery.com/modules/converters/json"; }
59
virtual ExternalFunction*
60
getExternalFunction(const String& aLocalname);
70
theFactory = Zorba::getInstance(0)->getItemFactory();
76
//*****************************************************************************
77
//*****************************************************************************
78
class JsonFunction : public ContextualExternalFunction
81
const JsonModule* theModule;
82
zorba::String getOptionValue(zorba::Item& aOptionsItem, const char* aOptionName) const;
84
JsonFunction(const JsonModule* aModule)
85
: theModule(aModule) {};
90
getURI() const { return theModule->getURI(); }
94
//*****************************************************************************
95
//*****************************************************************************
96
class ParseFunction : public JsonFunction
99
ParseFunction(const JsonModule* aModule);
102
getLocalName() const { return "parse-internal"; }
104
virtual ItemSequence_t
105
evaluate(const ExternalFunction::Arguments_t& args,
106
const StaticContext* aSctxCtx,
107
const DynamicContext* aDynCtx) const;
110
class SerializeFunction : public JsonFunction
112
class StringStreamSequence : public ItemSequence, public Iterator, public StringIteratorStreamBuf
115
Iterator_t input_iter;
116
std::vector<std::vector<String> > headers;
122
std::vector<String> line;
127
ItemFactory* theFactory;
128
Item streamable_item;
130
StringStreamSequence(ItemSequence* input);
131
virtual ~StringStreamSequence() {}
135
virtual void close();
136
virtual bool isOpen() const;
137
virtual bool next( Item &result );
140
Iterator_t getIterator() {return this;}
142
//for StringIteratorStreamBuf
143
virtual bool next(std::string &next_string);
144
virtual bool reset();
147
releaseStream(std::istream* stream) { delete stream; }
151
SerializeFunction(const JsonModule* aModule);
154
getLocalName() const { return "serialize-internal"; }
156
virtual ItemSequence_t
157
evaluate(const ExternalFunction::Arguments_t& args,
158
const StaticContext* aSctxCtx,
159
const DynamicContext* aDynCtx) const;
164
} /* namespace jsonmodule */
165
} /* namespace zorba */
167
#endif /* ZORBA_JSONMODULE_JSON_H */