1 : /******************************************************************************
2 : * $Id: ogr_ods.h 24173 2012-03-29 21:09:52Z rouault $
3 : *
4 : * Project: ODS Translator
5 : * Purpose: Definition of classes for OGR OpenOfficeSpreadsheet .ods driver.
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2012, Even Rouault <even dot rouault at mines dash paris dot org>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef _OGR_ODS_H_INCLUDED
31 : #define _OGR_ODS_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "ogr_mem.h"
35 :
36 : #include "ogr_expat.h"
37 :
38 : #include <vector>
39 : #include <string>
40 : #include <set>
41 :
42 : /************************************************************************/
43 : /* OGRODSLayer */
44 : /************************************************************************/
45 :
46 : class OGRODSDataSource;
47 :
48 : class OGRODSLayer : public OGRMemLayer
49 115 : {
50 : OGRODSDataSource* poDS;
51 : int bUpdated;
52 : int bHasHeaderLine;
53 :
54 : public:
55 : OGRODSLayer( OGRODSDataSource* poDSIn,
56 : const char * pszName,
57 : int bUpdateIn = FALSE);
58 :
59 : void SetUpdated(int bUpdatedIn = TRUE);
60 :
61 : int GetHasHeaderLine() { return bHasHeaderLine; }
62 75 : void SetHasHeaderLine(int bIn) { bHasHeaderLine = bIn; }
63 :
64 570 : const char *GetName() { return OGRMemLayer::GetLayerDefn()->GetName(); };
65 11 : OGRwkbGeometryType GetGeomType() { return wkbNone; }
66 28 : virtual OGRSpatialReference *GetSpatialRef() { return NULL; }
67 :
68 : /* For external usage. Mess with FID */
69 : virtual OGRFeature * GetNextFeature();
70 : virtual OGRFeature *GetFeature( long nFeatureId );
71 : virtual OGRErr SetFeature( OGRFeature *poFeature );
72 : virtual OGRErr DeleteFeature( long nFID );
73 :
74 : /* For internal usage, for cell resolver */
75 264 : OGRFeature * GetNextFeatureWithoutFIDHack() { return OGRMemLayer::GetNextFeature(); }
76 106 : OGRErr SetFeatureWithoutFIDHack( OGRFeature *poFeature ) { SetUpdated(); return OGRMemLayer::SetFeature(poFeature); }
77 :
78 570 : OGRErr CreateFeature( OGRFeature *poFeature )
79 570 : { SetUpdated(); return OGRMemLayer::CreateFeature(poFeature); }
80 :
81 422 : virtual OGRErr CreateField( OGRFieldDefn *poField,
82 : int bApproxOK = TRUE )
83 422 : { SetUpdated(); return OGRMemLayer::CreateField(poField, bApproxOK); }
84 :
85 0 : virtual OGRErr DeleteField( int iField )
86 0 : { SetUpdated(); return OGRMemLayer::DeleteField(iField); }
87 :
88 0 : virtual OGRErr ReorderFields( int* panMap )
89 0 : { SetUpdated(); return OGRMemLayer::ReorderFields(panMap); }
90 :
91 26 : virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags )
92 26 : { SetUpdated(); return OGRMemLayer::AlterFieldDefn(iField, poNewFieldDefn, nFlags); }
93 :
94 : virtual OGRErr SyncToDisk();
95 : };
96 :
97 : /************************************************************************/
98 : /* OGRODSDataSource */
99 : /************************************************************************/
100 : #define STACK_SIZE 5
101 :
102 : typedef enum
103 : {
104 : STATE_DEFAULT,
105 : STATE_TABLE,
106 : STATE_ROW,
107 : STATE_CELL,
108 : STATE_TEXTP,
109 : } HandlerStateEnum;
110 :
111 : typedef struct
112 : {
113 : HandlerStateEnum eVal;
114 : int nBeginDepth;
115 : } HandlerState;
116 :
117 : class OGRODSDataSource : public OGRDataSource
118 : {
119 : char* pszName;
120 : int bUpdatable;
121 : int bUpdated;
122 : int bAnalysedFile;
123 :
124 : int nLayers;
125 : OGRLayer **papoLayers;
126 :
127 : VSILFILE* fpSettings;
128 : std::string osCurrentConfigTableName;
129 : std::string osConfigName;
130 : int nFlags;
131 : std::set<std::string> osSetLayerHasSplitter;
132 : void AnalyseSettings();
133 :
134 : VSILFILE* fpContent;
135 : void AnalyseFile();
136 :
137 : int bFirstLineIsHeaders;
138 : int bAutodetectTypes;
139 :
140 : XML_Parser oParser;
141 : int bStopParsing;
142 : int nWithoutEventCounter;
143 : int nDataHandlerCounter;
144 : int nCurLine;
145 : int nEmptyRowsAccumulated;
146 : int nRowsRepeated;
147 : int nCurCol;
148 : int nCellsRepeated;
149 : int bEndTableParsing;
150 :
151 : OGRODSLayer *poCurLayer;
152 :
153 : int nStackDepth;
154 : int nDepth;
155 : HandlerState stateStack[STACK_SIZE];
156 :
157 : CPLString osValueType;
158 : CPLString osValue;
159 : std::string osFormula;
160 :
161 : std::vector<std::string> apoFirstLineValues;
162 : std::vector<std::string> apoFirstLineTypes;
163 : std::vector<std::string> apoCurLineValues;
164 : std::vector<std::string> apoCurLineTypes;
165 :
166 : void PushState(HandlerStateEnum eVal);
167 : void startElementDefault(const char *pszName, const char **ppszAttr);
168 : void startElementTable(const char *pszName, const char **ppszAttr);
169 : void endElementTable(const char *pszName);
170 : void startElementRow(const char *pszName, const char **ppszAttr);
171 : void endElementRow(const char *pszName);
172 : void startElementCell(const char *pszName, const char **ppszAttr);
173 : void endElementCell(const char *pszName);
174 : void dataHandlerTextP(const char *data, int nLen);
175 :
176 : void DetectHeaderLine();
177 :
178 : OGRFieldType GetOGRFieldType(const char* pszValue,
179 : const char* pszValueType);
180 :
181 : void DeleteLayer( const char *pszLayerName );
182 :
183 : public:
184 : OGRODSDataSource();
185 : ~OGRODSDataSource();
186 :
187 : int Open( const char * pszFilename,
188 : VSILFILE* fpContentIn,
189 : VSILFILE* fpSettingsIn,
190 : int bUpdatableIn );
191 : int Create( const char * pszName, char **papszOptions );
192 :
193 9 : virtual const char* GetName() { return pszName; }
194 :
195 : virtual int GetLayerCount();
196 : virtual OGRLayer* GetLayer( int );
197 :
198 : virtual int TestCapability( const char * );
199 :
200 : virtual OGRLayer* CreateLayer( const char * pszLayerName,
201 : OGRSpatialReference *poSRS,
202 : OGRwkbGeometryType eType,
203 : char ** papszOptions );
204 : virtual OGRErr DeleteLayer(int iLayer);
205 :
206 : virtual OGRErr SyncToDisk();
207 :
208 : void startElementCbk(const char *pszName, const char **ppszAttr);
209 : void endElementCbk(const char *pszName);
210 : void dataHandlerCbk(const char *data, int nLen);
211 :
212 : void startElementStylesCbk(const char *pszName, const char **ppszAttr);
213 : void endElementStylesCbk(const char *pszName);
214 : void dataHandlerStylesCbk(const char *data, int nLen);
215 :
216 1468 : int GetUpdatable() { return bUpdatable; }
217 9 : void SetUpdated() { bUpdated = TRUE; }
218 : };
219 :
220 : /************************************************************************/
221 : /* OGRODSDriver */
222 : /************************************************************************/
223 :
224 : class OGRODSDriver : public OGRSFDriver
225 226 : {
226 : public:
227 : ~OGRODSDriver();
228 :
229 : virtual const char* GetName();
230 : virtual OGRDataSource* Open( const char *, int );
231 : virtual int TestCapability( const char * );
232 :
233 : virtual OGRDataSource *CreateDataSource( const char *pszName,
234 : char ** = NULL );
235 : virtual OGRErr DeleteDataSource( const char *pszName );
236 : };
237 :
238 :
239 : #endif /* ndef _OGR_ODS_H_INCLUDED */
|