LibreOffice Module xmloff (master) 1
xmlerror.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
29
30#include <rtl/ustrbuf.hxx>
31
32using ::com::sun::star::uno::Any;
33using ::com::sun::star::uno::Sequence;
34using ::com::sun::star::uno::Reference;
35using ::com::sun::star::xml::sax::XLocator;
36using ::com::sun::star::xml::sax::SAXParseException;
37
38
40
41
43{
44public:
45
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);
53
54 sal_Int32 nId;
55
57
58 // XLocator information:
59 sal_Int32 nRow;
60 sal_Int32 nColumn;
61 OUString sPublicId;
62 OUString sSystemId;
63
65 Sequence<OUString> aParams;
66};
67
68
69ErrorRecord::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)
79{
80}
81
83{
84}
85
87{
88}
89
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 )
98{
99 m_aErrors.emplace_back( nId, rParams, rExceptionMessage,
100 nRow, nColumn, rPublicId, rSystemId );
101
102#ifdef DBG_UTIL
103
104 // give detailed assertion on this message
105
106 OUStringBuffer sMessage( "An error or a warning has occurred during XML import/export!\n" );
107
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" );
136
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++ )
142 {
143 sMessage.append( " " + OUString::number(i) + ": " + pParams[i] + "\n" );
144 }
145
146 // the exception message
147 sMessage.append( "Exception-Message: " + rExceptionMessage + "\n" );
148
149 // position (if given)
150 if( (nRow != -1) || (nColumn != -1) )
151 {
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" );
161 }
162
163 SAL_WARN( "xmloff", sMessage.makeStringAndClear() );
164#endif
165}
166
168 sal_Int32 nId,
169 const Sequence<OUString> & rParams,
170 const OUString& rExceptionMessage,
171 const Reference<XLocator> & rLocator)
172{
173 if ( rLocator.is() )
174 {
175 AddRecord( nId, rParams, rExceptionMessage,
176 rLocator->getLineNumber(), rLocator->getColumnNumber(),
177 rLocator->getPublicId(), rLocator->getSystemId() );
178 }
179 else
180 {
181 AddRecord( nId, rParams, rExceptionMessage,
182 -1, -1, "", "" );
183 }
184}
185
187{
188 // search first error/warning that matches the nIdMask
189 for( const auto& rError : m_aErrors )
190 {
191 if ( (rError.nId & nIdMask) != 0 )
192 {
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 );
198 }
199 }
200}
201
202/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ErrorRecord: contains all information for one error.
Definition: xmlerror.cxx:43
sal_Int32 nId
Definition: xmlerror.cxx:54
sal_Int32 nRow
message of original exception (if available)
Definition: xmlerror.cxx:59
sal_Int32 nColumn
row number where error occurred (or -1 for unknown)
Definition: xmlerror.cxx:60
Sequence< OUString > aParams
public identifier
Definition: xmlerror.cxx:65
OUString sPublicId
column number where error occurred (or -1)
Definition: xmlerror.cxx:61
OUString sExceptionMessage
error ID
Definition: xmlerror.cxx:56
ErrorRecord(sal_Int32 nId, const Sequence< OUString > &rParams, OUString aExceptionMessage, sal_Int32 nRow, sal_Int32 nColumn, OUString aPublicId, OUString aSystemId)
Definition: xmlerror.cxx:69
OUString sSystemId
public identifier
Definition: xmlerror.cxx:62
XMLErrors()
list of error records
Definition: xmlerror.cxx:82
void ThrowErrorAsSAXException(sal_Int32 nIdMask)
location
Definition: xmlerror.cxx:186
ErrorList m_aErrors
Definition: xmlerror.hxx:107
~XMLErrors()
Definition: xmlerror.cxx:86
void AddRecord(sal_Int32 nId, const css::uno::Sequence< OUString > &rParams, const OUString &rExceptionMessage, sal_Int32 nRow, sal_Int32 nColumn, const OUString &rPublicId, const OUString &rSystemId)
add a new entry to the list of error messages
#define SAL_WARN(area, stream)
int i
sal_Int16 nId
OUString sMessage
#define XMLERROR_CLASS_OTHER
Definition: xmlerror.hxx:43
#define XMLERROR_MASK_FLAG
Definition: xmlerror.hxx:30
#define XMLERROR_CLASS_FORMAT
Definition: xmlerror.hxx:41
#define XMLERROR_CLASS_IO
Definition: xmlerror.hxx:40
#define XMLERROR_MASK_NUMBER
Definition: xmlerror.hxx:32
#define XMLERROR_FLAG_ERROR
Definition: xmlerror.hxx:36
#define XMLERROR_CLASS_API
Definition: xmlerror.hxx:42
#define XMLERROR_FLAG_SEVERE
Definition: xmlerror.hxx:37
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
#define XMLERROR_MASK_CLASS
Definition: xmlerror.hxx:31
sal_Int32 nLength
Definition: xmltoken.cxx:38