~zorba-coders/zorba/bug-1188035-csv

« back to all changes in this revision

Viewing changes to src/com/zorba-xquery/www/modules/converters/json.xq.src/json.h

  • Committer: Tarmac
  • Author(s): Paul J. Lucas
  • Date: 2012-01-24 01:56:27 UTC
  • mfrom: (79.1.1 data-converters-module)
  • Revision ID: tarmac-20120124015627-v8to61s7w8of53m6
Removed old JSON module. Approved: Matthias Brantner, Paul J. Lucas

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2006-2008 The FLWOR Foundation.
3
 
 * 
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
7
 
 * 
8
 
 * http://www.apache.org/licenses/LICENSE-2.0
9
 
 * 
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.
15
 
 */
16
 
#ifndef ZORBA_JSONMODULE_JSON_H
17
 
#define ZORBA_JSONMODULE_JSON_H
18
 
 
19
 
#include <map>
20
 
 
21
 
#include <zorba/zorba.h>
22
 
#include <zorba/iterator.h>
23
 
#include <zorba/function.h>
24
 
#include <zorba/external_module.h>
25
 
 
26
 
#include "stringiterator_streambuf.h"
27
 
 
28
 
namespace zorba
29
 
{
30
 
  namespace jsonmodule
31
 
  {
32
 
//*****************************************************************************
33
 
//*****************************************************************************
34
 
    class JsonModule : public ExternalModule
35
 
    {
36
 
    private:
37
 
      static ItemFactory* theFactory;
38
 
 
39
 
    protected:
40
 
      class ltstr
41
 
      {
42
 
      public:
43
 
        bool operator()(const String& s1, const String& s2) const
44
 
        {
45
 
          return s1.compare(s2) < 0;
46
 
        }
47
 
      };
48
 
 
49
 
      typedef std::map<String, ExternalFunction*, ltstr> FuncMap_t;
50
 
 
51
 
      FuncMap_t theFunctions;
52
 
 
53
 
    public:
54
 
      virtual ~JsonModule();
55
 
 
56
 
      virtual String
57
 
      getURI() const { return "http://www.zorba-xquery.com/modules/converters/json"; }
58
 
 
59
 
      virtual ExternalFunction*
60
 
      getExternalFunction(const String& aLocalname);
61
 
 
62
 
      virtual void
63
 
      destroy();
64
 
 
65
 
      static ItemFactory*
66
 
      getItemFactory()
67
 
      {
68
 
        if(!theFactory)
69
 
        {
70
 
          theFactory = Zorba::getInstance(0)->getItemFactory();
71
 
        }
72
 
        return theFactory;
73
 
      }
74
 
    };
75
 
 
76
 
//*****************************************************************************
77
 
//*****************************************************************************
78
 
    class JsonFunction : public ContextualExternalFunction
79
 
    {
80
 
    protected:
81
 
      const JsonModule* theModule;
82
 
      zorba::String getOptionValue(zorba::Item& aOptionsItem, const char* aOptionName) const;
83
 
    public:
84
 
      JsonFunction(const JsonModule* aModule)
85
 
      : theModule(aModule) {};
86
 
 
87
 
      ~JsonFunction() {};
88
 
 
89
 
      virtual String
90
 
      getURI() const { return theModule->getURI(); }
91
 
 
92
 
    };
93
 
 
94
 
//*****************************************************************************
95
 
//*****************************************************************************
96
 
    class ParseFunction : public JsonFunction
97
 
    {
98
 
    public:
99
 
      ParseFunction(const JsonModule* aModule);
100
 
 
101
 
      virtual String
102
 
      getLocalName() const { return "parse-internal"; }
103
 
 
104
 
      virtual ItemSequence_t
105
 
      evaluate(const ExternalFunction::Arguments_t& args,
106
 
               const StaticContext* aSctxCtx,
107
 
               const DynamicContext* aDynCtx) const;
108
 
    };
109
 
 
110
 
    class SerializeFunction : public JsonFunction
111
 
    {
112
 
      class StringStreamSequence : public ItemSequence, public Iterator, public StringIteratorStreamBuf
113
 
      {
114
 
      private:
115
 
        Iterator_t     input_iter;
116
 
        std::vector<std::vector<String> > headers;
117
 
        int line_index;
118
 
        bool has_next;
119
 
        bool is_open;
120
 
        int  open_count;
121
 
 
122
 
        std::vector<String> line;
123
 
        Item node_item;
124
 
      public:
125
 
        std::istream* is;
126
 
        String        theMapping;
127
 
        ItemFactory*  theFactory;
128
 
        Item          streamable_item;
129
 
      public:
130
 
        StringStreamSequence(ItemSequence* input);
131
 
        virtual ~StringStreamSequence() {}
132
 
 
133
 
        //for Iterator
134
 
        virtual void open();
135
 
        virtual void close();
136
 
        virtual bool isOpen() const;
137
 
        virtual bool next( Item &result );
138
 
 
139
 
        //for ItemSequence
140
 
        Iterator_t getIterator() {return this;}
141
 
 
142
 
        //for StringIteratorStreamBuf
143
 
        virtual bool next(std::string &next_string);
144
 
        virtual bool reset();
145
 
 
146
 
        static void
147
 
        releaseStream(std::istream* stream) { delete stream; }
148
 
 
149
 
      };
150
 
    public:
151
 
      SerializeFunction(const JsonModule* aModule);
152
 
 
153
 
      virtual String
154
 
      getLocalName() const { return "serialize-internal"; }
155
 
 
156
 
      virtual ItemSequence_t
157
 
      evaluate(const ExternalFunction::Arguments_t& args,
158
 
               const StaticContext* aSctxCtx,
159
 
               const DynamicContext* aDynCtx) const;
160
 
    };
161
 
 
162
 
 
163
 
 
164
 
  } /* namespace jsonmodule */
165
 
} /* namespace zorba */
166
 
 
167
 
#endif /* ZORBA_JSONMODULE_JSON_H */