xref: /aoo42x/main/xmloff/source/core/xmlerror.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "xmloff/xmlerror.hxx"
31 #include <tools/debug.hxx>
32 #include <rtl/ustring.hxx>
33 #include <com/sun/star/xml/sax/XLocator.hpp>
34 #include <com/sun/star/xml/sax/SAXParseException.hpp>
35 #include <com/sun/star/uno/Any.hxx>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 
39 
40 #include <rtl/ustrbuf.hxx>
41 #include <tools/string.hxx>
42 
43 
44 
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::Sequence;
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::xml::sax::XLocator;
51 using ::com::sun::star::xml::sax::SAXParseException;
52 
53 
54 //
55 /// ErrorRecord: contains all information for one error
56 //
57 
58 class ErrorRecord
59 {
60 public:
61 
62     ErrorRecord( sal_Int32 nId,
63                  const Sequence<OUString>& rParams,
64                  const OUString& rExceptionMessage,
65                  sal_Int32 nRow,
66                  sal_Int32 nColumn,
67                  const OUString& rPublicId,
68                  const OUString& rSystemId);
69     ~ErrorRecord();
70 
71     sal_Int32 nId;  /// error ID
72 
73     OUString sExceptionMessage;/// message of original exception (if available)
74 
75     // XLocator information:
76     sal_Int32 nRow;     /// row number where error occured (or -1 for unknown)
77     sal_Int32 nColumn;  /// column number where error occured (or -1)
78     OUString sPublicId; /// public identifier
79     OUString sSystemId; /// public identifier
80 
81     /// message Parameters
82     Sequence<OUString> aParams;
83 };
84 
85 
86 ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
87     const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
88     const OUString& rPublicId, const OUString& rSystemId) :
89         nId(nID),
90         sExceptionMessage(rExceptionMessage),
91         nRow(nRowNumber),
92         nColumn(nCol),
93         sPublicId(rPublicId),
94         sSystemId(rSystemId),
95         aParams(rParams)
96 {
97 }
98 
99 ErrorRecord::~ErrorRecord()
100 {
101 }
102 
103 
104 
105 
106 XMLErrors::XMLErrors()
107 {
108 }
109 
110 XMLErrors::~XMLErrors()
111 {
112 }
113 
114 void XMLErrors::AddRecord(
115     sal_Int32 nId,
116     const Sequence<OUString> & rParams,
117     const OUString& rExceptionMessage,
118     sal_Int32 nRow,
119     sal_Int32 nColumn,
120     const OUString& rPublicId,
121     const OUString& rSystemId )
122 {
123     aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
124                                     nRow, nColumn, rPublicId, rSystemId ) );
125 
126 #ifdef DBG_UTIL
127 
128     // give detailed assertion on this message
129 
130     OUStringBuffer sMessage;
131 
132     sMessage.appendAscii( "An error or a warning has occured during XML import/export!\n" );
133 
134     // ID & flags
135     sMessage.appendAscii( "Error-Id: 0x");
136     sMessage.append( nId, 16 );
137     sMessage.appendAscii( "\n    Flags: " );
138     sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
139     sMessage.append( nFlags >> 28, 16 );
140     if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
141         sMessage.appendAscii( " WARNING" );
142     if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
143         sMessage.appendAscii( " ERRROR" );
144     if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
145         sMessage.appendAscii( " SEVERE" );
146     sMessage.appendAscii( "\n    Class: " );
147     sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
148     sMessage.append( nClass >> 16, 16 );
149     if( (nClass & XMLERROR_CLASS_IO) != 0 )
150         sMessage.appendAscii( " IO" );
151     if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
152         sMessage.appendAscii( " FORMAT" );
153     if( (nClass & XMLERROR_CLASS_API) != 0 )
154         sMessage.appendAscii( " API" );
155     if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
156         sMessage.appendAscii( " OTHER" );
157     sMessage.appendAscii( "\n    Number: " );
158     sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
159     sMessage.append( nNumber, 16 );
160     sMessage.appendAscii( "\n");
161 
162     // the parameters
163     sMessage.appendAscii( "Parameters:\n" );
164     sal_Int32 nLength = rParams.getLength();
165     const OUString* pParams = rParams.getConstArray();
166     for( sal_Int32 i = 0; i < nLength; i++ )
167     {
168         sMessage.appendAscii( "    " );
169         sMessage.append( i );
170         sMessage.appendAscii( ": " );
171         sMessage.append( pParams[i] );
172         sMessage.appendAscii( "\n" );
173     }
174 
175     // the exception message
176     sMessage.appendAscii( "Exception-Message: " );
177     sMessage.append( rExceptionMessage );
178     sMessage.appendAscii( "\n" );
179 
180     // position (if given)
181     if( (nRow != -1) || (nColumn != -1) )
182     {
183         sMessage.appendAscii( "Position:\n    Public Identifier: " );
184         sMessage.append( rPublicId );
185         sMessage.appendAscii( "\n    System Identifier: " );
186         sMessage.append( rSystemId );
187         sMessage.appendAscii( "\n    Row, Column: " );
188         sMessage.append( nRow );
189         sMessage.appendAscii( "," );
190         sMessage.append( nColumn );
191         sMessage.appendAscii( "\n" );
192     }
193 
194     // convert to byte string and signal the error
195     ByteString aError( String( sMessage.makeStringAndClear() ),
196                        RTL_TEXTENCODING_ASCII_US );
197     DBG_ERROR( aError.GetBuffer() );
198 #endif
199 }
200 
201 void XMLErrors::AddRecord(
202     sal_Int32 nId,
203     const Sequence<OUString> & rParams,
204     const OUString& rExceptionMessage,
205     const Reference<XLocator> & rLocator)
206 {
207     if ( rLocator.is() )
208     {
209         AddRecord( nId, rParams, rExceptionMessage,
210                    rLocator->getLineNumber(), rLocator->getColumnNumber(),
211                    rLocator->getPublicId(), rLocator->getSystemId() );
212     }
213     else
214     {
215         OUString sEmpty;
216         AddRecord( nId, rParams, rExceptionMessage,
217                    -1, -1, sEmpty, sEmpty );
218     }
219 }
220 
221 void XMLErrors::AddRecord(
222     sal_Int32 nId,
223     const Sequence<OUString> & rParams,
224     const OUString& rExceptionMessage)
225 {
226     OUString sEmpty;
227     AddRecord( nId, rParams, rExceptionMessage, -1, -1, sEmpty, sEmpty );
228 }
229 
230 void XMLErrors::AddRecord(
231     sal_Int32 nId,
232     const Sequence<OUString> & rParams)
233 {
234     OUString sEmpty;
235     AddRecord( nId, rParams, sEmpty, -1, -1, sEmpty, sEmpty );
236 }
237 
238 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
239     throw( SAXParseException )
240 {
241     // search first error/warning that matches the nIdMask
242     for( ErrorList::iterator aIter = aErrors.begin();
243          aIter != aErrors.end();
244          aIter++ )
245     {
246         if ( (aIter->nId & nIdMask) != 0 )
247         {
248             // we throw the error
249             ErrorRecord& rErr = aErrors[0];
250             Any aAny;
251             aAny <<= rErr.aParams;
252             throw SAXParseException(
253                 rErr.sExceptionMessage, NULL, aAny,
254                 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
255         }
256     }
257 }
258