<commit_before>/*
  Copyright (c) <2007-2011> <<NAME> - <NAME>>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.
*/


#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>

#include "../ows/ows.h"



/*
 * Return the boundaries of the features returned by the request
 */
void wfs_gml_bounded_by(ows * o, wfs_request * wr, float xmin, float ymin, float xmax, float ymax, int srid)
{
    assert(o);
    assert(wr);

    if (!srid || srid == -1 || (xmin == ymin && xmax == ymax && xmin == xmax)) {
        if (ows_version_get(o->request->version) == 100)
            fprintf(o->output, "<gml:boundedBy><gml:null>missing</gml:null></gml:boundedBy>\n");
        else return; /* No Null boundedBy in WFS 1.1.0 SF-0 */
            
    } else {
        fprintf(o->output, "<gml:boundedBy>\n");

        if (wr->format == WFS_GML212) {
            fprintf(o->output, "  <gml:Box srsName=\"EPSG:%d\">", srid);
            fprintf(o->output, "<gml:coordinates decimal=\".\" cs=\",\" ts=\" \">%f,%f %f,%f</gml:coordinates>",
                               xmin, ymin, xmax, ymax);
            fprintf(o->output, "</gml:Box>\n");

        } else if (wr->format == WFS_GML311) {
            fprintf(o->output, "  <gml:Envelope srsName=\"urn:ogc:def:crs:EPSG::%d\">", srid);
            fprintf(o->output, "<gml:lowerCorner>%f %f</gml:lowerCorner>", xmin, ymin);
            fprintf(o->output, "<gml:upperCorner>%f %f</gml:upperCorner>", xmax, ymax);
            fprintf(o->output, "</gml:Envelope>\n");
        }
    
        fprintf(o->output, "</gml:boundedBy>\n");
    }
}


/*
 * Display the properties of one feature
 */
void wfs_gml_display_feature(ows * o, wfs_request * wr, buffer * layer_name, buffer * prefix,
                             char * prop_name, buffer * prop_type, char * value)
{
    buffer *time, *pkey, *value_encoded;
    bool gml_ns = false;

    assert(layer_name);
    assert(prop_name);
    assert(prop_type);
    assert(prefix);
    assert(value);
    assert(wr);
    assert(o);

    pkey = ows_psql_id_column(o, layer_name); /* pkey could be NULL !!! */

    /* No Pkey display in GML (default behaviour) */
    if (pkey && pkey->buf && !strcmp(prop_name, pkey->buf) && !o->expose_pk) return;

    if (strlen(value) == 0) return; /* Don't display empty property */ 

    /* We have to check if we use gml ns or not */
    if (in_list_str((ows_layer_get(o->layers, layer_name))->gml_ns, prop_name)) gml_ns = true;
    if (gml_ns && !strcmp("boundedBy", prop_name)) return; /* TODO handle it */
    if (gml_ns) fprintf(o->output, "   <gml:%s>", prop_name);
    else        fprintf(o->output, "   <%s:%s>", prefix->buf, prop_name);

#if 0
    if (gml_ns && !strcmp("boundedBy", prop_name)) fprintf(o->output, "<gml:Box>");
#endif

    /* PSQL date must be transformed into GML format */
    if (    buffer_cmp(prop_type, "timestamptz")
         || buffer_cmp(prop_type, "timestamp")
         || buffer_cmp(prop_type, "datetime")
         || buffer_cmp(prop_type, "date")) {
        time = ows_psql_timestamp_to_xml_time(value);
        fprintf(o->output, "%s", time->buf);
        buffer_free(time);

    /* PSQL boolean must be transformed into GML format */
    } else if (buffer_cmp(prop_type, "bool")) {
        if (!strcmp(value, "t")) fprintf(o->output, "true");
        if (!strcmp(value, "f")) fprintf(o->output, "false");

    } else if (    buffer_cmp(prop_type, "text")
                || buffer_ncmp(prop_type, "char", 4)
                || buffer_ncmp(prop_type, "varchar", 7)) {

	    value_encoded = buffer_encode_xml_entities_str(value);
            fprintf(o->output, "%s", value_encoded->buf);
            buffer_free(value_encoded);

    } else fprintf(o->output, "%s", value);
    
#if 0
    if (gml_ns && !strcmp("boundedBy", prop_name)) fprintf(o->output, "</gml:Box>");
#endif
    if (gml_ns) fprintf(o->output, "</gml:%s>\n", prop_name);
    else        fprintf(o->output, "</%s:%s>\n", prefix->buf, prop_name);

}


/*
 * Display in GML all feature members returned by the request
 */
void wfs_gml_feature_member(ows * o, wfs_request * wr, buffer * layer_name, list * properties, PGresult * res)
{
    int i, j, number, end, nb_fields;
    buffer *id_name, *ns_prefix, *prop_type;
    array * describe;
    list *mandatory_prop;

    assert(o);
    assert(wr);
    assert(res);
    assert(layer_name);
    /* NOTA: properties could be NULL ! */

    number = -1;
    id_name = ows_psql_id_column(o, layer_name);

    /* We could imagine layer without PK ! */
    if (id_name && id_name->use) number = ows_psql_column_number_id_column(o, layer_name);

    ns_prefix = ows_layer_ns_prefix(o->layers, layer_name);
    mandatory_prop = ows_psql_not_null_properties(o, layer_name);
    describe = ows_psql_describe_table(o, layer_name);

    /* display the results in gml */
    for (i = 0, end = PQntuples(res); i < end; i++) {
        fprintf(o->output, "  <gml:featureMember>\n");

        /* print layer's name and id according to GML version */
        if (id_name && id_name->use) {
            if (wr->format == WFS_GML311)
                fprintf(o->output,  "   <%s:%s gml:id=\"%s.%s\">\n",
                        ns_prefix->buf, layer_name->buf, layer_name->buf, PQgetvalue(res, i, number));
            else fprintf(o->output, "   <%s:%s fid=\"%s.%s\">\n",
                        ns_prefix->buf, layer_name->buf, layer_name->buf, PQgetvalue(res, i, number));
        } else fprintf(o->output,   "   <%s:%s>\n", ns_prefix->buf, layer_name->buf);

        /* print properties */
        for (j = 0, nb_fields = PQnfields(res) ; j < nb_fields ; j++) {

	    if (    !properties
                 || in_list_str(properties, PQfname(res, j))
                 || buffer_cmp(properties->first->value, "*")
                 || (ows_psql_not_null_properties(o, layer_name) 
                      && in_list_str(ows_psql_not_null_properties(o, layer_name), PQfname(res, j)))
                 || in_list_str((ows_layer_get(o->layers, layer_name))->gml_ns, PQfname(res, j)) ){
               prop_type = array_get(describe, PQfname(res, j));
               wfs_gml_display_feature(o, wr, layer_name, ns_prefix, PQfname(res,j), prop_type, PQgetvalue(res, i ,j));
	    }
        }

        fprintf(o->output, "   </%s:%s>\n", ns_prefix->buf, layer_name->buf);
        fprintf(o->output, "  </gml:featureMember>\n");
    }

    buffer_free(layer_name);
}


/*
 * Display in GML the fisrt node and the required namespaces
 */
static void wfs_gml_display_namespaces(ows * o, wfs_request * wr)
{
    array *namespaces;
    array_node *an;
    list_node *ln;
    buffer * ns_prefix;

    assert(o);
    assert(wr);

    namespaces = ows_layer_list_namespaces(o->layers);
    assert(namespaces);<commit_msg>Check is gml_ns list is not null. Bugfixe. reported (again) by <NAME><commit_after>/*
  Copyright (c) <2007-2011> <<NAME> - <NAME>>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.
*/


#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>

#include "../ows/ows.h"



/*
 * Return the boundaries of the features returned by the request
 */
void wfs_gml_bounded_by(ows * o, wfs_request * wr, float xmin, float ymin, float xmax, float ymax, int srid)
{
    assert(o);
    assert(wr);

    if (!srid || srid == -1 || (xmin == ymin && xmax == ymax && xmin == xmax)) {
        if (ows_version_get(o->request->version) == 100)
            fprintf(o->output, "<gml:boundedBy><gml:null>missing</gml:null></gml:boundedBy>\n");
        else return; /* No Null boundedBy in WFS 1.1.0 SF-0 */
            
    } else {
        fprintf(o->output, "<gml:boundedBy>\n");

        if (wr->format == WFS_GML212) {
            fprintf(o->output, "  <gml:Box srsName=\"EPSG:%d\">", srid);
            fprintf(o->output, "<gml:coordinates decimal=\".\" cs=\",\" ts=\" \">%f,%f %f,%f</gml:coordinates>",
                               xmin, ymin, xmax, ymax);
            fprintf(o->output, "</gml:Box>\n");

        } else if (wr->format == WFS_GML311) {
            fprintf(o->output, "  <gml:Envelope srsName=\"urn:ogc:def:crs:EPSG::%d\">", srid);
            fprintf(o->output, "<gml:lowerCorner>%f %f</gml:lowerCorner>", xmin, ymin);
            fprintf(o->output, "<gml:upperCorner>%f %f</gml:upperCorner>", xmax, ymax);
            fprintf(o->output, "</gml:Envelope>\n");
        }
    
        fprintf(o->output, "</gml:boundedBy>\n");
    }
}


/*
 * Display the properties of one feature
 */
void wfs_gml_display_feature(ows * o, wfs_request * wr, buffer * layer_name, buffer * prefix,
                             char * prop_name, buffer * prop_type, char * value)
{
    buffer *time, *pkey, *value_encoded;
    bool gml_ns = false;

    assert(layer_name);
    assert(prop_name);
    assert(prop_type);
    assert(prefix);
    assert(value);
    assert(wr);
    assert(o);

    pkey = ows_psql_id_column(o, layer_name); /* pkey could be NULL !!! */

    /* No Pkey display in GML (default behaviour) */
    if (pkey && pkey->buf && !strcmp(prop_name, pkey->buf) && !o->expose_pk) return;

    if (strlen(value) == 0) return; /* Don't display empty property */ 

    /* We have to check if we use gml ns or not */
    if ((ows_layer_get(o->layers, layer_name))->gml_ns 
         && (in_list_str((ows_layer_get(o->layers, layer_name))->gml_ns, prop_name))) gml_ns = true;
    if (gml_ns && !strcmp("boundedBy", prop_name)) return; /* TODO handle it */
    if (gml_ns) fprintf(o->output, "   <gml:%s>", prop_name);
    else        fprintf(o->output, "   <%s:%s>", prefix->buf, prop_name);

#if 0
    if (gml_ns && !strcmp("boundedBy", prop_name)) fprintf(o->output, "<gml:Box>");
#endif

    /* PSQL date must be transformed into GML format */
    if (    buffer_cmp(prop_type, "timestamptz")
         || buffer_cmp(prop_type, "timestamp")
         || buffer_cmp(prop_type, "datetime")
         || buffer_cmp(prop_type, "date")) {
        time = ows_psql_timestamp_to_xml_time(value);
        fprintf(o->output, "%s", time->buf);
        buffer_free(time);

    /* PSQL boolean must be transformed into GML format */
    } else if (buffer_cmp(prop_type, "bool")) {
        if (!strcmp(value, "t")) fprintf(o->output, "true");
        if (!strcmp(value, "f")) fprintf(o->output, "false");

    } else if (    buffer_cmp(prop_type, "text")
                || buffer_ncmp(prop_type, "char", 4)
                || buffer_ncmp(prop_type, "varchar", 7)) {

	    value_encoded = buffer_encode_xml_entities_str(value);
            fprintf(o->output, "%s", value_encoded->buf);
            buffer_free(value_encoded);

    } else fprintf(o->output, "%s", value);
    
#if 0
    if (gml_ns && !strcmp("boundedBy", prop_name)) fprintf(o->output, "</gml:Box>");
#endif
    if (gml_ns) fprintf(o->output, "</gml:%s>\n", prop_name);
    else        fprintf(o->output, "</%s:%s>\n", prefix->buf, prop_name);

}


/*
 * Display in GML all feature members returned by the request
 */
void wfs_gml_feature_member(ows * o, wfs_request * wr, buffer * layer_name, list * properties, PGresult * res)
{
    int i, j, number, end, nb_fields;
    buffer *id_name, *ns_prefix, *prop_type;
    array * describe;
    list *mandatory_prop;

    assert(o);
    assert(wr);
    assert(res);
    assert(layer_name);
    /* NOTA: properties could be NULL ! */

    number = -1;
    id_name = ows_psql_id_column(o, layer_name);

    /* We could imagine layer without PK ! */
    if (id_name && id_name->use) number = ows_psql_column_number_id_column(o, layer_name);

    ns_prefix = ows_layer_ns_prefix(o->layers, layer_name);
    mandatory_prop = ows_psql_not_null_properties(o, layer_name);
    describe = ows_psql_describe_table(o, layer_name);

    /* display the results in gml */
    for (i = 0, end = PQntuples(res); i < end; i++) {
        fprintf(o->output, "  <gml:featureMember>\n");

        /* print layer's name and id according to GML version */
        if (id_name && id_name->use) {
            if (wr->format == WFS_GML311)
                fprintf(o->output,  "   <%s:%s gml:id=\"%s.%s\">\n",
                        ns_prefix->buf, layer_name->buf, layer_name->buf, PQgetvalue(res, i, number));
            else fprintf(o->output, "   <%s:%s fid=\"%s.%s\">\n",
                        ns_prefix->buf, layer_name->buf, layer_name->buf, PQgetvalue(res, i, number));
        } else fprintf(o->output,   "   <%s:%s>\n", ns_prefix->buf, layer_name->buf);

        /* print properties */
        for (j = 0, nb_fields = PQnfields(res) ; j < nb_fields ; j++) {

	    if (    !properties
                 || in_list_str(properties, PQfname(res, j))
                 || buffer_cmp(properties->first->value, "*")
                 || (ows_psql_not_null_properties(o, layer_name) 
                      && in_list_str(ows_psql_not_null_properties(o, layer_name), PQfname(res, j)))
                 || ((ows_layer_get(o->layers, layer_name))->gml_ns  
                      && in_list_str((ows_layer_get(o->layers, layer_name))->gml_ns, PQfname(res, j)))) {
               prop_type = array_get(describe, PQfname(res, j));
               wfs_gml_display_feature(o, wr, layer_name, ns_prefix, PQfname(res,j), prop_type, PQgetvalue(res, i ,j));
	    }
        }

        fprintf(o->output, "   </%s:%s>\n", ns_prefix->buf, layer_name->buf);
        fprintf(o->output, "  </gml:featureMember>\n");
    }

    buffer_free(layer_name);
}


/*
 * Display in GML the fisrt node and the required namespaces
 */
static void wfs_gml_display_namespaces(ows * o, wfs_request * wr)
{
    array *namespaces;
    array_node *an;
    list_node *ln;
    buffer * ns_prefix;

    assert(o);
    assert(wr);

    namespaces = ows_layer_list_namespaces(o->layers);
    assert(namespaces);
{'fraction_non_alphanumeric': 0.15063291139240506, 'fraction_numerical': 0.004177215189873418, 'mean_word_length': 2.880402750491159, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 202, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1277424', 'n_tokens_mistral': 5471, 'n_tokens_neox': 5340, 'n_words': 1352}