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