Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / core / xmlerror.cxx
blobdf99c4a281fb7ae74b2379363742313f537c0465
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 m_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( "An error or a warning has occurred during XML import/export!\n" );
108 // ID & flags
109 sMessage.append(
110 "Error-Id: 0x"
111 + OUString::number( nId, 16 )
112 + "\n Flags: " );
113 sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
114 sMessage.append( nFlags >> 28, 16 );
115 if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
116 sMessage.append( " WARNING" );
117 if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
118 sMessage.append( " ERROR" );
119 if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
120 sMessage.append( " SEVERE" );
121 sMessage.append( "\n Class: " );
122 sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
123 sMessage.append( nClass >> 16, 16 );
124 if( (nClass & XMLERROR_CLASS_IO) != 0 )
125 sMessage.append( " IO" );
126 if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
127 sMessage.append( " FORMAT" );
128 if( (nClass & XMLERROR_CLASS_API) != 0 )
129 sMessage.append( " API" );
130 if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
131 sMessage.append( " OTHER" );
132 sMessage.append( "\n Number: " );
133 sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
134 sMessage.append( OUString::number(nNumber, 16)
135 + "\n" );
137 // the parameters
138 sMessage.append( "Parameters:\n" );
139 sal_Int32 nLength = rParams.getLength();
140 const OUString* pParams = rParams.getConstArray();
141 for( sal_Int32 i = 0; i < nLength; i++ )
143 sMessage.append( " " + OUString::number(i) + ": " + pParams[i] + "\n" );
146 // the exception message
147 sMessage.append( "Exception-Message: " + rExceptionMessage + "\n" );
149 // position (if given)
150 if( (nRow != -1) || (nColumn != -1) )
152 sMessage.append( "Position:\n Public Identifier: "
153 + rPublicId
154 + "\n System Identifier: "
155 + rSystemId
156 + "\n Row, Column: "
157 + OUString::number( nRow )
158 + ","
159 + OUString::number( nColumn )
160 + "\n" );
163 SAL_WARN( "xmloff", sMessage.makeStringAndClear() );
164 #endif
167 void XMLErrors::AddRecord(
168 sal_Int32 nId,
169 const Sequence<OUString> & rParams,
170 const OUString& rExceptionMessage,
171 const Reference<XLocator> & rLocator)
173 if ( rLocator.is() )
175 AddRecord( nId, rParams, rExceptionMessage,
176 rLocator->getLineNumber(), rLocator->getColumnNumber(),
177 rLocator->getPublicId(), rLocator->getSystemId() );
179 else
181 AddRecord( nId, rParams, rExceptionMessage,
182 -1, -1, "", "" );
186 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
188 // search first error/warning that matches the nIdMask
189 for( const auto& rError : m_aErrors )
191 if ( (rError.nId & nIdMask) != 0 )
193 // we throw the error
194 ErrorRecord& rErr = m_aErrors[0];
195 throw SAXParseException(
196 rErr.sExceptionMessage, nullptr, Any(rErr.aParams),
197 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */