COPASI API  4.16.103
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CRDFParser Class Reference

#include <CRDFParser.h>

Inheritance diagram for CRDFParser:
Inheritance graph
[legend]
Collaboration diagram for CRDFParser:
Collaboration graph
[legend]

Public Member Functions

 CRDFParser ()
 
CRDFGraphparse (std::istream &stream)
 
 ~CRDFParser ()
 

Static Public Member Functions

static void ErrorHandler (void *, raptor_locator *pLocator, const char *message)
 
static void FatalErrorHandler (void *, raptor_locator *pLocator, const char *message)
 
static unsigned char * GenerateIdHandler (void *pGraph, raptor_genid_type type, unsigned char *userNodeId)
 
static CRDFGraphgraphFromXml (const std::string &xml)
 
static void NameSpaceHandler (void *pGraph, raptor_namespace *pNameSpace)
 
static void TripleHandler (void *pGraph, const raptor_statement *pTriple)
 
static void WarningHandler (void *, raptor_locator *pLocator, const char *message)
 

Private Attributes

raptor_parser * mpParser
 

Additional Inherited Members

- Private Member Functions inherited from CRaptorInit
 CRaptorInit ()
 
 ~CRaptorInit ()
 
- Static Private Member Functions inherited from CRaptorInit
static bool isLocalURI (raptor_uri *pURI)
 

Detailed Description

Definition at line 29 of file CRDFParser.h.

Constructor & Destructor Documentation

CRDFParser::CRDFParser ( )

Default Constructor

Definition at line 40 of file CRDFParser.cpp.

References ErrorHandler(), FatalErrorHandler(), mpParser, and WarningHandler().

40  :
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 }
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
raptor_parser * mpParser
Definition: CRDFParser.h:122
static void FatalErrorHandler(void *, raptor_locator *pLocator, const char *message)
Definition: CRDFParser.cpp:232
CRDFParser::~CRDFParser ( )

Destructor

Definition at line 51 of file CRDFParser.cpp.

References mpParser.

52 {
53  if (mpParser != NULL)
54  raptor_free_parser(mpParser);
55 }
raptor_parser * mpParser
Definition: CRDFParser.h:122

Member Function Documentation

void CRDFParser::ErrorHandler ( void ,
raptor_locator *  pLocator,
const char *  message 
)
static

A static handler to interface with the C parser library. This is called whenever the parser encounters an error;

Parameters
void* userdata (ignored)
raptor_locator* pLocator
constchar * message

Definition at line 241 of file CRDFParser.cpp.

References CCopasiMessage::ERROR, and MCAnnotation.

Referenced by CRDFParser().

242 {
244  raptor_locator_line(pLocator),
245  raptor_locator_column(pLocator),
246  message);
247 }
#define MCAnnotation
void CRDFParser::FatalErrorHandler ( void ,
raptor_locator *  pLocator,
const char *  message 
)
static

A static handler to interface with the C parser library. This is called whenever the parser encounters a fatal error;

Parameters
void* userdata (ignored)
raptor_locator* pLocator
constchar * message

Definition at line 232 of file CRDFParser.cpp.

References CCopasiMessage::ERROR, and MCAnnotation.

Referenced by CRDFParser().

233 {
235  raptor_locator_line(pLocator),
236  raptor_locator_column(pLocator),
237  message);
238 }
#define MCAnnotation
unsigned char * CRDFParser::GenerateIdHandler ( void pGraph,
raptor_genid_type  type,
unsigned char *  userNodeId 
)
static

A static handler to interface with the C parser library. This is called whenever a node id needs to be generated.

Parameters
void* pGraph
raptor_genid_typetype
unsignedchar * userNodeId
Returns
unsigned char * generatedId

Definition at line 217 of file CRDFParser.cpp.

Referenced by parse().

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 }
CRDFGraph * CRDFParser::graphFromXml ( const std::string &  xml)
static

Convert an RDF/XML syntax string to an RDF Graph. If the conversion fails a NULL pointer is returned. Otherwise a pointer to the resulting graph is returned. It is the responsibility of the caller to destroy the created graph.

Parameters
conststd::string & xml
Returns
CRDFGraph * graphFromXml

Definition at line 30 of file CRDFParser.cpp.

References parse().

Referenced by CQRDFTreeView::enterProtected(), CMIRIAMInfo::load(), and CRDFGraphConverter::SBML2Copasi().

31 {
32  CRDFParser Parser;
33 
34  std::istringstream XML;
35  XML.str(xml);
36 
37  return Parser.parse(XML);
38 }
CRDFGraph * parse(std::istream &stream)
Definition: CRDFParser.cpp:57
void CRDFParser::NameSpaceHandler ( void pGraph,
raptor_namespace *  pNameSpace 
)
static

A static handler to interface with the C parser library. This is called whenever a namespace is declared.

Parameters
void* pGraph
raptor_namespace* pNameSpace

Definition at line 199 of file CRDFParser.cpp.

Referenced by parse().

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 }
CRDFGraph * CRDFParser::parse ( std::istream &  stream)

Parse the stream. If the parsing fails a NULL pointer is returned. Otherwise a pointer to the resulting graph is returned. It is the responsibility of the caller to destroy the created graph. &param std::istream & stream

Returns
CRDFGraph * pGraph

Definition at line 57 of file CRDFParser.cpp.

References BUFFER_SIZE, C_INT32, fatalError, GenerateIdHandler(), CRDFGraph::guessGraphRoot(), mpParser, NameSpaceHandler(), pdelete, pRaptorFreeUri, and TripleHandler().

Referenced by graphFromXml().

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 }
#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
#define fatalError()
#define C_INT32
Definition: copasi.h:90
bool guessGraphRoot(const std::string &about=" ")
Definition: CRDFGraph.cpp:97
raptor_parser * mpParser
Definition: CRDFParser.h:122
#define BUFFER_SIZE
#define pRaptorFreeUri(p)
Definition: CRaptorInit.h:47
static unsigned char * GenerateIdHandler(void *pGraph, raptor_genid_type type, unsigned char *userNodeId)
Definition: CRDFParser.cpp:217
void CRDFParser::TripleHandler ( void pGraph,
const raptor_statement *  pTriple 
)
static

A static handler to interface with the C parser library. This is called whenever an RDF triple is created. Its only purpose is to add the triple to the graph which must provide addTriplet(CRDFSubject, std::string, CRDFObject);

Parameters
void* pGraph
constraptor_statement * pTriple

Definition at line 121 of file CRDFParser.cpp.

References CRDFSubject::BLANK_NODE, CRDFObject::BLANK_NODE, fatalError, CRaptorInit::isLocalURI(), CRDFObject::LITERAL, CRDFLiteral::PLAIN, CRDFSubject::RESOURCE, CRDFObject::RESOURCE, CRDFSubject::setBlankNodeId(), CRDFObject::setBlankNodeId(), CRDFLiteral::setDataType(), CRDFLiteral::setLanguage(), CRDFLiteral::setLexicalData(), CRDFObject::setLiteral(), CRDFSubject::setResource(), CRDFObject::setResource(), CRDFLiteral::setType(), CRDFSubject::setType(), CRDFObject::setType(), and CRDFLiteral::TYPED.

Referenced by parse().

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 }
void setLanguage(const std::string &language)
Definition: CRDFLiteral.cpp:45
void setLiteral(const CRDFLiteral &literal)
Definition: CRDFObject.cpp:106
#define fatalError()
void setDataType(const std::string &dataType)
Definition: CRDFLiteral.cpp:51
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFSubject.cpp:88
void setType(const eObjectType &type)
Definition: CRDFObject.cpp:82
void setResource(const std::string &resource, const bool &isLocal)
Definition: CRDFSubject.cpp:76
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
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFObject.cpp:100
void setType(const eSubjectType &type)
Definition: CRDFSubject.cpp:70
void CRDFParser::WarningHandler ( void ,
raptor_locator *  pLocator,
const char *  message 
)
static

A static handler to interface with the C parser library. This is called whenever the parser encounters a warning;

Parameters
void* userdata (ignored)
raptor_locator* pLocator
constchar * message

Definition at line 250 of file CRDFParser.cpp.

References MCAnnotation, and CCopasiMessage::WARNING.

Referenced by CRDFParser().

251 {
253  raptor_locator_line(pLocator),
254  raptor_locator_column(pLocator),
255  message);
256 }
#define MCAnnotation

Member Data Documentation

raptor_parser* CRDFParser::mpParser
private

Pointer to the underlying C parser from the Raptor library

Definition at line 122 of file CRDFParser.h.

Referenced by CRDFParser(), parse(), and ~CRDFParser().


The documentation for this class was generated from the following files: