VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkXMLDataElement.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00027 #ifndef __vtkXMLDataElement_h 00028 #define __vtkXMLDataElement_h 00029 00030 #include "vtkObject.h" 00031 00032 class vtkXMLDataParser; 00033 00034 class VTK_COMMON_EXPORT vtkXMLDataElement : public vtkObject 00035 { 00036 public: 00037 vtkTypeMacro(vtkXMLDataElement,vtkObject); 00038 void PrintSelf(ostream& os, vtkIndent indent); 00039 static vtkXMLDataElement* New(); 00040 00042 00043 vtkGetStringMacro(Name); 00044 virtual void SetName (const char* _arg); 00046 00048 00049 vtkGetStringMacro(Id); 00050 vtkSetStringMacro(Id); 00052 00055 const char* GetAttribute(const char* name); 00056 00059 void SetAttribute(const char* name, const char* value); 00060 00062 00063 void SetCharacterData(const char* c, int length); 00064 void AddCharacterData(const char* c, size_t length); 00065 vtkGetStringMacro(CharacterData); 00067 00069 00071 int GetScalarAttribute(const char* name, int& value); 00072 int GetScalarAttribute(const char* name, float& value); 00073 int GetScalarAttribute(const char* name, double& value); 00074 int GetScalarAttribute(const char* name, unsigned long& value); 00076 00078 00082 void SetIntAttribute(const char* name, int value); 00083 void SetFloatAttribute(const char* name, float value); 00084 void SetDoubleAttribute(const char* name, double value); 00085 void SetUnsignedLongAttribute(const char* name, unsigned long value); 00087 00089 00091 int GetVectorAttribute(const char* name, int length, int* value); 00092 int GetVectorAttribute(const char* name, int length, float* value); 00093 int GetVectorAttribute(const char* name, int length, double* value); 00094 int GetVectorAttribute(const char* name, int length, unsigned long* value); 00096 00098 00099 void SetVectorAttribute(const char* name, int length, const int* value); 00100 void SetVectorAttribute(const char* name, int length, const float* value); 00101 void SetVectorAttribute(const char* name, int length, const double* value); 00102 void SetVectorAttribute(const char* name, int length, const unsigned long* value); 00104 00105 #ifdef VTK_USE_64BIT_IDS 00106 //BTX 00107 int GetScalarAttribute(const char* name, vtkIdType& value); 00108 void SetIdTypeAttribute(const char* name, vtkIdType value); 00109 int GetVectorAttribute(const char* name, int length, vtkIdType* value); 00110 void SetVectorAttribute(const char* name, int length, const vtkIdType* value); 00111 //ETX 00112 #endif 00113 00116 int GetWordTypeAttribute(const char* name, int& value); 00117 00119 00120 vtkGetMacro(NumberOfAttributes, int); 00122 00124 const char* GetAttributeName(int idx); 00125 00128 const char* GetAttributeValue(int idx); 00129 00131 00132 virtual void RemoveAttribute(const char *name); 00133 virtual void RemoveAllAttributes(); 00135 00137 00138 vtkXMLDataElement* GetParent(); 00139 void SetParent(vtkXMLDataElement* parent); 00141 00143 virtual vtkXMLDataElement* GetRoot(); 00144 00146 int GetNumberOfNestedElements(); 00147 00149 vtkXMLDataElement* GetNestedElement(int index); 00150 00152 void AddNestedElement(vtkXMLDataElement* element); 00153 00155 virtual void RemoveNestedElement(vtkXMLDataElement *); 00156 00158 virtual void RemoveAllNestedElements(); 00159 00161 00164 vtkXMLDataElement* FindNestedElement(const char* id); 00165 vtkXMLDataElement* FindNestedElementWithName(const char* name); 00166 vtkXMLDataElement* FindNestedElementWithNameAndId( 00167 const char* name, const char* id); 00168 vtkXMLDataElement* FindNestedElementWithNameAndAttribute( 00169 const char* name, const char* att_name, const char* att_value); 00171 00174 vtkXMLDataElement* LookupElementWithName(const char* name); 00175 00177 vtkXMLDataElement* LookupElement(const char* id); 00178 00180 00182 vtkGetMacro(XMLByteIndex, unsigned long); 00183 vtkSetMacro(XMLByteIndex, unsigned long); 00185 00190 virtual int IsEqualTo(vtkXMLDataElement *elem); 00191 00195 virtual void DeepCopy(vtkXMLDataElement *elem); 00196 00198 00203 vtkSetClampMacro(AttributeEncoding,int,VTK_ENCODING_NONE,VTK_ENCODING_UNKNOWN); 00204 vtkGetMacro(AttributeEncoding, int); 00206 00208 00209 void PrintXML(ostream& os, vtkIndent indent); 00210 void PrintXML(const char* fname); 00212 00214 00219 vtkGetMacro(CharacterDataWidth,int); 00220 vtkSetMacro(CharacterDataWidth,int); 00222 00223 protected: 00224 vtkXMLDataElement(); 00225 ~vtkXMLDataElement(); 00226 00227 // The name of the element from the XML file. 00228 char* Name; 00229 // The value of the "id" attribute, if any was given. 00230 char* Id; 00231 00232 int CharacterDataWidth; 00233 00234 // Data inside of the tag's open and close. ie <X> character data </X> 00235 char* CharacterData; // Null terminated buffer. 00236 size_t CharacterDataBlockSize; // Allocation size if buffer needs expand 00237 size_t CharacterDataBufferSize; // Allocated size. 00238 size_t EndOfCharacterData; // Number of bytes used. 00239 00240 // Tags that have specialized character data handlers 00241 // can set this flag to improve performance. The default is unset. 00242 int IgnoreCharacterData; 00243 00244 // Get/Set the stream position of the elements inline data. 00245 vtkGetMacro(InlineDataPosition,unsigned long); 00246 vtkSetMacro(InlineDataPosition,unsigned long); 00247 // The offset into the XML stream where the inline data begins. 00248 unsigned long InlineDataPosition; 00249 // The offset into the XML stream where the element begins. 00250 unsigned long XMLByteIndex; 00251 00252 // The raw property name/value pairs read from the XML attributes. 00253 char** AttributeNames; 00254 char** AttributeValues; 00255 int NumberOfAttributes; 00256 int AttributesSize; 00257 int AttributeEncoding; 00258 00259 // The set of nested elements. 00260 int NumberOfNestedElements; 00261 int NestedElementsSize; 00262 vtkXMLDataElement** NestedElements; 00263 // The parent of this element. 00264 vtkXMLDataElement* Parent; 00265 00266 // Internal utility methods. 00267 vtkXMLDataElement* LookupElementInScope(const char* id); 00268 vtkXMLDataElement* LookupElementUpScope(const char* id); 00269 static int IsSpace(char c); 00270 void PrintCharacterData(ostream &os,vtkIndent indent); 00271 static void PrintWithEscapedData(ostream& os, const char* data); 00272 00273 //BTX 00274 friend class vtkXMLDataParser; 00275 friend class vtkXMLMaterialParser; 00276 //ETX 00277 00278 private: 00279 vtkXMLDataElement(const vtkXMLDataElement&); // Not implemented. 00280 void operator=(const vtkXMLDataElement&); // Not implemented. 00281 }; 00282 00283 //---------------------------------------------------------------------------- 00284 inline 00285 void vtkXMLDataElement::AddCharacterData(const char* data, size_t length) 00286 { 00287 if (this->IgnoreCharacterData){ return; } 00288 // This is the index where we start to put the new data at. 00289 size_t eod=this->EndOfCharacterData-1; 00290 // Check if the new data will write off the end. If it does 00291 // resize the character data buffer. 00292 this->EndOfCharacterData+=length; 00293 if (this->EndOfCharacterData>=this->CharacterDataBufferSize) 00294 { 00295 while(this->EndOfCharacterData>=this->CharacterDataBufferSize) 00296 { 00297 this->CharacterDataBufferSize+=this->CharacterDataBlockSize; 00298 } 00299 this->CharacterData 00300 = static_cast<char *>(realloc(this->CharacterData,this->CharacterDataBufferSize)); 00301 } 00302 // put the new data at the end of the buffer, and null terminate. 00303 char *pCD=this->CharacterData+eod; 00304 memmove(pCD,data,length); 00305 pCD[length]='\0'; 00306 return; 00307 } 00308 00309 #endif