<commit_before>/*
 * Copyright (C) 2014 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration. All Rights Reserved.
 */
/**
 * @exports WmsCapabilities
 * @version $Id: WmsCapabilities.js 3055 2015-04-29 21:39:51Z tgaskins $
 */
define([
        '../error/ArgumentError',
        '../util/Logger',
        '../ogc/WmsLayerCapabilities'
    ],
    function (ArgumentError,
              Logger,
              WmsLayerCapabilities) {
        "use strict";

        /**
         * Constructs an WMS Capabilities instance from an XML DOM.
         * @alias WMSCapabilities
         * @constructor
         * @classdesc Represents a WMS Capabilities document. This object holds as properties all the fields
         * specified in the given WMS Capabilities document. Most fields can be accessed as properties named
         * according to their document names converted to camel case. For example, "version", "service.title",
         * "service.contactInformation.contactPersonPrimary". The exceptions are online resources, whose property
         * path has been shortened. For example "capability.request.getMap.formats" and "capability.request.getMap.getUrl".
         * @param {{}} xmlDom An XML DOM representing the WMS Capabilities document.
         * @throws {ArgumentError} If the specified XML DOM is null or undefined.
         */
        var WmsCapabilities = function (xmlDom) {
            if (!xmlDom) {
                throw new ArgumentError(
                    Logger.logMessage(Logger.LEVEL_SEVERE, "WmsCapabilities", "constructor", "No XML DOM specified."));
            }

            this.assembleDocument(xmlDom);
        };

        WmsCapabilities.prototype.namedLayers = function (namedLayersArray, layerToStart) {
            var namedLayers = namedLayersArray || [];
            var layers = layerToStart || this.capability.layers;

            if (layers === undefined) {
                return namedLayers;
            }

            for (var i = 0, len = layers.length; i < len; i++) {
                var layer = layers[i];
                if (layer.name) {
                    namedLayers.push(layer);
                }
                if (layer.layers) {
                    this.namedLayers(namedLayers, layer.layers);
                }
            }

            return namedLayers;
        };

        WmsCapabilities.prototype.namedLayer = function (name) {
            if (name === undefined) {
                return null;
            }

            var namedLayers = this.namedLayers();

            for (var i = 0, len = namedLayers.length; i < len; i++) {
                if (name === namedLayers[i].name) {
                    return namedLayers[i];
                }
            }
        }

        WmsCapabilities.prototype.assembleDocument = function (dom) {
            var root = dom.documentElement;

            this.version = root.getAttribute("version");
            this.updateSequence = root.getAttribute("updateSequence");

            var children = root.children || root.childNodes;
            for (var c = 0; c < children.length; c++) {
                var child = children[c];

                if (child.localName === "Service") {
                    this.service = this.assembleService(child);
                } else if (child.localName === "Capability") {
                    this.capability = this.assembleCapability(child);
                }
            }
        };

        WmsCapabilities.prototype.assembleService = function (element) {
            var service = {
                capsDoc: this
            };

            var children = element.children || element.childNodes;
            for (var c = 0; c < children.length; c++) {<commit_msg>Polish utility methods -Explicit null return for named layer search without success -Input check of layer name in getNamedLayer -Get/Set name addition to methods -Named layer accumulation refactor to clarify getNamedLayers signature<commit_after>/*
 * Copyright (C) 2014 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration. All Rights Reserved.
 */
/**
 * @exports WmsCapabilities
 * @version $Id: WmsCapabilities.js 3055 2015-04-29 21:39:51Z tgaskins $
 */
define([
        '../error/ArgumentError',
        '../util/Logger',
        '../ogc/WmsLayerCapabilities'
    ],
    function (ArgumentError,
              Logger,
              WmsLayerCapabilities) {
        "use strict";

        /**
         * Constructs an WMS Capabilities instance from an XML DOM.
         * @alias WMSCapabilities
         * @constructor
         * @classdesc Represents a WMS Capabilities document. This object holds as properties all the fields
         * specified in the given WMS Capabilities document. Most fields can be accessed as properties named
         * according to their document names converted to camel case. For example, "version", "service.title",
         * "service.contactInformation.contactPersonPrimary". The exceptions are online resources, whose property
         * path has been shortened. For example "capability.request.getMap.formats" and "capability.request.getMap.getUrl".
         * @param {{}} xmlDom An XML DOM representing the WMS Capabilities document.
         * @throws {ArgumentError} If the specified XML DOM is null or undefined.
         */
        var WmsCapabilities = function (xmlDom) {
            if (!xmlDom) {
                throw new ArgumentError(
                    Logger.logMessage(Logger.LEVEL_SEVERE, "WmsCapabilities", "constructor", "No XML DOM specified."));
            }

            this.assembleDocument(xmlDom);
        };

        WmsCapabilities.prototype.getNamedLayers = function () {
            return this.accumulateNamedLayers(this.capability.layers);
        };

        /**
         * Accumulates the named layers recursively from the provided initial array of layers. An optional array for accumulated
         * results is provided.
         * @param {WmsLayerCapabilities[]} startLayers the layer array to start accumulation, named layers of this array are included in accumulation
         * @param {WmsLayerCapabilities[]} namedLayersArray optional accumulation array
         * @returns an array of named WmsLayerCapabilities
         */
        WmsCapabilities.prototype.accumulateNamedLayers = function (startLayers, namedLayersArray) {
            var namedLayers = namedLayersArray || [];
            
            if (!startLayers) {
                return namedLayers;
            }

            for (var i = 0, len = startLayers.length; i < len; i++) {
                var layer = startLayers[i];
                if (layer.name) {
                    namedLayers.push(layer);
                }
                if (layer.layers) {
                    this.accumulateNamedLayers(layer.layers, namedLayers);
                }
            }

            return namedLayers;
        };

        /**
         * Searches for a named layer matching the provided name and returns the WmsLayerCapabilities object representing 
         * the named layer.
         * @param {String} name the layer name to find
         * @returns {WmsLayerCapabilities} if a matching named layer is found or null
         * @throws {ArgumentError} If the specified name is null or empty.
         */
        WmsCapabilities.prototype.getNamedLayer = function (name) {
            if (!name || (name.length === 0)) {
                throw new ArgumentError(
                    Logger.logMessage(Logger.LEVEL_SEVERE, "WmsCapabilities", "getNamedLayer", "No WMS layer name provided."));
            }

            var namedLayers = this.namedLayers();

            for (var i = 0, len = namedLayers.length; i < len; i++) {
                if (name === namedLayers[i].name) {
                    return namedLayers[i];
                }
            }

            return null;
        };

        WmsCapabilities.prototype.assembleDocument = function (dom) {
            var root = dom.documentElement;

            this.version = root.getAttribute("version");
            this.updateSequence = root.getAttribute("updateSequence");

            var children = root.children || root.childNodes;
            for (var c = 0; c < children.length; c++) {
                var child = children[c];

                if (child.localName === "Service") {
                    this.service = this.assembleService(child);
                } else if (child.localName === "Capability") {
                    this.capability = this.assembleCapability(child);
                }
            }
        };

        WmsCapabilities.prototype.assembleService = function (element) {
            var service = {
                capsDoc: this
            };

            var children = element.children || element.childNodes;
            for (var c = 0; c < children.length; c++) {
{'fraction_non_alphanumeric': 0.09217002237136465, 'fraction_numerical': 0.00592841163310962, 'mean_word_length': 1.8851242336237497, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '744080', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2167, 'n_words': 669}