xref: /trunk/main/xmloff/source/core/xmlerror.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
163bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
363bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
463bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
563bba73cSAndrew Rist  * distributed with this work for additional information
663bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
763bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
863bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
963bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1163bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1363bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1463bba73cSAndrew Rist  * software distributed under the License is distributed on an
1563bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1663bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
1763bba73cSAndrew Rist  * specific language governing permissions and limitations
1863bba73cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2063bba73cSAndrew Rist  *************************************************************/
2163bba73cSAndrew Rist 
2263bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <rtl/ustring.hxx>
29cdf0e10cSrcweir #include <com/sun/star/xml/sax/XLocator.hpp>
30cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
31cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
32cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
33cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37cdf0e10cSrcweir #include <tools/string.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using ::rtl::OUString;
42cdf0e10cSrcweir using ::rtl::OUStringBuffer;
43cdf0e10cSrcweir using ::com::sun::star::uno::Any;
44cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
45cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
46cdf0e10cSrcweir using ::com::sun::star::xml::sax::XLocator;
47cdf0e10cSrcweir using ::com::sun::star::xml::sax::SAXParseException;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir //
51cdf0e10cSrcweir /// ErrorRecord: contains all information for one error
52cdf0e10cSrcweir //
53cdf0e10cSrcweir 
54cdf0e10cSrcweir class ErrorRecord
55cdf0e10cSrcweir {
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     ErrorRecord( sal_Int32 nId,
59cdf0e10cSrcweir                  const Sequence<OUString>& rParams,
60cdf0e10cSrcweir                  const OUString& rExceptionMessage,
61cdf0e10cSrcweir                  sal_Int32 nRow,
62cdf0e10cSrcweir                  sal_Int32 nColumn,
63cdf0e10cSrcweir                  const OUString& rPublicId,
64cdf0e10cSrcweir                  const OUString& rSystemId);
65cdf0e10cSrcweir     ~ErrorRecord();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     sal_Int32 nId;  /// error ID
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     OUString sExceptionMessage;/// message of original exception (if available)
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     // XLocator information:
72*86e1cf34SPedro Giffuni     sal_Int32 nRow;     /// row number where error occurred (or -1 for unknown)
73*86e1cf34SPedro Giffuni     sal_Int32 nColumn;  /// column number where error occurred (or -1)
74cdf0e10cSrcweir     OUString sPublicId; /// public identifier
75cdf0e10cSrcweir     OUString sSystemId; /// public identifier
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /// message Parameters
78cdf0e10cSrcweir     Sequence<OUString> aParams;
79cdf0e10cSrcweir };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
ErrorRecord(sal_Int32 nID,const Sequence<OUString> & rParams,const OUString & rExceptionMessage,sal_Int32 nRowNumber,sal_Int32 nCol,const OUString & rPublicId,const OUString & rSystemId)82cdf0e10cSrcweir ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
83cdf0e10cSrcweir     const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
84cdf0e10cSrcweir     const OUString& rPublicId, const OUString& rSystemId) :
85cdf0e10cSrcweir         nId(nID),
86cdf0e10cSrcweir         sExceptionMessage(rExceptionMessage),
87cdf0e10cSrcweir         nRow(nRowNumber),
88cdf0e10cSrcweir         nColumn(nCol),
89cdf0e10cSrcweir         sPublicId(rPublicId),
90cdf0e10cSrcweir         sSystemId(rSystemId),
91cdf0e10cSrcweir         aParams(rParams)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
~ErrorRecord()95cdf0e10cSrcweir ErrorRecord::~ErrorRecord()
96cdf0e10cSrcweir {
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
XMLErrors()102cdf0e10cSrcweir XMLErrors::XMLErrors()
103cdf0e10cSrcweir {
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
~XMLErrors()106cdf0e10cSrcweir XMLErrors::~XMLErrors()
107cdf0e10cSrcweir {
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
AddRecord(sal_Int32 nId,const Sequence<OUString> & rParams,const OUString & rExceptionMessage,sal_Int32 nRow,sal_Int32 nColumn,const OUString & rPublicId,const OUString & rSystemId)110cdf0e10cSrcweir void XMLErrors::AddRecord(
111cdf0e10cSrcweir     sal_Int32 nId,
112cdf0e10cSrcweir     const Sequence<OUString> & rParams,
113cdf0e10cSrcweir     const OUString& rExceptionMessage,
114cdf0e10cSrcweir     sal_Int32 nRow,
115cdf0e10cSrcweir     sal_Int32 nColumn,
116cdf0e10cSrcweir     const OUString& rPublicId,
117cdf0e10cSrcweir     const OUString& rSystemId )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
120cdf0e10cSrcweir                                     nRow, nColumn, rPublicId, rSystemId ) );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir #ifdef DBG_UTIL
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     // give detailed assertion on this message
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     OUStringBuffer sMessage;
127cdf0e10cSrcweir 
128*86e1cf34SPedro Giffuni     sMessage.appendAscii( "An error or a warning has occurred during XML import/export!\n" );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     // ID & flags
131cdf0e10cSrcweir     sMessage.appendAscii( "Error-Id: 0x");
132cdf0e10cSrcweir     sMessage.append( nId, 16 );
133cdf0e10cSrcweir     sMessage.appendAscii( "\n    Flags: " );
134cdf0e10cSrcweir     sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
135cdf0e10cSrcweir     sMessage.append( nFlags >> 28, 16 );
136cdf0e10cSrcweir     if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
137cdf0e10cSrcweir         sMessage.appendAscii( " WARNING" );
138cdf0e10cSrcweir     if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
139cdf0e10cSrcweir         sMessage.appendAscii( " ERRROR" );
140cdf0e10cSrcweir     if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
141cdf0e10cSrcweir         sMessage.appendAscii( " SEVERE" );
142cdf0e10cSrcweir     sMessage.appendAscii( "\n    Class: " );
143cdf0e10cSrcweir     sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
144cdf0e10cSrcweir     sMessage.append( nClass >> 16, 16 );
145cdf0e10cSrcweir     if( (nClass & XMLERROR_CLASS_IO) != 0 )
146cdf0e10cSrcweir         sMessage.appendAscii( " IO" );
147cdf0e10cSrcweir     if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
148cdf0e10cSrcweir         sMessage.appendAscii( " FORMAT" );
149cdf0e10cSrcweir     if( (nClass & XMLERROR_CLASS_API) != 0 )
150cdf0e10cSrcweir         sMessage.appendAscii( " API" );
151cdf0e10cSrcweir     if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
152cdf0e10cSrcweir         sMessage.appendAscii( " OTHER" );
153cdf0e10cSrcweir     sMessage.appendAscii( "\n    Number: " );
154cdf0e10cSrcweir     sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
155cdf0e10cSrcweir     sMessage.append( nNumber, 16 );
156cdf0e10cSrcweir     sMessage.appendAscii( "\n");
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     // the parameters
159cdf0e10cSrcweir     sMessage.appendAscii( "Parameters:\n" );
160cdf0e10cSrcweir     sal_Int32 nLength = rParams.getLength();
161cdf0e10cSrcweir     const OUString* pParams = rParams.getConstArray();
162cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nLength; i++ )
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         sMessage.appendAscii( "    " );
165cdf0e10cSrcweir         sMessage.append( i );
166cdf0e10cSrcweir         sMessage.appendAscii( ": " );
167cdf0e10cSrcweir         sMessage.append( pParams[i] );
168cdf0e10cSrcweir         sMessage.appendAscii( "\n" );
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     // the exception message
172cdf0e10cSrcweir     sMessage.appendAscii( "Exception-Message: " );
173cdf0e10cSrcweir     sMessage.append( rExceptionMessage );
174cdf0e10cSrcweir     sMessage.appendAscii( "\n" );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     // position (if given)
177cdf0e10cSrcweir     if( (nRow != -1) || (nColumn != -1) )
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         sMessage.appendAscii( "Position:\n    Public Identifier: " );
180cdf0e10cSrcweir         sMessage.append( rPublicId );
181cdf0e10cSrcweir         sMessage.appendAscii( "\n    System Identifier: " );
182cdf0e10cSrcweir         sMessage.append( rSystemId );
183cdf0e10cSrcweir         sMessage.appendAscii( "\n    Row, Column: " );
184cdf0e10cSrcweir         sMessage.append( nRow );
185cdf0e10cSrcweir         sMessage.appendAscii( "," );
186cdf0e10cSrcweir         sMessage.append( nColumn );
187cdf0e10cSrcweir         sMessage.appendAscii( "\n" );
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     // convert to byte string and signal the error
191cdf0e10cSrcweir     ByteString aError( String( sMessage.makeStringAndClear() ),
192cdf0e10cSrcweir                        RTL_TEXTENCODING_ASCII_US );
193cdf0e10cSrcweir     DBG_ERROR( aError.GetBuffer() );
194cdf0e10cSrcweir #endif
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
AddRecord(sal_Int32 nId,const Sequence<OUString> & rParams,const OUString & rExceptionMessage,const Reference<XLocator> & rLocator)197cdf0e10cSrcweir void XMLErrors::AddRecord(
198cdf0e10cSrcweir     sal_Int32 nId,
199cdf0e10cSrcweir     const Sequence<OUString> & rParams,
200cdf0e10cSrcweir     const OUString& rExceptionMessage,
201cdf0e10cSrcweir     const Reference<XLocator> & rLocator)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     if ( rLocator.is() )
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         AddRecord( nId, rParams, rExceptionMessage,
206cdf0e10cSrcweir                    rLocator->getLineNumber(), rLocator->getColumnNumber(),
207cdf0e10cSrcweir                    rLocator->getPublicId(), rLocator->getSystemId() );
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir     else
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         OUString sEmpty;
212cdf0e10cSrcweir         AddRecord( nId, rParams, rExceptionMessage,
213cdf0e10cSrcweir                    -1, -1, sEmpty, sEmpty );
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
AddRecord(sal_Int32 nId,const Sequence<OUString> & rParams,const OUString & rExceptionMessage)217cdf0e10cSrcweir void XMLErrors::AddRecord(
218cdf0e10cSrcweir     sal_Int32 nId,
219cdf0e10cSrcweir     const Sequence<OUString> & rParams,
220cdf0e10cSrcweir     const OUString& rExceptionMessage)
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     OUString sEmpty;
223cdf0e10cSrcweir     AddRecord( nId, rParams, rExceptionMessage, -1, -1, sEmpty, sEmpty );
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
AddRecord(sal_Int32 nId,const Sequence<OUString> & rParams)226cdf0e10cSrcweir void XMLErrors::AddRecord(
227cdf0e10cSrcweir     sal_Int32 nId,
228cdf0e10cSrcweir     const Sequence<OUString> & rParams)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     OUString sEmpty;
231cdf0e10cSrcweir     AddRecord( nId, rParams, sEmpty, -1, -1, sEmpty, sEmpty );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
ThrowErrorAsSAXException(sal_Int32 nIdMask)234cdf0e10cSrcweir void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
235cdf0e10cSrcweir     throw( SAXParseException )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     // search first error/warning that matches the nIdMask
238cdf0e10cSrcweir     for( ErrorList::iterator aIter = aErrors.begin();
239cdf0e10cSrcweir          aIter != aErrors.end();
240cdf0e10cSrcweir          aIter++ )
241cdf0e10cSrcweir     {
242cdf0e10cSrcweir         if ( (aIter->nId & nIdMask) != 0 )
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir             // we throw the error
245cdf0e10cSrcweir             ErrorRecord& rErr = aErrors[0];
246cdf0e10cSrcweir             Any aAny;
247cdf0e10cSrcweir             aAny <<= rErr.aParams;
248cdf0e10cSrcweir             throw SAXParseException(
249cdf0e10cSrcweir                 rErr.sExceptionMessage, NULL, aAny,
250cdf0e10cSrcweir                 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir }
254