bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / core / xmlerror.cxx
blobfeb4ac0bdfe7aa6f771a0e73a7ba9e1ea6917f0d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <xmloff/xmlerror.hxx>
21 #include <rtl/ustring.hxx>
22 #include <osl/diagnose.h>
23 #include <com/sun/star/xml/sax/XLocator.hpp>
24 #include <com/sun/star/xml/sax/SAXParseException.hpp>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
29 #include <rtl/ustrbuf.hxx>
31 using ::com::sun::star::uno::Any;
32 using ::com::sun::star::uno::Sequence;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::xml::sax::XLocator;
35 using ::com::sun::star::xml::sax::SAXParseException;
39 /// ErrorRecord: contains all information for one error
42 class ErrorRecord
44 public:
46 ErrorRecord( sal_Int32 nId,
47 const Sequence<OUString>& rParams,
48 const OUString& rExceptionMessage,
49 sal_Int32 nRow,
50 sal_Int32 nColumn,
51 const OUString& rPublicId,
52 const OUString& rSystemId);
53 ~ErrorRecord();
55 sal_Int32 nId; /// error ID
57 OUString sExceptionMessage;/// message of original exception (if available)
59 // XLocator information:
60 sal_Int32 nRow; /// row number where error occurred (or -1 for unknown)
61 sal_Int32 nColumn; /// column number where error occurred (or -1)
62 OUString sPublicId; /// public identifier
63 OUString sSystemId; /// public identifier
65 /// message Parameters
66 Sequence<OUString> aParams;
70 ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
71 const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
72 const OUString& rPublicId, const OUString& rSystemId) :
73 nId(nID),
74 sExceptionMessage(rExceptionMessage),
75 nRow(nRowNumber),
76 nColumn(nCol),
77 sPublicId(rPublicId),
78 sSystemId(rSystemId),
79 aParams(rParams)
83 ErrorRecord::~ErrorRecord()
90 XMLErrors::XMLErrors()
94 XMLErrors::~XMLErrors()
98 void XMLErrors::AddRecord(
99 sal_Int32 nId,
100 const Sequence<OUString> & rParams,
101 const OUString& rExceptionMessage,
102 sal_Int32 nRow,
103 sal_Int32 nColumn,
104 const OUString& rPublicId,
105 const OUString& rSystemId )
107 aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
108 nRow, nColumn, rPublicId, rSystemId ) );
110 #ifdef DBG_UTIL
112 // give detailed assertion on this message
114 OUStringBuffer sMessage;
116 sMessage.appendAscii( "An error or a warning has occurred during XML import/export!\n" );
118 // ID & flags
119 sMessage.appendAscii( "Error-Id: 0x");
120 sMessage.append( nId, 16 );
121 sMessage.appendAscii( "\n Flags: " );
122 sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
123 sMessage.append( nFlags >> 28, 16 );
124 if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
125 sMessage.appendAscii( " WARNING" );
126 if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
127 sMessage.appendAscii( " ERROR" );
128 if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
129 sMessage.appendAscii( " SEVERE" );
130 sMessage.appendAscii( "\n Class: " );
131 sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
132 sMessage.append( nClass >> 16, 16 );
133 if( (nClass & XMLERROR_CLASS_IO) != 0 )
134 sMessage.appendAscii( " IO" );
135 if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
136 sMessage.appendAscii( " FORMAT" );
137 if( (nClass & XMLERROR_CLASS_API) != 0 )
138 sMessage.appendAscii( " API" );
139 if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
140 sMessage.appendAscii( " OTHER" );
141 sMessage.appendAscii( "\n Number: " );
142 sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
143 sMessage.append( nNumber, 16 );
144 sMessage.appendAscii( "\n");
146 // the parameters
147 sMessage.appendAscii( "Parameters:\n" );
148 sal_Int32 nLength = rParams.getLength();
149 const OUString* pParams = rParams.getConstArray();
150 for( sal_Int32 i = 0; i < nLength; i++ )
152 sMessage.appendAscii( " " );
153 sMessage.append( i );
154 sMessage.appendAscii( ": " );
155 sMessage.append( pParams[i] );
156 sMessage.appendAscii( "\n" );
159 // the exception message
160 sMessage.appendAscii( "Exception-Message: " );
161 sMessage.append( rExceptionMessage );
162 sMessage.appendAscii( "\n" );
164 // position (if given)
165 if( (nRow != -1) || (nColumn != -1) )
167 sMessage.appendAscii( "Position:\n Public Identifier: " );
168 sMessage.append( rPublicId );
169 sMessage.appendAscii( "\n System Identifier: " );
170 sMessage.append( rSystemId );
171 sMessage.appendAscii( "\n Row, Column: " );
172 sMessage.append( nRow );
173 sMessage.appendAscii( "," );
174 sMessage.append( nColumn );
175 sMessage.appendAscii( "\n" );
178 // convert to byte string and signal the error
179 OString aError(OUStringToOString(sMessage.makeStringAndClear(),
180 RTL_TEXTENCODING_ASCII_US));
181 OSL_FAIL( aError.getStr() );
182 #endif
185 void XMLErrors::AddRecord(
186 sal_Int32 nId,
187 const Sequence<OUString> & rParams,
188 const OUString& rExceptionMessage,
189 const Reference<XLocator> & rLocator)
191 if ( rLocator.is() )
193 AddRecord( nId, rParams, rExceptionMessage,
194 rLocator->getLineNumber(), rLocator->getColumnNumber(),
195 rLocator->getPublicId(), rLocator->getSystemId() );
197 else
199 OUString sEmpty;
200 AddRecord( nId, rParams, rExceptionMessage,
201 -1, -1, sEmpty, sEmpty );
205 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
206 throw( SAXParseException )
208 // search first error/warning that matches the nIdMask
209 for( ErrorList::iterator aIter = aErrors.begin();
210 aIter != aErrors.end();
211 ++aIter )
213 if ( (aIter->nId & nIdMask) != 0 )
215 // we throw the error
216 ErrorRecord& rErr = aErrors[0];
217 Any aAny;
218 aAny <<= rErr.aParams;
219 throw SAXParseException(
220 rErr.sExceptionMessage, NULL, aAny,
221 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */