OW_HDBCommon.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00031 #include "OW_config.h"
00032 #include "OW_HDBCommon.hpp"
00033 #include "OW_CIMObjectPath.hpp"
00034 #include "OW_StringBuffer.hpp"
00035 #include "OW_CIMValue.hpp"
00036 #include "OW_CIMUrl.hpp"
00037 
00038 #include <algorithm> // for std::sort
00039 
00040 namespace OW_NAMESPACE
00041 {
00042 
00043 
00044 HDBUtilKeyArray::HDBUtilKeyArray(const CIMPropertyArray& props)
00045    : m_props(props)
00046 {
00047    std::sort(m_props.begin(), m_props.end());
00048 }
00049 
00050 namespace
00051 {
00052 
00053 void asKeyString(StringBuffer& out, const CIMValue& val);
00054 
00055 struct KeySorter
00056 {
00057    bool operator()(const CIMProperty& x, const CIMProperty& y) const
00058    {
00059       return x.getName().compareToIgnoreCase(y.getName()) < 0;
00060    }
00061 };
00062 
00063 String
00064 modelPath(const CIMObjectPath& cop)
00065 {
00066    StringBuffer rv(cop.getClassName().toString().toLowerCase());
00067    CIMPropertyArray keys(cop.getKeys());
00068    std::sort(keys.begin(), keys.end(), KeySorter());
00069    for (size_t i = 0; i < keys.size(); i++)
00070    {
00071       const CIMProperty& cp(keys[i]);
00072       if (i > 0)
00073       {
00074          rv += ',';
00075       }
00076       else
00077       {
00078          rv += '.';
00079       }
00080       rv += cp.getName().toLowerCase();
00081       rv += "=\"";
00082       asKeyString(rv, cp.getValue());
00083    }
00084    return rv.releaseString();
00085 }
00086 
00087 String
00088 objectPathAsKeyString(const CIMObjectPath& cop)
00089 {
00090    StringBuffer rv;
00091    CIMUrl url = cop.getNameSpaceUrl();
00092    if (!url.isLocal())
00093    {
00094       rv += url.getProtocol().toLowerCase();
00095       rv += "://";
00096       rv += url.getHost().toLowerCase();
00097       if (url.getPort() != 5988)
00098       {
00099          rv += ":";
00100          rv += url.getPort();
00101       }
00102    }
00103    rv += '/';
00104    rv += cop.getNameSpace();
00105    rv += ':';
00106    rv += modelPath(cop);
00107    return rv.releaseString();
00108 }
00109 
00110 
00111 void asKeyString(StringBuffer& out, const CIMValue& val)
00112 {
00113    if (!val)
00114    {
00115       OW_THROW(HDBException, "NULL value not allowed in a key property");
00116    }
00117    out += '"';
00118    if (val.getType() == CIMDataType::REFERENCE)
00119    {
00120       // arrays of references aren't allowed, so we don't need to handle that.
00121       out += CIMObjectPath::escape(objectPathAsKeyString(val.toCIMObjectPath()));
00122    }
00123    else
00124    {
00125       out += CIMObjectPath::escape(val.toMOF());
00126    }
00127    out += '"';
00128 }
00129 
00130 } // end unnamed namespace
00131 
00132 void
00133 HDBUtilKeyArray::toString(StringBuffer& out)
00134 {
00135    for (size_t i = 0; i < m_props.size(); i++)
00136    {
00137       char c = (i == 0) ? '.' : ',';
00138       out += c;
00139       out += m_props[i].getName().toLowerCase();
00140       out += '=';
00141       asKeyString(out, m_props[i].getValue());
00142    }
00143 }
00144 
00145 
00146 } // end namespace OW_NAMESPACE
00147 
00148 
00149 

Generated on Thu Feb 9 08:47:59 2006 for openwbem by  doxygen 1.4.6