2003-09-11 12:26:53 +02:00
|
|
|
#ifndef ARABICA_ATTRIBUTES_LIST_IMPL_H
|
|
|
|
#define ARABICA_ATTRIBUTES_LIST_IMPL_H
|
2002-06-21 13:16:28 +02:00
|
|
|
// SAX default implementation for AttributeList.
|
|
|
|
// $Id$
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <SAX/ArabicaConfig.hpp>
|
|
|
|
#include <AttributeList.hpp>
|
2002-06-21 13:16:28 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2007-09-05 11:49:18 +02:00
|
|
|
namespace Arabica
|
|
|
|
{
|
2002-06-21 13:16:28 +02:00
|
|
|
namespace SAX
|
|
|
|
{
|
|
|
|
|
2007-09-06 00:08:51 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2002-06-21 13:16:28 +02:00
|
|
|
class Attribute
|
|
|
|
{
|
|
|
|
public:
|
2007-09-06 00:08:51 +02:00
|
|
|
Attribute(const string_type& name, const string_adaptor& type, const string_adaptor& value)
|
2002-06-21 13:16:28 +02:00
|
|
|
: name_(name), type_(type), value_(value)
|
|
|
|
{}
|
|
|
|
virtual ~Attribute() { }
|
|
|
|
|
|
|
|
public:
|
2007-09-06 00:08:51 +02:00
|
|
|
string_type name_;
|
|
|
|
const string_type& type_;
|
|
|
|
string_type value_;
|
2002-06-21 13:16:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const std::string empty_;
|
|
|
|
const std::string const types[] = { empty_, "CDATA", "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", "NOTATION" }; // TV
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation for AttributeList.
|
|
|
|
*
|
|
|
|
* <p>AttributeList implements the deprecated SAX1 {@link
|
2007-09-05 14:57:07 +02:00
|
|
|
* AttributeList AttributeList} interface, and has been
|
|
|
|
* replaced by the new SAX2 {@link AttributesImpl
|
2002-06-21 13:16:28 +02:00
|
|
|
* AttributesImpl} interface.</p>
|
|
|
|
*
|
|
|
|
* <p>This class provides a convenience implementation of the SAX
|
2007-09-05 14:57:07 +02:00
|
|
|
* {@link AttributeList AttributeList} interface. This
|
2002-06-21 13:16:28 +02:00
|
|
|
* implementation is useful both for SAX parser writers, who can use
|
|
|
|
* it to provide attributes to the application, and for SAX application
|
|
|
|
* writers, who can use it to create a persistent copy of an element's
|
|
|
|
* attribute specifications:</p>
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* private AttributeList myatts;
|
|
|
|
*
|
2007-09-06 00:08:51 +02:00
|
|
|
* void startElement(const string_type& name, const AttributeList& atts)
|
2002-06-21 13:16:28 +02:00
|
|
|
* {
|
|
|
|
* // create a persistent copy of the attribute list
|
|
|
|
* // for use outside this method
|
|
|
|
* AttributeListImpl myatts(atts);
|
|
|
|
* [...]
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p>Please note that SAX parsers are not required to use this
|
|
|
|
* class to provide an implementation of AttributeList; it is
|
|
|
|
* supplied only as an optional convenience. In particular,
|
|
|
|
* parser writers are encouraged to invent more efficient
|
|
|
|
* implementations.</p>
|
|
|
|
*
|
|
|
|
* @deprecated This class implements a deprecated interface,
|
2007-09-05 14:57:07 +02:00
|
|
|
* {@link AttributeList AttributeList};
|
2002-06-21 13:16:28 +02:00
|
|
|
* that interface has been replaced by
|
2007-09-05 14:57:07 +02:00
|
|
|
* {@link Attributes Attributes},
|
2002-06-21 13:16:28 +02:00
|
|
|
* which is implemented in the
|
2007-09-05 14:57:07 +02:00
|
|
|
* {@link AttributesImpl
|
2002-06-21 13:16:28 +02:00
|
|
|
* AttributesImpl} helper class.
|
|
|
|
* @since SAX 1.0
|
|
|
|
* @author Jez Higgins,
|
|
|
|
* <a href="mailto:jez@jezuk.co.uk">jez@jezuk.co.uk</a>
|
|
|
|
* @version 2.0
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList
|
|
|
|
* @see DocumentHandler#startElement
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
class AttributeListImpl : public AttributeList<string_type, string_adaptor>
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-09-05 14:57:07 +02:00
|
|
|
AttributeListImpl() : atts_() { }
|
|
|
|
AttributeListImpl(const AttributeList& atts)
|
2002-06-21 13:16:28 +02:00
|
|
|
: atts_(atts.getLength())
|
|
|
|
{
|
|
|
|
setAttributeList(atts);
|
|
|
|
} // AttributeListImpl
|
|
|
|
|
2007-09-05 14:57:07 +02:00
|
|
|
AttributeListImpl& operator=(const AttributeList& atts)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
setAttributeList(atts);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
} // operator=
|
|
|
|
|
2007-09-05 14:57:07 +02:00
|
|
|
virtual ~AttributeListImpl() { clear(); }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods specific to this class.
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Set the attribute list, discarding previous contents.
|
|
|
|
*
|
|
|
|
* <p>This method allows an application writer to reuse an
|
|
|
|
* attribute list easily.</p>
|
|
|
|
*
|
|
|
|
* @param atts The attribute list to copy.
|
|
|
|
*/
|
|
|
|
void setAttributeList(const AttributeList& atts)
|
|
|
|
{
|
|
|
|
int count = atts.getLength();
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
|
|
|
|
} // setAttributeList
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an attribute to an attribute list.
|
|
|
|
*
|
|
|
|
* <p>This method is provided for SAX parser writers, to allow them
|
|
|
|
* to build up an attribute list incrementally before delivering
|
|
|
|
* it to the application.</p>
|
|
|
|
*
|
|
|
|
* @param name The attribute name.
|
|
|
|
* @param type The attribute type ("NMTOKEN" for an enumeration).
|
2002-10-08 03:00:22 +02:00
|
|
|
* @param value The attribute value.
|
2002-06-21 13:16:28 +02:00
|
|
|
* @see #removeAttribute
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see DocumentHandler#startElement
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
void addAttribute(const string_type& name, const string_adaptor& type, const string_adaptor& value)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-06 00:08:51 +02:00
|
|
|
atts_.push_back(new Attribute<string_type, string_adaptor>(name, type, value)); // TV
|
2002-06-21 13:16:28 +02:00
|
|
|
} // addAttribute
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an attribute from the list.
|
|
|
|
*
|
|
|
|
* <p>SAX application writers can use this method to filter an
|
|
|
|
* attribute out of an AttributeList. Note that invoking this
|
|
|
|
* method will change the length of the attribute list and
|
|
|
|
* some of the attribute's indices.</p>
|
|
|
|
*
|
|
|
|
* <p>If the requested attribute is not in the list, this is
|
|
|
|
* a no-op.</p>
|
|
|
|
*
|
|
|
|
* @param name The attribute name.
|
|
|
|
* @see #addAttribute
|
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
void removeAttribute(const string_type& name)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int i = index(name);
|
|
|
|
|
|
|
|
if(i < 0) return;
|
|
|
|
delete atts_[i];
|
|
|
|
atts_.erase(atts_.begin() + i);
|
|
|
|
} // removeAttribute
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the attribute list.
|
|
|
|
*
|
|
|
|
* <p>SAX parser writers can use this method to reset the attribute
|
|
|
|
* list between DocumentHandler.startElement events. Normally,
|
|
|
|
* it will make sense to reuse the same AttributeListImpl object
|
|
|
|
* rather than allocating a new one each time.</p>
|
|
|
|
*
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see DocumentHandler#startElement
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
while(!atts_.empty())
|
|
|
|
{
|
|
|
|
delete atts_.back();
|
|
|
|
atts_.pop_back();
|
|
|
|
}
|
|
|
|
} // clear
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of AttributeList
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Return the number of attributes in the list.
|
|
|
|
*
|
|
|
|
* @return The number of attributes in the list.
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getLength
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
|
|
|
virtual int getLength() const
|
|
|
|
{
|
|
|
|
return atts_.size();
|
|
|
|
} // getLength
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of an attribute (by position).
|
|
|
|
*
|
|
|
|
* @param i The position of the attribute in the list.
|
2002-10-08 03:00:22 +02:00
|
|
|
* @return The attribute name as a string, or an empty string if there
|
2002-06-21 13:16:28 +02:00
|
|
|
* is no attribute at that position.
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getName(int)
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
virtual const string_type& getName(int i) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(i > atts_.size())
|
|
|
|
return empty_;
|
|
|
|
return atts_[i]->name_;
|
|
|
|
} // getName
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of an attribute (by position).
|
|
|
|
*
|
|
|
|
* @param i The position of the attribute in the list.
|
|
|
|
* @return The attribute type as a string ("NMTOKEN" for an
|
|
|
|
* enumeration, and "CDATA" if no declaration was
|
2002-10-08 03:00:22 +02:00
|
|
|
* read), or an empty string if there is no attribute at
|
2002-06-21 13:16:28 +02:00
|
|
|
* that position.
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getType(int)
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
virtual const string_type& getType(int i) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(i > atts_.size())
|
|
|
|
return empty_;
|
|
|
|
return atts_[i]->type_;
|
|
|
|
} // getType
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of an attribute (by position).
|
|
|
|
*
|
|
|
|
* @param i The position of the attribute in the list.
|
2002-10-08 03:00:22 +02:00
|
|
|
* @return The attribute value as a string, or an empty string if
|
2002-06-21 13:16:28 +02:00
|
|
|
* there is no attribute at that position.
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getValue(int)
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
virtual const string_type& getValue(int i) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(i > atts_.size())
|
|
|
|
return empty_;
|
|
|
|
return atts_[i]->value_;
|
|
|
|
} // getValue
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of an attribute (by name).
|
|
|
|
*
|
|
|
|
* @param name The attribute name.
|
|
|
|
* @return The attribute type as a string ("NMTOKEN" for an
|
|
|
|
* enumeration, and "CDATA" if no declaration was
|
|
|
|
* read).
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getType(java.lang.String)
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
virtual const string_type& getType(const string_adaptor& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int i = index(name);
|
|
|
|
return i < 0 ? empty_ : getType(i);
|
|
|
|
} // getType
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of an attribute (by name).
|
|
|
|
*
|
|
|
|
* @param name The attribute name.
|
2007-09-05 14:57:07 +02:00
|
|
|
* @see AttributeList#getValue(java.lang.String)
|
2002-06-21 13:16:28 +02:00
|
|
|
*/
|
2007-09-06 00:08:51 +02:00
|
|
|
virtual const string_type& getValue(const string_adaptor& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int i = index(name);
|
|
|
|
return i < 0 ? empty_ : getValue(i);
|
|
|
|
} // getValue
|
|
|
|
|
|
|
|
private:
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Internal state.
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-09-06 00:08:51 +02:00
|
|
|
std::vector<Attribute<string_type, string_adaptor> *> atts_;
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2007-09-06 00:08:51 +02:00
|
|
|
int index(const string_type& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int i = 0, res = -1;
|
2007-09-06 00:08:51 +02:00
|
|
|
std::vector<Attribute<string_type, string_adaptor>*>::const_iterator iter;
|
2002-06-21 13:16:28 +02:00
|
|
|
for (iter = atts_.begin() ; iter != atts_.end() ; i++, iter++)
|
|
|
|
if ((*iter)->name_ == name) {
|
|
|
|
res = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} // index
|
|
|
|
|
2007-09-05 14:57:07 +02:00
|
|
|
bool operator==(const AttributeList&) const; // not implemented
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class AttributeListImpl
|
|
|
|
|
2007-09-05 11:49:18 +02:00
|
|
|
} // namespace SAX
|
|
|
|
} // namespace Arabica
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
// end of file
|
|
|
|
|