COPASI API  4.16.103
CRDFUtilities.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAM/CRDFUtilities.cpp,v $
3 // $Revision: 1.4 $
4 // $Name: $
5 // $Author: gauges $
6 // $Date: 2009/02/02 16:12:51 $
7 // End CVS Header
8 
9 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
11 // and The University of Manchester.
12 // All rights reserved.
13 
14 #include <algorithm>
15 
16 #include "copasi.h"
17 
18 #include "CRDFUtilities.h"
19 
20 // static
21 unsigned C_INT32 CRDFUtilities::fixLocalFileAboutReference(std::string & rdfXml,
22  const std::string & newId,
23  const std::string & oldId)
24 {
25  // Nothing to do
26  if (newId == oldId || rdfXml == "")
27  return 0;
28 
29  // Determine the name space qualifier for http://www.w3.org/1999/02/22-rdf-syntax-ns#
30  std::string Qualifier =
31  getNameSpaceQualifier(rdfXml, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
32 
33  // Find all Qualifier:Description elements where the Qualifier:about attribute is
34  // "#oldId"
35  std::string::size_type start = 0;
36  std::string::size_type end = 0;
37  unsigned C_INT32 count = 0;
38 
39  // Find the next Qualifier:Description element.
40  while (findNextElement(rdfXml, Qualifier + "Description", start, end))
41  {
42  // Determine the end of the element, i.e, a > which is not enclosed in " or '
43  std::string::size_type pos = start;
44 
45  // Check whether we have a Qualifier:about attribute
46  pos = rdfXml.find(Qualifier + "about=", start);
47  if (pos < end && pos != std::string::npos)
48  {
49  pos += Qualifier.length() + 6;
50  const char Quote = rdfXml[pos];
51  pos++; // advance past Quote
52 
53  // Check whether we have a local id indicate by #
54  if (rdfXml[pos] == '#')
55  {
56  pos++; // advance past #
57  std::string::size_type len = rdfXml.find(Quote, pos) - pos;
58 
59  // Check whether we have a local file reference to the old id
60  // or the old id was not given
61  if (oldId == "" ||
62  rdfXml.substr(pos, len) == oldId)
63  {
64  rdfXml.replace(pos, len, newId);
65  count++;
66  }
67  }
68  }
69  }
70 
71  return count;
72 }
73 
74 // static
75 unsigned C_INT32 CRDFUtilities::fixSBMLRdf(std::string & rdfXml)
76 {
77 
78  // Nothing to do
79  if (rdfXml == "")
80  return 0;
81 
82  std::string::size_type start = 0;
83  std::string::size_type end = 0;
84  std::string::size_type pos = 0;
85  unsigned C_INT32 count = 0;
86 
87  // Fix broken SBML RDF:
88  // <dc:creator rdf:parseType="Resource">
89  // <rdf:Bag>
90  // It is not allowed to use the attribute rdf:parseType="Resource" and <rdf:Bag> simultaneously
91 
92  // Determine the rdf name space qualifier for:
93  std::string RDFQualifier =
94  getNameSpaceQualifier(rdfXml, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
95 
96  // We first look for all elements having the attribute rdf:parseType="Resource"
97  while (findNextElement(rdfXml, "", start, end))
98  {
99  if ((pos = std::min(rdfXml.find(RDFQualifier + "parseType=\"Resource\"", start),
100  rdfXml.find(RDFQualifier + "parseType='Resource'", start))) > end)
101  continue;
102 
103  // We found the attribute.
104  // Check whether the next element is an rdf:Bag
105  // Remember the current candidate
106  std::string::size_type currentStart = start;
107  std::string::size_type currentEnd = end;
108  if (findNextElement(rdfXml, "", start, end) &&
109  findNextElement(rdfXml, RDFQualifier + "Bag", currentStart, currentEnd) &&
110  start == currentStart &&
111  end == currentEnd)
112  {
113  // The next element is a bag element. We therefore have to remove the attribute.
114  rdfXml.erase(pos, RDFQualifier.length() + 20);
115  end -= RDFQualifier.length() + 20;
116 
117  count++;
118  }
119  }
120 
121  start = 0;
122  end = 0;
123  pos = 0;
124 
125  // Fix broken SBML RDF:
126  // <vCard:ORG>
127  // <vCard:Orgname>ORGANIZATION_NAME</vCard:Orgname>
128  // </vCard:ORG>
129  // The <vCard:ORG> element must have the attribute rdf:parseType="Resource".
130 
131  // Determine the vcard name space qualifier for:
132  std::string VCardQualifier =
133  getNameSpaceQualifier(rdfXml, "http://www.w3.org/2001/vcard-rdf/3.0#");
134 
135  // We first look for all elements having the attribute rdf:parseType="Resource"
136  while (findNextElement(rdfXml, VCardQualifier + "ORG", start, end))
137  {
138  // Check whether the attribute rdf:parseType="Resource" is present.
139  if ((pos = std::min(rdfXml.find(RDFQualifier + "parseType=\"Resource\"", start),
140  rdfXml.find(RDFQualifier + "parseType='Resource'", start))) < end)
141  continue;
142 
143  // The attribute is missing we insert it.
144  rdfXml.insert(end, " " + RDFQualifier + "parseType=\"Resource\"");
145  count++;
146  }
147  return count;
148 }
149 
150 // static
151 std::string CRDFUtilities::getNameSpaceQualifier(const std::string & rdfXml,
152  const std::string & nameSpace)
153 {
154  std::string::size_type start = 0;
155  std::string::size_type end = 0;
156 
157  while (true)
158  {
159  // Locate first name space declaration
160  start = rdfXml.find("xmlns:", end);
161  if (start == std::string::npos)
162  break;
163 
164  start += 6;
165  end = rdfXml.find("=", start);
166  if (end == std::string::npos)
167  break;
168 
169  if (rdfXml.compare(end + 1, nameSpace.length() + 2, "\"" + nameSpace + "\"") &&
170  rdfXml.compare(end + 1, nameSpace.length() + 2, "'" + nameSpace + "'"))
171  continue;
172 
173  // We have the qualifier
174  return rdfXml.substr(start, end - start) + ":";
175  }
176 
177  return "";
178 }
179 
180 // static
181 bool CRDFUtilities::findNextElement(const std::string & rdfXml,
182  const std::string & elementName,
183  std::string::size_type & start,
184  std::string::size_type & end)
185 {
186  static std::string WhiteSpace = "\x20\x09\x0D\x0A";
187 
188  bool ignore = false;
189  bool ignoreDouble = false;
190  bool ignoreSingle = false;
191 
192  if (end >= rdfXml.length())
193  return false;
194 
195  std::string::const_iterator it = rdfXml.begin() + end;
196  std::string::const_iterator itEnd = rdfXml.end();
197 
198  if (*it == '>' && it < itEnd) ++it;
199 
200  start = std::string::npos;
201  end = std::string::npos;
202 
203  while (true)
204  {
205  for (; it < itEnd && end == std::string::npos; ++it)
206  {
207  switch (*it)
208  {
209  case '\'':
210  if (!ignoreDouble)
211  {
212  ignore = !ignore;
213  ignoreSingle = !ignoreSingle;
214  }
215  break;
216 
217  case '\"':
218  if (!ignoreSingle)
219  {
220  ignore = !ignore;
221  ignoreDouble = !ignoreDouble;
222  }
223  break;
224 
225  case '<':
226  if (!ignore)
227  start = it - rdfXml.begin();
228  break;
229 
230  case '>':
231  if (!ignore && start != std::string::npos)
232  end = it - rdfXml.begin();
233  break;
234 
235  default:
236  break;
237  }
238  }
239 
240  if (end == std::string::npos)
241  return false;
242 
243  if (elementName == "")
244  return true;
245 
246  std::string::size_type NameStart = rdfXml.find_first_not_of(WhiteSpace, start + 1);
247  std::string::size_type NameEnd = rdfXml.find_first_of(WhiteSpace + "/>", NameStart);
248 
249  if (rdfXml.substr(NameStart, NameEnd - NameStart) == elementName)
250  return true;
251 
252  start = std::string::npos;
253  end = std::string::npos;
254  }
255 
256  return false;
257 }
258 
260 {}
261 
263 {}
static unsigned C_INT32 fixSBMLRdf(std::string &rdfXml)
static unsigned C_INT32 fixLocalFileAboutReference(std::string &rdfXml, const std::string &newId, const std::string &oldId)
#define C_INT32
Definition: copasi.h:90
static bool findNextElement(const std::string &rdfXml, const std::string &elementName, std::string::size_type &start, std::string::size_type &end)
virtual ~CRDFUtilities()
static std::string getNameSpaceQualifier(const std::string &rdfXml, const std::string &nameSpace)
#define min(a, b)
Definition: f2c.h:175