Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "reportformula.hxx"
21 :
22 : #include <rtl/ustrbuf.hxx>
23 :
24 :
25 : namespace rptui
26 : {
27 :
28 :
29 : using ::com::sun::star::uno::Any;
30 :
31 : namespace
32 : {
33 :
34 0 : const OUString& lcl_getExpressionPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
35 : {
36 0 : static OUString s_sPrefix( "rpt:" );
37 0 : if ( _pTakeLengthOrNull )
38 0 : *_pTakeLengthOrNull = s_sPrefix.getLength();
39 0 : return s_sPrefix;
40 : }
41 :
42 :
43 0 : const OUString& lcl_getFieldPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
44 : {
45 0 : static OUString s_sPrefix( "field:" );
46 0 : if ( _pTakeLengthOrNull )
47 0 : *_pTakeLengthOrNull = s_sPrefix.getLength();
48 0 : return s_sPrefix;
49 : }
50 : }
51 :
52 :
53 : //= ReportFormula
54 :
55 :
56 0 : ReportFormula::ReportFormula( const OUString& _rFormula )
57 0 : :m_eType( Invalid )
58 : {
59 0 : impl_construct( _rFormula );
60 0 : }
61 :
62 :
63 0 : ReportFormula::ReportFormula( const BindType _eType, const OUString& _rFieldOrExpression )
64 0 : :m_eType( _eType )
65 : {
66 0 : switch ( m_eType )
67 : {
68 : case Expression:
69 : {
70 0 : if ( _rFieldOrExpression.startsWith( lcl_getExpressionPrefix() ) )
71 0 : m_sCompleteFormula = _rFieldOrExpression;
72 : else
73 0 : m_sCompleteFormula = lcl_getExpressionPrefix() + _rFieldOrExpression;
74 : }
75 0 : break;
76 :
77 : case Field:
78 : {
79 0 : OUStringBuffer aBuffer;
80 0 : aBuffer.append( lcl_getFieldPrefix() );
81 0 : aBuffer.appendAscii( "[" );
82 0 : aBuffer.append( _rFieldOrExpression );
83 0 : aBuffer.appendAscii( "]" );
84 0 : m_sCompleteFormula = aBuffer.makeStringAndClear();
85 : }
86 0 : break;
87 : default:
88 : OSL_FAIL( "ReportFormula::ReportFormula: illegal bind type!" );
89 0 : return;
90 : }
91 :
92 0 : m_sUndecoratedContent = _rFieldOrExpression;
93 : }
94 :
95 0 : ReportFormula::~ReportFormula()
96 : {
97 0 : }
98 :
99 0 : void ReportFormula::impl_construct( const OUString& _rFormula )
100 : {
101 0 : m_sCompleteFormula = _rFormula;
102 :
103 0 : sal_Int32 nPrefixLen( -1 );
104 : // is it an ordinary expression?
105 0 : if ( m_sCompleteFormula.startsWith( lcl_getExpressionPrefix( &nPrefixLen ) ) )
106 : {
107 0 : m_eType = Expression;
108 0 : m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
109 0 : return;
110 : }
111 :
112 : /// does it refer to a field?
113 0 : if ( m_sCompleteFormula.startsWith( lcl_getFieldPrefix( &nPrefixLen ) ) )
114 : {
115 0 : if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
116 0 : && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
117 0 : && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
118 : )
119 : {
120 0 : m_eType = Field;
121 0 : m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
122 0 : return;
123 : }
124 : }
125 :
126 0 : m_eType = Invalid;
127 : }
128 :
129 :
130 0 : OUString ReportFormula::getBracketedFieldOrExpression() const
131 : {
132 0 : bool bIsField = ( getType() == Field );
133 0 : OUStringBuffer aFieldContent;
134 0 : if ( bIsField )
135 0 : aFieldContent.appendAscii( "[" );
136 0 : aFieldContent.append( getUndecoratedContent() );
137 0 : if ( bIsField )
138 0 : aFieldContent.appendAscii( "]" );
139 :
140 0 : return aFieldContent.makeStringAndClear();
141 : }
142 :
143 0 : const OUString& ReportFormula::getUndecoratedContent() const
144 : {
145 0 : return m_sUndecoratedContent;
146 : }
147 0 : const OUString& ReportFormula::getCompleteFormula() const { return m_sCompleteFormula; }
148 0 : bool ReportFormula::isValid() const { return getType() != Invalid; }
149 0 : ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
150 : {
151 0 : if ( this == &_rHd )
152 0 : return *this;
153 0 : m_eType = _rHd.m_eType;
154 0 : m_sCompleteFormula = _rHd.m_sCompleteFormula;
155 0 : m_sUndecoratedContent = _rHd.m_sUndecoratedContent;
156 0 : return *this;
157 : }
158 :
159 0 : OUString ReportFormula::getEqualUndecoratedContent() const
160 : {
161 0 : OUStringBuffer aBuffer;
162 0 : aBuffer.appendAscii( "=" );
163 0 : aBuffer.append( getUndecoratedContent() );
164 0 : return aBuffer.makeStringAndClear();
165 : }
166 :
167 :
168 : } // namespace rptui
169 :
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|