merge the formfield patch from ooo-build
[ooovba.git] / reportdesign / source / core / misc / reportformula.cxx
blob6997e0da737a202972fbb392be04366ce574c3ba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: reportformula.cxx,v $
10 * $Revision: 1.4 $
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 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
31 #include "reportformula.hxx"
33 /** === begin UNO includes === **/
34 /** === end UNO includes === **/
36 #include <rtl/ustrbuf.hxx>
38 //........................................................................
39 namespace rptui
41 //........................................................................
43 /** === begin UNO using === **/
44 using ::com::sun::star::uno::Any;
45 /** === end UNO using === **/
47 namespace
49 //----------------------------------------------------------------
50 const ::rtl::OUString& lcl_getExpressionPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
52 static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "rpt:" ) );
53 if ( _pTakeLengthOrNull )
54 *_pTakeLengthOrNull = s_sPrefix.getLength();
55 return s_sPrefix;
58 //----------------------------------------------------------------
59 const ::rtl::OUString& lcl_getFieldPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
61 static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "field:" ) );
62 if ( _pTakeLengthOrNull )
63 *_pTakeLengthOrNull = s_sPrefix.getLength();
64 return s_sPrefix;
68 //====================================================================
69 //= ReportFormula
70 //====================================================================
71 //--------------------------------------------------------------------
72 ReportFormula::ReportFormula()
73 :m_eType( Invalid )
77 //--------------------------------------------------------------------
78 ReportFormula::ReportFormula( const ::rtl::OUString& _rFormula )
79 :m_eType( Invalid )
81 impl_construct( _rFormula );
84 //--------------------------------------------------------------------
85 ReportFormula::ReportFormula( const BindType _eType, const ::rtl::OUString& _rFieldOrExpression )
86 :m_eType( _eType )
88 switch ( m_eType )
90 case Expression:
92 if ( _rFieldOrExpression.indexOf( lcl_getExpressionPrefix() ) == 0 )
93 m_sCompleteFormula = _rFieldOrExpression;
94 else
95 m_sCompleteFormula = lcl_getExpressionPrefix() + _rFieldOrExpression;
97 break;
99 case Field:
101 ::rtl::OUStringBuffer aBuffer;
102 aBuffer.append( lcl_getFieldPrefix() );
103 aBuffer.appendAscii( "[" );
104 aBuffer.append( _rFieldOrExpression );
105 aBuffer.appendAscii( "]" );
106 m_sCompleteFormula = aBuffer.makeStringAndClear();
108 break;
109 default:
110 OSL_ENSURE( false, "ReportFormula::ReportFormula: illegal bind type!" );
111 return;
114 m_sUndecoratedContent = _rFieldOrExpression;
116 //--------------------------------------------------------------------
117 ReportFormula::~ReportFormula()
120 //--------------------------------------------------------------------
121 void ReportFormula::impl_construct( const ::rtl::OUString& _rFormula )
123 m_sCompleteFormula = _rFormula;
125 sal_Int32 nPrefixLen( -1 );
126 // is it an ordinary expression?
127 if ( m_sCompleteFormula.indexOf( lcl_getExpressionPrefix( &nPrefixLen ) ) == 0 )
129 m_eType = Expression;
130 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
131 return;
134 /// does it refer to a field?
135 if ( m_sCompleteFormula.indexOf( lcl_getFieldPrefix( &nPrefixLen ) ) == 0 )
137 if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
138 && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
139 && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
142 m_eType = Field;
143 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
144 return;
148 m_eType = Invalid;
151 //--------------------------------------------------------------------
152 ::rtl::OUString ReportFormula::getBracketedFieldOrExpression() const
154 bool bIsField = ( getType() == Field );
155 ::rtl::OUStringBuffer aFieldContent;
156 if ( bIsField )
157 aFieldContent.appendAscii( "[" );
158 aFieldContent.append( getUndecoratedContent() );
159 if ( bIsField )
160 aFieldContent.appendAscii( "]" );
162 return aFieldContent.makeStringAndClear();
164 //--------------------------------------------------------------------
165 const ::rtl::OUString& ReportFormula::getUndecoratedContent() const
167 return m_sUndecoratedContent;
169 const ::rtl::OUString& ReportFormula::getCompleteFormula() const { return m_sCompleteFormula; }
170 bool ReportFormula::isValid() const { return getType() != Invalid; }
171 ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
173 if ( this == &_rHd )
174 return *this;
175 m_eType = _rHd.m_eType;
176 m_sCompleteFormula = _rHd.m_sCompleteFormula;
177 m_sUndecoratedContent = _rHd.m_sUndecoratedContent;
178 return *this;
180 //--------------------------------------------------------------------
181 ::rtl::OUString ReportFormula::getEqualUndecoratedContent() const
183 ::rtl::OUStringBuffer aBuffer;
184 aBuffer.appendAscii( "=" );
185 aBuffer.append( getUndecoratedContent() );
186 return aBuffer.makeStringAndClear();
189 //........................................................................
190 } // namespace rptui
191 //........................................................................