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 #include "precompiled_reportdesign.hxx"
24 #include "reportformula.hxx"
25 
26 /** === begin UNO includes === **/
27 /** === end UNO includes === **/
28 
29 #include <rtl/ustrbuf.hxx>
30 
31 //........................................................................
32 namespace rptui
33 {
34 //........................................................................
35 
36 	/** === begin UNO using === **/
37     using ::com::sun::star::uno::Any;
38 	/** === end UNO using === **/
39 
40     namespace
41     {
42     	//----------------------------------------------------------------
lcl_getExpressionPrefix(sal_Int32 * _pTakeLengthOrNull=NULL)43         const ::rtl::OUString&  lcl_getExpressionPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
44         {
45             static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "rpt:" ) );
46             if ( _pTakeLengthOrNull )
47                 *_pTakeLengthOrNull = s_sPrefix.getLength();
48             return s_sPrefix;
49         }
50 
51         //----------------------------------------------------------------
lcl_getFieldPrefix(sal_Int32 * _pTakeLengthOrNull=NULL)52         const ::rtl::OUString&  lcl_getFieldPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
53         {
54             static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "field:" ) );
55             if ( _pTakeLengthOrNull )
56                 *_pTakeLengthOrNull = s_sPrefix.getLength();
57             return s_sPrefix;
58         }
59     }
60 
61 	//====================================================================
62 	//= ReportFormula
63 	//====================================================================
64 	//--------------------------------------------------------------------
ReportFormula(const::rtl::OUString & _rFormula)65     ReportFormula::ReportFormula( const ::rtl::OUString& _rFormula )
66         :m_eType( Invalid )
67     {
68         impl_construct( _rFormula );
69     }
70 
71 	//--------------------------------------------------------------------
ReportFormula(const BindType _eType,const::rtl::OUString & _rFieldOrExpression)72     ReportFormula::ReportFormula( const BindType _eType, const ::rtl::OUString& _rFieldOrExpression )
73         :m_eType( _eType )
74     {
75         switch ( m_eType )
76         {
77         case Expression:
78         {
79             if ( _rFieldOrExpression.indexOf( lcl_getExpressionPrefix() ) == 0 )
80                 m_sCompleteFormula = _rFieldOrExpression;
81             else
82                 m_sCompleteFormula = lcl_getExpressionPrefix() + _rFieldOrExpression;
83         }
84         break;
85 
86         case Field:
87         {
88             ::rtl::OUStringBuffer aBuffer;
89             aBuffer.append( lcl_getFieldPrefix() );
90             aBuffer.appendAscii( "[" );
91             aBuffer.append( _rFieldOrExpression );
92             aBuffer.appendAscii( "]" );
93             m_sCompleteFormula = aBuffer.makeStringAndClear();
94         }
95         break;
96         default:
97             OSL_ENSURE( false, "ReportFormula::ReportFormula: illegal bind type!" );
98             return;
99         }
100 
101         m_sUndecoratedContent = _rFieldOrExpression;
102     }
103     //--------------------------------------------------------------------
~ReportFormula()104     ReportFormula::~ReportFormula()
105     {
106     }
107 	//--------------------------------------------------------------------
impl_construct(const::rtl::OUString & _rFormula)108     void ReportFormula::impl_construct( const ::rtl::OUString& _rFormula )
109     {
110         m_sCompleteFormula = _rFormula;
111 
112         sal_Int32 nPrefixLen( -1 );
113         // is it an ordinary expression?
114         if ( m_sCompleteFormula.indexOf( lcl_getExpressionPrefix( &nPrefixLen ) ) == 0 )
115         {
116             m_eType = Expression;
117             m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
118             return;
119         }
120 
121         /// does it refer to a field?
122         if ( m_sCompleteFormula.indexOf( lcl_getFieldPrefix( &nPrefixLen ) ) == 0 )
123         {
124             if  (   ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
125                 &&  ( m_sCompleteFormula[ nPrefixLen ] == '[' )
126                 &&  ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
127                 )
128             {
129                 m_eType = Field;
130                 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
131                 return;
132             }
133         }
134 
135         m_eType = Invalid;
136     }
137 
138     //--------------------------------------------------------------------
getBracketedFieldOrExpression() const139     ::rtl::OUString ReportFormula::getBracketedFieldOrExpression() const
140     {
141         bool bIsField = ( getType() == Field );
142         ::rtl::OUStringBuffer aFieldContent;
143         if ( bIsField )
144             aFieldContent.appendAscii( "[" );
145         aFieldContent.append( getUndecoratedContent() );
146         if ( bIsField )
147             aFieldContent.appendAscii( "]" );
148 
149         return aFieldContent.makeStringAndClear();
150     }
151     //--------------------------------------------------------------------
getUndecoratedContent() const152     const ::rtl::OUString& ReportFormula::getUndecoratedContent() const
153     {
154         return m_sUndecoratedContent;
155     }
getCompleteFormula() const156     const ::rtl::OUString&  ReportFormula::getCompleteFormula() const { return m_sCompleteFormula; }
isValid() const157     bool                    ReportFormula::isValid() const { return getType() != Invalid; }
operator =(class ReportFormula const & _rHd)158     ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
159     {
160         if ( this == &_rHd )
161             return *this;
162         m_eType                 = _rHd.m_eType;
163         m_sCompleteFormula      = _rHd.m_sCompleteFormula;
164         m_sUndecoratedContent   = _rHd.m_sUndecoratedContent;
165         return *this;
166     }
167     //--------------------------------------------------------------------
getEqualUndecoratedContent() const168     ::rtl::OUString ReportFormula::getEqualUndecoratedContent() const
169     {
170         ::rtl::OUStringBuffer aBuffer;
171         aBuffer.appendAscii( "=" );
172         aBuffer.append( getUndecoratedContent() );
173         return aBuffer.makeStringAndClear();
174     }
175 
176 //........................................................................
177 } // namespace rptui
178 //........................................................................
179 
180