Commit d02e4fb3 authored by Sebastien Barre's avatar Sebastien Barre
Browse files

ENH: bug fix, character data was ignored

parent aae4b810
......@@ -20,7 +20,7 @@
#include <ctype.h>
vtkCxxRevisionMacro(vtkXMLDataElement, "1.24");
vtkCxxRevisionMacro(vtkXMLDataElement, "1.24.6.1");
vtkStandardNewMacro(vtkXMLDataElement);
//----------------------------------------------------------------------------
......@@ -43,6 +43,7 @@ vtkXMLDataElement::vtkXMLDataElement()
this->XMLByteIndex = 0;
this->AttributeEncoding = VTK_ENCODING_UTF_8;
this->CharacterData = 0;
}
//----------------------------------------------------------------------------
......@@ -57,6 +58,7 @@ vtkXMLDataElement::~vtkXMLDataElement()
this->RemoveAllNestedElements();
delete [] this->NestedElements;
this->SetCharacterData(0, 0);
}
//----------------------------------------------------------------------------
......@@ -116,6 +118,52 @@ void vtkXMLDataElement::ReadXMLAttributes(const char** atts, int encoding)
}
}
//----------------------------------------------------------------------------
void vtkXMLDataElement::SetCharacterData(const char* c, int length)
{
if (this->CharacterData)
{
delete [] this->CharacterData;
this->CharacterData = 0;
}
if (c && length > 0)
{
this->CharacterData = new char[length + 1];
strncpy(this->CharacterData, c, length);
this->CharacterData[length] = 0;
}
this->Modified();
}
//----------------------------------------------------------------------------
void vtkXMLDataElement::AddCharacterData(const char* c, int length)
{
if (!c || length <= 0)
{
return;
}
char* old_data = this->CharacterData;
int old_length = (old_data)? strlen(old_data): 0;
int total_length = old_length + length;
this->CharacterData = new char[total_length + 1];
this->CharacterData[0] = 0;
if (old_length > 0)
{
strncpy(this->CharacterData, old_data, old_length);
this->CharacterData[old_length] = 0;
}
strncat(this->CharacterData, c, length);
this->CharacterData[total_length] = 0;
if (old_data)
{
delete [] old_data;
}
}
//----------------------------------------------------------------------------
void vtkXMLDataElement::SetAttribute(const char *name, const char *value)
{
......@@ -729,12 +777,17 @@ int vtkXMLDataElement::GetWordTypeAttribute(const char* name, int& value)
return 0;
#endif
}
else if (strcmp(v, "String") == 0)
{
value = VTK_STRING;
return 1;
}
else
{
vtkErrorMacro("Unknown data type \"" << v << "\". Supported types are:\n"
"Int8, Int16, Int32, Int64,\n"
"UInt8, UInt16, UInt32, UInt64,\n"
"Float32, Float64\n");
"Float32, Float64, String\n");
return 0;
}
}
......@@ -877,8 +930,11 @@ int vtkXMLDataElement::IsEqualTo(vtkXMLDataElement *elem)
if (this->GetNumberOfAttributes() != elem->GetNumberOfAttributes() ||
this->GetNumberOfNestedElements() != elem->GetNumberOfNestedElements() ||
(this->GetName() != elem->GetName() &&
(!this->GetName() || !elem->GetName() || strcmp(this->GetName(),
elem->GetName()))))
(!this->GetName() || !elem->GetName() ||
strcmp(this->GetName(), elem->GetName()))) ||
(this->GetCharacterData() != elem->GetCharacterData() &&
(!this->GetCharacterData() || !elem->GetCharacterData() ||
strcmp(this->GetCharacterData(), elem->GetCharacterData()))))
{
return 0;
}
......@@ -920,6 +976,8 @@ void vtkXMLDataElement::DeepCopy(vtkXMLDataElement *elem)
this->SetId(elem->GetId());
this->SetXMLByteIndex(elem->GetXMLByteIndex());
this->SetAttributeEncoding(elem->GetAttributeEncoding());
const char *elem_cdata = elem->GetCharacterData();
this->SetCharacterData(elem_cdata, elem_cdata ? strlen(elem_cdata) : 0);
// Copy attributes
......@@ -954,5 +1012,7 @@ void vtkXMLDataElement::PrintSelf(ostream& os, vtkIndent indent)
os << indent << "Id: " << (this->Id? this->Id : "(none)") << "\n";
os << indent << "NumberOfAttributes: " << this->NumberOfAttributes << "\n";
os << indent << "AttributeEncoding: " << this->AttributeEncoding << "\n";
os << indent << "CharacterData: " <<
(this->CharacterData? this->CharacterData : "(null)") << endl;
}
......@@ -55,7 +55,13 @@ public:
// Set the attribute with the given name and value. If it doesn't exist,
// adds it.
void SetAttribute(const char* name, const char* value);
// Description:
// Set/Get the character data between XML start/end tags.
void SetCharacterData(const char* c, int length);
void AddCharacterData(const char* c, int length);
vtkGetStringMacro(CharacterData);
// Description:
// Get the attribute with the given name and converted to a scalar
// value. Returns whether value was extracted.
......@@ -169,9 +175,10 @@ public:
vtkSetMacro(XMLByteIndex, unsigned long);
// Description:
// Check if the instance has the same name, attributes and nested elements
// contents than the given element (this method is applied recursively
// on the nested elements, and they must be stored in the same order).
// Check if the instance has the same name, attributes, character data
// and nested elements contents than the given element (this method is
// applied recursively on the nested elements, and they must be stored in
// the same order).
// Warning: Id, Parent, XMLByteIndex are ignored.
virtual int IsEqualTo(vtkXMLDataElement *elem);
......@@ -202,6 +209,9 @@ protected:
// The value of the "id" attribute, if any was given.
char* Id;
// The character data between the start and end tags of this element.
char* CharacterData;
// The offset into the XML stream where the element begins.
unsigned long XMLByteIndex;
......@@ -236,6 +246,7 @@ protected:
//BTX
friend class vtkXMLDataParser;
friend class vtkXMLMaterialParser;
//ETX
private:
......
......@@ -29,7 +29,7 @@
typedef vtkstd::vector<vtkXMLDataElement*> vtkXMLUtilitiesDataElementContainer;
vtkStandardNewMacro(vtkXMLUtilities);
vtkCxxRevisionMacro(vtkXMLUtilities, "1.6");
vtkCxxRevisionMacro(vtkXMLUtilities, "1.6.6.1");
#define VTK_XML_UTILITIES_FACTORED_POOL_NAME "FactoredPool"
#define VTK_XML_UTILITIES_FACTORED_NAME "Factored"
......@@ -303,16 +303,31 @@ void vtkXMLUtilities::FlattenElement(vtkXMLDataElement *elem,
}
}
// Nested elements and close
const char *cdata = elem->GetCharacterData();
int nb_nested = elem->GetNumberOfNestedElements();
if (!nb_nested)
int need_close_tag = (nb_nested || cdata);
if (!need_close_tag)
{
os << "/>";
}
else
{
os << '>';
}
// cdata
if (cdata)
{
vtkXMLUtilities::EncodeString(
cdata, elem->GetAttributeEncoding(), os, VTK_ENCODING_UTF_8, 1);
}
// Nested elements
if (nb_nested)
{
if (indent)
{
os << '\n';
......@@ -334,8 +349,15 @@ void vtkXMLUtilities::FlattenElement(vtkXMLDataElement *elem,
{
os << *indent;
}
}
// Close
if (need_close_tag)
{
os << "</" << elem->GetName() << '>';
}
if (indent)
{
os << '\n';
......@@ -581,12 +603,14 @@ int vtkXMLUtilities::FactorElementsInternal(vtkXMLDataElement *tree,
{
similar_trees[i]->RemoveAllAttributes();
similar_trees[i]->RemoveAllNestedElements();
similar_trees[i]->SetCharacterData(NULL, 0);
similar_trees[i]->SetName(VTK_XML_UTILITIES_FACTORED_REF_NAME);
similar_trees[i]->SetAttribute("Id", id.str());
}
tree->RemoveAllAttributes();
tree->RemoveAllNestedElements();
tree->SetCharacterData(NULL, 0);
tree->SetName(VTK_XML_UTILITIES_FACTORED_REF_NAME);
tree->SetAttribute("Id", id.str());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment