LibreOffice Module reportdesign (master)  1
reportformula.cxx
Go to the documentation of this file.
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  namespace
28  {
29 
30  const char sExpressionPrefix[] = "rpt:";
31  const char sFieldPrefix[] = "field:";
32  }
33 
34 
35  //= ReportFormula
36 
37 
38  ReportFormula::ReportFormula( const OUString& _rFormula )
39  :m_eType( Invalid )
40  {
41  m_sCompleteFormula = _rFormula;
42 
43  // is it an ordinary expression?
44  if ( m_sCompleteFormula.startsWith( sExpressionPrefix, &m_sUndecoratedContent ) )
45  {
47  return;
48  }
49 
51  if ( m_sCompleteFormula.startsWith( sFieldPrefix ) )
52  {
53  sal_Int32 nPrefixLen = strlen(sFieldPrefix);
54  if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
55  && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
56  && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
57  )
58  {
59  m_eType = Field;
60  m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
61  return;
62  }
63  }
64 
65  m_eType = Invalid;
66  }
67 
68 
69  ReportFormula::ReportFormula( const BindType _eType, const OUString& _rFieldOrExpression )
70  :m_eType( _eType )
71  {
72  switch ( m_eType )
73  {
74  case Expression:
75  {
76  if ( _rFieldOrExpression.startsWith( sExpressionPrefix ) )
77  m_sCompleteFormula = _rFieldOrExpression;
78  else
79  m_sCompleteFormula = sExpressionPrefix + _rFieldOrExpression;
80  }
81  break;
82 
83  case Field:
84  {
85  m_sCompleteFormula = sFieldPrefix + OUString::Concat(u"[") + _rFieldOrExpression + "]";
86  }
87  break;
88  default:
89  OSL_FAIL( "ReportFormula::ReportFormula: illegal bind type!" );
90  return;
91  }
92 
93  m_sUndecoratedContent = _rFieldOrExpression;
94  }
95 
97  {
98  }
99 
101  {
102  bool bIsField = ( getType() == Field );
103  OUStringBuffer aFieldContent;
104  if ( bIsField )
105  aFieldContent.append( "[" );
106  aFieldContent.append( getUndecoratedContent() );
107  if ( bIsField )
108  aFieldContent.append( "]" );
109 
110  return aFieldContent.makeStringAndClear();
111  }
112 
113  bool ReportFormula::isValid() const { return getType() != Invalid; }
114 
116  {
117  return "=" + getUndecoratedContent();
118  }
119 
120 
121 } // namespace rptui
122 
123 
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & getUndecoratedContent() const
gets the undecorated formula content
BindType getType() const
returns the type of the binding represented by the formula
OUString getBracketedFieldOrExpression() const
returns a bracketed field name of the formula denotes a field reference, or the undecorated expressio...
OUString getEqualUndecoratedContent() const
OUString m_sUndecoratedContent
float u
ReportFormula(const OUString &_rFormula)
constructs a ReportFormula object from a string
bool isValid() const
returns whether the object denotes a valid formula
const EnumerationType m_eType