summaryrefslogtreecommitdiff
path: root/sw/source/core/model/SearchResultLocator.cxx
blob: 089859683cb483b4d38bbbb503232892d307c02f (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
/* -*- 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 <SearchResultLocator.hxx>
#include <node.hxx>
#include <drawdoc.hxx>
#include <frame.hxx>
#include <cntfrm.hxx>
#include <viewsh.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentLayoutAccess.hxx>

#include <tools/XmlWalker.hxx>
#include <tools/stream.hxx>

#include <boost/property_tree/json_parser.hpp>

#include <svx/svdpage.hxx>
#include <svx/svdobj.hxx>

namespace sw::search
{
void SearchResultLocator::findOne(LocationResult& rResult, SearchIndexData const& rSearchIndexData)
{
    if (rSearchIndexData.meType == NodeType::WriterNode)
    {
        SwNodes const& rNodes = mpDocument->GetNodes();
        if (rSearchIndexData.mnNodeIndex >= sal_Int32(rNodes.Count()))
            return;
        SwNode* pNode = rNodes[rSearchIndexData.mnNodeIndex];

        auto* pContentNode = pNode->GetContentNode();
        auto* pShell = mpDocument->getIDocumentLayoutAccess().GetCurrentViewShell();

        if (pContentNode && pShell)
        {
            const SwFrame* pFrame
                = pContentNode->getLayoutFrame(pShell->GetLayout(), nullptr, nullptr);
            SwRect const& rArea = pFrame->getFrameArea();

            rResult.mbFound = true;
            rResult.maRectangles.emplace_back(rArea.Left(), rArea.Top(),
                                              rArea.Left() + rArea.Width(),
                                              rArea.Top() + rArea.Height());
        }
    }
    else if (rSearchIndexData.meType == NodeType::SdrObject)
    {
        IDocumentDrawModelAccess& rDrawModelAccess = mpDocument->getIDocumentDrawModelAccess();
        auto* pModel = rDrawModelAccess.GetDrawModel();
        for (sal_uInt16 nPage = 0; nPage < pModel->GetPageCount(); ++nPage)
        {
            SdrPage* pPage = pModel->GetPage(nPage);
            for (size_t nObject = 0; nObject < pPage->GetObjCount(); ++nObject)
            {
                SdrObject* pObject = pPage->GetObj(nObject);
                if (pObject)
                {
                    if (pObject->GetName() == rSearchIndexData.maObjectName)
                    {
                        auto aRect = o3tl::convert(pObject->GetLogicRect(), o3tl::Length::mm100,
                                                   o3tl::Length::twip);
                        rResult.mbFound = true;
                        rResult.maRectangles.emplace_back(aRect.Left(), aRect.Top(),
                                                          aRect.Left() + aRect.GetWidth(),
                                                          aRect.Top() + aRect.GetHeight());
                    }
                }
            }
        }
    }
}

LocationResult SearchResultLocator::find(std::vector<SearchIndexData> const& rSearchIndexDataVector)
{
    LocationResult aResult;
    for (auto const& rSearchIndexData : rSearchIndexDataVector)
        findOne(aResult, rSearchIndexData);

    return aResult;
}

/** Trying to parse the payload as JSON
 *
 *  Returns true if parsing was successful and the payload was identified as JSON, else false
 */
bool SearchResultLocator::tryParseJSON(const char* pPayload,
                                       std::vector<sw::search::SearchIndexData>& rDataVector)
{
    boost::property_tree::ptree aTree;
    std::stringstream aStream(pPayload);
    try
    {
        boost::property_tree::read_json(aStream, aTree);
    }
    catch (const boost::property_tree::json_parser_error& /*exception*/)
    {
        return false;
    }

    for (auto& rEachNode : boost::make_iterator_range(aTree.equal_range("")))
    {
        auto const& rEach = rEachNode.second;

        sal_Int32 nType = rEach.get<sal_Int32>("type", 0);
        sal_Int32 nIndex = rEach.get<sal_Int32>("index", -1);

        // Don't add search data elements that don't have valid data
        if (nType > 0 && nIndex >= 0)
            rDataVector.emplace_back(sw::search::NodeType(nType), nIndex);
    }

    return true;
}

/** Trying to parse the payload as XML
 *
 *  Returns true if parsing was successful and the payload was identified as XML, else false
 */
bool SearchResultLocator::tryParseXML(const char* pPayload,
                                      std::vector<sw::search::SearchIndexData>& rDataVector)
{
    const OString aPayloadString(pPayload);

    SvMemoryStream aStream(const_cast<char*>(aPayloadString.getStr()), aPayloadString.getLength(),
                           StreamMode::READ);

    tools::XmlWalker aWalker;

    if (!aWalker.open(&aStream))
        return false;

    if (aWalker.name() != "indexing")
        return true;

    aWalker.children();
    while (aWalker.isValid())
    {
        if (aWalker.name() == "paragraph")
        {
            OString sType = aWalker.attribute("type");
            OString sIndex = aWalker.attribute("index");

            if (!sType.isEmpty() && !sIndex.isEmpty())
            {
                sw::search::SearchIndexData aData;
                aData.mnNodeIndex = sIndex.toInt32();
                aData.meType = sw::search::NodeType(sType.toInt32());

                rDataVector.push_back(aData);
            }
        }
        aWalker.next();
    }
    aWalker.parent();
    return true;
}

LocationResult SearchResultLocator::findForPayload(const char* pPayload)
{
    std::vector<sw::search::SearchIndexData> aDataVector;

    // Try parse the payload as JSON, if not recognised as JSON, try parse
    // it as XML
    tryParseJSON(pPayload, aDataVector) || tryParseXML(pPayload, aDataVector);

    return find(aDataVector);
}

} // end sw namespace

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