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 <xmloff/xmlerror.hxx>
21 #include <rtl/ustring.hxx>
22 #include <sal/log.hxx>
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>
28 
29 #include <rtl/ustrbuf.hxx>
30 
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;
36 
37 
39 
40 
42 {
43 public:
44 
45  ErrorRecord( sal_Int32 nId,
46  const Sequence<OUString>& rParams,
47  const OUString& rExceptionMessage,
48  sal_Int32 nRow,
49  sal_Int32 nColumn,
50  const OUString& rPublicId,
51  const OUString& rSystemId);
52 
53  sal_Int32 nId;
54 
56 
57  // XLocator information:
58  sal_Int32 nRow;
59  sal_Int32 nColumn;
60  OUString sPublicId;
61  OUString sSystemId;
62 
65 };
66 
67 
68 ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
69  const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
70  const OUString& rPublicId, const OUString& rSystemId) :
71  nId(nID),
72  sExceptionMessage(rExceptionMessage),
73  nRow(nRowNumber),
74  nColumn(nCol),
75  sPublicId(rPublicId),
76  sSystemId(rSystemId),
77  aParams(rParams)
78 {
79 }
80 
82 {
83 }
84 
86 {
87 }
88 
90  sal_Int32 nId,
91  const Sequence<OUString> & rParams,
92  const OUString& rExceptionMessage,
93  sal_Int32 nRow,
94  sal_Int32 nColumn,
95  const OUString& rPublicId,
96  const OUString& rSystemId )
97 {
98  aErrors.emplace_back( nId, rParams, rExceptionMessage,
99  nRow, nColumn, rPublicId, rSystemId );
100 
101 #ifdef DBG_UTIL
102 
103  // give detailed assertion on this message
104 
105  OUStringBuffer sMessage;
106 
107  sMessage.append( "An error or a warning has occurred during XML import/export!\n" );
108 
109  // ID & flags
110  sMessage.append( "Error-Id: 0x");
111  sMessage.append( nId, 16 );
112  sMessage.append( "\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( nNumber, 16 );
135  sMessage.append( "\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( " " );
144  sMessage.append( i );
145  sMessage.append( ": " );
146  sMessage.append( pParams[i] );
147  sMessage.append( "\n" );
148  }
149 
150  // the exception message
151  sMessage.append( "Exception-Message: " );
152  sMessage.append( rExceptionMessage );
153  sMessage.append( "\n" );
154 
155  // position (if given)
156  if( (nRow != -1) || (nColumn != -1) )
157  {
158  sMessage.append( "Position:\n Public Identifier: " );
159  sMessage.append( rPublicId );
160  sMessage.append( "\n System Identifier: " );
161  sMessage.append( rSystemId );
162  sMessage.append( "\n Row, Column: " );
163  sMessage.append( nRow );
164  sMessage.append( "," );
165  sMessage.append( nColumn );
166  sMessage.append( "\n" );
167  }
168 
169  SAL_WARN( "xmloff", sMessage.makeStringAndClear() );
170 #endif
171 }
172 
174  sal_Int32 nId,
175  const Sequence<OUString> & rParams,
176  const OUString& rExceptionMessage,
177  const Reference<XLocator> & rLocator)
178 {
179  if ( rLocator.is() )
180  {
181  AddRecord( nId, rParams, rExceptionMessage,
182  rLocator->getLineNumber(), rLocator->getColumnNumber(),
183  rLocator->getPublicId(), rLocator->getSystemId() );
184  }
185  else
186  {
187  AddRecord( nId, rParams, rExceptionMessage,
188  -1, -1, "", "" );
189  }
190 }
191 
192 void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
193 {
194  // search first error/warning that matches the nIdMask
195  for( const auto& rError : aErrors )
196  {
197  if ( (rError.nId & nIdMask) != 0 )
198  {
199  // we throw the error
200  ErrorRecord& rErr = aErrors[0];
201  throw SAXParseException(
202  rErr.sExceptionMessage, nullptr, Any(rErr.aParams),
203  rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
204  }
205  }
206 }
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define XMLERROR_MASK_FLAG
Definition: xmlerror.hxx:30
Sequence< OUString > aParams
public identifier
Definition: xmlerror.cxx:64
OUString sMessage
#define XMLERROR_CLASS_OTHER
Definition: xmlerror.hxx:43
sal_Int32 nRow
message of original exception (if available)
Definition: xmlerror.cxx:58
sal_Int16 nId
#define XMLERROR_CLASS_IO
Definition: xmlerror.hxx:40
OUString sPublicId
column number where error occurred (or -1)
Definition: xmlerror.cxx:60
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
XMLErrors()
list of error records
Definition: xmlerror.cxx:81
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
#define XMLERROR_CLASS_API
Definition: xmlerror.hxx:42
#define XMLERROR_CLASS_FORMAT
Definition: xmlerror.hxx:41
int i
ErrorRecord: contains all information for one error.
Definition: xmlerror.cxx:41
ErrorRecord(sal_Int32 nId, const Sequence< OUString > &rParams, const OUString &rExceptionMessage, sal_Int32 nRow, sal_Int32 nColumn, const OUString &rPublicId, const OUString &rSystemId)
Definition: xmlerror.cxx:68
OUString sSystemId
public identifier
Definition: xmlerror.cxx:61
ErrorList aErrors
Definition: xmlerror.hxx:107
#define XMLERROR_FLAG_SEVERE
Definition: xmlerror.hxx:37
#define XMLERROR_MASK_CLASS
Definition: xmlerror.hxx:31
#define XMLERROR_FLAG_ERROR
Definition: xmlerror.hxx:36
#define SAL_WARN(area, stream)
OUString sExceptionMessage
error ID
Definition: xmlerror.cxx:55
sal_Int32 nLength
Definition: xmltoken.cxx:36
void ThrowErrorAsSAXException(sal_Int32 nIdMask)
location
Definition: xmlerror.cxx:192
sal_Int32 nColumn
row number where error occurred (or -1 for unknown)
Definition: xmlerror.cxx:59
#define XMLERROR_MASK_NUMBER
Definition: xmlerror.hxx:32
sal_Int32 nId
Definition: xmlerror.cxx:53
~XMLErrors()
Definition: xmlerror.cxx:85