Blame Resource.h

Packit a4aae4
Packit a4aae4
// -*- mode: c++; c-basic-offset:4 -*-
Packit a4aae4
Packit a4aae4
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
Packit a4aae4
// Access Protocol.
Packit a4aae4
Packit a4aae4
// Copyright (c) 2003 OPeNDAP, Inc.
Packit a4aae4
// Author: James Gallagher <jgallagher@opendap.org>
Packit a4aae4
//
Packit a4aae4
// This library is free software; you can redistribute it and/or
Packit a4aae4
// modify it under the terms of the GNU Lesser General Public
Packit a4aae4
// License as published by the Free Software Foundation; either
Packit a4aae4
// version 2.1 of the License, or (at your option) any later version.
Packit a4aae4
//
Packit a4aae4
// This library is distributed in the hope that it will be useful,
Packit a4aae4
// but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4aae4
// Lesser General Public License for more details.
Packit a4aae4
//
Packit a4aae4
// You should have received a copy of the GNU Lesser General Public
Packit a4aae4
// License along with this library; if not, write to the Free Software
Packit a4aae4
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit a4aae4
//
Packit a4aae4
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
Packit a4aae4
Packit a4aae4
#ifndef resource_h
Packit a4aae4
#define resource_h
Packit a4aae4
Packit a4aae4
#include <string>
Packit a4aae4
#include <iostream>
Packit a4aae4
Packit a4aae4
#ifndef _error_h
Packit a4aae4
#include "Error.h"
Packit a4aae4
#endif
Packit a4aae4
Packit a4aae4
using namespace std;
Packit a4aae4
Packit a4aae4
namespace libdap
Packit a4aae4
{
Packit a4aae4
Packit a4aae4
/** Bind an ancillary resource with the rule that should be used when
Packit a4aae4
    combining it with a primary resource. Ancillary resources are always
Packit a4aae4
    specified using URLs. If an ancillary resource is a local file, use
Packit a4aae4
    file:// URLs.
Packit a4aae4
Packit a4aae4
    Note that operator<< is defined for Resource as a function.
Packit a4aae4
Packit a4aae4
    @brief Associate a rule with an ancillary resource.
Packit a4aae4
    @author James Gallagher <jgallagher@opendap.org> */
Packit a4aae4
class Resource
Packit a4aae4
{
Packit a4aae4
public:
Packit a4aae4
Packit a4aae4
    /** The AIS uses this enumeration to describe how a given ancillary should
Packit a4aae4
    be merged into a primary data source.
Packit a4aae4
Packit a4aae4
    
    Packit a4aae4
        
  • overwrite: Attributes in the ancillary source overwrite those in
  • Packit a4aae4
        the primary source. New values are added.
    Packit a4aae4
        
  • replace: The ancillary source replaces the primary. All of the
  • Packit a4aae4
        Attributes in the primary are removed.
    Packit a4aae4
        
  • fallback: The ancillary resource provides a set of fallback values
  • Packit a4aae4
        if the primary data source lacks any attributes. Note that this does
    Packit a4aae4
        not apply to individual attributes, but to an entire set. The fallback
    Packit a4aae4
        attributes are used only if the original data source lacks attributes
    Packit a4aae4
        altogether. 
    Packit a4aae4
        
    Packit a4aae4
    Packit a4aae4
        @brief How are ancillary resources used.
    Packit a4aae4
        @author James Gallagher <jgallagher@opendap.org> */
    Packit a4aae4
        enum rule { overwrite, replace, fallback };
    Packit a4aae4
    Packit a4aae4
        /** Build a Resource with a null URL and set the combination rule to the
    Packit a4aae4
        default. */
    Packit a4aae4
        Resource() : d_url(""), d_rule(overwrite)
    Packit a4aae4
        {}
    Packit a4aae4
    Packit a4aae4
        /** Build a resource. Set the combination rule to the default value,
    Packit a4aae4
        which is overwrite. 
    Packit a4aae4
        @param u The ancillary resource URL. */
    Packit a4aae4
        Resource(const string &u) : d_url(u), d_rule(overwrite)
    Packit a4aae4
        {}
    Packit a4aae4
    Packit a4aae4
        /** Build a Resource.
    Packit a4aae4
        @param u The ancillary resource URL.
    Packit a4aae4
        @param r The combination rule. */
    Packit a4aae4
        Resource(const string &u, const rule &r) : d_url(u), d_rule(r)
    Packit a4aae4
        {}
    Packit a4aae4
    Packit a4aae4
        /** Build a Resource.
    Packit a4aae4
    Packit a4aae4
            Note: If this is used in a callback, make sure to check the value of
    Packit a4aae4
        r before calling this constructor. Exceptions thrown
    Packit a4aae4
        within callbacks are not portable. Valid values are "overwrite",
    Packit a4aae4
        "replace" and "fallback". The constructor accepts "default" as a
    Packit a4aae4
        synonym for "overwrite".
    Packit a4aae4
    Packit a4aae4
        @param u The ancillary resource URL.
    Packit a4aae4
        @param r The name of the combination rule. */
    Packit a4aae4
        Resource(const string &u, const string &r) throw(Error) : d_url(u)
    Packit a4aae4
        {
    Packit a4aae4
            if (r == "replace")
    Packit a4aae4
                d_rule = replace;
    Packit a4aae4
            else if (r == "fallback")
    Packit a4aae4
                d_rule = fallback;
    Packit a4aae4
            else if (r == "overwrite" || r == "default")
    Packit a4aae4
                d_rule = overwrite;
    Packit a4aae4
            else
    Packit a4aae4
                throw Error(string("An AIS Resource object was created with an unknown rule type '") + r);
    Packit a4aae4
        }
    Packit a4aae4
    Packit a4aae4
        virtual ~Resource()
    Packit a4aae4
    {}
    Packit a4aae4
    Packit a4aae4
        /** Return the resource URL. */
    Packit a4aae4
        virtual string get_url() const
    Packit a4aae4
        {
    Packit a4aae4
            return d_url;
    Packit a4aae4
        }
    Packit a4aae4
    Packit a4aae4
        /** Set the resource URL.
    Packit a4aae4
        @param u The resource's URL. */
    Packit a4aae4
        virtual void set_url(const string &u)
    Packit a4aae4
        {
    Packit a4aae4
            d_url = u;
    Packit a4aae4
        }
    Packit a4aae4
    Packit a4aae4
        /** Return combination rule for this resource. */
    Packit a4aae4
        virtual Resource::rule get_rule() const
    Packit a4aae4
        {
    Packit a4aae4
            return d_rule;
    Packit a4aae4
        }
    Packit a4aae4
    Packit a4aae4
        /** Set the resource's combination rule.
    Packit a4aae4
        @param r The combination rule. */
    Packit a4aae4
        virtual void set_rule(const Resource::rule &r)
    Packit a4aae4
        {
    Packit a4aae4
            d_rule = r;
    Packit a4aae4
        }
    Packit a4aae4
    Packit a4aae4
        /** Write the XML for this resource. This function is defined in
    Packit a4aae4
        AISResoruces. 
    Packit a4aae4
        @param os Write to this ostream.
    Packit a4aae4
        @paran r The Resource to write. */
    Packit a4aae4
        friend ostream &operator<<(ostream &os, const Resource &r);
    Packit a4aae4
    Packit a4aae4
    Packit a4aae4
    private:
    Packit a4aae4
    Packit a4aae4
        string d_url;
    Packit a4aae4
        Resource::rule d_rule;
    Packit a4aae4
    };
    Packit a4aae4
    Packit a4aae4
    } // namespace libdap
    Packit a4aae4
    Packit a4aae4
    #endif // resource_h