summaryrefslogtreecommitdiff
path: root/src/lib/VSDXMLHelper.cpp
blob: bacc2c64da0ce7200d5013af282493c10082c640 (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
/* -*- 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/.
 */

#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
#define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE 1
#endif

#include <memory>
#include <sstream>
#include <istream>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <librevenge-stream/librevenge-stream.h>
#include "VSDXMLHelper.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 (unsigned i = 0; i < segments.size(); ++i)
  {
    if (segments[i] == "..")
      normalizedSegments.pop_back();
    else if (segments[i] != "." && !segments[i].empty())
      normalizedSegments.push_back(segments[i]);
  }

  target.clear();
  for (unsigned j = 0; j < normalizedSegments.size(); ++j)
  {
    if (!target.empty())
      target.append("/");
    target.append(normalizedSegments[j]);
  }

  // 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, 0, 0, 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 0;
  std::map<std::string, libvisio::VSDXRelationship>::const_iterator iter = m_relsByType.find(type);
  if (iter != m_relsByType.end())
    return &(iter->second);
  return 0;
}

const libvisio::VSDXRelationship *libvisio::VSDXRelationships::getRelationshipById(const char *id) const
{
  if (!id)
    return 0;
  std::map<std::string, libvisio::VSDXRelationship>::const_iterator iter = m_relsById.find(id);
  if (iter != m_relsById.end())
    return &(iter->second);
  return 0;
}

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