1 | #include <fstream.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string> |
---|
5 | #include <strstream.h> |
---|
6 | #include <iostream.h> |
---|
7 | #include <xercesc/util/XMLUniDefs.hpp> |
---|
8 | #include <xercesc/sax2/Attributes.hpp> |
---|
9 | #include <xercesc/sax/SAXParseException.hpp> |
---|
10 | #include <xercesc/sax/SAXException.hpp> |
---|
11 | #include "xmlParser.hxx" |
---|
12 | |
---|
13 | |
---|
14 | // Global Variables |
---|
15 | //-------------------------------------------------------------------------------- |
---|
16 | const XMLCh* gxAttrItemLength = 0; |
---|
17 | static int giBrLnCtr = 0; |
---|
18 | static int giGrNameCtr = 0; |
---|
19 | static bool gbLastBranch = false; |
---|
20 | |
---|
21 | XMLCh gxAttrGroupName[100]; |
---|
22 | XMLCh gxAttrBranchLength[10]; |
---|
23 | char *gcBranchLengthArray[100]; |
---|
24 | char *gcGroupNameArray[100]; |
---|
25 | const char *gcLastItem; |
---|
26 | //-------------------------------------------------------------------------------- |
---|
27 | |
---|
28 | Sax2Handler::Sax2Handler(const char* const encodingName, const XMLFormatter::UnRepFlags unRepFlags, ofstream &outFile) : |
---|
29 | fFormatter(encodingName, this, XMLFormatter::NoEscapes, unRepFlags), |
---|
30 | out(outFile) |
---|
31 | { |
---|
32 | } |
---|
33 | |
---|
34 | Sax2Handler::~Sax2Handler() |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | // Output FormatterTarget interface |
---|
39 | //-------------------------------------------------------------------------------- |
---|
40 | void Sax2Handler::writeChars(const XMLByte* const toWrite) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | void Sax2Handler::writeChars(const XMLByte* const toWrite, const unsigned int count, |
---|
45 | XMLFormatter* const formatter) |
---|
46 | { |
---|
47 | // writing to the destination file "out" |
---|
48 | out.write ((char *) toWrite, (int) count) ; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | //DocumentHandler |
---|
53 | //-------------------------------------------------------------------------------- |
---|
54 | void Sax2Handler::startDocument() |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | void Sax2Handler::endDocument() |
---|
59 | { |
---|
60 | fFormatter << chCloseParen << chSemiColon; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void Sax2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname, |
---|
65 | const XMLCh* const qName, const Attributes& attributes) |
---|
66 | { |
---|
67 | XMLCh attrLength[25]; XMLString::transcode("length",attrLength,24); |
---|
68 | XMLCh attrGroupName[25]; XMLString::transcode("groupname",attrGroupName,24); |
---|
69 | |
---|
70 | const char *temp = XMLString::transcode(localname); |
---|
71 | fFormatter << XMLFormatter::NoEscapes ; |
---|
72 | |
---|
73 | if (strcmp(temp,"COMMENT")==0) { |
---|
74 | fFormatter << chOpenSquare ; |
---|
75 | } |
---|
76 | |
---|
77 | if (strcmp(temp,"BRANCH")==0) { |
---|
78 | if (gbLastBranch) { |
---|
79 | fFormatter << chComma ; |
---|
80 | gbLastBranch = false; |
---|
81 | } |
---|
82 | fFormatter << chOpenParen ; |
---|
83 | gcBranchLengthArray[giBrLnCtr++] = XMLString::transcode(attributes.getValue(attrLength)); |
---|
84 | gcGroupNameArray[giGrNameCtr++] = XMLString::transcode(attributes.getValue(attrGroupName)); |
---|
85 | } |
---|
86 | |
---|
87 | gxAttrItemLength = attributes.getValue(attrLength); |
---|
88 | |
---|
89 | if (strcmp(temp,"ITEM")==0) { |
---|
90 | if ((gcLastItem && strcmp(gcLastItem,temp)==0) || gbLastBranch) |
---|
91 | fFormatter << chComma ; |
---|
92 | } |
---|
93 | |
---|
94 | XMLCh attrItem[25]; XMLString::transcode("itemname",attrItem,24); |
---|
95 | fFormatter << XMLFormatter::AttrEscapes |
---|
96 | << attributes.getValue(attrItem) |
---|
97 | << XMLFormatter::NoEscapes ; |
---|
98 | } |
---|
99 | |
---|
100 | void Sax2Handler::endElement(const XMLCh* const uri, const XMLCh* const localname, |
---|
101 | const XMLCh* const qname) |
---|
102 | { |
---|
103 | const char *temp = XMLString::transcode(localname); |
---|
104 | if (strcmp(temp,"BRANCH")==0) { |
---|
105 | gbLastBranch = true; |
---|
106 | |
---|
107 | fFormatter << chCloseParen ; |
---|
108 | |
---|
109 | XMLString::transcode(gcGroupNameArray[--giGrNameCtr],gxAttrGroupName,99); |
---|
110 | if (gxAttrGroupName) fFormatter << gxAttrGroupName ; |
---|
111 | |
---|
112 | XMLString::transcode(gcBranchLengthArray[--giBrLnCtr],gxAttrBranchLength,9); |
---|
113 | fFormatter << chColon << gxAttrBranchLength ; |
---|
114 | } |
---|
115 | else if (strcmp(temp,"ITEM")==0) { |
---|
116 | fFormatter << chColon << gxAttrItemLength; |
---|
117 | } |
---|
118 | else if (strcmp(temp,"COMMENT")==0) { |
---|
119 | fFormatter << chCloseSquare << chOpenParen ; |
---|
120 | } |
---|
121 | |
---|
122 | gcLastItem = temp; |
---|
123 | } |
---|
124 | |
---|
125 | void Sax2Handler::characters(const XMLCh* const chars, const unsigned int length) |
---|
126 | { |
---|
127 | fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes); |
---|
128 | } |
---|
129 | |
---|
130 | void Sax2Handler::ignorableWhitespace(const XMLCh* const chars, const unsigned int length) |
---|
131 | { |
---|
132 | fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void Sax2Handler::processingInstruction (const XMLCh* const target, const XMLCh* const data ) |
---|
137 | { |
---|
138 | } |
---|
139 | |
---|
140 | // Sax2Handler: Overrides of the SAX ErrorHandler interface |
---|
141 | //-------------------------------------------------------------------------------- |
---|
142 | void Sax2Handler::error(const SAXParseException& e) |
---|
143 | { |
---|
144 | cerr << "\nError at file " << StrX(e.getSystemId()) |
---|
145 | << ", line " << e.getLineNumber() |
---|
146 | << ", char " << e.getColumnNumber() |
---|
147 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
148 | } |
---|
149 | |
---|
150 | void Sax2Handler::fatalError(const SAXParseException& e) |
---|
151 | { |
---|
152 | cerr << "\nFatal Error at file " << StrX(e.getSystemId()) |
---|
153 | << ", line " << e.getLineNumber() |
---|
154 | << ", char " << e.getColumnNumber() |
---|
155 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
156 | } |
---|
157 | |
---|
158 | void Sax2Handler::warning(const SAXParseException& e) |
---|
159 | { |
---|
160 | cerr << "\nWarning at file " << StrX(e.getSystemId()) |
---|
161 | << ", line " << e.getLineNumber() |
---|
162 | << ", char " << e.getColumnNumber() |
---|
163 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
164 | } |
---|
165 | |
---|