LibreOffice Module sw (master) 1
IndexingExport.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 */
10
11#include <IndexingExport.hxx>
12
13#include <ndtxt.hxx>
14#include <ndole.hxx>
15#include <ndgrf.hxx>
16#include <svx/svdobj.hxx>
17#include <svx/svdotext.hxx>
18#include <editeng/outlobj.hxx>
19#include <editeng/editobj.hxx>
20#include <swtable.hxx>
21#include <deque>
22
23namespace sw
24{
25namespace
26{
27class IndexingNodeHandler : public ModelTraverseHandler
28{
29private:
30 tools::XmlWriter& m_rXmlWriter;
31
32 std::deque<SwNode*> maNodeStack;
33
34public:
35 IndexingNodeHandler(tools::XmlWriter& rXmlWriter)
36 : m_rXmlWriter(rXmlWriter)
37 {
38 }
39
40 void handleNode(SwNode* pNode) override
41 {
42 if (pNode->IsOLENode())
43 {
44 handleOLENode(pNode->GetOLENode());
45 }
46 else if (pNode->IsGrfNode())
47 {
48 handleGraphicNode(pNode->GetGrfNode());
49 }
50 else if (pNode->IsTextNode())
51 {
52 handleTextNode(pNode->GetTextNode());
53 }
54 else if (pNode->IsTableNode())
55 {
56 handleTableNode(pNode->GetTableNode());
57 }
58 else if (pNode->IsSectionNode())
59 {
60 handleSectionNode(pNode->GetSectionNode());
61 }
62 else if (pNode->IsEndNode())
63 {
64 handleEndNode(pNode->GetEndNode());
65 }
66 }
67
68 void handleOLENode(const SwOLENode* pOleNode)
69 {
70 auto pFrameFormat = pOleNode->GetFlyFormat();
71 m_rXmlWriter.startElement("object");
72 m_rXmlWriter.attribute("alt", pOleNode->GetTitle());
73 m_rXmlWriter.attribute("name", pFrameFormat->GetName());
74 m_rXmlWriter.attribute("object_type", "ole");
75 m_rXmlWriter.endElement();
76 }
77
78 void handleGraphicNode(const SwGrfNode* pGraphicNode)
79 {
80 auto pFrameFormat = pGraphicNode->GetFlyFormat();
81 m_rXmlWriter.startElement("object");
82 m_rXmlWriter.attribute("alt", pGraphicNode->GetTitle());
83 m_rXmlWriter.attribute("name", pFrameFormat->GetName());
84 m_rXmlWriter.attribute("object_type", "graphic");
85 m_rXmlWriter.endElement();
86 }
87
88 void handleTextNode(const SwTextNode* pTextNode)
89 {
90 SwNodeOffset nParentIndex(-1);
91 if (!maNodeStack.empty() && maNodeStack.back())
92 {
93 nParentIndex = maNodeStack.back()->GetIndex();
94 }
95 const OUString& rString
96 = pTextNode->GetText().replaceAll(OUStringChar(CH_TXTATR_BREAKWORD), "");
97 if (rString.isEmpty())
98 return;
99 m_rXmlWriter.startElement("paragraph");
100 m_rXmlWriter.attribute("index", sal_Int32(pTextNode->GetIndex()));
101 m_rXmlWriter.attribute("node_type", "writer");
102 if (nParentIndex >= SwNodeOffset(0))
103 m_rXmlWriter.attribute("parent_index", sal_Int32(nParentIndex));
104 m_rXmlWriter.content(rString);
105 m_rXmlWriter.endElement();
106 }
107
108 void handleSdrObject(SdrObject* pObject) override
109 {
110 if (pObject->GetName().isEmpty())
111 return;
112
113 m_rXmlWriter.startElement("object");
114 m_rXmlWriter.attribute("name", pObject->GetName());
115 m_rXmlWriter.attribute("alt", pObject->GetTitle());
116 m_rXmlWriter.attribute("object_type", "shape");
117 m_rXmlWriter.attribute("description", pObject->GetDescription());
118
119 m_rXmlWriter.endElement();
120
121 SdrTextObj* pTextObject = dynamic_cast<SdrTextObj*>(pObject);
122 if (!pTextObject)
123 return;
124
125 OutlinerParaObject* pOutlinerParagraphObject = pTextObject->GetOutlinerParaObject();
126 if (!pOutlinerParagraphObject)
127 return;
128
129 const EditTextObject& aEdit = pOutlinerParagraphObject->GetTextObject();
130 for (sal_Int32 nParagraph = 0; nParagraph < aEdit.GetParagraphCount(); ++nParagraph)
131 {
132 OUString sText = aEdit.GetText(nParagraph);
133
134 m_rXmlWriter.startElement("paragraph");
135 m_rXmlWriter.attribute("index", nParagraph);
136 m_rXmlWriter.attribute("node_type", "common");
137 m_rXmlWriter.attribute("object_name", pObject->GetName());
138 m_rXmlWriter.content(sText);
139 m_rXmlWriter.endElement();
140 }
141 }
142
143 void handleTableNode(SwTableNode* pTableNode)
144 {
145 const SwTableFormat* pFormat = pTableNode->GetTable().GetFrameFormat();
146 OUString sName = pFormat->GetName();
147
148 m_rXmlWriter.startElement("object");
149 m_rXmlWriter.attribute("index", sal_Int32(pTableNode->GetIndex()));
150 m_rXmlWriter.attribute("name", sName);
151 m_rXmlWriter.attribute("object_type", "table");
152 m_rXmlWriter.endElement();
153
154 maNodeStack.push_back(pTableNode);
155 }
156
157 void handleSectionNode(SwSectionNode* pSectionNode)
158 {
159 m_rXmlWriter.startElement("object");
160 m_rXmlWriter.attribute("index", sal_Int32(pSectionNode->GetIndex()));
161 m_rXmlWriter.attribute("name", pSectionNode->GetSection().GetSectionName());
162 m_rXmlWriter.attribute("object_type", "section");
163 m_rXmlWriter.endElement();
164
165 maNodeStack.push_back(pSectionNode);
166 }
167
168 void handleEndNode(SwEndNode* pEndNode)
169 {
170 if (!maNodeStack.empty() && pEndNode->StartOfSectionNode() == maNodeStack.back())
171 {
172 maNodeStack.pop_back();
173 }
174 }
175};
176
177} // end anonymous namespace
178
180 : m_aModelTraverser(pDoc)
181 , m_aXmlWriter(&rStream)
182{
183}
184
186{
187 bool bResult = m_aXmlWriter.startDocument(2);
188 if (!bResult)
189 return false;
190
191 m_aXmlWriter.startElement("indexing");
192 m_aModelTraverser.addNodeHandler(std::make_shared<IndexingNodeHandler>(m_aXmlWriter));
195
197
198 return true;
199}
200
201} // end sw namespace
202
203/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Int32 GetParagraphCount() const=0
virtual OUString GetText(sal_Int32 nPara) const=0
const EditTextObject & GetTextObject() const
virtual OutlinerParaObject * GetOutlinerParaObject() const override
Definition: doc.hxx:192
Ends a section of nodes in the document model.
Definition: node.hxx:348
const OUString & GetName() const
Definition: format.hxx:131
OUString GetTitle() const
Definition: ndnotxt.cxx:258
Base class of the Writer document model elements.
Definition: node.hxx:84
bool IsGrfNode() const
Definition: node.hxx:675
SwFrameFormat * GetFlyFormat() const
If node is in a fly return the respective format.
Definition: node.cxx:731
SwGrfNode * GetGrfNode()
Definition: ndgrf.hxx:154
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:871
SwSectionNode * GetSectionNode()
Definition: node.hxx:626
SwNodeOffset GetIndex() const
Definition: node.hxx:296
bool IsEndNode() const
Definition: node.hxx:651
bool IsSectionNode() const
Definition: node.hxx:663
bool IsTableNode() const
Definition: node.hxx:659
bool IsTextNode() const
Definition: node.hxx:655
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:137
SwOLENode * GetOLENode()
Inline methods from Node.hxx.
Definition: ndole.hxx:164
SwTableNode * GetTableNode()
Definition: node.hxx:618
SwEndNode * GetEndNode()
Definition: node.hxx:602
bool IsOLENode() const
Definition: node.hxx:671
const SwSection & GetSection() const
Definition: node.hxx:558
const OUString & GetSectionName() const
Definition: section.hxx:169
const SwTable & GetTable() const
Definition: node.hxx:513
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:204
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:86
const OUString & GetText() const
Definition: ndtxt.hxx:222
tools::XmlWriter m_aXmlWriter
IndexingExport(SvStream &rStream, SwDoc *pDoc)
ModelTraverser m_aModelTraverser
void addNodeHandler(std::shared_ptr< ModelTraverseHandler > pHandler)
void content(const OString &sValue)
bool startDocument(sal_Int32 nIndent=2, bool bWriteXmlHeader=true)
void attribute(const OString &sTagName, const OString &aValue)
void startElement(const OString &sName)
EmbeddedObjectRef * pObject
#define CH_TXTATR_BREAKWORD
Definition: hintids.hxx:173
const char * sName
Dialog to specify the properties of date form field.
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16