1 // Base class for elements of property controlled subsystems
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "PropertyBasedElement.hxx"
20 #include <boost/algorithm/string/predicate.hpp>
25 //----------------------------------------------------------------------------
26 PropertyBasedElement::PropertyBasedElement(SGPropertyNode* node):
29 _node->addChangeListener(this);
32 //----------------------------------------------------------------------------
33 PropertyBasedElement::~PropertyBasedElement()
39 //----------------------------------------------------------------------------
40 void PropertyBasedElement::removeListener()
42 _node->removeChangeListener(this);
45 //----------------------------------------------------------------------------
46 void PropertyBasedElement::destroy()
51 // TODO check if really not in use anymore
52 if( _node->getParent() )
54 ->removeChild(_node->getName(), _node->getIndex());
57 //----------------------------------------------------------------------------
58 SGConstPropertyNode_ptr PropertyBasedElement::getProps() const
63 //----------------------------------------------------------------------------
64 SGPropertyNode_ptr PropertyBasedElement::getProps()
69 //----------------------------------------------------------------------------
70 bool PropertyBasedElement::hasDataProp(const std::string& name) const
72 return getDataProp<SGPropertyNode*>(name) != NULL;
75 //----------------------------------------------------------------------------
76 void PropertyBasedElement::removeDataProp(const std::string& name)
78 SGPropertyNode* node = getDataProp<SGPropertyNode*>(name);
80 _node->removeChild(node);
83 //----------------------------------------------------------------------------
84 static const std::string DATA_PREFIX("data-");
86 //----------------------------------------------------------------------------
87 std::string PropertyBasedElement::dataPropToAttrName(const std::string& name)
89 // http://www.w3.org/TR/html5/dom.html#attr-data-*
91 // 3. Insert the string data- at the front of name.
93 std::string attr_name;
94 for( std::string::const_iterator cur = name.begin();
98 // If name contains a "-" (U+002D) character followed by a lowercase ASCII
99 // letter, throw a SyntaxError exception and abort these steps.
102 std::string::const_iterator next = cur + 1;
103 if( next != name.end() && islower(*next) )
104 return std::string();
107 // For each uppercase ASCII letter in name, insert a "-" (U+002D)
108 // character before the character and replace the character with the same
109 // character converted to ASCII lowercase.
112 attr_name.push_back('-');
113 attr_name.push_back( tolower(*cur) );
116 attr_name.push_back( *cur );
118 return DATA_PREFIX + attr_name;
121 //----------------------------------------------------------------------------
122 std::string PropertyBasedElement::attrToDataPropName(const std::string& name)
124 // http://www.w3.org/TR/html5/dom.html#attr-data-*
126 // For each "-" (U+002D) character in the name that is followed by a
127 // lowercase ASCII letter, remove the "-" (U+002D) character and replace the
128 // character that followed it by the same character converted to ASCII
131 if( !boost::starts_with(name, DATA_PREFIX) )
132 return std::string();
134 std::string data_name;
135 for( std::string::const_iterator cur = name.begin() + DATA_PREFIX.length();
139 std::string::const_iterator next = cur + 1;
140 if( *cur == '-' && next != name.end() && islower(*next) )
142 data_name.push_back( toupper(*next) );
146 data_name.push_back(*cur);
151 } // namespace simgear