File: mxml_attribute.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (192 lines) | stat: -rw-r--r-- 5,470 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
   Mini XML lib PLUS for C++

   Attribute class

   Author: Giancarlo Niccolai <gc@falconpl.org>
*/

#include <mxml.h>
#include <mxml_attribute.h>
#include <mxml_error.h>
#include <mxml_utility.h>

#include <falcon/fassert.h>

#include <ctype.h>

namespace MXML {

Attribute::Attribute( Falcon::Stream &in, int style, int l, int p ):
   Element( l, p )
{
   Falcon::uint32 chr, quotechr=0;
   int iStatus = 0;
   Falcon::String entity;
   markBegin(); // default start

   m_value = "";
   m_name = "";

   in.get( chr );
   while ( iStatus < 6 && in.good() && ! in.eof() ) {
      nextChar();
      //std::cout << "LINE: " << line() << "  POS: " << character() << std::endl;
      switch ( iStatus ) {
         // begin
         case 0:
            // no attributes found - should not happen as I have been called by
            // node only if an attribute is to be read.
            fassert( chr != '>' && chr !='/');
            switch ( chr ) {
               case MXML_LINE_TERMINATOR: nextLine(); break;
               // We repeat line terminator here for portability
               case MXML_SOFT_LINE_TERMINATOR: break;
               case ' ': case '\t':
                  throw new MalformedError( Error::errInvalidAtt, this );

               default:
                  if ( isalpha( chr ) ) {
                     m_name = chr;
                     iStatus = 1;
                     markBegin();
                  }
                  else {
                     throw new MalformedError( Error::errInvalidAtt, this );
                  }
            }
         break;

         // scanning for a name
         case 1:
            if ( isalnum( chr ) || chr == '_' || chr == '-' || chr == ':' ) {
               m_name += chr;
            }
            else if( chr == MXML_LINE_TERMINATOR ) {
               nextLine();
               iStatus = 2; // waiting for a '='
            }
            // We repeat line terminator here for portability
            else if ( chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r' ) {
               iStatus = 2;
            }
            else if ( chr == '=' ) {
               iStatus = 3;
            }
            else {
               throw MalformedError( Error::errMalformedAtt, this );
            }
         break;

         // waiting for '='
         case 2:
            if ( chr == '=' ) {
               iStatus = 3;
            }
            else if( chr == MXML_LINE_TERMINATOR ) {
               nextLine() ;
            }
            // We repeat line terminator here for portability
            else if ( chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r' ) {
            }
            else {
               throw MalformedError( Error::errMalformedAtt, this );
            }
         break;

         // waiting for ' or "
         case 3:
            if ( chr == '\'' || chr == '"' ) {
               iStatus = 4;
               quotechr = chr;
            }
            else if( chr == MXML_LINE_TERMINATOR ) {
               nextLine() ;
            }
            // We repeat line terminator here for portability
            else if ( chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r' ) {
            }
            else {
               throw MalformedError( Error::errMalformedAtt, this );
            }
         break;

         // scanning the attribute content ( until next quotechr )
         case 4:
            if ( chr == quotechr ) {
               iStatus = 6;
            }
            else if ( chr == '&' && !( style & MXML_STYLE_NOESCAPE) ) {
               iStatus = 5;
               entity = "";
            }
            else if( chr == MXML_LINE_TERMINATOR ) {
               nextLine();
               m_value += chr;
            }
            else {
               m_value += chr;
            }
         break;

         case 5:
            if ( chr == quotechr ) {
               iStatus = 6;
            }
            else if ( chr == ';' ) {
               if ( entity == "" ) {
                  throw MalformedError( Error::errWrongEntity, this );
               }

               iStatus = 4;

               // we see if we have a predef entity (also known as escape)
               if ( entity == "amp" ) chr = '&';
               else if ( entity == "lt" ) chr = '<';
               else if ( entity == "gt" ) chr = '>';
               else if ( entity == "quot" ) chr = '"';
               else if ( entity == "apos" ) chr = '\'';
               else {
                  // for now we take save the unexisting entity
                  chr = ';';
                  m_value += "&" + entity;
               }

               m_value += chr;
            }
            else if ( !isalnum( chr ) && chr != '-' && chr != '_' && chr != '#' ) {
               //error
               throw MalformedError( Error::errUnclosedEntity, this );
            }
            else {
               entity += chr;
            }
         break;
      }
      if ( iStatus < 6 )
         in.get( chr );
   }

   if ( ! in.good() ) throw IOError( Error::errIo, this );

   if ( iStatus < 6 ) {
      throw MalformedError( Error::errMalformedAtt, this );
   }
}

void Attribute::write( Falcon::Stream &out, const int style ) const
{
   out.writeString( m_name );
   out.write( "=\"", 2 );

   if ( style & MXML_STYLE_NOESCAPE )
      out.writeString( m_value );
   else
      MXML::writeEscape( out, m_value );

   out.put( '\"' );
}

}

/* end of mxml_attribute.cpp */