Salome HOME
4d11153b2b65bf93bbe54f3989108d3512eb6833
[modules/paravis.git] / src / Plugins / JSONReader / JSONParser / vtkJSONParser.h
1 // Copyright (C) 2015-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Roman NIKOLAEV (roman.nikolaev@opencascade.com)
20
21 #ifndef __vtkJSONParser_h_
22 #define __vtkJSONParser_h_
23
24 #include <vtkObject.h>
25 #include <vector>
26 #include <stdio.h>
27 #include <exception>
28 #include <string> 
29
30
31 class vtkTable;
32 class vtkJSONNode;
33 class vtkJSONMetaNode;
34 class vtkJSONInfoNode;
35
36 //---------------------------------------------------
37 class VTK_EXPORT vtkJSONException : public std::exception {
38  public:
39     vtkJSONException(const char *reason);
40     ~vtkJSONException() throw ();
41     const char* what() const throw();
42  protected:
43   std::string Reason;
44 }; 
45
46 class VTK_EXPORT vtkJSONParser : public vtkObject
47 {
48 public:
49   static vtkJSONParser* New();
50
51   // Description:
52   // Specifies the name of the file
53   vtkGetStringMacro(FileName);
54   vtkSetStringMacro(FileName);
55
56   virtual int Parse(vtkTable* theTable);
57
58 protected:
59   
60   //Struct to store cursor information
61   //----------------------------------
62   struct Info {
63   public:
64     char type;
65     long ln;
66     long cn;
67     long pos;
68     
69     Info() {
70       type = 0;
71       ln = -1;  
72       cn = -1;
73       pos = -1;
74     }
75   };
76
77   vtkJSONParser();
78   ~vtkJSONParser();
79
80   // name of the file to read from
81   //----------------------------------
82   char* FileName;
83
84   // current line and column
85   //----------------------------------
86   int LineNumber;
87   int ColumnNumber;
88
89   // markup information
90   //----------------------------------
91   std::vector<Info> CInfoVector;
92
93   // file
94   //----------------------------------
95   FILE* File; 
96
97   // Nodes
98   //----------------------------------
99   std::vector<vtkJSONNode*> Nodes;
100   vtkJSONNode*              CurrentNode;  
101   vtkJSONMetaNode*          MetaNode;
102
103   // Nodes
104   //----------------------------------
105   std::vector<char> ExpectedCharacters;
106
107   // Flags
108   //----------------------------------
109   bool InsideQuotes;
110   bool ParseList;
111   bool ParseObjectList;
112   bool ShortNamesFilled;
113
114   // Last parced string
115   //----------------------------------
116   char* LastString;
117   std::vector<const char*> Strings;
118   std::vector<const char*> CurrentList;
119   std::vector<const char*> ShortNames;
120   
121   // Last parced values
122   //----------------------------------  
123   double LastValue; 
124
125 private:
126   vtkJSONParser(const vtkJSONParser&); // Not implemented.
127   void operator=(const vtkJSONParser&); // Not implemented.
128
129   vtkJSONMetaNode* GetMetaNode();
130   vtkJSONInfoNode* GetInfoNode();
131
132   void processOCB();
133   void processCCB();
134
135   void processOSB();
136   void processCSB();
137
138   void processCOMMA();
139
140   void processCOLON();
141
142   void processENDL();
143
144   void processQTS();
145
146   void processCharacter(const char ch);
147
148   void processMetaNode();
149   void processInfoNode();
150
151   void readDoubleValue();
152
153   char* getString(long b, long e);
154
155   void allowsDigits();
156
157   bool isDigitsAllowed();
158
159   void checkShortName(const char* unit);
160
161   void finalize(vtkTable *t);
162
163   void clean();
164
165   void throwSimpleException(const char* message);
166
167   void throwException(const char* message);
168
169   void throwException(const char* message, int ln, int cn);
170   
171   void throwException(const char* message, int ln);
172 };
173 #endif //__vtkJSONParser_h_