1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "precompiled_reportdesign.hxx"
28 #include "reportformula.hxx"
29 
30 /** === begin UNO includes === **/
31 /** === end UNO includes === **/
32 
33 #include <rtl/ustrbuf.hxx>
34 
35 //........................................................................
36 namespace rptui
37 {
38 //........................................................................
39 
40 	/** === begin UNO using === **/
41     using ::com::sun::star::uno::Any;
42 	/** === end UNO using === **/
43 
44     namespace
45     {
46     	//----------------------------------------------------------------
47         const ::rtl::OUString&  lcl_getExpressionPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
48         {
49             static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "rpt:" ) );
50             if ( _pTakeLengthOrNull )
51                 *_pTakeLengthOrNull = s_sPrefix.getLength();
52             return s_sPrefix;
53         }
54 
55         //----------------------------------------------------------------
56         const ::rtl::OUString&  lcl_getFieldPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
57         {
58             static ::rtl::OUString s_sPrefix( RTL_CONSTASCII_USTRINGPARAM( "field:" ) );
59             if ( _pTakeLengthOrNull )
60                 *_pTakeLengthOrNull = s_sPrefix.getLength();
61             return s_sPrefix;
62         }
63     }
64 
65 	//====================================================================
66 	//= ReportFormula
67 	//====================================================================
68 	//--------------------------------------------------------------------
69     ReportFormula::ReportFormula( const ::rtl::OUString& _rFormula )
70         :m_eType( Invalid )
71     {
72         impl_construct( _rFormula );
73     }
74 
75 	//--------------------------------------------------------------------
76     ReportFormula::ReportFormula( const BindType _eType, const ::rtl::OUString& _rFieldOrExpression )
77         :m_eType( _eType )
78     {
79         switch ( m_eType )
80         {
81         case Expression:
82         {
83             if ( _rFieldOrExpression.indexOf( lcl_getExpressionPrefix() ) == 0 )
84                 m_sCompleteFormula = _rFieldOrExpression;
85             else
86                 m_sCompleteFormula = lcl_getExpressionPrefix() + _rFieldOrExpression;
87         }
88         break;
89 
90         case Field:
91         {
92             ::rtl::OUStringBuffer aBuffer;
93             aBuffer.append( lcl_getFieldPrefix() );
94             aBuffer.appendAscii( "[" );
95             aBuffer.append( _rFieldOrExpression );
96             aBuffer.appendAscii( "]" );
97             m_sCompleteFormula = aBuffer.makeStringAndClear();
98         }
99         break;
100         default:
101             OSL_ENSURE( false, "ReportFormula::ReportFormula: illegal bind type!" );
102             return;
103         }
104 
105         m_sUndecoratedContent = _rFieldOrExpression;
106     }
107     //--------------------------------------------------------------------
108     ReportFormula::~ReportFormula()
109     {
110     }
111 	//--------------------------------------------------------------------
112     void ReportFormula::impl_construct( const ::rtl::OUString& _rFormula )
113     {
114         m_sCompleteFormula = _rFormula;
115 
116         sal_Int32 nPrefixLen( -1 );
117         // is it an ordinary expression?
118         if ( m_sCompleteFormula.indexOf( lcl_getExpressionPrefix( &nPrefixLen ) ) == 0 )
119         {
120             m_eType = Expression;
121             m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
122             return;
123         }
124 
125         /// does it refer to a field?
126         if ( m_sCompleteFormula.indexOf( lcl_getFieldPrefix( &nPrefixLen ) ) == 0 )
127         {
128             if  (   ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
129                 &&  ( m_sCompleteFormula[ nPrefixLen ] == '[' )
130                 &&  ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
131                 )
132             {
133                 m_eType = Field;
134                 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
135                 return;
136             }
137         }
138 
139         m_eType = Invalid;
140     }
141 
142     //--------------------------------------------------------------------
143     ::rtl::OUString ReportFormula::getBracketedFieldOrExpression() const
144     {
145         bool bIsField = ( getType() == Field );
146         ::rtl::OUStringBuffer aFieldContent;
147         if ( bIsField )
148             aFieldContent.appendAscii( "[" );
149         aFieldContent.append( getUndecoratedContent() );
150         if ( bIsField )
151             aFieldContent.appendAscii( "]" );
152 
153         return aFieldContent.makeStringAndClear();
154     }
155     //--------------------------------------------------------------------
156     const ::rtl::OUString& ReportFormula::getUndecoratedContent() const
157     {
158         return m_sUndecoratedContent;
159     }
160     const ::rtl::OUString&  ReportFormula::getCompleteFormula() const { return m_sCompleteFormula; }
161     bool                    ReportFormula::isValid() const { return getType() != Invalid; }
162     ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
163     {
164         if ( this == &_rHd )
165             return *this;
166         m_eType                 = _rHd.m_eType;
167         m_sCompleteFormula      = _rHd.m_sCompleteFormula;
168         m_sUndecoratedContent   = _rHd.m_sUndecoratedContent;
169         return *this;
170     }
171     //--------------------------------------------------------------------
172     ::rtl::OUString ReportFormula::getEqualUndecoratedContent() const
173     {
174         ::rtl::OUStringBuffer aBuffer;
175         aBuffer.appendAscii( "=" );
176         aBuffer.append( getUndecoratedContent() );
177         return aBuffer.makeStringAndClear();
178     }
179 
180 //........................................................................
181 } // namespace rptui
182 //........................................................................
183 
184