sync master with lastest vba changes
[ooovba.git] / reportdesign / source / core / misc / reportformula.cxx
blobe8f8f195613239bc1d48eb89aa8a0385fa65f7d2
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;
117 //--------------------------------------------------------------------
118 void ReportFormula::impl_construct( const ::rtl::OUString& _rFormula )
120 m_sCompleteFormula = _rFormula;
122 sal_Int32 nPrefixLen( -1 );
123 // is it an ordinary expression?
124 if ( m_sCompleteFormula.indexOf( lcl_getExpressionPrefix( &nPrefixLen ) ) == 0 )
126 m_eType = Expression;
127 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
128 return;
131 /// does it refer to a field?
132 if ( m_sCompleteFormula.indexOf( lcl_getFieldPrefix( &nPrefixLen ) ) == 0 )
134 if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
135 && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
136 && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
139 m_eType = Field;
140 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
141 return;
145 m_eType = Invalid;
148 //--------------------------------------------------------------------
149 ::rtl::OUString ReportFormula::getBracketedFieldOrExpression() const
151 bool bIsField = ( getType() == Field );
152 ::rtl::OUStringBuffer aFieldContent;
153 if ( bIsField )
154 aFieldContent.appendAscii( "[" );
155 aFieldContent.append( getUndecoratedContent() );
156 if ( bIsField )
157 aFieldContent.appendAscii( "]" );
159 return aFieldContent.makeStringAndClear();
162 //........................................................................
163 } // namespace rptui
164 //........................................................................