1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
38 /// ErrorRecord: contains all information for one error
45 ErrorRecord( sal_Int32 nId
,
46 const Sequence
<OUString
>& rParams
,
47 const OUString
& rExceptionMessage
,
50 const OUString
& rPublicId
,
51 const OUString
& rSystemId
);
54 sal_Int32 nId
; /// error ID
56 OUString sExceptionMessage
;/// message of original exception (if available)
58 // XLocator information:
59 sal_Int32 nRow
; /// row number where error occurred (or -1 for unknown)
60 sal_Int32 nColumn
; /// column number where error occurred (or -1)
61 OUString sPublicId
; /// public identifier
62 OUString sSystemId
; /// public identifier
64 /// message Parameters
65 Sequence
<OUString
> aParams
;
69 ErrorRecord::ErrorRecord( sal_Int32 nID
, const Sequence
<OUString
>& rParams
,
70 const OUString
& rExceptionMessage
, sal_Int32 nRowNumber
, sal_Int32 nCol
,
71 const OUString
& rPublicId
, const OUString
& rSystemId
) :
73 sExceptionMessage(rExceptionMessage
),
82 ErrorRecord::~ErrorRecord()
87 XMLErrors::XMLErrors()
91 XMLErrors::~XMLErrors()
95 void XMLErrors::AddRecord(
97 const Sequence
<OUString
> & rParams
,
98 const OUString
& rExceptionMessage
,
101 const OUString
& rPublicId
,
102 const OUString
& rSystemId
)
104 aErrors
.push_back( ErrorRecord( nId
, rParams
, rExceptionMessage
,
105 nRow
, nColumn
, rPublicId
, rSystemId
) );
109 // give detailed assertion on this message
111 OUStringBuffer sMessage
;
113 sMessage
.append( "An error or a warning has occurred during XML import/export!\n" );
116 sMessage
.append( "Error-Id: 0x");
117 sMessage
.append( nId
, 16 );
118 sMessage
.append( "\n Flags: " );
119 sal_Int32 nFlags
= (nId
& XMLERROR_MASK_FLAG
);
120 sMessage
.append( nFlags
>> 28, 16 );
121 if( (nFlags
& XMLERROR_FLAG_WARNING
) != 0 )
122 sMessage
.append( " WARNING" );
123 if( (nFlags
& XMLERROR_FLAG_ERROR
) != 0 )
124 sMessage
.append( " ERROR" );
125 if( (nFlags
& XMLERROR_FLAG_SEVERE
) != 0 )
126 sMessage
.append( " SEVERE" );
127 sMessage
.append( "\n Class: " );
128 sal_Int32 nClass
= (nId
& XMLERROR_MASK_CLASS
);
129 sMessage
.append( nClass
>> 16, 16 );
130 if( (nClass
& XMLERROR_CLASS_IO
) != 0 )
131 sMessage
.append( " IO" );
132 if( (nClass
& XMLERROR_CLASS_FORMAT
) != 0 )
133 sMessage
.append( " FORMAT" );
134 if( (nClass
& XMLERROR_CLASS_API
) != 0 )
135 sMessage
.append( " API" );
136 if( (nClass
& XMLERROR_CLASS_OTHER
) != 0 )
137 sMessage
.append( " OTHER" );
138 sMessage
.append( "\n Number: " );
139 sal_Int32 nNumber
= (nId
& XMLERROR_MASK_NUMBER
);
140 sMessage
.append( nNumber
, 16 );
141 sMessage
.append( "\n");
144 sMessage
.append( "Parameters:\n" );
145 sal_Int32 nLength
= rParams
.getLength();
146 const OUString
* pParams
= rParams
.getConstArray();
147 for( sal_Int32 i
= 0; i
< nLength
; i
++ )
149 sMessage
.append( " " );
150 sMessage
.append( i
);
151 sMessage
.append( ": " );
152 sMessage
.append( pParams
[i
] );
153 sMessage
.append( "\n" );
156 // the exception message
157 sMessage
.append( "Exception-Message: " );
158 sMessage
.append( rExceptionMessage
);
159 sMessage
.append( "\n" );
161 // position (if given)
162 if( (nRow
!= -1) || (nColumn
!= -1) )
164 sMessage
.append( "Position:\n Public Identifier: " );
165 sMessage
.append( rPublicId
);
166 sMessage
.append( "\n System Identifier: " );
167 sMessage
.append( rSystemId
);
168 sMessage
.append( "\n Row, Column: " );
169 sMessage
.append( nRow
);
170 sMessage
.append( "," );
171 sMessage
.append( nColumn
);
172 sMessage
.append( "\n" );
175 // convert to byte string and signal the error
176 OString
aError(OUStringToOString(sMessage
.makeStringAndClear(),
177 RTL_TEXTENCODING_ASCII_US
));
178 OSL_FAIL( aError
.getStr() );
182 void XMLErrors::AddRecord(
184 const Sequence
<OUString
> & rParams
,
185 const OUString
& rExceptionMessage
,
186 const Reference
<XLocator
> & rLocator
)
190 AddRecord( nId
, rParams
, rExceptionMessage
,
191 rLocator
->getLineNumber(), rLocator
->getColumnNumber(),
192 rLocator
->getPublicId(), rLocator
->getSystemId() );
196 AddRecord( nId
, rParams
, rExceptionMessage
,
201 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask
)
202 throw( SAXParseException
)
204 // search first error/warning that matches the nIdMask
205 for( ErrorList::iterator aIter
= aErrors
.begin();
206 aIter
!= aErrors
.end();
209 if ( (aIter
->nId
& nIdMask
) != 0 )
211 // we throw the error
212 ErrorRecord
& rErr
= aErrors
[0];
213 throw SAXParseException(
214 rErr
.sExceptionMessage
, nullptr, Any(rErr
.aParams
),
215 rErr
.sPublicId
, rErr
.sSystemId
, rErr
.nRow
, rErr
.nColumn
);
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */