Bump for 3.6-28
[LibreOffice.git] / xmloff / source / core / xmlerror.cxx
blob8d765d251ed39ff3a1d883c83db02744f0788aec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "xmloff/xmlerror.hxx"
30 #include <tools/debug.hxx>
31 #include <rtl/ustring.hxx>
32 #include <com/sun/star/xml/sax/XLocator.hpp>
33 #include <com/sun/star/xml/sax/SAXParseException.hpp>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/uno/Sequence.hxx>
39 #include <rtl/ustrbuf.hxx>
43 using ::rtl::OUString;
44 using ::rtl::OUStringBuffer;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::xml::sax::XLocator;
49 using ::com::sun::star::xml::sax::SAXParseException;
53 /// ErrorRecord: contains all information for one error
56 class ErrorRecord
58 public:
60 ErrorRecord( sal_Int32 nId,
61 const Sequence<OUString>& rParams,
62 const OUString& rExceptionMessage,
63 sal_Int32 nRow,
64 sal_Int32 nColumn,
65 const OUString& rPublicId,
66 const OUString& rSystemId);
67 ~ErrorRecord();
69 sal_Int32 nId; /// error ID
71 OUString sExceptionMessage;/// message of original exception (if available)
73 // XLocator information:
74 sal_Int32 nRow; /// row number where error occurred (or -1 for unknown)
75 sal_Int32 nColumn; /// column number where error occurred (or -1)
76 OUString sPublicId; /// public identifier
77 OUString sSystemId; /// public identifier
79 /// message Parameters
80 Sequence<OUString> aParams;
84 ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
85 const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
86 const OUString& rPublicId, const OUString& rSystemId) :
87 nId(nID),
88 sExceptionMessage(rExceptionMessage),
89 nRow(nRowNumber),
90 nColumn(nCol),
91 sPublicId(rPublicId),
92 sSystemId(rSystemId),
93 aParams(rParams)
97 ErrorRecord::~ErrorRecord()
104 XMLErrors::XMLErrors()
108 XMLErrors::~XMLErrors()
112 void XMLErrors::AddRecord(
113 sal_Int32 nId,
114 const Sequence<OUString> & rParams,
115 const OUString& rExceptionMessage,
116 sal_Int32 nRow,
117 sal_Int32 nColumn,
118 const OUString& rPublicId,
119 const OUString& rSystemId )
121 aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
122 nRow, nColumn, rPublicId, rSystemId ) );
124 #ifdef DBG_UTIL
126 // give detailed assertion on this message
128 OUStringBuffer sMessage;
130 sMessage.appendAscii( "An error or a warning has occurred during XML import/export!\n" );
132 // ID & flags
133 sMessage.appendAscii( "Error-Id: 0x");
134 sMessage.append( nId, 16 );
135 sMessage.appendAscii( "\n Flags: " );
136 sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
137 sMessage.append( nFlags >> 28, 16 );
138 if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
139 sMessage.appendAscii( " WARNING" );
140 if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
141 sMessage.appendAscii( " ERRROR" );
142 if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
143 sMessage.appendAscii( " SEVERE" );
144 sMessage.appendAscii( "\n Class: " );
145 sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
146 sMessage.append( nClass >> 16, 16 );
147 if( (nClass & XMLERROR_CLASS_IO) != 0 )
148 sMessage.appendAscii( " IO" );
149 if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
150 sMessage.appendAscii( " FORMAT" );
151 if( (nClass & XMLERROR_CLASS_API) != 0 )
152 sMessage.appendAscii( " API" );
153 if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
154 sMessage.appendAscii( " OTHER" );
155 sMessage.appendAscii( "\n Number: " );
156 sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
157 sMessage.append( nNumber, 16 );
158 sMessage.appendAscii( "\n");
160 // the parameters
161 sMessage.appendAscii( "Parameters:\n" );
162 sal_Int32 nLength = rParams.getLength();
163 const OUString* pParams = rParams.getConstArray();
164 for( sal_Int32 i = 0; i < nLength; i++ )
166 sMessage.appendAscii( " " );
167 sMessage.append( i );
168 sMessage.appendAscii( ": " );
169 sMessage.append( pParams[i] );
170 sMessage.appendAscii( "\n" );
173 // the exception message
174 sMessage.appendAscii( "Exception-Message: " );
175 sMessage.append( rExceptionMessage );
176 sMessage.appendAscii( "\n" );
178 // position (if given)
179 if( (nRow != -1) || (nColumn != -1) )
181 sMessage.appendAscii( "Position:\n Public Identifier: " );
182 sMessage.append( rPublicId );
183 sMessage.appendAscii( "\n System Identifier: " );
184 sMessage.append( rSystemId );
185 sMessage.appendAscii( "\n Row, Column: " );
186 sMessage.append( nRow );
187 sMessage.appendAscii( "," );
188 sMessage.append( nColumn );
189 sMessage.appendAscii( "\n" );
192 // convert to byte string and signal the error
193 rtl::OString aError(rtl::OUStringToOString(sMessage.makeStringAndClear(),
194 RTL_TEXTENCODING_ASCII_US));
195 OSL_FAIL( aError.getStr() );
196 #endif
199 void XMLErrors::AddRecord(
200 sal_Int32 nId,
201 const Sequence<OUString> & rParams,
202 const OUString& rExceptionMessage,
203 const Reference<XLocator> & rLocator)
205 if ( rLocator.is() )
207 AddRecord( nId, rParams, rExceptionMessage,
208 rLocator->getLineNumber(), rLocator->getColumnNumber(),
209 rLocator->getPublicId(), rLocator->getSystemId() );
211 else
213 OUString sEmpty;
214 AddRecord( nId, rParams, rExceptionMessage,
215 -1, -1, sEmpty, sEmpty );
219 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
220 throw( SAXParseException )
222 // search first error/warning that matches the nIdMask
223 for( ErrorList::iterator aIter = aErrors.begin();
224 aIter != aErrors.end();
225 ++aIter )
227 if ( (aIter->nId & nIdMask) != 0 )
229 // we throw the error
230 ErrorRecord& rErr = aErrors[0];
231 Any aAny;
232 aAny <<= rErr.aParams;
233 throw SAXParseException(
234 rErr.sExceptionMessage, NULL, aAny,
235 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */