COPASI API  4.16.103
CConstants.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 #include <stdlib.h>
12 
13 #include "copasi.h"
14 
15 #include "CConstants.h"
16 #include "CRDFGraph.h"
19 
20 //static
21 const CMIRIAMResources * CMIRIAMResourceObject::mpResources = NULL;
22 
23 size_t CMIRIAMResourceObject::getResource(const std::string & URI)
24 {return mpResources->getMIRIAMResourceIndex(URI);}
25 
26 //static
28 {return *mpResources;}
29 
30 // static
31 void CMIRIAMResourceObject::unescapeId(std::string & id)
32 {
33  // We have to convert all %[0-9a-fA-F][0-9a-fA-F] character sequences to utf8 characters.
34  std::string::size_type pos;
35 
36  for (pos = 0; pos < id.length(); pos++)
37  if (id[pos] == '%' &&
38  id.find_first_not_of("0123456789abcdefABCDEF", pos + 1) > pos + 2)
39  {
40  char ascii[2];
41  ascii[0] = (unsigned char) strtol(id.substr(pos + 1 , 2).c_str(), NULL, 16);
42  ascii[1] = 0x0;
43  id.replace(pos, 3, CCopasiXMLInterface::utf8(ascii));
44  }
45 }
46 
47 // static
48 std::string CMIRIAMResourceObject::trimId(const std::string & id)
49 {
50  std::string Id = id;
51 
52  /* Trim leading and trailing whitespaces from the string */
53  std::string::size_type begin = Id.find_first_not_of("\x20\x09\x0d\x0a");
54 
55  if (begin == std::string::npos)
56  {
57  Id = "";
58  }
59  else
60  {
61  std::string::size_type end = id.find_last_not_of("\x20\x09\x0d\x0a");
62 
63  if (end == std::string::npos)
64  Id = id.substr(begin);
65  else
66  Id = id.substr(begin, end - begin + 1);
67  }
68 
69  return Id;
70 }
71 
73  mId(),
74  mpNode(pNode)
75 {
76  if (mpNode != NULL)
78 }
79 
80 //static
82 {
83  mpResources = pResources;
84 }
85 
86 CMIRIAMResourceObject::CMIRIAMResourceObject(const std::string & displayName, const std::string & id):
87  mResource(C_INVALID_INDEX),
88  mId(),
89  mpNode(NULL)
90 {
91  setDisplayName(displayName);
92  mId = id;
93 }
94 
96  mResource(src.mResource),
97  mId(src.mId),
98  mpNode(NULL)
99 {}
100 
101 bool CMIRIAMResourceObject::setId(const std::string & id)
102 {
103  mId = trimId(id);
104 
105  // Empty IDs are not allowed.
106  if (mId == "")
107  return false;
108 
109  // Check whether the resource is known.
110  if (mResource == C_INVALID_INDEX)
111  return true;
112 
113  return isValid();
114 }
115 
116 const std::string & CMIRIAMResourceObject::getId() const
117 {return mId;}
118 
119 bool CMIRIAMResourceObject::setURI(const std::string & URI)
120 {
121  mResource = getResource(URI);
122 
123  if (mResource == C_INVALID_INDEX)
124  {
126  }
127 
128  extractId(URI);
129 
130  return isValid();
131 }
132 
133 std::string CMIRIAMResourceObject::getURI() const
134 {
135  return (mpResources->getMIRIAMResource(mResource)).getMIRIAMURI() + ":" + mId;
136 }
137 
139 {
141 
142  if (URL == "http://identifiers.org/unknown")
143  {
144  return mId;
145  }
146 
147  return URL + "/" + mId;
148 }
149 
151 {
152  mpNode = pNode;
153 
154  if (mpNode != NULL)
155  return setURI(mpNode->getObject().getResource());
156 
157  return true;
158 }
159 
161 {return mpNode;}
162 
163 bool CMIRIAMResourceObject::setDisplayName(const std::string & displayName)
164 {
166 
167  if (mResource == C_INVALID_INDEX)
168  return false;
169 
170  return true;
171 }
172 
174 {
175  // Check whether the resource is known.
176  if (mResource == C_INVALID_INDEX)
177  return "";
178 
179  return (mpResources->getMIRIAMResource(mResource)).getMIRIAMDisplayName();
180 }
181 
183 {
184  // Check whether the resource is known.
185  if (mResource == C_INVALID_INDEX)
186  return false;
187 
188  // Empty IDs are not allowed.
189  if (mId == "")
190  return false;
191 
192  // TODO Check whether the Id matches the regular expression.
193  return true;
194 }
195 
196 bool CMIRIAMResourceObject::isValid(const std::string & URI) const
197 {
200  return false;
201 
202  return true;
203 }
204 
205 void CMIRIAMResourceObject::extractId(const std::string & URI)
206 {
207  mId = "";
208 
209  // Check whether the resource is known.
210  if (mpResources == NULL ||
212  {
213  mId = URI;
214  return;
215  }
216 
217  int offset;
218  const std::string * pTmp = & mpResources->getMIRIAMResource(mResource).getMIRIAMURI();
219 
220  if (URI.substr(0, pTmp->length()) == *pTmp &&
221  URI.length() > pTmp->length())
222  {
223  offset = (pTmp->at(pTmp->length() - 1) == '/') ? 0 : 1;
224  mId = URI.substr(pTmp->length() + offset);
225  }
226 
227  if (mId == "")
228  {
230 
231  if (URI.substr(0, Tmp.length()) == Tmp &&
232  URI.length() > Tmp.length())
233  {
234  offset = (Tmp[Tmp.length() - 1] == '/') ? 0 : 1;
235  mId = URI.substr(Tmp.length() + offset);
236  }
237  }
238 
239  if (mId == "")
240  {
241  // We need to check for deprecated URIs
242  const CCopasiParameterGroup * pDeprecated = &(mpResources->getMIRIAMResource(mResource)).getMIRIAMDeprecated();
243  CCopasiParameterGroup::index_iterator itDeprecated = pDeprecated->beginIndex();
244  CCopasiParameterGroup::index_iterator endDeprecated = pDeprecated->endIndex();
245 
246  for (; itDeprecated != endDeprecated; ++itDeprecated)
247  if (URI.substr(0, (*itDeprecated)->getValue().pSTRING->length()) == *(*itDeprecated)->getValue().pSTRING &&
248  URI.length() > (*itDeprecated)->getValue().pSTRING->length())
249  {
250  const std::string& uri = *(*itDeprecated)->getValue().pSTRING;
251  offset = (uri[uri.length() - 1] == '/') ? 0 : 1;
252  mId = URI.substr(uri.length() + offset);
253  break;
254  }
255  }
256 
257  unescapeId(mId);
258 
259  return;
260 }
261 
263 {}
const std::string & getMIRIAMURI() const
size_t getResource(const std::string &URI)
Definition: CConstants.cpp:23
static const CMIRIAMResources * mpResources
Definition: CConstants.h:73
bool isValid() const
Definition: CConstants.cpp:182
std::string getIdentifiersOrgURL() const
static void setMIRIAMResources(const CMIRIAMResources *pResources)
Definition: CConstants.cpp:81
const CMIRIAMResource & getMIRIAMResource(const size_t index) const
std::string getURI() const
Definition: CConstants.cpp:133
virtual ~CMIRIAMResourceObject()
Definition: CConstants.cpp:262
static void unescapeId(std::string &id)
Definition: CConstants.cpp:31
index_iterator endIndex() const
#define C_INVALID_INDEX
Definition: copasi.h:222
bool setURI(const std::string &URI)
Definition: CConstants.cpp:119
bool setId(const std::string &id)
Definition: CConstants.cpp:101
CEvaluationNode * mpNode
bool setDisplayName(const std::string &displayName)
Definition: CConstants.cpp:163
bool setNode(CRDFNode *pNode)
Definition: CConstants.cpp:150
CRDFNode * getNode() const
Definition: CConstants.cpp:160
static std::string trimId(const std::string &id)
Definition: CConstants.cpp:48
CRDFObject & getObject()
Definition: CRDFNode.cpp:94
static const CMIRIAMResources & getResourceList()
Definition: CConstants.cpp:27
#define MCAnnotation
std::string getDisplayName() const
Definition: CConstants.cpp:173
index_iterator beginIndex() const
std::string getIdentifiersOrgURL() const
Definition: CConstants.cpp:138
size_t getResourceIndexFromDisplayName(const std::string &displayName) const
const std::string & getId() const
Definition: CConstants.cpp:116
void extractId(const std::string &URI)
Definition: CConstants.cpp:205
size_t getMIRIAMResourceIndex(const std::string &URI) const
static std::string utf8(const std::string &str)
parameterGroup::iterator index_iterator
const std::string & getResource() const
Definition: CRDFObject.cpp:94