RavEngine
Loading...
Searching...
No Matches
SnSimpleXmlWriter.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SN_SIMPLE_XML_WRITER_H
30#define SN_SIMPLE_XML_WRITER_H
31
32#include "foundation/PxArray.h"
33#include "SnXmlMemoryPoolStreams.h"
34
35namespace physx { namespace Sn {
37 {
38 public:
39
41 {
43 TXmlWriterType& mWriter;
44 STagWatcher( const STagWatcher& inOther );
45 STagWatcher& operator-( const STagWatcher& inOther );
46 STagWatcher( TXmlWriterType& inWriter, const char* inTagName )
47 : mWriter( inWriter )
48 {
49 mWriter.beginTag( inTagName );
50 }
51 ~STagWatcher() { mWriter.endTag(); }
52 protected:
53 STagWatcher& operator=(const STagWatcher&);
54 };
55
56 virtual ~SimpleXmlWriter(){}
57 virtual void beginTag( const char* inTagname ) = 0;
58 virtual void endTag() = 0;
59 virtual void addAttribute( const char* inName, const char* inValue ) = 0;
60 virtual void writeContentTag( const char* inTag, const char* inContent ) = 0;
61 virtual void addContent( const char* inContent ) = 0;
62 virtual PxU32 tabCount() = 0;
63 private:
64 SimpleXmlWriter& operator=(const SimpleXmlWriter&);
65 };
66
67 template<typename TStreamType>
69 {
71 TStreamType& mStream;
73 SimpleXmlWriterImpl& operator=( const SimpleXmlWriterImpl& inOther );
75 bool mTagOpen;
76 PxU32 mInitialTagDepth;
77 public:
78
79 SimpleXmlWriterImpl( TStreamType& inStream, PxAllocatorCallback& inAllocator, PxU32 inInitialTagDepth = 0 )
80 : mWrapper( inAllocator )
81 , mStream( inStream )
82 , mTags( mWrapper )
83 , mTagOpen( false )
84 , mInitialTagDepth( inInitialTagDepth )
85 {
86 }
87 virtual ~SimpleXmlWriterImpl()
88 {
89 while( mTags.size() )
90 endTag();
91 }
92 PxU32 tabCount() { return mTags.size() + mInitialTagDepth; }
93
94 void writeTabs( PxU32 inSize )
95 {
96 inSize += mInitialTagDepth;
97 for ( PxU32 idx =0; idx < inSize; ++idx )
98 mStream << "\t";
99 }
100 void beginTag( const char* inTagname )
101 {
102 closeTag();
103 writeTabs(mTags.size());
104 mTags.pushBack( inTagname );
105 mStream << "<" << inTagname;
106 mTagOpen = true;
107 }
108 void addAttribute( const char* inName, const char* inValue )
109 {
110 PX_ASSERT( mTagOpen );
111 mStream << " " << inName << "=" << "\"" << inValue << "\"";
112 }
113 void closeTag(bool useNewline = true)
114 {
115 if ( mTagOpen )
116 {
117 mStream << " " << ">";
118 if (useNewline )
119 mStream << "\n";
120 }
121 mTagOpen = false;
122 }
123 void doEndOpenTag()
124 {
125 mStream << "</" << mTags.back() << ">" << "\n";
126 }
127 void endTag()
128 {
129 PX_ASSERT( mTags.size() );
130 if ( mTagOpen )
131 mStream << " " << "/>" << "\n";
132 else
133 {
134 writeTabs(mTags.size()-1);
135 doEndOpenTag();
136 }
137 mTagOpen = false;
138 mTags.popBack();
139 }
140
141 static bool IsNormalizableWhitespace(char c) { return c == 0x9 || c == 0xA || c == 0xD; }
142 static bool IsValidXmlCharacter(char c) { return IsNormalizableWhitespace(c) || c >= 0x20; }
143
144 void addContent( const char* inContent )
145 {
146 closeTag(false);
147 //escape xml
148 for( ; *inContent; inContent++ )
149 {
150 switch (*inContent)
151 {
152 case '<':
153 mStream << "&lt;";
154 break;
155 case '>':
156 mStream << "&gt;";
157 break;
158 case '&':
159 mStream << "&amp;";
160 break;
161 case '\'':
162 mStream << "&apos;";
163 break;
164 case '"':
165 mStream << "&quot;";
166 break;
167 default:
168 if (IsValidXmlCharacter(*inContent)) {
169 if (IsNormalizableWhitespace(*inContent))
170 {
171 char s[32];
172 Pxsnprintf(s, 32, "&#x%02X;", unsigned(*inContent));
173 mStream << s;
174 }
175 else
176 mStream << *inContent;
177 }
178 break;
179 }
180 }
181 }
182
183 void writeContentTag( const char* inTag, const char* inContent )
184 {
185 beginTag( inTag );
186 addContent( inContent );
187 doEndOpenTag();
188 mTags.popBack();
189 }
190 void insertXml( const char* inXml )
191 {
192 closeTag();
193 mStream << inXml;
194 }
195 };
196
197 struct BeginTag
198 {
199 const char* mTagName;
200 BeginTag( const char* inTagName )
201 : mTagName( inTagName ) { }
202 };
203
204 struct EndTag
205 {
206 EndTag() {}
207 };
208
209 struct Att
210 {
211 const char* mAttName;
212 const char* mAttValue;
213 Att( const char* inAttName, const char* inAttValue )
214 : mAttName( inAttName )
215 , mAttValue( inAttValue ) { }
216 };
217
218 struct Content
219 {
220 const char* mContent;
221 Content( const char* inContent )
222 : mContent( inContent ) { }
223 };
224
225
227 {
228 const char* mTagName;
229 const char* mContent;
230 ContentTag( const char* inTagName, const char* inContent )
231 : mTagName( inTagName )
232 , mContent( inContent ) { }
233 };
234
235 inline SimpleXmlWriter& operator<<( SimpleXmlWriter& inWriter, const BeginTag& inTag ) { inWriter.beginTag( inTag.mTagName ); return inWriter; }
236 inline SimpleXmlWriter& operator<<( SimpleXmlWriter& inWriter, const EndTag& inTag ) { PX_UNUSED(inTag); inWriter.endTag(); return inWriter; }
237 inline SimpleXmlWriter& operator<<( SimpleXmlWriter& inWriter, const Att& inTag ) { inWriter.addAttribute(inTag.mAttName, inTag.mAttValue); return inWriter; }
238 inline SimpleXmlWriter& operator<<( SimpleXmlWriter& inWriter, const Content& inTag ) { inWriter.addContent(inTag.mContent); return inWriter; }
239 inline SimpleXmlWriter& operator<<( SimpleXmlWriter& inWriter, const ContentTag& inTag ) { inWriter.writeContentTag(inTag.mTagName, inTag.mContent); return inWriter; }
240
241 inline void writeProperty( SimpleXmlWriter& inWriter, MemoryBuffer& tempBuffer, const char* inPropName )
242 {
243 PxU8 data = 0;
244 tempBuffer.write( &data, sizeof(PxU8) );
245 inWriter.writeContentTag( inPropName, reinterpret_cast<const char*>( tempBuffer.mBuffer ) );
246 tempBuffer.clear();
247 }
248
249 template<typename TDataType>
250 inline void writeProperty( SimpleXmlWriter& inWriter, MemoryBuffer& tempBuffer, const char* inPropName, TDataType inValue )
251 {
252 tempBuffer << inValue;
253 writeProperty( inWriter, tempBuffer, inPropName );
254 }
255} }
256#endif
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE T popBack()
Definition PxArray.h:311
PX_FORCE_INLINE const T & back() const
Definition PxArray.h:224
Definition SnSimpleXmlWriter.h:69
Definition SnSimpleXmlWriter.h:37
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition SnSimpleXmlWriter.h:210
Definition SnSimpleXmlWriter.h:198
Definition SnSimpleXmlWriter.h:227
Definition SnSimpleXmlWriter.h:219
Definition SnSimpleXmlWriter.h:205
Definition SnSimpleXmlWriter.h:41
Helper struct to encapsulate the user allocator callback Useful for array and hash templates.
Definition PxProfileAllocatorWrapper.h:46
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152