<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asascience.ncsos.outputformatter.gc;

import com.asascience.ncsos.gc.GetCapabilitiesRequestHandler;
import com.asascience.ncsos.go.GetObservationRequestHandler;
import com.asascience.ncsos.outputformatter.BaseOutputFormatter;
import com.asascience.ncsos.service.BaseRequestHandler;
import com.asascience.ncsos.util.VocabDefinitions;

import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import ucar.nc2.constants.FeatureType;
import ucar.nc2.time.CalendarDateRange;
import ucar.unidata.geoloc.LatLonRect;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;

/**
 * @author kwilcox
 */
public class GetCapsFormatter extends BaseOutputFormatter {

    public static final String CONTENTS = "Contents";
    public static final String EPSG4326 = "EPSG::4326";
    public static final String HTTP = "HTTP";
    public static final String OPERATIONS_METADATA = "OperationsMetadata";
    public static final String SERVICE_IDENTIFICATION = "ServiceIdentification";
    public static final String SERVICE_PROVIDER = "ServiceProvider";
    
    private boolean exceptionFlag = false;
    private final static String TEMPLATE = "templates/GC.xml";
    private GetCapabilitiesRequestHandler handler = null;

    public GetCapsFormatter(GetCapabilitiesRequestHandler handler) {
        super();
        this.handler = handler;
    }

    @Override
    protected String getTemplateLocation() {
        return TEMPLATE;
    }

    public void parseServiceIdentification(HashMap<String, Object> attrs) {
        Namespace owsns = this.getNamespace("ows");
        Element si = this.getRoot().getChild(SERVICE_IDENTIFICATION, owsns);
        // Name
        si.getChild("Title", owsns).setText((String)attrs.get("title"));
        // Title
        si.getChild("Abstract", owsns).setText((String)attrs.get("summary"));
        // Access
        si.getChild("AccessConstraints", owsns).setText((String)attrs.get("access_constraints"));
        // Keywords
        Element keywords = si.getChild("Keywords", owsns);
        try {
            for (String keyword : ((String)attrs.get("keywords")).split(",")) {
                keywords.addContent(new Element("Keyword", owsns).setText(keyword));
            }
        } catch(Exception e) {
            // No keywords
        }
        // Fees
        si.getChild("Fees", owsns).setText((String)attrs.get("fees"));
    }

    public void removeServiceIdentification() {
        this.getRoot().removeChild(SERVICE_IDENTIFICATION, this.getNamespace("ows"));
    }

    public void parseServiceDescription() {
        Namespace owsns = this.getNamespace("ows");
        Element si = this.getRoot().getChild(SERVICE_PROVIDER, owsns);
        // ProviderName
        si.getChild("ProviderName", owsns).setText((String)this.handler.getGlobalAttribute("publisher_name", "No global attribute 'publisher_name' found."));<commit_msg>Add empty keyword tag if no keywords specified #163<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asascience.ncsos.outputformatter.gc;

import com.asascience.ncsos.gc.GetCapabilitiesRequestHandler;
import com.asascience.ncsos.go.GetObservationRequestHandler;
import com.asascience.ncsos.outputformatter.BaseOutputFormatter;
import com.asascience.ncsos.service.BaseRequestHandler;
import com.asascience.ncsos.util.VocabDefinitions;

import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import ucar.nc2.constants.FeatureType;
import ucar.nc2.time.CalendarDateRange;
import ucar.unidata.geoloc.LatLonRect;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;

/**
 * @author kwilcox
 */
public class GetCapsFormatter extends BaseOutputFormatter {

    public static final String CONTENTS = "Contents";
    public static final String EPSG4326 = "EPSG::4326";
    public static final String HTTP = "HTTP";
    public static final String OPERATIONS_METADATA = "OperationsMetadata";
    public static final String SERVICE_IDENTIFICATION = "ServiceIdentification";
    public static final String SERVICE_PROVIDER = "ServiceProvider";
    
    private boolean exceptionFlag = false;
    private final static String TEMPLATE = "templates/GC.xml";
    private GetCapabilitiesRequestHandler handler = null;

    public GetCapsFormatter(GetCapabilitiesRequestHandler handler) {
        super();
        this.handler = handler;
    }

    @Override
    protected String getTemplateLocation() {
        return TEMPLATE;
    }

    public void parseServiceIdentification(HashMap<String, Object> attrs) {
        Namespace owsns = this.getNamespace("ows");
        Element si = this.getRoot().getChild(SERVICE_IDENTIFICATION, owsns);
        // Name
        si.getChild("Title", owsns).setText((String)attrs.get("title"));
        // Title
        si.getChild("Abstract", owsns).setText((String)attrs.get("summary"));
        // Access
        si.getChild("AccessConstraints", owsns).setText((String)attrs.get("access_constraints"));
        // Keywords
        Element keywords = si.getChild("Keywords", owsns);
        try {
   
            for (String keyword : ((String)attrs.get("keywords")).split(",")) {
                keywords.addContent(new Element("Keyword", owsns).setText(keyword));
            }
        } catch(Exception e) {
            // No keywords
        	 keywords.addContent(new Element("Keyword", owsns));
        }
        // Fees
        si.getChild("Fees", owsns).setText((String)attrs.get("fees"));
    }

    public void removeServiceIdentification() {
        this.getRoot().removeChild(SERVICE_IDENTIFICATION, this.getNamespace("ows"));
    }

    public void parseServiceDescription() {
        Namespace owsns = this.getNamespace("ows");
        Element si = this.getRoot().getChild(SERVICE_PROVIDER, owsns);
        // ProviderName
        si.getChild("ProviderName", owsns).setText((String)this.handler.getGlobalAttribute("publisher_name", "No global attribute 'publisher_name' found."));
{'fraction_non_alphanumeric': 0.11305176246427437, 'fraction_numerical': 0.0036519530009526836, 'mean_word_length': 3.6487084870848707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4232679', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1795, 'n_words': 374}