COPASI API  4.16.103
CRDFParser.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 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <sstream>
16 #include <string.h>
17 
18 #include "copasi.h"
19 
20 #include "CRDFParser.h"
21 #include "CRDFGraph.h"
22 #include "CRDFSubject.h"
23 #include "CRDFObject.h"
24 #include "CRDFLiteral.h"
25 
28 
29 // static
30 CRDFGraph * CRDFParser::graphFromXml(const std::string & xml)
31 {
32  CRDFParser Parser;
33 
34  std::istringstream XML;
35  XML.str(xml);
36 
37  return Parser.parse(XML);
38 }
39 
41  CRaptorInit(),
42  mpParser(NULL)
43 {
44  mpParser = raptor_new_parser("rdfxml");
45 
46  raptor_set_fatal_error_handler(mpParser, NULL, &CRDFParser::FatalErrorHandler);
47  raptor_set_error_handler(mpParser, NULL, &CRDFParser::ErrorHandler);
48  raptor_set_warning_handler(mpParser, NULL, &CRDFParser::WarningHandler);
49 }
50 
52 {
53  if (mpParser != NULL)
54  raptor_free_parser(mpParser);
55 }
56 
57 CRDFGraph * CRDFParser::parse(std::istream & stream)
58 {
59  bool success = true;
60  bool done = false;
61 
62  stream.imbue(std::locale::classic());
63  stream.precision(16);
64 
65  // Create the new graph
66  CRDFGraph * pGraph = new CRDFGraph;
67 
68  unsigned C_INT32 BUFFER_SIZE = 0xfffe;
69  char * pBuffer = new char[BUFFER_SIZE + 1];
70 
71  stream.get(pBuffer, BUFFER_SIZE, 0);
72 
73  if (stream.gcount() != 0)
74  {
75  raptor_uri * pURI = raptor_new_uri((unsigned char *) "#");
76 
77  if (raptor_start_parse(mpParser, pURI))
78  fatalError();
79 
80  raptor_set_statement_handler(mpParser, pGraph, &CRDFParser::TripleHandler);
81  raptor_set_namespace_handler(mpParser, pGraph, &CRDFParser::NameSpaceHandler);
82  raptor_set_generate_id_handler(mpParser, pGraph, &CRDFParser::GenerateIdHandler);
83 
84  while (!done)
85  {
86  if (stream.eof()) done = true;
87 
88  if (stream.fail() && !done)
89  fatalError();
90 
91  if (raptor_parse_chunk(mpParser,
92  (unsigned char *) pBuffer,
93  strlen(pBuffer),
94  done ? 1 : 0))
95  {
96  done = true;
97  success = false;
98  }
99 
100  stream.get(pBuffer, BUFFER_SIZE, 0);
101  }
102 
103  pRaptorFreeUri(pURI);
104  }
105 
106  delete [] pBuffer;
107 
108  if (!success)
109  {
110  pdelete(pGraph);
111  return NULL;
112  }
113 
114  if (pGraph)
115  pGraph->guessGraphRoot();
116 
117  return pGraph;
118 }
119 
120 // static
121 void CRDFParser::TripleHandler(void * pGraph, const raptor_statement * pTriple)
122 {
123  // We need to create a CRDFSubject, Predicate, and CRDFObject
124  CRDFSubject Subject;
125  std::string Predicate;
126  CRDFObject Object;
127  CRDFLiteral Literal;
128 
129  switch (pTriple->subject_type)
130  {
131  case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
133  Subject.setResource((char *) raptor_uri_as_string((raptor_uri *) pTriple->subject),
134  CRaptorInit::isLocalURI((raptor_uri *) pTriple->subject));
135  break;
136 
137  case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS:
139  Subject.setBlankNodeId((char *) pTriple->subject);
140  break;
141 
142  default:
143  fatalError();
144  }
145 
146  switch (pTriple->predicate_type)
147  {
148  case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
149  Predicate = (char *) raptor_uri_as_string((raptor_uri *) pTriple->predicate);
150  break;
151 
152  default:
153  fatalError();
154  }
155 
156  switch (pTriple->object_type)
157  {
158  case RAPTOR_IDENTIFIER_TYPE_ORDINAL:
159  case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
161  Object.setResource((char *) raptor_uri_as_string((raptor_uri *) pTriple->object),
162  CRaptorInit::isLocalURI((raptor_uri *) pTriple->object));
163  break;
164 
165  case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS:
167  Object.setBlankNodeId((char *) pTriple->object);
168  break;
169 
170  case RAPTOR_IDENTIFIER_TYPE_LITERAL:
172 
173  if (pTriple->object_literal_datatype != NULL)
174  {
175  Literal.setType(CRDFLiteral::TYPED);
176  Literal.setDataType((const char *) raptor_uri_as_string(pTriple->object_literal_datatype));
177  }
178  else
179  {
180  Literal.setType(CRDFLiteral::PLAIN);
181 
182  if (pTriple->object_literal_language != NULL)
183  Literal.setLanguage((const char *) pTriple->object_literal_language);
184  }
185 
186  Literal.setLexicalData((char *) pTriple->object);
187  Object.setLiteral(Literal);
188  break;
189 
190  default:
191  fatalError();
192  }
193 
194  // Add the triplet to the graph
195  static_cast<CRDFGraph *>(pGraph)->addTriplet(Subject, Predicate, Object);
196 }
197 
198 // static
199 void CRDFParser::NameSpaceHandler(void * pGraph, raptor_namespace * pNameSpace)
200 {
201  const unsigned char * pURI =
202  raptor_uri_as_string(raptor_namespace_get_uri(pNameSpace));
203  std::string URI("");
204 
205  if (pURI) URI = (const char *) pURI;
206 
207  const unsigned char * pPrefix =
208  raptor_namespace_get_prefix(pNameSpace);
209  std::string Prefix("");
210 
211  if (pPrefix) Prefix = (const char *) pPrefix;
212 
213  static_cast<CRDFGraph *>(pGraph)->addNameSpace(Prefix, URI);
214 }
215 
216 // static
217 unsigned char * CRDFParser::GenerateIdHandler(void * pGraph,
218  raptor_genid_type /* type */,
219  unsigned char * existingNodeId)
220 {
221  std::string NodeId;
222 
223  if (existingNodeId != NULL)
224  NodeId = static_cast<CRDFGraph *>(pGraph)->generatedNodeId((char *) existingNodeId).c_str();
225  else
226  NodeId = static_cast<CRDFGraph *>(pGraph)->generatedNodeId().c_str();
227 
228  return (unsigned char *) strdup(NodeId.c_str());
229 }
230 
231 // static
232 void CRDFParser::FatalErrorHandler(void *, raptor_locator * pLocator, const char * message)
233 {
235  raptor_locator_line(pLocator),
236  raptor_locator_column(pLocator),
237  message);
238 }
239 
240 // static
241 void CRDFParser::ErrorHandler(void *, raptor_locator * pLocator, const char * message)
242 {
244  raptor_locator_line(pLocator),
245  raptor_locator_column(pLocator),
246  message);
247 }
248 
249 // static
250 void CRDFParser::WarningHandler(void *, raptor_locator * pLocator, const char * message)
251 {
253  raptor_locator_line(pLocator),
254  raptor_locator_column(pLocator),
255  message);
256 }
void setLanguage(const std::string &language)
Definition: CRDFLiteral.cpp:45
#define pdelete(p)
Definition: copasi.h:215
static void TripleHandler(void *pGraph, const raptor_statement *pTriple)
Definition: CRDFParser.cpp:121
static void NameSpaceHandler(void *pGraph, raptor_namespace *pNameSpace)
Definition: CRDFParser.cpp:199
void setLiteral(const CRDFLiteral &literal)
Definition: CRDFObject.cpp:106
#define fatalError()
static void ErrorHandler(void *, raptor_locator *pLocator, const char *message)
Definition: CRDFParser.cpp:241
static void WarningHandler(void *, raptor_locator *pLocator, const char *message)
Definition: CRDFParser.cpp:250
CRDFGraph * parse(std::istream &stream)
Definition: CRDFParser.cpp:57
#define C_INT32
Definition: copasi.h:90
void setDataType(const std::string &dataType)
Definition: CRDFLiteral.cpp:51
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFSubject.cpp:88
bool guessGraphRoot(const std::string &about=" ")
Definition: CRDFGraph.cpp:97
void setType(const eObjectType &type)
Definition: CRDFObject.cpp:82
raptor_parser * mpParser
Definition: CRDFParser.h:122
void setResource(const std::string &resource, const bool &isLocal)
Definition: CRDFSubject.cpp:76
#define MCAnnotation
void setType(const eLiteralType &type)
Definition: CRDFLiteral.cpp:39
void setLexicalData(const std::string &lexicalData)
Definition: CRDFLiteral.cpp:57
static bool isLocalURI(raptor_uri *pURI)
Definition: CRaptorInit.cpp:36
void setResource(const std::string &resource, const bool &isLocal)
Definition: CRDFObject.cpp:88
static void FatalErrorHandler(void *, raptor_locator *pLocator, const char *message)
Definition: CRDFParser.cpp:232
#define BUFFER_SIZE
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFObject.cpp:100
void setType(const eSubjectType &type)
Definition: CRDFSubject.cpp:70
static CRDFGraph * graphFromXml(const std::string &xml)
Definition: CRDFParser.cpp:30
#define pRaptorFreeUri(p)
Definition: CRaptorInit.h:47
static unsigned char * GenerateIdHandler(void *pGraph, raptor_genid_type type, unsigned char *userNodeId)
Definition: CRDFParser.cpp:217