1 : /**********************************************************************
2 : * $Id: iom_error.cpp 12582 2007-10-29 09:38:21Z pka $
3 : *
4 : * Project: iom - The INTERLIS Object Model
5 : * Purpose: For more information, please see <http://iom.sourceforge.net>
6 : * Author: Claude Eisenhut
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2007, Claude Eisenhut
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 :
31 : /** @file
32 : * implementation of error utilities
33 : * @defgroup error error handling functions
34 : * @{
35 : */
36 :
37 : #include <iostream>
38 : #include <string>
39 : #include <stdlib.h>
40 :
41 : #include <iom/iom_p.h>
42 :
43 649 : IomBasket ErrorUtility::errs;
44 : int ErrorUtility::errc=0;
45 : XMLCh ErrorUtility::itoabuf[40];
46 : IOM_ERRLISTENER ErrorUtility::listener=iom_stderrlistener;
47 :
48 :
49 0 : void ErrorUtility::notifyerr(IomObject obj)
50 : {
51 0 : if(listener){
52 0 : (*listener)(&(*obj)); // do not increment useCount!
53 : }
54 0 : }
55 :
56 0 : void ErrorUtility::init()
57 : {
58 0 : if(errs.isNull()){
59 0 : errs=dbgnew iom_basket();
60 : }
61 0 : }
62 :
63 : /** cleanup error module. This function is a part of iom_end().
64 : */
65 0 : void ErrorUtility::at_iom_end()
66 : {
67 0 : if(!errs.isNull()){
68 0 : errs=0;
69 : }
70 0 : }
71 :
72 : /** issues a any object that denotes an error.
73 : */
74 0 : extern "C" void iom_issueanyerr(IOM_OBJECT err)
75 : {
76 0 : ErrorUtility::init();
77 0 : IomObject obj(err);
78 0 : ErrorUtility::errs->addObject(obj);
79 0 : ErrorUtility::notifyerr(obj);
80 0 : }
81 :
82 :
83 : /** issues a general IOM error.
84 : */
85 0 : extern "C" void iom_issueerr(const char *message)
86 : {
87 0 : ErrorUtility::init();
88 0 : IomObject obj(dbgnew iom_object());
89 0 : XMLString::binToText( ErrorUtility::errc++,ErrorUtility::itoabuf,sizeof(ErrorUtility::itoabuf)-1,10);
90 0 : obj->setOid(ErrorUtility::itoabuf);
91 0 : obj->setTag(ParserHandler::getTagId(X("iomerr04.errors.Error")));
92 0 : obj->setAttrValue(ParserHandler::getTagId("message"),X(message));
93 0 : ErrorUtility::errs->addObject(obj);
94 0 : ErrorUtility::notifyerr(obj);
95 :
96 0 : }
97 :
98 : /** issues a post parsing error.
99 : */
100 0 : extern "C" void iom_issuesemerr(const char *message,IOM_OID bid,IOM_OID oid)
101 : {
102 0 : ErrorUtility::init();
103 0 : IomObject obj(dbgnew iom_object());
104 0 : XMLString::binToText( ErrorUtility::errc++,ErrorUtility::itoabuf,sizeof(ErrorUtility::itoabuf)-1,10);
105 0 : obj->setOid(ErrorUtility::itoabuf);
106 0 : obj->setTag(ParserHandler::getTagId(X("iomerr04.errors.SemanticError")));
107 0 : obj->setAttrValue(ParserHandler::getTagId("message"),X(message));
108 0 : obj->setAttrValue(ParserHandler::getTagId("bid"),X(bid));
109 0 : if(oid){
110 0 : obj->setAttrValue(ParserHandler::getTagId("oid"),X(oid));
111 : }
112 0 : ErrorUtility::errs->addObject(obj);
113 0 : ErrorUtility::notifyerr(obj);
114 :
115 0 : }
116 :
117 : /** issues an XML parse error or warning..
118 : */
119 0 : extern "C" void iom_issueparserr(const char *message,int kind,int line,int col)
120 : {
121 0 : ErrorUtility::init();
122 0 : IomObject obj(dbgnew iom_object());
123 0 : XMLString::binToText( ErrorUtility::errc++,ErrorUtility::itoabuf,sizeof(ErrorUtility::itoabuf)-1,10);
124 0 : obj->setOid(ErrorUtility::itoabuf);
125 0 : obj->setTag(ParserHandler::getTagId(X("iomerr04.errors.XmlParseError")));
126 0 : obj->setAttrValue(ParserHandler::getTagId("message"),X(message));
127 : const char *kind_c;
128 0 : switch(kind){
129 : case IOM_ERRKIND_XMLPARSER:
130 0 : kind_c="XmlParser";
131 0 : break;
132 : case IOM_ERRKIND_MISSING:
133 0 : kind_c="Missing";
134 0 : break;
135 : case IOM_ERRKIND_INVALID:
136 0 : kind_c="Invalid";
137 0 : break;
138 : case IOM_ERRKIND_OTHER:
139 : default:
140 0 : kind_c="Other";
141 : break;
142 : }
143 0 : obj->setAttrValue(ParserHandler::getTagId("kind"),X(kind_c));
144 0 : XMLString::binToText( line,ErrorUtility::itoabuf,sizeof(ErrorUtility::itoabuf)-1,10);
145 0 : obj->setAttrValue(ParserHandler::getTagId("line"),ErrorUtility::itoabuf);
146 0 : XMLString::binToText( col,ErrorUtility::itoabuf,sizeof(ErrorUtility::itoabuf)-1,10);
147 0 : obj->setAttrValue(ParserHandler::getTagId("col"),ErrorUtility::itoabuf);
148 0 : ErrorUtility::errs->addObject(obj);
149 0 : ErrorUtility::notifyerr(obj);
150 0 : }
151 :
152 : /** sets a new error listener.
153 : * returns the old or 0.
154 : */
155 0 : extern "C" IOM_ERRLISTENER iom_seterrlistener(IOM_ERRLISTENER newlistener)
156 : {
157 0 : IOM_ERRLISTENER old=ErrorUtility::listener;
158 0 : ErrorUtility::listener=newlistener;
159 0 : return old;
160 : }
161 :
162 : /** error listener that dumps all errors to stderr.
163 : * Can be used in a iom_eterrlistener() call.
164 : */
165 0 : extern "C" void iom_stderrlistener(IOM_OBJECT errobj1)
166 : {
167 0 : IomObject errobj(errobj1);
168 0 : if(errobj->getTag()==ParserHandler::getTagId(X("iomerr04.errors.Error"))){
169 0 : std::cerr << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("message")))) << std::endl;
170 0 : }else if(errobj->getTag()==ParserHandler::getTagId(X("iomerr04.errors.XmlParseError"))){
171 0 : std::cerr << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("kind")))) << ", ";
172 0 : std::cerr << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("line")))) << ", ";
173 0 : std::cerr << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("col")))) << ": ";
174 0 : std::cerr << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("message")))) << std::endl;
175 0 : }else if(errobj->getTag()==ParserHandler::getTagId(X("iomerr04.errors.SemanticError"))){
176 0 : std::cerr << "basket " << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("bid"))));
177 0 : const XMLCh *oid=errobj->getAttrValue(ParserHandler::getTagId(X("oid")));
178 0 : if(oid){
179 0 : std::cerr << ", object " << StrX(oid);
180 : }
181 0 : std::cerr << ": " << StrX(errobj->getAttrValue(ParserHandler::getTagId(X("message")))) << std::endl;
182 : }else{
183 0 : std::cerr << "ERROR: " << errobj->getTag_c() << std::endl;
184 0 : errobj->dumpAttrs();
185 0 : }
186 1947 : }
187 :
188 : /** @}
189 : */
190 :
|