Isis 3 Programmer Reference
IsisXMLHandler.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <string>
9 
10 #include <xercesc/sax2/SAX2XMLReader.hpp>
11 #include <xercesc/util/XMLException.hpp>
12 
13 #include <sstream>
14 
15 #include "IException.h"
16 
17 #include "IString.h"
18 
19 #include "IsisXMLHandler.h"
20 #include "IsisXMLChTrans.h"
21 
22 using namespace std;
23 
24 namespace XERCES = XERCES_CPP_NAMESPACE;
25 
26 
27 // Constructors
28 
29 IsisXMLHandler::IsisXMLHandler() {
30  value = NULL;
31  outputEndTag = 0;
32 }
33 
34 
35 IsisXMLHandler::IsisXMLHandler(char *PencodingName,
36  bool &PexpandNamespaces,
37  XERCES::SAX2XMLReader* &Pparser,
38  QString *Pvalue) {
39 
40  encodingName = PencodingName;
41  expandNamespaces = PexpandNamespaces;
42  parser = Pparser;
43  value = Pvalue;
44 
45  outputEndTag = 0;
46 
47  prevDocHandler = parser->getContentHandler();
48  prevErrorHandler = parser->getErrorHandler();
49 
50  parser->setContentHandler(this);
51  parser->setErrorHandler(this);
52 }
53 
54 
55 IsisXMLHandler::IsisXMLHandler(char *PencodingName,
56  bool &PexpandNamespaces,
57  XERCES::SAX2XMLReader* &Pparser) {
58 
59  value = NULL;
60  outputEndTag = 0;
61 
62  encodingName = PencodingName;
63  expandNamespaces = PexpandNamespaces;
64  parser = Pparser;
65 
66  prevDocHandler = parser->getContentHandler();
67  prevErrorHandler = parser->getErrorHandler();
68 
69  parser->setContentHandler(this);
70  parser->setErrorHandler(this);
71 }
72 
73 
74 IsisXMLHandler::~IsisXMLHandler() {}
75 
76 
77 // IsisXMLHandler: Overrides the SAX ErrorHandler
78 void IsisXMLHandler::error(const XERCES::SAXParseException &e) {
79  ostringstream os;
80  os << "Error in application XML file line: " << e.getLineNumber()
81  << " char: " << e.getColumnNumber() << ". "
82  << XERCES::XMLString::transcode(e.getMessage());
83  throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
84 }
85 
86 void IsisXMLHandler::fatalError(const XERCES::SAXParseException &e) {
87  ostringstream os;
88  os << "Error in application XML file line: " << e.getLineNumber()
89  << " char: " << e.getColumnNumber() << ". "
90  << XERCES::XMLString::transcode(e.getMessage());
91  throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
92 }
93 
94 void IsisXMLHandler::warning(const XERCES::SAXParseException &e) {
95  ostringstream os;
96  os << "Error in application XML file line: " << e.getLineNumber()
97  << " char: " << e.getColumnNumber() << ". "
98  << XERCES::XMLString::transcode(e.getMessage());
99  throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
100 }
101 
102 
103 // IsisXMLHandler: Overrides of the SAX DocumentHandler interface
104 void IsisXMLHandler::characters(const XMLCh *const chars,
105  const XMLSize_t length) {
106 
107  if(value != NULL) {
108  QString str;
109  str = XERCES::XMLString::transcode(chars);
110  str = str.trimmed();
111  *value += str;
112  }
113 }
114 
115 
116 void IsisXMLHandler::endDocument() {
117 }
118 
119 
120 void IsisXMLHandler::endElement(const XMLCh *const uri,
121  const XMLCh *const localname,
122  const XMLCh *const qname) {
123 
124  if(outputEndTag > 0) {
125  QString str;
126  str = XERCES::XMLString::transcode(localname);
127  *value += "</" + str + ">";
128  outputEndTag--;
129  }
130  else {
131  parser->setContentHandler(prevDocHandler);
132  parser->setErrorHandler(prevErrorHandler);
133  }
134 }
135 
136 
137 void IsisXMLHandler::ignorableWhitespace(const XMLCh *const chars,
138  const unsigned int length) {
139 }
140 
141 
142 void IsisXMLHandler::processingInstruction(const XMLCh *const target,
143  const XMLCh *const data) {
144 }
145 
146 
147 void IsisXMLHandler::startDocument() {
148 }
149 
150 
151 void IsisXMLHandler::startElement(const XMLCh *const uri,
152  const XMLCh *const localname,
153  const XMLCh *const qname,
154  const XERCES::Attributes &attributes) {
155 
156  if(value != NULL) {
157  QString str;
158  str = XERCES::XMLString::transcode(localname);
159  // Note: need to build the attributes into the string too
160  *value += "<" + str + ">";
161  outputEndTag++;
162  }
163 }
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
std
Namespace for the standard library.