summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmxmlparser.cpp
blob: 7cb71a873df482d1959c012c7155e32d64af40ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2002 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#include "pmxmlparser.h"

#include <tqbuffer.h>
#include <tdelocale.h>

#include "pmpart.h"
#include "pmscene.h"
#include "pmxmlhelper.h"
#include "pmprototypemanager.h"
#include "pmdocumentformat.h"
#include "pmdebug.h"

PMXMLParser::PMXMLParser( PMPart* part, TQIODevice* dev )
      : PMParser( part, dev )
{
   init( );
}

PMXMLParser::PMXMLParser( PMPart* part, const TQByteArray& array )
      : PMParser( part, array )
{
   init( );
}

void PMXMLParser::init( )
{
   m_pDoc = 0;
   m_majorDocumentFormat = 1;
   m_minorDocumentFormat = 0;
}

PMXMLParser::~PMXMLParser( )
{
   if( m_pDoc )
      delete m_pDoc;
}

bool PMXMLParser::initDocument( )
{
   if( !m_pDoc )
   {
      m_pDoc = new TQDomDocument( "KPOVMODELER" );
      if( m_pDoc->setContent( m_pDevice ) )
         return true;
      else
      {
         printError( i18n( "Could not load the documents data!" ) );
         setFatalError( );
         return false;
      }
   }
   return true;
}

void PMXMLParser::topParse( )
{
   if( initDocument( ) )
   {
      TQDomElement e = m_pDoc->documentElement( );
      // read the format number
      // assume 1.0 on error
      TQString fstring = e.attribute( "majorFormat", "1" );
      bool ok = true;
      int format = fstring.toInt( &ok );
      if( !ok || ( format < 1 ) )
         format = 1;
      m_majorDocumentFormat = format;

      fstring = e.attribute( "minorFormat", "0" );
      ok = true;
      format = fstring.toInt( &ok );
      if( !ok || ( format < 0 ) )
         format = 0;
      m_minorDocumentFormat = format;

      if( ( m_majorDocumentFormat > c_majorDocumentFormat )
          || ( m_majorDocumentFormat == c_majorDocumentFormat )
          && ( m_minorDocumentFormat > c_minorDocumentFormat ) )
         printWarning( i18n( "This document was created with a newer version of KPovModeler. "
                             "The whole document may not be loaded correctly." ) );

      if( e.tagName( ) == "objects" )
      {
         parseChildObjects( e, 0 );
      }
      else if( e.tagName( ) == "scene" )
      {
         PMScene* scene = new PMScene( m_pPart );
         insertChild( scene, 0 );
         PMXMLHelper hlp( e, m_pPart, this,
                          m_majorDocumentFormat, m_minorDocumentFormat );
         scene->readAttributes( hlp );
         parseChildObjects( e, scene );
      }
      else
      {
         printError( i18n( "Wrong top level tag" ) );
         setFatalError( );
      }
   }
}

void PMXMLParser::parseChildObjects( TQDomElement& e, PMObject* parent )
{
   TQDomNode c = e.firstChild( );
   while( !c.isNull( ) )
   {
      if( c.isElement( ) )
      {
         TQDomElement ce = c.toElement( );
         PMPrototypeManager* m = m_pPart->prototypeManager( );
         PMObject* obj = m->newObject( m->className( ce.tagName( ) ) );
         if( obj )
         {
            PMXMLHelper hlp( ce, m_pPart, this,
                             m_majorDocumentFormat, m_minorDocumentFormat );
            obj->readAttributes( hlp );
            if( insertChild( obj, parent ) )
            {
               parseChildObjects( ce, obj );

               if( obj->isA( "Declare" ) )
                  checkID( ( PMDeclare* ) obj );
            }
            else
               delete obj;
         }
         else if( ce.tagName( ) != "extra_data" )
            printError( i18n( "Unknown object %1" ).arg( ce.tagName( ) ) );
      }
      c = c.nextSibling( );
   }
}


void PMXMLParser::quickParse( TQStringList& list )
{
   if( initDocument( ) )
   {
      TQDomElement e = m_pDoc->documentElement( );
      if( ( e.tagName( ) == "objects" ) || ( e.tagName( ) == "scene" ) )
      {
         TQDomNode c = e.firstChild( );

         while( !c.isNull( ) )
         {
            if( c.isElement( ) )
            {
               TQDomElement ce = c.toElement( );
               TQString type = m_pPart->prototypeManager( )->className( ce.tagName( ) );
               if( !type.isNull( ) )
                  list.append( type );
            }
            c = c.nextSibling( );
         }
      }
      else
         printError( i18n( "Wrong top level tag" ) );
   }
}