summaryrefslogtreecommitdiff
path: root/src/lib/VSDXMLHelper.cpp
blob: 6f483b8ca275d70d86052e4e4d3cc0a974c7c766 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libvisio 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 "VSDXMLHelper.h"

#include <memory>
#include <sstream>
#include <istream>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <librevenge-stream/librevenge-stream.h>
#include "libvisio_utils.h"
#include "libvisio_xml.h"

// VSDXRelationship

libvisio::VSDXRelationship::VSDXRelationship(xmlTextReaderPtr reader)
  : m_id(), m_type(), m_target()
{
  if (reader)
    // TODO: check whether we are actually parsing "Relationship" element
  {
    while (xmlTextReaderMoveToNextAttribute(reader))
    {
      const xmlChar *name = xmlTextReaderConstName(reader);
      const xmlChar *value = xmlTextReaderConstValue(reader);
      if (xmlStrEqual(name, BAD_CAST("Id")))
        m_id = (const char *)value;
      else if (xmlStrEqual(name, BAD_CAST("Type")))
        m_type = (const char *)value;
      else if (xmlStrEqual(name, BAD_CAST("Target")))
        m_target = (const char *)value;
    }
    // VSD_DEBUG_MSG(("Relationship : %s type: %s target: %s\n", m_id.c_str(), m_type.c_str(), m_target.c_str()));
  }
}

libvisio::VSDXRelationship::VSDXRelationship()
  : m_id(), m_type(), m_target()
{
}

libvisio::VSDXRelationship::~VSDXRelationship()
{
}

void libvisio::VSDXRelationship::rebaseTarget(const char *baseDir)
{
  std::string target(baseDir ? baseDir : "");
  if (!target.empty())
    target += "/";
  target += m_target;

  // normalize path by resolving any ".." or "." segments
  // and eliminating duplicate path separators
  std::vector<std::string> segments;
  boost::split(segments, target, boost::is_any_of("/\\"));
  std::vector<std::string> normalizedSegments;

  for (auto &segment : segments)
  {
    if (segment == "..")
      normalizedSegments.pop_back();
    else if (segment != "." && !segment.empty())
      normalizedSegments.push_back(segment);
  }

  target.clear();
  for (const auto &normalizedSegment : normalizedSegments)
  {
    if (!target.empty())
      target.append("/");
    target.append(normalizedSegment);
  }

  // VSD_DEBUG_MSG(("VSDXRelationship::rebaseTarget %s -> %s\n", m_target.c_str(), target.c_str()));
  m_target = target;
}


// VSDXRelationships

libvisio::VSDXRelationships::VSDXRelationships(librevenge::RVNGInputStream *input)
  : m_relsByType(), m_relsById()
{
  if (input)
  {
    const std::shared_ptr<xmlTextReader> reader(
      xmlReaderForStream(input, nullptr, nullptr, XML_PARSE_NOBLANKS|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_RECOVER),
      xmlFreeTextReader);
    if (reader)
    {
      bool inRelationships = false;
      int ret = xmlTextReaderRead(reader.get());
      while (ret == 1)
      {
        const xmlChar *name = xmlTextReaderConstName(reader.get());
        if (name)
        {
          if (xmlStrEqual(name, BAD_CAST("Relationships")))
          {
            if (xmlTextReaderNodeType(reader.get()) == 1)
            {
              // VSD_DEBUG_MSG(("Relationships ON\n"));
              inRelationships = true;
            }
            else if (xmlTextReaderNodeType(reader.get()) == 15)
            {
              // VSD_DEBUG_MSG(("Relationships OFF\n"));
              inRelationships = false;
            }
          }
          else if (xmlStrEqual(name, BAD_CAST("Relationship")))
          {
            if (inRelationships)
            {
              VSDXRelationship relationship(reader.get());
              m_relsByType[relationship.getType()] = relationship;
              m_relsById[relationship.getId()] = relationship;
            }
          }
        }
        ret = xmlTextReaderRead(reader.get());
      }
    }
  }
}

libvisio::VSDXRelationships::~VSDXRelationships()
{
}

void libvisio::VSDXRelationships::rebaseTargets(const char *baseDir)
{
  std::map<std::string, libvisio::VSDXRelationship>::iterator iter;
  for (iter = m_relsByType.begin(); iter != m_relsByType.end(); ++iter)
    iter->second.rebaseTarget(baseDir);
  for (iter = m_relsById.begin(); iter != m_relsById.end(); ++iter)
    iter->second.rebaseTarget(baseDir);
}

const libvisio::VSDXRelationship *libvisio::VSDXRelationships::getRelationshipByType(const char *type) const
{
  if (!type)
    return nullptr;
  auto iter = m_relsByType.find(type);
  if (iter != m_relsByType.end())
    return &(iter->second);
  return nullptr;
}

const libvisio::VSDXRelationship *libvisio::VSDXRelationships::getRelationshipById(const char *id) const
{
  if (!id)
    return nullptr;
  auto iter = m_relsById.find(id);
  if (iter != m_relsById.end())
    return &(iter->second);
  return nullptr;
}

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