00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005
00006 #include "AttributeRep.h"
00007 #include "FCO.h"
00008
00009 #include <algorithm>
00010 #include "globals.h"
00011 #include "Dumper.h"
00012 extern Globals global_vars;
00013 extern int ind;
00014
00015
00016 AttributeRep::AttributeRep( BON::FCO& ptr)
00017 : Any( ptr)
00018 {
00019 }
00020
00021
00022 AttributeRep::~AttributeRep()
00023 {
00024 }
00025
00026 std::string AttributeRep::getName() const
00027 {
00028 if ( this->m_ptr)
00029 {
00030 return m_ptr->getName();
00031 }
00032 return "NullPtrError";
00033 }
00034
00035 bool AttributeRep::isGlobal()
00036 {
00037 return m_ptr->getAttribute("GlobalScope")->getBooleanValue();
00038 }
00039
00040
00041 bool AttributeRep::isViewable()
00042 {
00043 return m_ptr->getAttribute("IsViewable")->getBooleanValue();
00044 }
00045
00046
00047 std::string AttributeRep::getPrompt()
00048 {
00049 return Dumper::xmlFilter( m_ptr->getAttribute("Prompt")->getStringValue());
00050 }
00051
00052
00053 std::string AttributeRep::dumpHelp()
00054 {
00055 std::string mmm;
00056 std::string help = Dumper::xmlFilter( m_ptr->getAttribute("Help")->getStringValue());
00057 if( !help.empty())
00058 mmm += indStr() + "<regnode name=\"help\" value = \"" + help + "\"></regnode>\n";
00059
00060 return mmm;
00061 }
00062
00063
00064 std::string AttributeRep::getMetaRef(const std::string & owner)
00065 {
00066 std::string m_ref;
00067 std::string token = "";
00068 if ( !owner.empty()) token = "/" + owner;
00069 m_ref = askMetaRef(token);
00070
00071 return m_ref;
00072 }
00073
00074
00075 void AttributeRep::getXY( unsigned int * x, unsigned int * y) const
00076 {
00077 BON::Point p = BON::Atom( m_ptr)->getRegistry()->getLocation("Attributes");
00078 *x = p.first;
00079 *y = p.second;
00080 }
00081
00082
00083 bool AttributeRep::lessThan( const AttributeRep * rep) const
00084 {
00085 std::string m = getName();
00086 unsigned int x, y;
00087 unsigned int rep_x, rep_y;
00088 getXY( &x, &y);
00089 rep->getXY( &rep_x, &rep_y);
00090 return ( y < rep_y || (y == rep_y && x < rep_x ));
00091 }
00092
00093
00094 void AttributeRep::addOwner( FCO * owner)
00095 {
00096 std::vector<FCO*>::iterator it =
00097 std::find( m_ownerList.begin(), m_ownerList.end(), owner);
00098
00099 if ( it == m_ownerList.end())
00100 m_ownerList.push_back( owner);
00101 else
00102 global_vars.err << MSG_ERROR << m_ptr << " attribute owned by " << owner->getPtr() << " twice\n";
00103 }
00104
00105
00106 int AttributeRep::hasAnyOwner() const
00107 {
00108 return m_ownerList.size();
00109 }
00110
00111
00112 EnumAttributeRep::EnumAttributeRep( BON::FCO& ptr)
00113 : AttributeRep( ptr), m_noOfItems(0), m_noOfDefault(0)
00114 {
00115 m_items.clear();
00116 m_itemsVal.clear();
00117 }
00118
00119
00120 bool EnumAttributeRep::getMenuItems()
00121 {
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 std::string items = Dumper::xmlFilter( m_ptr->getAttribute("MenuItems")->getStringValue());
00135
00136 items += '\n';
00137 std::string::size_type len = items.length();
00138
00139 std::string whitespace = "\r\n\t ";
00140 std::string::size_type start_i = 0;
00141 while (start_i < len && whitespace.find(items[start_i]) != std::string::npos )
00142 ++start_i;
00143
00144 std::string::size_type i = start_i, comma_i = 0;
00145 while( i < len )
00146 {
00147 if ( items[i] == '\n' && i - start_i > 0)
00148 {
00149 if ( comma_i != 0)
00150 {
00151 std::string::size_type st,end;
00152
00153 st = start_i;
00154 while( st < comma_i && whitespace.find( items[st]) != std::string::npos) ++st;
00155 end = comma_i-1;
00156 while( end > start_i && whitespace.find( items[end]) != std::string::npos) --end;
00157
00158 if ( end>=st)
00159 m_items.push_back(items.substr( st, end - st + 1));
00160 else
00161 {
00162 m_items.push_back("Error");
00163 global_vars.err << MSG_ERROR << "Error: Enumitem #" << 1 + m_noOfItems << " of " << getPtr() << " has wrong format. (If comma is used a second value is required.)\n";
00164 }
00165
00166
00167 st = comma_i+1;
00168 while( st < i && whitespace.find( items[st]) != std::string::npos) ++st;
00169 end = i;
00170 while( end > comma_i+1 && whitespace.find( items[end]) != std::string::npos) --end;
00171
00172 if ( end>=st && comma_i+1 < i)
00173 m_itemsVal.push_back(items.substr( st, end - st + 1));
00174 else
00175 {
00176 m_itemsVal.push_back("Error");
00177 global_vars.err << MSG_ERROR << "Error: Enumitem #" << 1 + m_noOfItems << " of " << getPtr() << " has wrong format. (If comma is used a second value is required.)\n";
00178 }
00179
00180 comma_i = 0;
00181 }
00182 else
00183 {
00184 std::string::size_type st,end;
00185
00186 st = start_i;
00187 while( st < i && whitespace.find( items[st]) != std::string::npos) ++st;
00188 end = i;
00189 while( end > start_i && whitespace.find( items[end]) != std::string::npos) --end;
00190
00191 if ( end>=st)
00192 {
00193 m_items.push_back(items.substr( st, end - st + 1));
00194 m_itemsVal.push_back(items.substr( st, end - st + 1));
00195 }
00196 }
00197 ++m_noOfItems;
00198 start_i = i+1;
00199 while (start_i < len && whitespace.find(items[start_i]) != std::string::npos) ++start_i;
00200 i = start_i-1;
00201 }
00202 else if ( items[i] == ',')
00203 {
00204 comma_i = i;
00205 }
00206 ++i;
00207 }
00208
00209 if( m_noOfItems == 0)
00210 global_vars.err << MSG_ERROR << "Error: No enumeration items found. Please check: " << getPtr() << ".\n";
00211
00212 bool filled_def_item = m_ptr->getAttribute( "DefaultItem")->getStatus() >= BON::AS_Here;
00213 std::string def_item = Dumper::xmlFilter( m_ptr->getAttribute("DefaultItem")->getStringValue());
00214
00215 bool found ( false);
00216 i = 0;
00217 while (i < m_noOfItems && !found)
00218 {
00219 if ( m_items[i] == def_item ) found = true;
00220 else ++i;
00221 }
00222
00223 if (found)
00224 m_noOfDefault = i;
00225 else if( filled_def_item)
00226 global_vars.err << MSG_ERROR << "Error: Default enumitem not found among enumerated items at " << getPtr() << ".\n";
00227
00228 return true;
00229 }
00230
00231
00232 std::string EnumAttributeRep::doDumpAttr(const std::string& owner)
00233 {
00234 if (m_noOfItems == 0) getMenuItems();
00235 std::string mmm = "";
00236
00237 std::string m_ref = getMetaRef( owner);
00238 std::string viewable = isViewable()?"": " viewable = \"no\"";
00239 mmm += indStr() + "<attrdef name=\"" + getName() + "\" metaref = \"" + m_ref + "\"" + viewable + " valuetype = \"enum\" defvalue = \"";
00240 if( m_noOfDefault < m_itemsVal.size())
00241 mmm += m_itemsVal[m_noOfDefault];
00242 else
00243 global_vars.err << MSG_ERROR << "Error: Default enumitem not found in vector of enumerated items. Please check: " << getPtr() << ".\n";
00244
00245 mmm += "\">\n";
00246
00247 ++ind;
00248
00249 std::string prompt = getPrompt();
00250 if ( !prompt.empty())
00251 mmm += indStr() + "<dispname>" + prompt + "</dispname>\n";
00252
00253 for(int i = 0; i < m_noOfItems; ++i)
00254 {
00255 mmm+= indStr() + "<enumitem dispname = \"" + m_items[i] + "\" value = \"" + m_itemsVal[i] + "\"></enumitem>\n";
00256 }
00257
00258 mmm += dumpHelp();
00259
00260 --ind;
00261 mmm += indStr() + "</attrdef>\n";
00262 return mmm;
00263 }
00264
00265
00266 std::string BoolAttributeRep::doDumpAttr(const std::string& owner)
00267 {
00268 std::string mmm = "";
00269 bool on;
00270 on = m_ptr->getAttribute("BooleanDefault")->getBooleanValue();
00271
00272 std::string m_ref = getMetaRef( owner);
00273 std::string viewable = isViewable()?"": " viewable = \"no\"";
00274
00275
00276 mmm += indStr() + "<attrdef name=\"" + getName() + "\" metaref = \"" + m_ref + "\"" + viewable + " valuetype = \"boolean\" defvalue = \"";
00277 if (on) mmm += "true";
00278 else mmm +="false";
00279 mmm += "\">\n";
00280
00281 ++ind;
00282
00283 std::string prompt = getPrompt();
00284 if ( !prompt.empty())
00285 mmm += indStr() + "<dispname>" + prompt + "</dispname>\n";
00286
00287 mmm += dumpHelp();
00288
00289 --ind;
00290 mmm += indStr() + "</attrdef>\n";
00291 return mmm;
00292 }
00293
00294
00295 std::string FieldAttributeRep::doDumpAttr(const std::string& owner)
00296 {
00297 std::string mmm = "";
00298 std::string val_type = m_ptr->getAttribute("DataType")->getStringValue();
00299 std::string def_val = Dumper::xmlFilter( m_ptr->getAttribute("FieldDefault")->getStringValue());
00300 std::string content_type = Dumper::xmlFilter( m_ptr->getAttribute("ContentType")->getStringValue());
00301 int multiline = m_ptr->getAttribute("Multiline")->getIntegerValue();
00302
00303 std::string m_ref = getMetaRef( owner);
00304 std::string viewable = isViewable()?"": " viewable = \"no\"";
00305
00306
00307 mmm += indStr() + "<attrdef name=\"" + getName() + "\" metaref = \"" + m_ref + "\"" + viewable + " valuetype = \"" + val_type + "\" defvalue = \"" + def_val + "\">\n";
00308
00309 ++ind;
00310
00311 std::string prompt = getPrompt();
00312 if ( !prompt.empty())
00313 mmm += indStr() + "<dispname>" + prompt + "</dispname>\n";
00314
00315 if( multiline > 1)
00316 {
00317 char mul[10];
00318 sprintf(mul, "%i", multiline);
00319 mmm += indStr() + "<regnode name=\"multiLine\" value = \"" + mul + "\"></regnode>\n";
00320 }
00321
00322 if( !content_type.empty())
00323 mmm += indStr() + "<regnode name=\"content-type\" value = \"" + content_type + "\"></regnode>\n";
00324
00325 mmm += dumpHelp();
00326
00327 --ind;
00328 mmm += indStr() + "</attrdef>\n";
00329 return mmm;
00330 }