Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / xmloff / source / core / xmlerror.cxx
bloba3ace36f71d2f031b832aa7787ee63db221bf64f
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 <utility>
21 #include <xmloff/xmlerror.hxx>
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
24 #include <com/sun/star/xml/sax/XLocator.hpp>
25 #include <com/sun/star/xml/sax/SAXParseException.hpp>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
30 #include <rtl/ustrbuf.hxx>
32 using ::com::sun::star::uno::Any;
33 using ::com::sun::star::uno::Sequence;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::xml::sax::XLocator;
36 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 OUString aExceptionMessage,
49 sal_Int32 nRow,
50 sal_Int32 nColumn,
51 OUString aPublicId,
52 OUString aSystemId);
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 OUString aExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
71 OUString aPublicId, OUString aSystemId) :
72 nId(nID),
73 sExceptionMessage(std::move(aExceptionMessage)),
74 nRow(nRowNumber),
75 nColumn(nCol),
76 sPublicId(std::move(aPublicId)),
77 sSystemId(std::move(aSystemId)),
78 aParams(rParams)
82 XMLErrors::XMLErrors()
86 XMLErrors::~XMLErrors()
90 void XMLErrors::AddRecord(
91 sal_Int32 nId,
92 const Sequence<OUString> & rParams,
93 const OUString& rExceptionMessage,
94 sal_Int32 nRow,
95 sal_Int32 nColumn,
96 const OUString& rPublicId,
97 const OUString& rSystemId )
99 aErrors.emplace_back( nId, rParams, rExceptionMessage,
100 nRow, nColumn, rPublicId, rSystemId );
102 #ifdef DBG_UTIL
104 // give detailed assertion on this message
106 OUStringBuffer sMessage;
108 sMessage.append( "An error or a warning has occurred during XML import/export!\n" );
110 // ID & flags
111 sMessage.append( "Error-Id: 0x");
112 sMessage.append( nId, 16 );
113 sMessage.append( "\n Flags: " );
114 sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
115 sMessage.append( nFlags >> 28, 16 );
116 if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
117 sMessage.append( " WARNING" );
118 if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
119 sMessage.append( " ERROR" );
120 if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
121 sMessage.append( " SEVERE" );
122 sMessage.append( "\n Class: " );
123 sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
124 sMessage.append( nClass >> 16, 16 );
125 if( (nClass & XMLERROR_CLASS_IO) != 0 )
126 sMessage.append( " IO" );
127 if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
128 sMessage.append( " FORMAT" );
129 if( (nClass & XMLERROR_CLASS_API) != 0 )
130 sMessage.append( " API" );
131 if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
132 sMessage.append( " OTHER" );
133 sMessage.append( "\n Number: " );
134 sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
135 sMessage.append( nNumber, 16 );
136 sMessage.append( "\n");
138 // the parameters
139 sMessage.append( "Parameters:\n" );
140 sal_Int32 nLength = rParams.getLength();
141 const OUString* pParams = rParams.getConstArray();
142 for( sal_Int32 i = 0; i < nLength; i++ )
144 sMessage.append( " " );
145 sMessage.append( i );
146 sMessage.append( ": " );
147 sMessage.append( pParams[i] );
148 sMessage.append( "\n" );
151 // the exception message
152 sMessage.append( "Exception-Message: " );
153 sMessage.append( rExceptionMessage );
154 sMessage.append( "\n" );
156 // position (if given)
157 if( (nRow != -1) || (nColumn != -1) )
159 sMessage.append( "Position:\n Public Identifier: " );
160 sMessage.append( rPublicId );
161 sMessage.append( "\n System Identifier: " );
162 sMessage.append( rSystemId );
163 sMessage.append( "\n Row, Column: " );
164 sMessage.append( nRow );
165 sMessage.append( "," );
166 sMessage.append( nColumn );
167 sMessage.append( "\n" );
170 SAL_WARN( "xmloff", sMessage.makeStringAndClear() );
171 #endif
174 void XMLErrors::AddRecord(
175 sal_Int32 nId,
176 const Sequence<OUString> & rParams,
177 const OUString& rExceptionMessage,
178 const Reference<XLocator> & rLocator)
180 if ( rLocator.is() )
182 AddRecord( nId, rParams, rExceptionMessage,
183 rLocator->getLineNumber(), rLocator->getColumnNumber(),
184 rLocator->getPublicId(), rLocator->getSystemId() );
186 else
188 AddRecord( nId, rParams, rExceptionMessage,
189 -1, -1, "", "" );
193 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
195 // search first error/warning that matches the nIdMask
196 for( const auto& rError : aErrors )
198 if ( (rError.nId & nIdMask) != 0 )
200 // we throw the error
201 ErrorRecord& rErr = aErrors[0];
202 throw SAXParseException(
203 rErr.sExceptionMessage, nullptr, Any(rErr.aParams),
204 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */