summaryrefslogtreecommitdiff
path: root/sw/source/filter/indexing/IndexingExport.cxx
blob: b02bef0c9fe213aa7f30182ed86db3863ee355da (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <IndexingExport.hxx>

#include <ndtxt.hxx>
#include <ndole.hxx>
#include <ndnotxt.hxx>
#include <ndgrf.hxx>
#include <svx/svdobj.hxx>
#include <svx/svdotext.hxx>
#include <editeng/outlobj.hxx>
#include <editeng/editobj.hxx>
#include <swtable.hxx>
#include <deque>

namespace sw
{
namespace
{
class IndexingNodeHandler : public ModelTraverseHandler
{
private:
    tools::XmlWriter& m_rXmlWriter;

    std::deque<SwNode*> maNodeStack;

public:
    IndexingNodeHandler(tools::XmlWriter& rXmlWriter)
        : m_rXmlWriter(rXmlWriter)
    {
    }

    void handleNode(SwNode* pNode) override
    {
        if (pNode->IsOLENode())
        {
            handleOLENode(pNode->GetOLENode());
        }
        else if (pNode->IsGrfNode())
        {
            handleGraphicNode(pNode->GetGrfNode());
        }
        else if (pNode->IsTextNode())
        {
            handleTextNode(pNode->GetTextNode());
        }
        else if (pNode->IsTableNode())
        {
            handleTableNode(pNode->GetTableNode());
        }

        if (pNode->IsEndNode())
        {
            handleEndNode(pNode->GetEndNode());
        }
    }

    void handleOLENode(SwOLENode* pOleNode)
    {
        auto pFrameFormat = pOleNode->GetFlyFormat();
        m_rXmlWriter.startElement("ole");
        m_rXmlWriter.attribute("alt", pOleNode->GetTitle());
        m_rXmlWriter.attribute("name", pFrameFormat->GetName());
        m_rXmlWriter.endElement();
    }

    void handleGraphicNode(SwGrfNode* pGraphicNode)
    {
        auto pFrameFormat = pGraphicNode->GetFlyFormat();
        m_rXmlWriter.startElement("graphic");
        m_rXmlWriter.attribute("alt", pGraphicNode->GetTitle());
        m_rXmlWriter.attribute("name", pFrameFormat->GetName());
        m_rXmlWriter.endElement();
    }

    void handleTextNode(SwTextNode* pTextNode)
    {
        sal_Int64 nParentIndex = -1;
        if (!maNodeStack.empty() && maNodeStack.back())
        {
            nParentIndex = maNodeStack.back()->GetIndex();
        }
        const OUString& rString
            = pTextNode->GetText().replaceAll(OUStringChar(CH_TXTATR_BREAKWORD), "");
        m_rXmlWriter.startElement("paragraph");
        m_rXmlWriter.attribute("index", pTextNode->GetIndex());
        if (nParentIndex >= 0)
            m_rXmlWriter.attribute("parent", nParentIndex);
        m_rXmlWriter.content(rString);
        m_rXmlWriter.endElement();
    }

    void handleSdrObject(SdrObject* pObject) override
    {
        if (pObject->GetName().isEmpty())
            return;
        m_rXmlWriter.startElement("shape");
        m_rXmlWriter.attribute("name", pObject->GetName());
        m_rXmlWriter.attribute("alt", pObject->GetTitle());
        m_rXmlWriter.attribute("description", pObject->GetDescription());

        SdrTextObj* pTextObject = dynamic_cast<SdrTextObj*>(pObject);
        if (pTextObject)
        {
            OutlinerParaObject* pOutlinerParagraphObject = pTextObject->GetOutlinerParaObject();
            const EditTextObject& aEdit = pOutlinerParagraphObject->GetTextObject();
            for (sal_Int32 nParagraph = 0; nParagraph < aEdit.GetParagraphCount(); ++nParagraph)
            {
                OUString sText = aEdit.GetText(nParagraph);

                m_rXmlWriter.startElement("paragraph");
                m_rXmlWriter.attribute("index", nParagraph);
                m_rXmlWriter.content(sText);
                m_rXmlWriter.endElement();
            }
        }

        m_rXmlWriter.endElement();
    }

    void handleTableNode(SwTableNode* pTableNode)
    {
        const SwTableFormat* pFormat = pTableNode->GetTable().GetFrameFormat();
        OUString sName = pFormat->GetName();

        m_rXmlWriter.startElement("table");
        m_rXmlWriter.attribute("index", pTableNode->GetIndex());
        m_rXmlWriter.attribute("name", sName);

        maNodeStack.push_back(pTableNode);
    }

    void handleEndNode(SwEndNode* pEndNode)
    {
        if (!maNodeStack.empty() && pEndNode->StartOfSectionNode() == maNodeStack.back())
        {
            maNodeStack.pop_back();
            m_rXmlWriter.endElement();
        }
    }
};

} // end anonymous namespace

IndexingExport::IndexingExport(SvStream& rStream, SwDoc* pDoc)
    : m_aModelTraverser(pDoc)
    , m_aXmlWriter(&rStream)
{
}

bool IndexingExport::runExport()
{
    bool bResult = m_aXmlWriter.startDocument(2);
    if (!bResult)
        return false;

    m_aXmlWriter.startElement("indexing");
    m_aModelTraverser.addNodeHandler(std::make_shared<IndexingNodeHandler>(m_aXmlWriter));
    m_aModelTraverser.traverse();
    m_aXmlWriter.endElement();

    m_aXmlWriter.endDocument();

    return true;
}

} // end sw namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */