00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "tools.h"
00037 #include "cds_resource.h"
00038
00039 #define RESOURCE_PART_SEP '~'
00040
00041 using namespace zmm;
00042
00043 CdsResource::CdsResource(int handlerType) : Object()
00044 {
00045 this->handlerType = handlerType;
00046 this->attributes = Ref<Dictionary>(new Dictionary());
00047 this->parameters = Ref<Dictionary>(new Dictionary());
00048 this->options = Ref<Dictionary>(new Dictionary());
00049 }
00050 CdsResource::CdsResource(int handlerType,
00051 Ref<Dictionary> attributes,
00052 Ref<Dictionary> parameters,
00053 Ref<Dictionary> options)
00054 {
00055 this->handlerType = handlerType;
00056 this->attributes = attributes;
00057 this->parameters = parameters;
00058 this->options = options;
00059 }
00060
00061 void CdsResource::addAttribute(String name, String value)
00062 {
00063 attributes->put(name, value);
00064 }
00065
00066 void CdsResource::removeAttribute(String name)
00067 {
00068 attributes->remove(name);
00069 }
00070
00071 void CdsResource::mergeAttributes(Ref<Dictionary> additional)
00072 {
00073 attributes->merge(additional);
00074 }
00075
00076
00077 void CdsResource::addParameter(String name, String value)
00078 {
00079 parameters->put(name, value);
00080 }
00081
00082 void CdsResource::addOption(String name, String value)
00083 {
00084 options->put(name, value);
00085 }
00086
00087 int CdsResource::getHandlerType()
00088 {
00089 return handlerType;
00090 }
00091
00092 Ref<Dictionary> CdsResource::getAttributes()
00093 {
00094 return attributes;
00095 }
00096
00097 Ref<Dictionary> CdsResource::getParameters()
00098 {
00099 return parameters;
00100 }
00101
00102 Ref<Dictionary> CdsResource::getOptions()
00103 {
00104 return options;
00105 }
00106
00107 String CdsResource::getAttribute(String name)
00108 {
00109 return attributes->get(name);
00110 }
00111
00112 String CdsResource::getParameter(String name)
00113 {
00114 return parameters->get(name);
00115 }
00116
00117 String CdsResource::getOption(String name)
00118 {
00119 return options->get(name);
00120 }
00121
00122 bool CdsResource::equals(Ref<CdsResource> other)
00123 {
00124 return (
00125 handlerType == other->handlerType &&
00126 attributes->equals(other->attributes) &&
00127 parameters->equals(other->parameters) &&
00128 options->equals(other->options)
00129 );
00130 }
00131
00132 Ref<CdsResource> CdsResource::clone()
00133 {
00134 return Ref<CdsResource>(new CdsResource(handlerType,
00135 attributes->clone(),
00136 parameters->clone(),
00137 options->clone()));
00138 }
00139
00140 String CdsResource::encode()
00141 {
00142
00143 Ref<StringBuffer> buf(new StringBuffer());
00144 *buf << handlerType;
00145 *buf << RESOURCE_PART_SEP;
00146 *buf << attributes->encode();
00147 *buf << RESOURCE_PART_SEP;
00148 *buf << parameters->encode();
00149 *buf << RESOURCE_PART_SEP;
00150 *buf << options->encode();
00151 return buf->toString();
00152 }
00153
00154 Ref<CdsResource> CdsResource::decode(String serial)
00155 {
00156 Ref<Array<StringBase> > parts = split_string(serial, RESOURCE_PART_SEP, true);
00157 int size = parts->size();
00158 if (size < 2 || size > 4)
00159 throw _Exception(_("CdsResource::decode: Could not parse resources"));
00160
00161 int handlerType = String(parts->get(0)).toInt();
00162
00163 Ref<Dictionary> attr(new Dictionary());
00164 attr->decode(parts->get(1));
00165
00166 Ref<Dictionary> par(new Dictionary());
00167
00168 if (size >= 3)
00169 par->decode(parts->get(2));
00170
00171 Ref<Dictionary> opt(new Dictionary());
00172
00173 if (size >= 4)
00174 opt->decode(parts->get(3));
00175
00176 Ref<CdsResource> resource(new CdsResource(handlerType, attr, par, opt));
00177
00178 return resource;
00179 }
00180
00181 void CdsResource::optimize()
00182 {
00183 attributes->optimize();
00184 parameters->optimize();
00185 options->optimize();
00186 }