This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrelationshipaccess.cxx
217 lines (180 loc) · 7.79 KB
/
relationshipaccess.cxx
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <comphelper/ofopxmlhelper.hxx>
#include <vector>
#include <comphelper/processfactory.hxx>
#include <comphelper/relationshipaccess.hxx>
#include <comphelper/vecstream.hxx>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <com/sun/star/embed/InvalidStorageException.hpp>
#include <com/sun/star/embed/StorageWrappedTargetException.hpp>
#include <com/sun/star/packages/NoEncryptionException.hpp>
#include <comphelper/diagnose_ex.hxx>
#include <comphelper/sequence.hxx>
#include <com/sun/star/io/BufferSizeExceededException.hpp>
#include <com/sun/star/io/NotConnectedException.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XEventListener.hpp>
#include <sal/types.h>
namespace comphelper
{
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::uno;
using namespace ::osl;
const beans::StringPair* lcl_findPairByName(const Sequence<beans::StringPair>& rSeq,
const OUString& rName)
{
return std::find_if(rSeq.begin(), rSeq.end(),
[&rName](const beans::StringPair& rPair) { return rPair.First == rName; });
}
void RelationshipAccessImpl::setRelationships(
css::uno::Sequence<css::uno::Sequence<css::beans::StringPair>> aRelInfo)
{
m_aRelInfo = std::move(aRelInfo);
}
void SAL_CALL RelationshipAccessImpl::clearRelationships() { m_aRelInfo.realloc(0); }
void SAL_CALL RelationshipAccessImpl::insertRelationships(
const Sequence<Sequence<beans::StringPair>>& aEntries, sal_Bool bReplace)
{
OUString aIDTag("Id");
const Sequence<Sequence<beans::StringPair>> aSeq = getAllRelationships();
std::vector<Sequence<beans::StringPair>> aResultVec;
aResultVec.reserve(aSeq.getLength() + aEntries.getLength());
std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec),
[&aIDTag, &aEntries](const Sequence<beans::StringPair>& rTargetRel)
{
auto pTargetPair = lcl_findPairByName(rTargetRel, aIDTag);
if (pTargetPair == rTargetRel.end())
return false;
bool bIsSourceSame = std::any_of(
aEntries.begin(), aEntries.end(),
[&pTargetPair](const Sequence<beans::StringPair>& rSourceEntry)
{
return std::find(rSourceEntry.begin(), rSourceEntry.end(),
*pTargetPair)
!= rSourceEntry.end();
});
// if no such element in the provided sequence
return !bIsSourceSame;
});
std::transform(
aEntries.begin(), aEntries.end(), std::back_inserter(aResultVec),
[&aIDTag](const Sequence<beans::StringPair>& rEntry) -> Sequence<beans::StringPair>
{
auto pPair = lcl_findPairByName(rEntry, aIDTag);
auto aResult = comphelper::sequenceToContainer<std::vector<beans::StringPair>>(rEntry);
auto nIDInd = std::distance(rEntry.begin(), pPair);
std::rotate(aResult.begin(), std::next(aResult.begin(), nIDInd),
std::next(aResult.begin(), nIDInd + 1));
return comphelper::containerToSequence(aResult);
});
m_aRelInfo = comphelper::containerToSequence(aResultVec);
}
void SAL_CALL RelationshipAccessImpl::removeRelationshipByID(const OUString& sID)
{
Sequence<Sequence<beans::StringPair>> aSeq = getAllRelationships();
const beans::StringPair aIDRel("Id", sID);
auto pRel = std::find_if(std::cbegin(aSeq), std::cend(aSeq),
[&aIDRel](const Sequence<beans::StringPair>& rRel)
{ return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
if (pRel != std::cend(aSeq))
{
auto nInd = static_cast<sal_Int32>(std::distance(std::cbegin(aSeq), pRel));
comphelper::removeElementAt(aSeq, nInd);
m_aRelInfo = std::move(aSeq);
// TODO/LATER: in future the unification of the ID could be checked
return;
}
throw container::NoSuchElementException();
}
void SAL_CALL RelationshipAccessImpl::insertRelationshipByID(
const OUString& sID, const uno::Sequence<beans::StringPair>& aEntry, sal_Bool bReplace)
{
const beans::StringPair aIDRel("Id", sID);
uno::Sequence<beans::StringPair>* pResult = nullptr;
// TODO/LATER: in future the unification of the ID could be checked
uno::Sequence<uno::Sequence<beans::StringPair>> aSeq = getAllRelationships();
for (sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++)
{
const auto& rRel = aSeq[nInd];
if (std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end())
pResult = &aSeq.getArray()[nInd];
}
if (!pResult)
{
const sal_Int32 nIDInd = aSeq.getLength();
aSeq.realloc(nIDInd + 1);
pResult = &aSeq.getArray()[nIDInd];
}
std::vector<beans::StringPair> aResult;
aResult.reserve(aEntry.getLength() + 1);
aResult.push_back(aIDRel);
std::copy_if(aEntry.begin(), aEntry.end(), std::back_inserter(aResult),
[](const beans::StringPair& rPair) { return rPair.First != "Id"; });
*pResult = comphelper::containerToSequence(aResult);
m_aRelInfo = aSeq;
}
Sequence<Sequence<beans::StringPair>>
SAL_CALL RelationshipAccessImpl::getRelationshipsByType(const OUString& sType)
{
const Sequence<Sequence<beans::StringPair>> aSeq = getAllRelationships();
std::vector<Sequence<beans::StringPair>> aResult;
aResult.reserve(aSeq.getLength());
std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult),
[&sType](const Sequence<beans::StringPair>& rRel)
{
auto pRel = lcl_findPairByName(rRel, "Type");
return pRel != rRel.end()
// the type is usually a URL, so the check should be case insensitive
&& pRel->Second.equalsIgnoreAsciiCase(sType);
});
return comphelper::containerToSequence(aResult);
}
OUString SAL_CALL RelationshipAccessImpl::getTypeByID(const OUString& sID)
{
const Sequence<beans::StringPair> aSeq = getRelationshipByID(sID);
auto pRel = lcl_findPairByName(aSeq, "Type");
if (pRel != aSeq.end())
return pRel->Second;
return OUString();
}
OUString SAL_CALL RelationshipAccessImpl::getTargetByID(const OUString& sID)
{
const Sequence<beans::StringPair> aSeq = getRelationshipByID(sID);
auto pRel = lcl_findPairByName(aSeq, "Target");
if (pRel != aSeq.end())
return pRel->Second;
return OUString();
}
sal_Bool SAL_CALL RelationshipAccessImpl::hasByID(const OUString& sID)
{
try
{
getRelationshipByID(sID);
return true;
}
catch (const container::NoSuchElementException&)
{
TOOLS_INFO_EXCEPTION("package.xstor", "Rethrow:");
}
return false;
}
Sequence<beans::StringPair>
SAL_CALL RelationshipAccessImpl::getRelationshipByID(const OUString& sID)
{
const Sequence<Sequence<beans::StringPair>> aSeq = getAllRelationships();
const beans::StringPair aIDRel("Id", sID);
auto pRel = std::find_if(aSeq.begin(), aSeq.end(),
[&aIDRel](const Sequence<beans::StringPair>& rRel)
{
auto id = lcl_findPairByName(rRel, "Id");
return id != rRel.end() && id->Second == aIDRel.Second;
});
if (pRel != aSeq.end())
return *pRel;
throw container::NoSuchElementException();
}
Sequence<Sequence<beans::StringPair>> SAL_CALL RelationshipAccessImpl::getAllRelationships()
{
return m_aRelInfo;
}
}