1 /*------------------------------------------------------------------------------
  2 Name:      QueryKeyData.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 
  7 #include <util/key/QueryKeyData.h>
  8 #include <util/Constants.h>
  9 #include <util/XmlBlasterException.h>
 10 #include <util/Global.h>
 11 #include <algorithm>
 12 #include <cctype>
 13 
 14 namespace org { namespace xmlBlaster { namespace util { namespace key {
 15 
 16 using namespace std;
 17 using namespace org::xmlBlaster::util;
 18 using namespace org::xmlBlaster::util::qos;
 19 
 20 QueryKeyData::QueryKeyData(Global& global) : KeyData(global), accessFilterVector_()
 21 {
 22 }
 23 
 24 QueryKeyData::QueryKeyData(Global& global, const string& query, const string& queryType) : KeyData(global), accessFilterVector_()
 25 {
 26    this->queryType_ = checkQueryType(queryType);
 27    if (isExact()) {
 28       setOid(query);
 29    }
 30    else if (isXPath()) {
 31       this->queryString_ = query;
 32    }
 33    else if (isDomain()) {
 34       setDomain(query);
 35    }
 36    else {
 37       throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setQueryType",
 38                                 "Your queryType=" + queryType_ + " is invalid, use one of '" + 
 39                                 Constants::EXACT + "' , '" + Constants::XPATH + "' , '" + Constants::D_O_M_A_I_N + "'");
 40    }
 41 }
 42 
 43 QueryKeyData::QueryKeyData(const QueryKeyData& key) : KeyData(key), accessFilterVector_(key.accessFilterVector_)
 44 {
 45 }
 46 
 47 QueryKeyData& QueryKeyData::operator =(const QueryKeyData& key) 
 48 {
 49    accessFilterVector_ = key.accessFilterVector_;
 50    return *this;
 51 }
 52 
 53 string QueryKeyData::checkQueryType(const string& queryType)
 54 {
 55    string tmp = queryType;
 56    // transform (tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
 57    string::iterator iter = tmp.begin();
 58    while (iter != tmp.end()) {
 59       *iter = (char)::toupper(*iter);
 60       iter++;
 61    }
 62 
 63    if (Constants::EXACT != tmp && Constants::XPATH !=tmp && Constants::D_O_M_A_I_N !=tmp)
 64       throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setQueryType",
 65                                 "Your queryType=" + queryType + " is invalid, use one of '" + 
 66                                 Constants::EXACT + "' , '" + Constants::XPATH + "' , '" + Constants::D_O_M_A_I_N + "'");
 67    return tmp;
 68 }
 69 
 70 void QueryKeyData::setQueryType(const string& queryType)
 71 {
 72    queryType_ = checkQueryType(queryType);
 73 }
 74 
 75 /**
 76  * Your XPath query string. 
 77  * NOTE: You need to set the query type first!
 78  * @param str Your tags in ASCII XML syntax
 79  */
 80 void QueryKeyData::setQueryString(const string& tags)
 81 {
 82    this->queryType_ = Constants::XPATH;
 83    this->queryString_ = tags;
 84 }
 85 
 86 void QueryKeyData::setOid(const string& oid)
 87 {
 88    this->queryType_ = Constants::EXACT;
 89    oid_ = oid;
 90 }
 91 
 92 string QueryKeyData::getQueryString() const
 93 {
 94    return queryString_;
 95 }
 96 
 97 /**
 98  * Return the filters or array with size==0 if none is specified. 
 99  * <p />
100  * For subscribe() and get() and cluster messages.
101  * @return never null
102  */
103 AccessFilterVector QueryKeyData::getAccessFilterVector() const 
104 {
105    return accessFilterVector_;
106 }
107 
108 void QueryKeyData::addFilter(const AccessFilterQos& qos) 
109 {
110    accessFilterVector_.insert(accessFilterVector_.end(), qos);
111 }
112 
113 string QueryKeyData::toXml() const
114 {
115    return toXml("");
116 }
117 
118 string QueryKeyData::toXml(const string& extraOffset) const
119 {
120    string ret;
121    string offset = Constants::OFFSET + extraOffset;
122 
123    ret += offset + "<key oid='" + oid_ + "'";
124    if (!getContentMime().empty())
125       ret += " contentMime='" + getContentMime() + "'";
126    if (!getContentMimeExtended().empty())
127       ret += " contentMimeExtended='" + getContentMimeExtended() + "'";
128    if (!getDomain().empty())
129       ret += " domain='" + getDomain() + "'";
130 
131    if (!getQueryType().empty() && Constants::EXACT != getQueryType())
132          ret += " queryType='" + getQueryType() + "'";
133    ret += ">";
134    if (!queryString_.empty()) {
135       ret += offset + Constants::INDENT + getQueryString();
136    }
137 
138    AccessFilterVector::const_iterator iter = accessFilterVector_.begin();
139    while (iter != accessFilterVector_.end()) {
140       ret += (*iter).toXml(extraOffset + Constants::INDENT);
141       iter++;
142    }
143    ret += "</key>";
144   return ret;
145 }
146 
147 QueryKeyData* QueryKeyData::getClone() const
148 {
149    return new QueryKeyData(*this);
150 }
151 
152 }}}} // namespace


syntax highlighted by Code2HTML, v. 0.9.1