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 "vtkCommonDataModelModule.h" // For export macro 00031 #include "vtkObject.h" 00032 00033 class vtkXMLDataParser; 00034 00035 class VTKCOMMONDATAMODEL_EXPORT vtkXMLDataElement : public vtkObject 00036 { 00037 public: 00038 vtkTypeMacro(vtkXMLDataElement,vtkObject); 00039 void PrintSelf(ostream& os, vtkIndent indent); 00040 static vtkXMLDataElement* New(); 00041 00043 00044 vtkGetStringMacro(Name); 00045 virtual void SetName (const char* _arg); 00047 00049 00050 vtkGetStringMacro(Id); 00051 vtkSetStringMacro(Id); 00053 00056 const char* GetAttribute(const char* name); 00057 00060 void SetAttribute(const char* name, const char* value); 00061 00063 00064 void SetCharacterData(const char* c, int length); 00065 void AddCharacterData(const char* c, size_t length); 00066 vtkGetStringMacro(CharacterData); 00068 00070 00072 int GetScalarAttribute(const char* name, int& value); 00073 int GetScalarAttribute(const char* name, float& value); 00074 int GetScalarAttribute(const char* name, double& value); 00075 int GetScalarAttribute(const char* name, unsigned long& value); 00077 00079 00083 void SetIntAttribute(const char* name, int value); 00084 void SetFloatAttribute(const char* name, float value); 00085 void SetDoubleAttribute(const char* name, double value); 00086 void SetUnsignedLongAttribute(const char* name, unsigned long value); 00088 00090 00092 int GetVectorAttribute(const char* name, int length, int* value); 00093 int GetVectorAttribute(const char* name, int length, float* value); 00094 int GetVectorAttribute(const char* name, int length, double* value); 00095 int GetVectorAttribute(const char* name, int length, unsigned long* value); 00097 00099 00100 void SetVectorAttribute(const char* name, int length, const int* value); 00101 void SetVectorAttribute(const char* name, int length, const float* value); 00102 void SetVectorAttribute(const char* name, int length, const double* value); 00103 void SetVectorAttribute(const char* name, int length, const unsigned long* value); 00105 00106 //BTX 00107 #ifdef VTK_TYPE_USE_LONG_LONG 00108 int GetScalarAttribute(const char* name, long long& value); 00109 int GetVectorAttribute(const char* name, int length, long long* value); 00110 void SetVectorAttribute(const char* name, int length, long long const* value); 00111 int GetScalarAttribute(const char* name, unsigned long long& value); 00112 int GetVectorAttribute(const char* name, int length, unsigned long long* value); 00113 void SetVectorAttribute(const char* name, int length, unsigned long long const* value); 00114 #endif 00115 #ifdef VTK_TYPE_USE___INT64 00116 int GetScalarAttribute(const char* name, __int64& value); 00117 int GetVectorAttribute(const char* name, int length, __int64* value); 00118 void SetVectorAttribute(const char* name, int length, __int64 const* value); 00119 int GetScalarAttribute(const char* name, unsigned __int64& value); 00120 int GetVectorAttribute(const char* name, int length, unsigned __int64* value); 00121 void SetVectorAttribute(const char* name, int length, unsigned __int64 const* value); 00122 #endif 00123 //ETX 00124 00127 int GetWordTypeAttribute(const char* name, int& value); 00128 00130 00131 vtkGetMacro(NumberOfAttributes, int); 00133 00135 const char* GetAttributeName(int idx); 00136 00139 const char* GetAttributeValue(int idx); 00140 00142 00143 virtual void RemoveAttribute(const char *name); 00144 virtual void RemoveAllAttributes(); 00146 00148 00149 vtkXMLDataElement* GetParent(); 00150 void SetParent(vtkXMLDataElement* parent); 00152 00154 virtual vtkXMLDataElement* GetRoot(); 00155 00157 int GetNumberOfNestedElements(); 00158 00160 vtkXMLDataElement* GetNestedElement(int index); 00161 00163 void AddNestedElement(vtkXMLDataElement* element); 00164 00166 virtual void RemoveNestedElement(vtkXMLDataElement *); 00167 00169 virtual void RemoveAllNestedElements(); 00170 00172 00175 vtkXMLDataElement* FindNestedElement(const char* id); 00176 vtkXMLDataElement* FindNestedElementWithName(const char* name); 00177 vtkXMLDataElement* FindNestedElementWithNameAndId( 00178 const char* name, const char* id); 00179 vtkXMLDataElement* FindNestedElementWithNameAndAttribute( 00180 const char* name, const char* att_name, const char* att_value); 00182 00185 vtkXMLDataElement* LookupElementWithName(const char* name); 00186 00188 vtkXMLDataElement* LookupElement(const char* id); 00189 00191 00193 vtkGetMacro(XMLByteIndex, vtkTypeInt64); 00194 vtkSetMacro(XMLByteIndex, vtkTypeInt64); 00196 00201 virtual int IsEqualTo(vtkXMLDataElement *elem); 00202 00206 virtual void DeepCopy(vtkXMLDataElement *elem); 00207 00209 00214 vtkSetClampMacro(AttributeEncoding,int,VTK_ENCODING_NONE,VTK_ENCODING_UNKNOWN); 00215 vtkGetMacro(AttributeEncoding, int); 00217 00219 00220 void PrintXML(ostream& os, vtkIndent indent); 00221 void PrintXML(const char* fname); 00223 00225 00230 vtkGetMacro(CharacterDataWidth,int); 00231 vtkSetMacro(CharacterDataWidth,int); 00233 00234 protected: 00235 vtkXMLDataElement(); 00236 ~vtkXMLDataElement(); 00237 00238 // The name of the element from the XML file. 00239 char* Name; 00240 // The value of the "id" attribute, if any was given. 00241 char* Id; 00242 00243 int CharacterDataWidth; 00244 00245 // Data inside of the tag's open and close. ie <X> character data </X> 00246 char* CharacterData; // Null terminated buffer. 00247 size_t CharacterDataBlockSize; // Allocation size if buffer needs expand 00248 size_t CharacterDataBufferSize; // Allocated size. 00249 size_t EndOfCharacterData; // Number of bytes used. 00250 00251 // Tags that have specialized character data handlers 00252 // can set this flag to improve performance. The default is unset. 00253 int IgnoreCharacterData; 00254 00255 // Get/Set the stream position of the elements inline data. 00256 vtkGetMacro(InlineDataPosition,vtkTypeInt64); 00257 vtkSetMacro(InlineDataPosition,vtkTypeInt64); 00258 // The offset into the XML stream where the inline data begins. 00259 vtkTypeInt64 InlineDataPosition; 00260 // The offset into the XML stream where the element begins. 00261 vtkTypeInt64 XMLByteIndex; 00262 00263 // The raw property name/value pairs read from the XML attributes. 00264 char** AttributeNames; 00265 char** AttributeValues; 00266 int NumberOfAttributes; 00267 int AttributesSize; 00268 int AttributeEncoding; 00269 00270 // The set of nested elements. 00271 int NumberOfNestedElements; 00272 int NestedElementsSize; 00273 vtkXMLDataElement** NestedElements; 00274 // The parent of this element. 00275 vtkXMLDataElement* Parent; 00276 00277 // Internal utility methods. 00278 vtkXMLDataElement* LookupElementInScope(const char* id); 00279 vtkXMLDataElement* LookupElementUpScope(const char* id); 00280 static int IsSpace(char c); 00281 void PrintCharacterData(ostream &os,vtkIndent indent); 00282 static void PrintWithEscapedData(ostream& os, const char* data); 00283 00284 //BTX 00285 friend class vtkXMLDataParser; 00286 friend class vtkXMLMaterialParser; 00287 //ETX 00288 00289 private: 00290 vtkXMLDataElement(const vtkXMLDataElement&); // Not implemented. 00291 void operator=(const vtkXMLDataElement&); // Not implemented. 00292 }; 00293 00294 //---------------------------------------------------------------------------- 00295 inline 00296 void vtkXMLDataElement::AddCharacterData(const char* data, size_t length) 00297 { 00298 if (this->IgnoreCharacterData){ return; } 00299 // This is the index where we start to put the new data at. 00300 size_t eod=this->EndOfCharacterData-1; 00301 // Check if the new data will write off the end. If it does 00302 // resize the character data buffer. 00303 this->EndOfCharacterData+=length; 00304 if (this->EndOfCharacterData>=this->CharacterDataBufferSize) 00305 { 00306 while(this->EndOfCharacterData>=this->CharacterDataBufferSize) 00307 { 00308 this->CharacterDataBufferSize+=this->CharacterDataBlockSize; 00309 } 00310 this->CharacterData 00311 = static_cast<char *>(realloc(this->CharacterData,this->CharacterDataBufferSize)); 00312 } 00313 // put the new data at the end of the buffer, and null terminate. 00314 char *pCD=this->CharacterData+eod; 00315 memmove(pCD,data,length); 00316 pCD[length]='\0'; 00317 return; 00318 } 00319 00320 #endif