wxArt2D
xmlparsdoc.cpp
1 /*!
2  \file canvas/src/xmlpars.cpp
3 
4  \brief a2dCanvasDocument - XML reader via Expat
5  \author Klaas Holwerda
6 
7  Copyright: 2001-2004 (C) Klaas Holwerda
8 
9  Licence: wxWidgets licence
10 
11  RCS-ID: $Id: xmlpars.cpp,v 1.70 2009/07/24 16:35:01 titato Exp $
12 */
13 
14 #include "a2dprec.h"
15 
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19 
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif
23 
24 #include "a2dprivate.h"
25 
26 #if wxART2D_USE_CVGIO
27 
28 #include "wx/canvas/canglob.h"
29 #include "wx/canvas/algos.h"
30 #include "wx/canvas/cameleon.h"
31 #include "wx/editor/candoc.h"
32 #include "wx/editor/xmlparsdoc.h"
33 
34 //----------------------------------------------------------------------------
35 // a2dIOHandlerDocCVGIn
36 //----------------------------------------------------------------------------
37 
39 {
40  // stay compatible for the moment
41  m_objectCreate[ wxT( "a2dOneColourFill" ) ] = wxT( "a2dFill" );
42  m_objectCreate[ wxT( "a2dLinearGradientFill" ) ] = wxT( "a2dFill" );
43  m_objectCreate[ wxT( "a2dRadialGradientFill" ) ] = wxT( "a2dFill" );
44  m_objectCreate[ wxT( "a2dOneColourStroke" ) ] = wxT( "a2dStroke" );
45  m_docClassInfo = &a2dCanvasDocument::ms_classInfo;
46 }
47 
48 
50 {
51 }
52 
53 bool a2dIOHandlerDocCVGIn::CanLoad( a2dDocumentInputStream& stream, const wxObject* obj, wxClassInfo* docClassInfo )
54 {
55  if ( obj && !wxDynamicCast( obj, a2dCanvasDocument ) && !wxDynamicCast( obj, a2dLayers ) )
56  return false;
57 
58  if ( docClassInfo && m_docClassInfo && !docClassInfo->IsKindOf( m_docClassInfo ) )
59  return false;
60 
61  m_streami = &stream;
62  SeekI( 0 );
63  char cheader[170];
64  cheader[169] = 0;
65 
66  Read( cheader, 169 );
67  SeekI( 0 );
68 
69  // XML header:
70  if ( memcmp( cheader, "<?xml ", 6 ) == 0 )
71  {
72  wxString buf = XmlDecodeStringToString( cheader, 169 );
73  // <cvg classname="a2dCanvasDocument">
74 
75  int pos;
76  pos = buf.Find( wxT( "<cvg" ) );
77  if ( pos == wxNOT_FOUND )
78  return false;
79 
80  pos = buf.Find( wxT( "classname" ) );
81  if ( pos == wxNOT_FOUND )
82  return false;
83 
84 #if wxUSE_STD_IOSTREAM
85  return ( m_streami->fail() || m_streami->bad() ) == 0;
86 #else
87  return true;
88 #endif
89  }
90 
91  return false;
92 }
93 
95 {
96  bool oke = true;
97 
98  m_streami = &stream;
99 
100  InitializeLoad();
101 
102  try
103  {
104 
105  Next();
106  Require( START_TAG, wxT( "cvg" ) );
107  Next();
108  if ( GetTagName() == wxT( "title" ) )
109  {
110  Next();
111  Require( END_TAG, wxT( "title" ) );
112  Next();
113  }
114  if ( GetTagName() == wxT( "desc" ) )
115  {
116  Next();
117  Require( END_TAG, wxT( "desc" ) );
118  Next();
119  }
120 
121  if ( GetTagName() == wxT( "o" ) && GetAttributeValue( wxT( "classname" ) ) == wxT( "a2dLayers" ) )
122  {
123  //add ON top existing setup
124  layers->Load( NULL, *this );
125  }
126 
127  Require( END_TAG, wxT( "cvg" ) );
128  Next();
129 
130  //now resolve references on id.
131  //CVG links on Id's
132  LinkReferences();
133  layers->UpdateIndexes();
134  }
135  catch ( a2dIOHandlerXMLException& error ) // error from the parser: process here
136  {
137  a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _( "CVG parsing error: '%s' at line %d column %d" ), error.getMessage().c_str(), error.getLineNumber(), error.getColumnNumber() );
138  oke = false;
139  }
140 
141  ResetLoad();
142  return oke;
143 }
144 
145 
146 
148 {
149  a2dCanvasObject* readobject;
150  m_streami = &stream;
151 
152  bool oke = true;
153 
154  InitializeLoad();
155 
156  try
157  {
158  Next();
159  Require( START_TAG, wxT( "cvg" ) );
160  Next();
161  if ( GetTagName() == wxT( "title" ) )
162  {
163  doc->SetTitle( GetContent() );
164  Next();
165  Require( END_TAG, wxT( "title" ) );
166  Next();
167  }
168  if ( GetTagName() == wxT( "desc" ) )
169  {
170  doc->SetDescription( GetContent() );
171  Next();
172  Require( END_TAG, wxT( "desc" ) );
173  Next();
174  }
175 
176  if ( GetTagName() == wxT( "a2dLayers" ) )
177  {
178  //add ON top existing setup
179  m_doc->GetLayerSetup()->Load( NULL, *this );
180  }
181 
182  //this way allows empty documents also
183  //Require( START_TAG, "canvasobject" );
184 
185  while( GetEventType() == START_TAG && GetTagName() == wxT( "o" ) )
186  {
187  wxString classname = GetAttributeValue( wxT( "classname" ) );
188  readobject = ( a2dCanvasObject* ) CreateObject( classname );
189  if ( !readobject )
190  {
191  a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _( "could not create a2dCanvasObject %s, will be skipped line %d" ),
192  GetAttributeValue( wxT( "classname" ) ).c_str(), GetCurrentLineNumber() );
193  return false;
194  }
195  else
196  {
197  parent->Append( readobject );
198  readobject->Load( NULL, *this );
199  }
200  }
201 
202  Require( END_TAG, wxT( "cvg" ) );
203  Next();
204 
205  //now resolve references on id.
206  //CVG links on Id's
207  LinkReferences();
208  }
209  catch ( a2dIOHandlerXMLException& error ) // error from the parser: process here
210  {
211  a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _( "CVG parsing error: '%s' at line %d column %d" ), error.getMessage().c_str(), error.getLineNumber(), error.getColumnNumber() );
212  oke = false;
213  }
214 
215  ResetLoad();
216 
217  return oke;
218 }
219 
220 //----------------------------------------------------------------------------
221 // a2dIOHandlerDocCVGOut
222 //----------------------------------------------------------------------------
223 
225 {
226 }
227 
228 
230 {
231 }
232 
234 {
235  m_streamo = &stream;
236 
237 #if wxUSE_STD_IOSTREAM
238  if ( stream.fail() || stream.bad() )
239  {
240 #else
241  if ( !stream.IsOk() )
242  {
243 #endif
244  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "Sorry, could not open stream for saving" ) );
245  return false;
246  }
247  InitializeSave();
248 
249  //check flag is used for writing once an object which is referenced many times
250  a2dWalker_SetCheck setp( false );
251  setp.Start( layers );
252 
253  WriteStartDocument( wxT( "1.0" ), wxT( "UTF-8" ) , true );
254 
255  WriteStartElementAttributes( wxT( "cvg" ) );
256  WriteAttribute( wxT( "classname" ), GetClassInfo()->GetClassName() );
258 
259  WriteElement( wxT( "title" ), wxT( "layersettings" ) );
260  WriteElement( wxT( "desc" ), wxT( "description" ) );
261 
262  a2dObjectList towritelayer;
263  layers->Save( NULL, *this, &towritelayer );
264 
265  WriteEndElement();
267 
268  ResetSave();
269 
270  return true;
271 }
272 
274 {
275  m_doc = ( a2dCanvasDocument* ) doc;
276  m_streamo = &stream;
277 
278 #if wxUSE_STD_IOSTREAM
279  if ( stream.fail() || stream.bad() )
280  {
281 #else
282  if ( !stream.IsOk() )
283  {
284 #endif
285  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "Sorry, could not open stream for saving" ) );
286  return false;
287  }
288 
289  InitializeSave();
290  m_doc->Save( *this, start );
291  ResetSave();
292 
293  return true;
294 }
295 
296 void a2dIOHandlerDocCVGOut::WriteCvgStartDocument( a2dDocumentOutputStream& stream )
297 {
298  m_streamo = &stream;
299 
300  InitializeSave();
301 
302  WriteStartDocument( wxT( "1.0" ), wxT( "UTF-8" ) , true );
303 
304  WriteStartElementAttributes( wxT( "cvg" ) );
305  WriteAttribute( wxT( "classname" ), GetClassInfo()->GetClassName() );
307 
308  WriteElement( wxT( "title" ), wxT( "" ) );
309  WriteElement( wxT( "desc" ), wxT( "" ) );
310 }
311 
312 void a2dIOHandlerDocCVGOut::WriteCvgEndDocument()
313 {
314  WriteEndElement();
316  ResetSave( );
317 }
318 
319 void a2dIOHandlerDocCVGOut::WriteObject( a2dCanvasObject* start )
320 {
321  //check flag is used for writing once an object which is referenced many times
322  a2dWalker_SetCheck setp( false );
323  setp.Start( start );
324 
325  a2dObjectList towrite;
326  towrite.push_back( start );
327 
328  a2dObjectList::iterator iter = towrite.begin();
329  while ( towrite.size() )
330  {
331  a2dObject* obj = *iter;
332  obj->Save( NULL, *this, &towrite );
333  towrite.erase( iter );
334  iter = towrite.begin();
335  }
336 }
337 
338 #endif /* wxART2D_USE_CVGIO */
set check on a2dObject flag false or true
Definition: algos.h:665
For exceptions thrown while parsing XML files.
Definition: genxmlpars.h:36
a2dHashMapCreateObject m_objectCreate
This is used to find a classname using a symbolic name.
Definition: gen.h:3549
#define wxDynamicCast(obj, className)
Define wxDynamicCast so that it will give a compiler error for unrelated types.
Definition: gen.h:75
void WriteElement(const wxString &name, const wxString &content=wxT(""), bool newLine=true)
Writes start and end tag.
Definition: genxmlpars.cpp:794
bool SaveLayers(a2dDocumentOutputStream &stream, a2dLayers *layers)
save a layer definition to a CVG file.
Definition: xmlparsdoc.cpp:233
void SetDescription(const wxString &desc)
Sets a description of the document.
Definition: candoc.h:430
XMLeventType Next()
Walks to next element and returns event type.
Definition: genxmlpars.cpp:422
~a2dIOHandlerDocCVGIn()
Destructor.
Definition: xmlparsdoc.cpp:49
void WriteStartElementAttributes(const wxString &name, bool newLine=true)
Writes start tag which has attributes.
Definition: genxmlpars.cpp:757
Ref Counted base object.
Definition: gen.h:1045
wxOutputStream a2dDocumentOutputStream
output stream based wxStreams
Definition: gen.h:3458
virtual int GetCurrentLineNumber()
where in the input was line the current tag
Definition: genxmlpars.cpp:349
~a2dIOHandlerDocCVGOut()
Destructor.
Definition: xmlparsdoc.cpp:229
a2dDocumentInputStream * m_streami
file or other string stream containing the format to parse.
Definition: gen.h:3734
const a2dError a2dError_FileCouldNotOpen
bool SaveStartAt(a2dDocumentOutputStream &stream, const a2dCanvasDocument *doc, a2dCanvasObject *start)
saves as CVG starting at object start
Definition: xmlparsdoc.cpp:273
a2dCanvasObject is the base class for Canvas Objects.
Definition: canobj.h:371
wxString GetContent()
Returns the current content.
Definition: genxmlpars.cpp:570
a2dLayers * GetLayerSetup()
Get the layersettings for the canvas.
Definition: candoc.h:555
virtual wxObject * CreateObject(const wxString &symbolicName)
Creates an specific object by name.
Definition: gen.cpp:4937
void WriteEndElement(bool newLine=true)
Writes correspondending end tag for the current start tag.
Definition: genxmlpars.cpp:862
virtual void ResetLoad()
Reset the object after loading.
Definition: genxmlpars.cpp:334
void WriteStartDocument(const wxString &version, const wxString &encoding, bool standalone)
Writes the XML header declaration.
Definition: genxmlpars.cpp:723
a2dIOHandlerStrIn & SeekI(wxFileOffset pos)
set stream at a position
Definition: gen.cpp:5021
a2dCanvasDocument * m_doc
the document to store/load the data found into
Definition: xmlparsdoc.h:85
const a2dError a2dError_XMLparse
virtual void InitializeSave()
Inits the IO handler for writing.
Definition: genxmlpars.cpp:695
virtual bool CanLoad(a2dDocumentInputStream &stream, const wxObject *obj=NULL, wxClassInfo *docClassInfo=NULL)
test header of the file to see if its CVG format
Definition: xmlparsdoc.cpp:53
virtual void ResetSave()
Reset the object after saving.
Definition: genxmlpars.cpp:703
A2DGENERALDLLEXP a2dSmrtPtr< a2dGeneralGlobal > a2dGeneralGlobals
a global pointer to get to global instance of important classes.
Definition: comevt.cpp:1148
wxStringInputStream a2dDocumentStringInputStream
string input stream based wxStreams
Definition: gen.h:3452
a2dIOHandlerDocCVGOut()
Constructor.
Definition: xmlparsdoc.cpp:224
virtual void InitializeLoad()
Inits the IO handler for reading.
Definition: genxmlpars.cpp:327
a2dIOHandlerDocCVGIn()
Constructor.
Definition: xmlparsdoc.cpp:38
void UpdateIndexes()
all non defined layers until wxMAXLAYER will be added a new childs.
Definition: layerinf.cpp:695
void WriteEndAttributes(bool close=false)
&quot;Closes&quot; the start tag after writing all attributes (writes the &quot;&gt;&quot; or &quot;/&gt;&quot; bracket).
Definition: genxmlpars.cpp:837
a2dCanvasDocument * m_doc
the document to store/load the data found into
Definition: xmlparsdoc.h:125
wxClassInfo * m_docClassInfo
Run-time class information that allows document instances to be constructed dynamically.
Definition: gen.h:3737
void Save(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dObjectList *towrite)
called from a2dIOHandlerXmlSerOut after a SaveObject.
Definition: candoc.cpp:1173
wxInputStream a2dDocumentInputStream
input stream based wxStreams
Definition: gen.h:3456
a2dWalker based algorithms
special a2dCanvasObject to make a multi view hierachy.
virtual void Load(wxObject *parent, a2dIOHandlerXmlSerIn &parser)
load object from CVG file
Definition: gen.cpp:1396
wxString GetAttributeValue(const wxString &attrib, const wxString &defaultv=wxT(""))
Returns the value of an attribute.
Definition: genxmlpars.cpp:450
Each a2dCanvasView needs to have a a2dCanvasDocument set in order to render data. ...
Definition: candoc.h:374
void WriteEndDocument()
Checks if all open tags are closed.
Definition: genxmlpars.cpp:730
XMLeventType GetEventType()
Returns the type of current event.
Definition: genxmlpars.cpp:606
void Require(const XMLeventType &type, wxString name)
Forces a special tag.
Definition: genxmlpars.cpp:390
wxString GetTagName()
Returns name of the current XML tag.
Definition: genxmlpars.cpp:565
void SetTitle(const wxString &title, bool notifyViews=false)
Sets the title for this document.
bool LoadLayers(a2dDocumentInputStream &stream, a2dLayers *layers)
load a layer definition from a CVG file.
Definition: xmlparsdoc.cpp:94
bool Load(a2dDocumentStringInputStream &stream, a2dCanvasDocument *doc, a2dCanvasObject *parent)
reading a CVG document and add the contents as children to a given a2dCanvasObject parent...
Definition: xmlparsdoc.cpp:147
virtual void Save(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dObjectList *towrite)
write all needed to an XML type of file called the CVG format
Definition: gen.cpp:1343
void Append(a2dCanvasObject *obj)
append a a2dCanvasObject to the childobjects
Definition: canobj.cpp:6224
list of a2dObject&#39;s
Definition: gen.h:3157
virtual bool LinkReferences(bool ignoreNonResolved=false)
link references to their destination
Definition: gen.cpp:4862
a2dDocumentOutputStream * m_streamo
file or other string stream containing the format to output to.
Definition: gen.h:3825
bool Start(a2dObject *object)
start removing properties from the object given, and down.
Definition: algos.h:634
general canvas module declarations and classes
xmlparsdoc.cpp Source File -- Sun Oct 12 2014 17:04:26 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation