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 | Sax2Handler::~Sax2Handler() {} |
---|
34 | |
---|
35 | // Output FormatterTarget interface |
---|
36 | //-------------------------------------------------------------------------------- |
---|
37 | void Sax2Handler::writeChars(const XMLByte* const toWrite) {} |
---|
38 | |
---|
39 | void Sax2Handler::writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const formatter) { |
---|
40 | // writing to the destination file "out" |
---|
41 | out.write ((char *) toWrite, (int) count) ; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | //DocumentHandler |
---|
46 | //-------------------------------------------------------------------------------- |
---|
47 | void Sax2Handler::startDocument() {} |
---|
48 | |
---|
49 | void Sax2Handler::endDocument() { |
---|
50 | fFormatter << chCloseParen << chSemiColon; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | void Sax2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname, |
---|
55 | const XMLCh* const qName, const Attributes& attributes) |
---|
56 | { |
---|
57 | XMLCh attrLength[25]; XMLString::transcode("length",attrLength,24); |
---|
58 | XMLCh attrGroupName[25]; XMLString::transcode("groupname",attrGroupName,24); |
---|
59 | |
---|
60 | const char *temp = XMLString::transcode(localname); |
---|
61 | fFormatter << XMLFormatter::NoEscapes ; |
---|
62 | |
---|
63 | if (strcmp(temp,"COMMENT")==0) { |
---|
64 | fFormatter << chOpenSquare ; |
---|
65 | } |
---|
66 | |
---|
67 | if (strcmp(temp,"BRANCH")==0) { |
---|
68 | if (gbLastBranch) { |
---|
69 | fFormatter << chComma ; |
---|
70 | gbLastBranch = false; |
---|
71 | } |
---|
72 | fFormatter << chOpenParen ; |
---|
73 | gcBranchLengthArray[giBrLnCtr++] = XMLString::transcode(attributes.getValue(attrLength)); |
---|
74 | gcGroupNameArray[giGrNameCtr++] = XMLString::transcode(attributes.getValue(attrGroupName)); |
---|
75 | } |
---|
76 | |
---|
77 | gxAttrItemLength = attributes.getValue(attrLength); |
---|
78 | |
---|
79 | if (strcmp(temp,"ITEM")==0) { |
---|
80 | if ((gcLastItem && strcmp(gcLastItem,temp)==0) || gbLastBranch) |
---|
81 | fFormatter << chComma ; |
---|
82 | } |
---|
83 | |
---|
84 | XMLCh attrItem[25]; XMLString::transcode("itemname",attrItem,24); |
---|
85 | fFormatter << XMLFormatter::AttrEscapes |
---|
86 | << attributes.getValue(attrItem) |
---|
87 | << XMLFormatter::NoEscapes ; |
---|
88 | } |
---|
89 | |
---|
90 | void Sax2Handler::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) { |
---|
91 | const char *temp = XMLString::transcode(localname); |
---|
92 | if (strcmp(temp,"BRANCH")==0) { |
---|
93 | gbLastBranch = true; |
---|
94 | |
---|
95 | fFormatter << chCloseParen ; |
---|
96 | |
---|
97 | XMLString::transcode(gcGroupNameArray[--giGrNameCtr],gxAttrGroupName,99); |
---|
98 | if (gxAttrGroupName) fFormatter << gxAttrGroupName ; |
---|
99 | |
---|
100 | XMLString::transcode(gcBranchLengthArray[--giBrLnCtr],gxAttrBranchLength,9); |
---|
101 | fFormatter << chColon << gxAttrBranchLength ; |
---|
102 | } |
---|
103 | else if (strcmp(temp,"ITEM")==0) { |
---|
104 | fFormatter << chColon << gxAttrItemLength; |
---|
105 | } |
---|
106 | else if (strcmp(temp,"COMMENT")==0) { |
---|
107 | fFormatter << chCloseSquare << chOpenParen ; |
---|
108 | } |
---|
109 | |
---|
110 | gcLastItem = temp; |
---|
111 | } |
---|
112 | |
---|
113 | void Sax2Handler::characters(const XMLCh* const chars, const unsigned int length) { |
---|
114 | fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes); |
---|
115 | } |
---|
116 | |
---|
117 | void Sax2Handler::ignorableWhitespace(const XMLCh* const chars, const unsigned int length) { |
---|
118 | fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | void Sax2Handler::processingInstruction (const XMLCh* const target, const XMLCh* const data ) {} |
---|
123 | |
---|
124 | // Sax2Handler: Overrides of the SAX ErrorHandler interface |
---|
125 | //-------------------------------------------------------------------------------- |
---|
126 | void Sax2Handler::error(const SAXParseException& e) { |
---|
127 | cerr << "\nError at file " << StrX(e.getSystemId()) |
---|
128 | << ", line " << e.getLineNumber() |
---|
129 | << ", char " << e.getColumnNumber() |
---|
130 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
131 | } |
---|
132 | |
---|
133 | void Sax2Handler::fatalError(const SAXParseException& e) { |
---|
134 | cerr << "\nFatal Error at file " << StrX(e.getSystemId()) |
---|
135 | << ", line " << e.getLineNumber() |
---|
136 | << ", char " << e.getColumnNumber() |
---|
137 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
138 | } |
---|
139 | |
---|
140 | void Sax2Handler::warning(const SAXParseException& e) { |
---|
141 | cerr << "\nWarning at file " << StrX(e.getSystemId()) |
---|
142 | << ", line " << e.getLineNumber() |
---|
143 | << ", char " << e.getColumnNumber() |
---|
144 | << "\n Message: " << StrX(e.getMessage()) << endl; |
---|
145 | } |
---|
146 | |
---|