Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: JML .jml Translator 4 : * Purpose: Definition of classes for OGR JML driver. 5 : * Author: Even Rouault, even dot rouault at spatialys dot com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2014, Even Rouault <even dot rouault at spatialys dot com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #ifndef OGR_JML_H_INCLUDED 14 : #define OGR_JML_H_INCLUDED 15 : 16 : #include "ogrsf_frmts.h" 17 : #include "ogr_p.h" 18 : 19 : #ifdef HAVE_EXPAT 20 : #include "ogr_expat.h" 21 : #endif 22 : 23 : #include <vector> 24 : 25 : class OGRJMLDataset; 26 : 27 : #ifdef HAVE_EXPAT 28 : 29 : /************************************************************************/ 30 : /* OGRJMLColumn */ 31 : /************************************************************************/ 32 : 33 : class OGRJMLColumn 34 : { 35 : public: 36 : CPLString osName; 37 : CPLString osType; 38 : CPLString osElementName; 39 : CPLString osAttributeName; 40 : CPLString osAttributeValue; 41 : bool bIsBody; /* if false: attribute */ 42 : 43 42 : OGRJMLColumn() : bIsBody(false) 44 : { 45 42 : } 46 : }; 47 : 48 : /************************************************************************/ 49 : /* OGRJMLLayer */ 50 : /************************************************************************/ 51 : 52 : class OGRJMLLayer final : public OGRLayer 53 : { 54 : GDALDataset *m_poDS = nullptr; 55 : OGRFeatureDefn *poFeatureDefn; 56 : 57 : int nNextFID; 58 : VSILFILE *fp; 59 : bool bHasReadSchema; 60 : 61 : struct XMLParserDeleter 62 : { 63 132 : void operator()(XML_Parser parser) 64 : { 65 132 : if (parser) 66 132 : XML_ParserFree(parser); 67 132 : } 68 : }; 69 : 70 : std::unique_ptr<XML_ParserStruct, XMLParserDeleter> poParser{}; 71 : 72 : int currentDepth; 73 : bool bStopParsing; 74 : int nWithoutEventCounter; 75 : int nDataHandlerCounter; 76 : 77 : bool bAccumulateElementValue; 78 : char *pszElementValue; 79 : int nElementValueLen; 80 : int nElementValueAlloc; 81 : 82 : OGRFeature *poFeature; 83 : OGRFeature **ppoFeatureTab; 84 : int nFeatureTabLength; 85 : int nFeatureTabIndex; 86 : 87 : bool bSchemaFinished; 88 : int nJCSGMLInputTemplateDepth; 89 : int nCollectionElementDepth; 90 : int nFeatureCollectionDepth; 91 : CPLString osCollectionElement; 92 : int nFeatureElementDepth; 93 : CPLString osFeatureElement; 94 : int nGeometryElementDepth; 95 : CPLString osGeometryElement; 96 : int nColumnDepth; 97 : int nNameDepth; 98 : int nTypeDepth; 99 : int nAttributeElementDepth; 100 : int iAttr; 101 : int iRGBField; 102 : CPLString osSRSName; 103 : 104 : OGRJMLColumn oCurColumn; 105 : std::vector<OGRJMLColumn> aoColumns; 106 : 107 : void AddStringToElementValue(const char *data, int nLen); 108 : void StopAccumulate(); 109 : 110 : void LoadSchema(); 111 : 112 : public: 113 : OGRJMLLayer(const char *pszLayerName, OGRJMLDataset *poDS, VSILFILE *fp); 114 : ~OGRJMLLayer() override; 115 : 116 28 : const char *GetName() const override 117 : { 118 28 : return poFeatureDefn->GetName(); 119 : } 120 : 121 : void ResetReading() override; 122 : OGRFeature *GetNextFeature() override; 123 : 124 : const OGRFeatureDefn *GetLayerDefn() const override; 125 : 126 : int TestCapability(const char *) const override; 127 : 128 1 : GDALDataset *GetDataset() override 129 : { 130 1 : return m_poDS; 131 : } 132 : 133 : void startElementCbk(const char *pszName, const char **ppszAttr); 134 : void endElementCbk(const char *pszName); 135 : void dataHandlerCbk(const char *data, int nLen); 136 : 137 : void startElementLoadSchemaCbk(const char *pszName, const char **ppszAttr); 138 : void endElementLoadSchemaCbk(const char *pszName); 139 : }; 140 : 141 : #endif /* HAVE_EXPAT */ 142 : 143 : /************************************************************************/ 144 : /* OGRJMLWriterLayer */ 145 : /************************************************************************/ 146 : 147 : class OGRJMLWriterLayer final : public OGRLayer 148 : { 149 : OGRJMLDataset *poDS; 150 : OGRFeatureDefn *poFeatureDefn; 151 : VSILFILE *fp; 152 : bool bFeaturesWritten; 153 : bool bAddRGBField; 154 : bool bAddOGRStyleField; 155 : bool bClassicGML; 156 : int nNextFID; 157 : CPLString osSRSAttr; 158 : OGREnvelope sLayerExtent; 159 : vsi_l_offset nBBoxOffset; 160 : 161 : void WriteColumnDeclaration(const char *pszName, const char *pszType); 162 : 163 : public: 164 : OGRJMLWriterLayer(const char *pszLayerName, OGRSpatialReference *poSRS, 165 : OGRJMLDataset *poDSIn, VSILFILE *fp, bool bAddRGBField, 166 : bool bAddOGRStyleField, bool bClassicGML); 167 : ~OGRJMLWriterLayer() override; 168 : 169 17 : void ResetReading() override 170 : { 171 17 : } 172 : 173 17 : OGRFeature *GetNextFeature() override 174 : { 175 17 : return nullptr; 176 : } 177 : 178 : OGRErr ICreateFeature(OGRFeature *poFeature) override; 179 : OGRErr CreateField(const OGRFieldDefn *poField, int bApproxOK) override; 180 : 181 309 : const OGRFeatureDefn *GetLayerDefn() const override 182 : { 183 309 : return poFeatureDefn; 184 : } 185 : 186 : int TestCapability(const char *) const override; 187 : 188 : GDALDataset *GetDataset() override; 189 : }; 190 : 191 : /************************************************************************/ 192 : /* OGRJMLDataset */ 193 : /************************************************************************/ 194 : 195 : class OGRJMLDataset final : public GDALDataset 196 : { 197 : OGRLayer *poLayer; 198 : 199 : VSILFILE *fp; /* Virtual file API */ 200 : bool bWriteMode; 201 : 202 : public: 203 : OGRJMLDataset(); 204 : ~OGRJMLDataset() override; 205 : 206 31 : int GetLayerCount() const override 207 : { 208 31 : return poLayer != nullptr ? 1 : 0; 209 : } 210 : 211 : const OGRLayer *GetLayer(int) const override; 212 : 213 : OGRLayer *ICreateLayer(const char *pszName, 214 : const OGRGeomFieldDefn *poGeomFieldDefn, 215 : CSLConstList papszOptions) override; 216 : 217 : int TestCapability(const char *) const override; 218 : 219 : static int Identify(GDALOpenInfo *poOpenInfo); 220 : static GDALDataset *Open(GDALOpenInfo *poOpenInfo); 221 : static GDALDataset *Create(const char *pszFilename, int nBands, int nXSize, 222 : int nYSize, GDALDataType eDT, 223 : CSLConstList papszOptions); 224 : }; 225 : 226 : #endif /* ndef OGR_JML_H_INCLUDED */