merge the formfield patch from ooo-build
[ooovba.git] / writerfilter / qa / cppunittests / odiapi / ExternalViewLogger.cxx
blob8da7996c12f94f620ee712c9b50a83a32b07e4ea
1 /* Copyright 2005 Sun Microsystems, Inc. */
3 #include "ExternalViewLogger.hxx"
4 #include <iostream>
5 #include <boost/assert.hpp>
6 #include <stdio.h>
8 #ifdef WNT
9 #define SNPRINTF(buffer, size, format, args) _snprintf(buffer, size, format, args)
10 #else
11 #define SNPRINTF(buffer, size, format, args) snprintf(buffer, size, format, args)
12 #endif
14 using namespace std;
16 namespace util
19 NodeDescription::NodeDescription(const string& parent, const string& refersTo, const string& value, bool inUse) :
20 mParentNodeId(parent),
21 mRefersToNodeId(refersTo),
22 mNodeValue(value),
23 mInUse(inUse)
26 ExternalViewLoggerImpl::ExternalViewLoggerImpl(const string& fileName) :
27 mFileName(fileName),
28 mFile(fileName.c_str())
30 if (!mFile)
31 throw "Cannot open file";
34 string ExternalViewLoggerImpl::getNewStyleName()
36 static int i = 0;
37 char buff[20];
38 SNPRINTF(buff, sizeof(buff), "Style_%d", i++);
39 return string(buff);
42 void ExternalViewLoggerImpl::beginTree()
44 mParentNodeStack.push("");
47 void ExternalViewLoggerImpl::dumpNodeContainer(const std::string& fileName)
49 std::ofstream file(fileName.c_str());
50 NodeContainer_t::iterator iter = mNodeContainer.begin();
51 NodeContainer_t::iterator iter_end = mNodeContainer.end();
52 for (; iter != iter_end; ++iter)
54 file << iter->first << string(" ") << iter->second->mParentNodeId << string(" ") << iter->second->mRefersToNodeId << string(" ") << iter->second->mNodeValue << endl;
58 void ExternalViewLoggerImpl::endTree()
60 //dumpNodeContainer(mFileName + string(".dmp"));
62 mFile << "digraph {" << endl;
63 mFile << "Root [shape=box, color=grey];" << endl;
65 while (!mParentNodeStack.empty())
66 mParentNodeStack.pop();
68 mParentNodeStack.push("Root");
70 NodeContainer_t::iterator iter = mNodeContainer.begin();
71 NodeContainer_t::iterator iter_end = mNodeContainer.end();
72 for (; iter != iter_end; ++iter)
74 if (isUnreferencedLeaf(iter->first))
76 string newStyleName = getNewStyleName();
77 mFile << newStyleName << " [shape=box];" << endl;
78 mFile << mParentNodeStack.top() << " -> " << newStyleName << endl;
79 mParentNodeStack.push(newStyleName);
80 dumpTree(iter->first);
81 mParentNodeStack.pop();
85 mFile << "}" << endl;
88 void ExternalViewLoggerImpl::beginNode(const std::string& nodeId, const std::string& value, const std::string& refersToNodeId, bool inUse)
90 mNodeContainer.insert(
91 NodeContainer_t::value_type(nodeId,
92 NodeDescription::Pointer_t(new NodeDescription(mParentNodeStack.top(), refersToNodeId, value, inUse))));
93 mParentNodeStack.push(nodeId);
96 void ExternalViewLoggerImpl::endNode(const std::string& nodeId)
98 mParentNodeStack.pop();
101 bool ExternalViewLoggerImpl::isLeaf(const std::string& nodeId)
103 bool isLeaf = true;
105 NodeContainer_t::const_iterator iter = mNodeContainer.begin();
106 NodeContainer_t::const_iterator iter_end = mNodeContainer.end();
107 for (; iter != iter_end; ++iter)
109 if (iter->second->mParentNodeId == nodeId)
111 isLeaf = false;
112 break;
115 return isLeaf;
118 bool ExternalViewLoggerImpl::isUnreferencedLeaf(const string& nodeId)
120 return isLeaf(nodeId) && !isReferenced(nodeId);
123 bool ExternalViewLoggerImpl::isReferenced(const string& nodeId)
125 bool isReferenced = false;
127 NodeContainer_t::const_iterator iter = mNodeContainer.begin();
128 NodeContainer_t::const_iterator iter_end = mNodeContainer.end();
129 for (; iter != iter_end; ++iter)
131 if (iter->second->mRefersToNodeId == nodeId)
133 isReferenced = true;
134 break;
137 return isReferenced;
140 bool ExternalViewLoggerImpl::isReferingToOtherNode(const string& nodeId)
142 NodeContainer_t::const_iterator iter = mNodeContainer.find(nodeId);
143 BOOST_ASSERT(iter != mNodeContainer.end());
144 return !iter->second->mRefersToNodeId.empty();
147 bool ExternalViewLoggerImpl::hasParent(const string& nodeId)
149 NodeContainer_t::const_iterator iter = mNodeContainer.find(nodeId);
150 BOOST_ASSERT(iter != mNodeContainer.end());
151 return iter->second->mParentNodeId != "Root" && iter->second->mParentNodeId != "";
154 string ExternalViewLoggerImpl::getValue(const string& nodeId)
156 return mNodeContainer.find(nodeId)->second->mNodeValue;
159 void ExternalViewLoggerImpl::dumpTree(const string& nodeId)
161 if (nodeId != "Root")
163 mFile << nodeId << " [label=\"(" << getValue(nodeId) << ")\",shape=box];" << endl;
164 mFile << mParentNodeStack.top() << " -> " << nodeId << ";" << endl;
165 if (isReferingToOtherNode(nodeId))
167 mParentNodeStack.push(nodeId);
168 dumpTree(mNodeContainer.find(nodeId)->second->mRefersToNodeId);
169 mParentNodeStack.pop();
173 if (hasParent(nodeId))
174 dumpTree(mNodeContainer.find(nodeId)->second->mParentNodeId);
177 } // namespace util