1 /*
2  * Copyright 2005-2007 Gerald Schmidt.
3  *
4  * This file is part of Xml Copy Editor.
5  *
6  * Xml Copy Editor is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * Xml Copy Editor is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Xml Copy Editor; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include "wrapexpat.h"
22 //#include "expat.h"
23 #include <stdexcept>
24 #include <iostream>
25 #include <sstream>
26 
WrapExpat(const char * encoding,bool nameSpaceAware)27 WrapExpat::WrapExpat ( const char *encoding, bool nameSpaceAware )
28 {
29 	p = ( nameSpaceAware ) ? XML_ParserCreateNS ( encoding, ( XML_Char ) ':' ) : XML_ParserCreate ( encoding );
30 	if ( p == 0 )
31 		throw runtime_error ( "WrapExpat::WrapExpat" );
32 }
33 
~WrapExpat()34 WrapExpat::~WrapExpat()
35 {
36 	if ( p )
37 		XML_ParserFree ( p );
38 }
39 
parse(const char * buffer,size_t size,bool isFinal)40 bool WrapExpat::parse ( const char *buffer, size_t size, bool isFinal )
41 {
42 	if ( !p )
43 		return false;
44 	if ( XML_Parse ( p, buffer, size, isFinal ) == XML_STATUS_ERROR )
45 		return false;
46 	return true;
47 
48 }
49 
parse(const string & buffer,bool isFinal)50 bool WrapExpat::parse ( const string &buffer, bool isFinal )
51 {
52 	return parse ( buffer.c_str(), buffer.size(), isFinal );
53 
54 	/*
55 	  if (!p)
56 	    return false;
57 
58 	  if (XML_Parse(p, buffer.c_str(), buffer.size(), isFinal) == XML_STATUS_ERROR)
59 	    return false;
60 	  return true;
61 	*/
62 
63 }
64 
getErrorPosition()65 pair<int, int> WrapExpat::getErrorPosition()
66 {
67 	return make_pair (
68 	           XML_GetCurrentLineNumber ( p ),// - 1,
69 	           XML_GetCurrentColumnNumber ( p ) );
70 }
71 
getLastError()72 wxString WrapExpat::getLastError()
73 {
74 	if ( !p )
75 		return _ ( "Unable to create parser instance" );
76 
77 	stringstream ss;
78 	ss << "Error at line ";
79 	ss << XML_GetCurrentLineNumber ( p );
80 	ss << ", column " << XML_GetCurrentColumnNumber ( p ) + 1 << ":" << endl;
81 	ss << XML_ErrorString ( XML_GetErrorCode ( p ) );
82 	return wxString ( ss.str().c_str(), wxConvUTF8 );
83 }
84 
xmliseTextNode(const string & textnode)85 string WrapExpat::xmliseTextNode ( const string &textnode )
86 {
87 	size_t size = textnode.size();
88 	string output;
89 
90 	for ( size_t i = 0; i < size; ++i )
91 	{
92 		char c = textnode[i];
93 		switch ( c )
94 		{
95 			case '<':
96 				output += "&lt;";
97 				break;
98 			case '>':
99 				output += "&gt;";
100 				break;
101 			case '&':
102 				output += "&amp;";
103 				break;
104 			default:
105 				output += textnode[i];
106 		}
107 	}
108 	return output;
109 }
110 
xmliseAttribute(const string & attribute)111 string WrapExpat::xmliseAttribute ( const string &attribute )
112 {
113 	string intermediate = xmliseTextNode ( attribute );
114 
115 	size_t size = intermediate.size();
116 	string output;
117 
118 	for ( size_t i = 0; i < size; ++i )
119 	{
120 		char c = intermediate[i];
121 		switch ( c )
122 		{
123 			case '"':
124 				output += "&quot;";
125 				break;
126 			case '\'':
127 				output += "&apos;";
128 				break;
129 			default:
130 				output += intermediate[i];
131 		}
132 	}
133 	return output;
134 }
135 
isWhitespace(const string & textnode)136 bool WrapExpat::isWhitespace ( const string &textnode )
137 {
138 	size_t size = textnode.size();
139 	for ( size_t i = 0; i < size; ++i )
140 		if ( !isspace ( textnode[i] ) )
141 			return false;
142 	return true;
143 }
144 
isEncodingError()145 bool WrapExpat::isEncodingError()
146 {
147 	if ( !p )
148 		return false;
149 
150 	int errorCode = XML_GetErrorCode ( p );
151 	return (
152 	           errorCode == XML_ERROR_UNKNOWN_ENCODING ||
153 	           errorCode == XML_ERROR_INCORRECT_ENCODING ) ?
154 	       true : false;
155 }
156 
157