summaryrefslogtreecommitdiff
path: root/writerfilter/qa/cppunittests/odiapi/ExternalViewLogger.cxx
blob: 996112740fac0d1cb20c74b4eda40d1893aea465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "ExternalViewLogger.hxx"
#include <iostream>
#include <boost/assert.hpp>
#include <stdio.h>

#ifdef WNT
    #define SNPRINTF(buffer, size, format, args) _snprintf(buffer, size, format, args)
#else
    #define SNPRINTF(buffer, size, format, args) snprintf(buffer, size, format, args)
#endif

using namespace std;

namespace util
{

  NodeDescription::NodeDescription(const string& parent, const string& refersTo, const string& value, bool inUse) :
    mParentNodeId(parent),
    mRefersToNodeId(refersTo),
    mNodeValue(value),
    mInUse(inUse)
  {}

  ExternalViewLoggerImpl::ExternalViewLoggerImpl(const string& fileName) :
    mFileName(fileName),
    mFile(fileName.c_str())
  {
    if (!mFile)
      throw "Cannot open file";
  }

  string ExternalViewLoggerImpl::getNewStyleName()
  {
    static int i = 0;
    char buff[20];
    SNPRINTF(buff, sizeof(buff), "Style_%d", i++);
    return string(buff);
  }

  void ExternalViewLoggerImpl::beginTree()
  {
    mParentNodeStack.push("");
  }

  void ExternalViewLoggerImpl::dumpNodeContainer(const std::string& fileName)
  {
    std::ofstream file(fileName.c_str());
    NodeContainer_t::iterator iter = mNodeContainer.begin();
    NodeContainer_t::iterator iter_end = mNodeContainer.end();
    for (; iter != iter_end; ++iter)
    {
        file << iter->first << string(" ") << iter->second->mParentNodeId << string(" ") << iter->second->mRefersToNodeId << string(" ") << iter->second->mNodeValue << endl;
    }
  }

  void ExternalViewLoggerImpl::endTree()
  {
    //dumpNodeContainer(mFileName + string(".dmp"));

    mFile << "digraph {" << endl;
    mFile << "Root [shape=box, color=grey];" << endl;

    while (!mParentNodeStack.empty())
      mParentNodeStack.pop();

    mParentNodeStack.push("Root");

    NodeContainer_t::iterator iter = mNodeContainer.begin();
    NodeContainer_t::iterator iter_end = mNodeContainer.end();
    for (; iter != iter_end; ++iter)
    {
      if (isUnreferencedLeaf(iter->first))
      {
        string newStyleName = getNewStyleName();
        mFile << newStyleName << " [shape=box];" << endl;
        mFile << mParentNodeStack.top() << " -> " << newStyleName << endl;
        mParentNodeStack.push(newStyleName);
        dumpTree(iter->first);
        mParentNodeStack.pop();
      }
    }

    mFile << "}" << endl;
  }

  void ExternalViewLoggerImpl::beginNode(const std::string& nodeId, const std::string& value, const std::string& refersToNodeId, bool inUse)
  {
    mNodeContainer.insert(
        NodeContainer_t::value_type(nodeId,
        NodeDescription::Pointer_t(new NodeDescription(mParentNodeStack.top(), refersToNodeId, value, inUse))));
    mParentNodeStack.push(nodeId);
  }

  void ExternalViewLoggerImpl::endNode(const std::string& nodeId)
  {
    mParentNodeStack.pop();
  }

  bool ExternalViewLoggerImpl::isLeaf(const std::string& nodeId)
  {
    bool isLeaf = true;

    NodeContainer_t::const_iterator iter = mNodeContainer.begin();
    NodeContainer_t::const_iterator iter_end = mNodeContainer.end();
    for (; iter != iter_end; ++iter)
    {
      if (iter->second->mParentNodeId == nodeId)
      {
        isLeaf = false;
        break;
      }
    }
    return isLeaf;
  }

  bool ExternalViewLoggerImpl::isUnreferencedLeaf(const string& nodeId)
  {
    return isLeaf(nodeId) && !isReferenced(nodeId);
  }

  bool ExternalViewLoggerImpl::isReferenced(const string& nodeId)
  {
    bool isReferenced = false;

    NodeContainer_t::const_iterator iter = mNodeContainer.begin();
    NodeContainer_t::const_iterator iter_end = mNodeContainer.end();
    for (; iter != iter_end; ++iter)
    {
      if (iter->second->mRefersToNodeId == nodeId)
      {
        isReferenced = true;
        break;
      }
    }
    return isReferenced;
  }

  bool ExternalViewLoggerImpl::isReferingToOtherNode(const string& nodeId)
  {
    NodeContainer_t::const_iterator iter = mNodeContainer.find(nodeId);
    BOOST_ASSERT(iter != mNodeContainer.end());
    return !iter->second->mRefersToNodeId.empty();
  }

  bool ExternalViewLoggerImpl::hasParent(const string& nodeId)
  {
    NodeContainer_t::const_iterator iter = mNodeContainer.find(nodeId);
    BOOST_ASSERT(iter != mNodeContainer.end());
    return iter->second->mParentNodeId != "Root" && iter->second->mParentNodeId != "";
  }

  string ExternalViewLoggerImpl::getValue(const string& nodeId)
  {
    return mNodeContainer.find(nodeId)->second->mNodeValue;
  }

  void ExternalViewLoggerImpl::dumpTree(const string& nodeId)
  {
    if (nodeId != "Root")
    {
      mFile << nodeId << " [label=\"(" << getValue(nodeId) << ")\",shape=box];" << endl;
      mFile << mParentNodeStack.top() << " -> " << nodeId << ";" << endl;
      if (isReferingToOtherNode(nodeId))
      {
        mParentNodeStack.push(nodeId);
        dumpTree(mNodeContainer.find(nodeId)->second->mRefersToNodeId);
        mParentNodeStack.pop();
      }
    }

    if (hasParent(nodeId))
      dumpTree(mNodeContainer.find(nodeId)->second->mParentNodeId);
  }

} // namespace util