1 /******************************************************************************
2  * $Id: gmlregistry.cpp 29240 2015-05-24 10:58:38Z rouault $
3  *
4  * Project:  GML registry
5  * Purpose:  GML reader
6  * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
7  *
8  ******************************************************************************
9  * Copyright (c) 2013, Even Rouault <even dot rouault at mines-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 OR
22  * 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 #include "gmlregistry.h"
31 
32 /************************************************************************/
33 /*                           Parse()                                    */
34 /************************************************************************/
35 
Parse()36 int GMLRegistry::Parse()
37 {
38     if( osRegistryPath.size() == 0 )
39     {
40         const char* pszFilename = CPLFindFile( "gdal", "gml_registry.xml" );
41         if( pszFilename )
42             osRegistryPath = pszFilename;
43     }
44     if( osRegistryPath.size() == 0 )
45         return FALSE;
46     CPLXMLNode* psRootNode = CPLParseXMLFile(osRegistryPath);
47     if( psRootNode == NULL )
48         return FALSE;
49     CPLXMLNode *psRegistryNode = CPLGetXMLNode( psRootNode, "=gml_registry" );
50     if( psRegistryNode == NULL )
51     {
52         CPLDestroyXMLNode(psRootNode);
53         return FALSE;
54     }
55     CPLXMLNode* psIter = psRegistryNode->psChild;
56     while( psIter != NULL )
57     {
58         if( psIter->eType == CXT_Element &&
59             strcmp(psIter->pszValue, "namespace") == 0 )
60         {
61             GMLRegistryNamespace oNameSpace;
62             if( oNameSpace.Parse(osRegistryPath, psIter) )
63             {
64                 aoNamespaces.push_back(oNameSpace);
65             }
66         }
67         psIter = psIter->psNext;
68     }
69     CPLDestroyXMLNode(psRootNode);
70     return TRUE;
71 }
72 
73 /************************************************************************/
74 /*                           Parse()                                    */
75 /************************************************************************/
76 
Parse(const char * pszRegistryFilename,CPLXMLNode * psNode)77 int GMLRegistryNamespace::Parse(const char* pszRegistryFilename, CPLXMLNode* psNode)
78 {
79     const char* pszPrefix = CPLGetXMLValue(psNode, "prefix", NULL);
80     const char* pszURI = CPLGetXMLValue(psNode, "uri", NULL);
81     if( pszPrefix == NULL || pszURI == NULL )
82         return FALSE;
83     osPrefix = pszPrefix;
84     osURI = pszURI;
85     const char* pszUseGlobalSRSName = CPLGetXMLValue(psNode, "useGlobalSRSName", NULL);
86     if( pszUseGlobalSRSName != NULL && strcmp(pszUseGlobalSRSName, "true") == 0 )
87         bUseGlobalSRSName = TRUE;
88 
89     CPLXMLNode* psIter = psNode->psChild;
90     while( psIter != NULL )
91     {
92         if( psIter->eType == CXT_Element &&
93             strcmp(psIter->pszValue, "featureType") == 0 )
94         {
95             GMLRegistryFeatureType oFeatureType;
96             if( oFeatureType.Parse(pszRegistryFilename, psIter) )
97             {
98                 aoFeatureTypes.push_back(oFeatureType);
99             }
100         }
101         psIter = psIter->psNext;
102     }
103     return TRUE;
104 }
105 
106 /************************************************************************/
107 /*                           Parse()                                    */
108 /************************************************************************/
109 
Parse(const char * pszRegistryFilename,CPLXMLNode * psNode)110 int GMLRegistryFeatureType::Parse(const char* pszRegistryFilename, CPLXMLNode* psNode)
111 {
112     const char* pszElementName = CPLGetXMLValue(psNode, "elementName", NULL);
113     const char* pszElementValue = CPLGetXMLValue(psNode, "elementValue", NULL);
114     const char* pszSchemaLocation = CPLGetXMLValue(psNode, "schemaLocation", NULL);
115     const char* pszGFSSchemaLocation = CPLGetXMLValue(psNode, "gfsSchemaLocation", NULL);
116     if( pszElementName == NULL || (pszSchemaLocation == NULL && pszGFSSchemaLocation == NULL) )
117         return FALSE;
118     osElementName = pszElementName;
119 
120     if( pszSchemaLocation != NULL )
121     {
122         if( strncmp(pszSchemaLocation, "http://", 7) != 0 &&
123             strncmp(pszSchemaLocation, "https://", 8) != 0 &&
124             CPLIsFilenameRelative(pszSchemaLocation ) )
125         {
126             pszSchemaLocation = CPLFormFilename(
127                 CPLGetPath(pszRegistryFilename), pszSchemaLocation, NULL );
128         }
129         osSchemaLocation = pszSchemaLocation;
130     }
131     else if( pszGFSSchemaLocation != NULL )
132     {
133         if( strncmp(pszGFSSchemaLocation, "http://", 7) != 0 &&
134             strncmp(pszGFSSchemaLocation, "https://", 8) != 0 &&
135             CPLIsFilenameRelative(pszGFSSchemaLocation ) )
136         {
137             pszGFSSchemaLocation = CPLFormFilename(
138                 CPLGetPath(pszRegistryFilename), pszGFSSchemaLocation, NULL );
139         }
140         osGFSSchemaLocation = pszGFSSchemaLocation;
141     }
142 
143     if ( pszElementValue != NULL )
144     {
145         osElementValue = pszElementValue;
146     }
147 
148     return TRUE;
149 }
150