1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: wrdtrans.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_transex3.hxx"
35 #include "wrdtrans.hxx"
38 // NOT FULLY DECLARED SERVICES
40 #include <vos/macros.hxx>
43 #include <tools/stream.hxx>
44 #include "wtratree.hxx"
46 #include <tools/string.hxx>
48 //************** Declaration WordTrans_ErrorList ******************//
50 typedef NAMESPACE_STD(vector)<ByteString> Stl_ByteStringList;
52 class WordTrans_ErrorList
57 WordTransformer::E_Error
59 const char * i_sErrorDescription );
60 void Clear(); /// Empties the list.
63 USHORT NrOfErrors() const;
64 WordTransformer::E_Error
66 USHORT i_nNr, /// [0 .. NrOfErrors()-1], other values return an empty error.
67 ByteString * o_pErrorText ) const; /// If o_pErrorText != 0, the String is filled with the description of the error.
70 Stl_ByteStringList aErrors;
75 //************** Implementation WordTransformer ******************//
78 WordTransformer::WordTransformer()
80 dpErrors(new WordTrans_ErrorList)
84 WordTransformer::~WordTransformer()
86 if (dpTransformer != 0)
92 WordTransformer::LoadWordlist( const ByteString & i_sWordlist_Filepath,
93 CharSet i_nWorkingCharSet,
94 CharSet i_nFileCharSet )
96 if (dpTransformer != 0)
99 SvFileStream aFile(String(i_sWordlist_Filepath,RTL_TEXTENCODING_ASCII_US),STREAM_STD_READ);
100 if (! aFile.IsOpen())
102 aFile.SetStreamCharSet( i_nFileCharSet ) ;
103 // aFile.SetTargetCharSet( i_nWorkingCharSet );
105 dpTransformer = new WordTransTree;
108 while ( aFile.ReadLine(sTrans) )
110 dpTransformer->AddWordPair(sTrans.GetToken(0,';'),sTrans.GetToken(1,';'));
118 WordTransformer::Transform(ByteString & io_sText)
120 // Initialization and precondition testing:
122 if (dpTransformer == 0)
124 dpErrors->AddError(ERROR_NO_WORDLIST,"Error: No wordlist was loaded.");
125 return dpErrors->NrOfErrors();
127 else if (io_sText.Len() > 63 * 1024)
129 dpErrors->AddError(ERROR_OUTPUTSTRING_TOO_LONG,"Error: Inputstring was too long (bigger than 63 KB).");
130 return dpErrors->NrOfErrors();
132 else if (io_sText.Len() == 0)
138 dpTransformer->InitTransformation(
139 io_sText.GetBuffer(),
142 for ( ; !dpTransformer->TextEndReached(); )
144 if (dpTransformer->TransformNextToken() != WordTransTree::OK)
149 io_sText = dpTransformer->Output();
150 return dpErrors->NrOfErrors();
154 WordTransformer::NrOfErrors() const
156 return dpErrors->NrOfErrors();
159 WordTransformer::E_Error
160 WordTransformer::GetError( USHORT i_nNr,
161 ByteString * o_pErrorText) const
163 return dpErrors->GetError(i_nNr,o_pErrorText);
167 WordTransformer::CreateError()
171 switch (dpTransformer->CurResult())
173 case WordTransTree::OK:
175 case WordTransTree::HOTKEY_LOST:
176 sErr = ByteString("Error: By replacement of string ");
177 sErr += dpTransformer->CurReplacedString();
179 sErr += dpTransformer->CurReplacingString();
180 sErr += "the hotkey at char '";
181 sErr += dpTransformer->CurHotkey();
182 sErr += "' was lost.";
183 dpErrors->AddError( ERROR_HOTKEY,sErr.GetBufferAccess());
184 sErr.ReleaseBufferAccess();
186 case WordTransTree::OUTPUT_OVERFLOW:
187 dpErrors->AddError(ERROR_OUTPUTSTRING_TOO_LONG,"Error: Output buffer overflow.");
190 dpErrors->AddError(OTHER_ERROR,"Error: Unknown error.");
194 //************** Implementation WordTrans_ErrorList ******************//
197 WordTrans_ErrorList::AddError( WordTransformer::E_Error i_eType,
198 const char * i_sErrorDescription )
200 ByteString sErrorType = "xxx";
201 char * pErrorChars = sErrorType.GetBufferAccess();
202 pErrorChars[0] = char(i_eType / 100 + '0');
203 pErrorChars[1] = char( (i_eType % 100) / 10 + '0');
204 pErrorChars[2] = char(i_eType % 10 + '0');
205 sErrorType += i_sErrorDescription;
207 aErrors.push_back(sErrorType);
208 sErrorType.ReleaseBufferAccess();
212 WordTrans_ErrorList::Clear()
214 aErrors.erase(aErrors.begin(),aErrors.end());
218 WordTrans_ErrorList::NrOfErrors() const
220 return aErrors.size();
223 WordTransformer::E_Error
224 WordTrans_ErrorList::GetError( USHORT i_nNr,
225 ByteString * o_pErrorText ) const
227 if ( i_nNr < aErrors.size() )
229 const ByteString & rError = aErrors[i_nNr];
230 const char * pErrorChars = rError.GetBuffer();
232 USHORT nError = USHORT( (pErrorChars[0] - '0') ) * 100
233 + (pErrorChars[1] - '0') * 10
234 + pErrorChars[2] - '0';
236 if (o_pErrorText != 0)
237 *o_pErrorText = pErrorChars+3;
239 return WordTransformer::E_Error(nError);
243 if (o_pErrorText != 0)
245 return WordTransformer::OK;