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