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()
43 _node->removeChangeListener(this);
46 //----------------------------------------------------------------------------
47 void PropertyBasedElement::destroy()
52 // TODO check if really not in use anymore
53 if( _node->getParent() )
55 ->removeChild(_node->getName(), _node->getIndex());
58 //----------------------------------------------------------------------------
59 SGConstPropertyNode_ptr PropertyBasedElement::getProps() const
64 //----------------------------------------------------------------------------
65 SGPropertyNode_ptr PropertyBasedElement::getProps()
70 //----------------------------------------------------------------------------
71 bool PropertyBasedElement::hasDataProp(const std::string& name) const
73 return getDataProp<SGPropertyNode*>(name) != NULL;
76 //----------------------------------------------------------------------------
77 void PropertyBasedElement::removeDataProp(const std::string& name)
79 SGPropertyNode* node = getDataProp<SGPropertyNode*>(name);
81 _node->removeChild(node);
84 //----------------------------------------------------------------------------
85 static const std::string DATA_PREFIX("data-");
87 //----------------------------------------------------------------------------
88 std::string PropertyBasedElement::dataPropToAttrName(const std::string& name)
90 // http://www.w3.org/TR/html5/dom.html#attr-data-*
92 // 3. Insert the string data- at the front of name.
94 std::string attr_name;
95 for( std::string::const_iterator cur = name.begin();
99 // If name contains a "-" (U+002D) character followed by a lowercase ASCII
100 // letter, throw a SyntaxError exception and abort these steps.
103 std::string::const_iterator next = cur + 1;
104 if( next != name.end() && islower(*next) )
105 return std::string();
108 // For each uppercase ASCII letter in name, insert a "-" (U+002D)
109 // character before the character and replace the character with the same
110 // character converted to ASCII lowercase.
113 attr_name.push_back('-');
114 attr_name.push_back( tolower(*cur) );
117 attr_name.push_back( *cur );
119 return DATA_PREFIX + attr_name;
122 //----------------------------------------------------------------------------
123 std::string PropertyBasedElement::attrToDataPropName(const std::string& name)
125 // http://www.w3.org/TR/html5/dom.html#attr-data-*
127 // For each "-" (U+002D) character in the name that is followed by a
128 // lowercase ASCII letter, remove the "-" (U+002D) character and replace the
129 // character that followed it by the same character converted to ASCII
132 if( !boost::starts_with(name, DATA_PREFIX) )
133 return std::string();
135 std::string data_name;
136 for( std::string::const_iterator cur = name.begin() + DATA_PREFIX.length();
140 std::string::const_iterator next = cur + 1;
141 if( *cur == '-' && next != name.end() && islower(*next) )
143 data_name.push_back( toupper(*next) );
147 data_name.push_back(*cur);
152 } // namespace simgear