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 : static const char sExpressionPrefix[] = "rpt:";
35 : static const char sFieldPrefix[] = "field:";
36 : }
37 :
38 :
39 : //= ReportFormula
40 :
41 :
42 0 : ReportFormula::ReportFormula( const OUString& _rFormula )
43 0 : :m_eType( Invalid )
44 : {
45 0 : impl_construct( _rFormula );
46 0 : }
47 :
48 :
49 0 : ReportFormula::ReportFormula( const BindType _eType, const OUString& _rFieldOrExpression )
50 0 : :m_eType( _eType )
51 : {
52 0 : switch ( m_eType )
53 : {
54 : case Expression:
55 : {
56 0 : if ( _rFieldOrExpression.startsWith( sExpressionPrefix ) )
57 0 : m_sCompleteFormula = _rFieldOrExpression;
58 : else
59 0 : m_sCompleteFormula = sExpressionPrefix + _rFieldOrExpression;
60 : }
61 0 : break;
62 :
63 : case Field:
64 : {
65 0 : OUStringBuffer aBuffer;
66 0 : aBuffer.append( sFieldPrefix );
67 0 : aBuffer.appendAscii( "[" );
68 0 : aBuffer.append( _rFieldOrExpression );
69 0 : aBuffer.appendAscii( "]" );
70 0 : m_sCompleteFormula = aBuffer.makeStringAndClear();
71 : }
72 0 : break;
73 : default:
74 : OSL_FAIL( "ReportFormula::ReportFormula: illegal bind type!" );
75 0 : return;
76 : }
77 :
78 0 : m_sUndecoratedContent = _rFieldOrExpression;
79 : }
80 :
81 0 : ReportFormula::~ReportFormula()
82 : {
83 0 : }
84 :
85 0 : void ReportFormula::impl_construct( const OUString& _rFormula )
86 : {
87 0 : m_sCompleteFormula = _rFormula;
88 :
89 : // is it an ordinary expression?
90 0 : if ( m_sCompleteFormula.startsWith( sExpressionPrefix ) )
91 : {
92 0 : sal_Int32 nPrefixLen = strlen(sExpressionPrefix);
93 0 : m_eType = Expression;
94 0 : m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
95 0 : return;
96 : }
97 :
98 : /// does it refer to a field?
99 0 : if ( m_sCompleteFormula.startsWith( sFieldPrefix ) )
100 : {
101 0 : sal_Int32 nPrefixLen = strlen(sFieldPrefix);
102 0 : if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
103 0 : && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
104 0 : && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
105 : )
106 : {
107 0 : m_eType = Field;
108 0 : m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
109 0 : return;
110 : }
111 : }
112 :
113 0 : m_eType = Invalid;
114 : }
115 :
116 :
117 0 : OUString ReportFormula::getBracketedFieldOrExpression() const
118 : {
119 0 : bool bIsField = ( getType() == Field );
120 0 : OUStringBuffer aFieldContent;
121 0 : if ( bIsField )
122 0 : aFieldContent.appendAscii( "[" );
123 0 : aFieldContent.append( getUndecoratedContent() );
124 0 : if ( bIsField )
125 0 : aFieldContent.appendAscii( "]" );
126 :
127 0 : return aFieldContent.makeStringAndClear();
128 : }
129 :
130 0 : bool ReportFormula::isValid() const { return getType() != Invalid; }
131 0 : ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
132 : {
133 0 : if ( this == &_rHd )
134 0 : return *this;
135 0 : m_eType = _rHd.m_eType;
136 0 : m_sCompleteFormula = _rHd.m_sCompleteFormula;
137 0 : m_sUndecoratedContent = _rHd.m_sUndecoratedContent;
138 0 : return *this;
139 : }
140 :
141 0 : OUString ReportFormula::getEqualUndecoratedContent() const
142 : {
143 0 : OUStringBuffer aBuffer;
144 0 : aBuffer.appendAscii( "=" );
145 0 : aBuffer.append( getUndecoratedContent() );
146 0 : return aBuffer.makeStringAndClear();
147 : }
148 :
149 :
150 : } // namespace rptui
151 :
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|