13#include <document.hxx>
14#include <tokenarray.hxx>
20#include <osl/file.hxx>
22#include <orcus/xml_structure_tree.hpp>
23#include <orcus/xml_namespace.hpp>
24#include <orcus/orcus_xml.hpp>
25#include <orcus/sax_parser_base.hpp>
26#include <orcus/stream.hpp>
28#include <com/sun/star/io/XInputStream.hpp>
36#define BUFFER_SIZE 4096
45 rStore.push_back(std::make_unique<ScOrcusXMLTreeParam::EntryData>(
eType));
47 return *rStore.back();
50void setEntityNameToUserData(
52 const orcus::xml_structure_tree::entity_name& entity,
const orcus::xml_structure_tree::walker& walker)
57OUString
toString(
const orcus::xml_structure_tree::entity_name& entity,
const orcus::xml_structure_tree::walker& walker)
63 std::string aShortName = walker.get_xmlns_short_name(entity.ns);
64 aBuf.appendAscii(aShortName.c_str());
67 aBuf.append(OUString(entity.name.data(), entity.name.size(), RTL_TEXTENCODING_UTF8));
68 return aBuf.makeStringAndClear();
72 weld::TreeView& rTreeCtrl, orcus::xml_structure_tree::walker& rWalker,
73 const orcus::xml_structure_tree::entity_name& rElemName,
bool bRepeat,
76 OUString sEntry(
toString(rElemName, rWalker));
77 std::unique_ptr<weld::TreeIter> xEntry(rTreeCtrl.
make_iterator());
78 rTreeCtrl.
insert(pParent, -1, &sEntry,
nullptr,
nullptr,
nullptr,
false, xEntry.get());
85 setEntityNameToUserData(rEntryData, rElemName, rWalker);
93 orcus::xml_structure_tree::entity_names_type aNames = rWalker.get_attributes();
96 for (
const orcus::xml_structure_tree::entity_name& rAttrName : aNames)
98 OUString sAttr(
toString(rAttrName, rWalker));
99 std::unique_ptr<weld::TreeIter> xAttr(rTreeCtrl.
make_iterator());
100 rTreeCtrl.
insert(xEntry.get(), -1, &sAttr,
nullptr,
nullptr,
nullptr,
false, xAttr.get());
104 setEntityNameToUserData(rAttrData, rAttrName, rWalker);
109 aNames = rWalker.get_children();
115 for (
const auto& rName : aNames)
117 orcus::xml_structure_tree::element aElem = rWalker.descend(rName);
118 populateTree(rTreeCtrl, rWalker, rName, aElem.repeat, xEntry.get(), rParam);
123class TreeUpdateSwitch
127 explicit TreeUpdateSwitch(
weld::TreeView& rTreeCtrl) : mrTreeCtrl(rTreeCtrl)
138void loadContentFromURL(
const OUString& rURL, std::string&
rStrm)
142 uno::Reference<io::XInputStream> xStrm = aContent.
openStream();
144 std::ostringstream aStrmBuf;
145 uno::Sequence<sal_Int8> aBytes;
146 size_t nBytesRead = 0;
149 nBytesRead = xStrm->readBytes(aBytes,
BUFFER_SIZE);
150 const sal_Int8*
p = aBytes.getConstArray();
151 aStrmBuf << std::string(
p,
p + nBytesRead);
155 rStrm = aStrmBuf.str();
170 loadContentFromURL(
maPath, aStrm);
175 orcus::xmlns_context cxt =
maNsRepo.create_context();
176 orcus::xml_structure_tree aXmlTree(cxt);
179 aXmlTree.parse(aStrm);
181 TreeUpdateSwitch aSwitch(rTreeCtrl);
184 orcus::xml_structure_tree::walker aWalker = aXmlTree.get_walker();
187 orcus::xml_structure_tree::element aElem = aWalker.root();
188 populateTree(rTreeCtrl, aWalker, aElem.name, aElem.repeat,
nullptr, rParam);
190 catch (
const orcus::malformed_xml_error& e)
192 SAL_WARN(
"sc.orcus",
"Malformed XML error: " << e.what());
194 catch (
const std::exception& e)
196 SAL_WARN(
"sc.orcus",
"parsing failed with an unknown error " << e.what());
210 if (osl::FileBase::getSystemPathFromFileURL(
maPath, aSysPath) != osl::FileBase::E_None)
214 const char* path = aOSysPath.getStr();
224 if (nsid == orcus::XMLNS_UNKNOWN_ID)
227 std::ostringstream os;
229 std::string alias = os.str();
230 filter.set_namespace_alias(alias, nsid);
253 std::for_each(rLink.maFieldPaths.begin(), rLink.maFieldPaths.end(),
254 [&
filter](
const OString& rFieldPath)
256 filter.append_field_link(rFieldPath, std::string_view());
260 std::for_each(rLink.maRowGroups.begin(), rLink.maRowGroups.end(),
261 [&
filter] (
const OString& rRowGroup)
263 filter.set_range_row_group(rRowGroup);
270 orcus::file_content content(path);
271 filter.read_stream(content.str());
275 catch (
const std::exception&)
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
virtual void finalize() override
virtual void importXML(const ScOrcusImportXMLParam &rParam) override
virtual void loadXMLStructure(weld::TreeView &rTreeCtrl, ScOrcusXMLTreeParam &rParam) override
ScOrcusXMLContextImpl(ScDocument &rDoc, OUString aPath)
XML namespace repository for this context.
virtual ~ScOrcusXMLContextImpl() override
orcus::xmlns_repository maNsRepo
css::uno::Reference< css::io::XInputStream > openStream()
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const=0
virtual void expand_row(const TreeIter &rIter)=0
virtual void insert(const TreeIter *pParent, int pos, const OUString *pStr, const OUString *pId, const OUString *pIconName, VirtualDevice *pImageSurface, bool bChildrenOnDemand, TreeIter *pRet)=0
virtual void set_image(int row, const OUString &rImage, int col=-1)=0
virtual void set_id(int row, const OUString &rId)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
OUString toString(OptionInfo const *info)
OUString toId(const void *pValue)
std::vector< RangeLink > maRangeLinks
std::vector< CellLink > maCellLinks
std::vector< size_t > maNamespaces
Custom data stored with each tree item.
Parameter used during call to ScOrcusFilters::loadXMLStructure().
UserDataStoreType m_UserDataStore
Store all custom data instances since the tree control doesn't manage the life cycle of user data.
std::vector< std::unique_ptr< EntryData > > UserDataStoreType
OUString maImgElementRepeat
OUString maImgElementDefault