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

#include <CRDFWriter.h>

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

Public Member Functions

 CRDFWriter ()
 
char * write (const CRDFGraph *pGraph)
 
 ~CRDFWriter ()
 

Static Public Member Functions

static std::string xmlFromGraph (const CRDFGraph *pGraph)
 

Private Member Functions

void initNamespaces (const CRDFGraph *pGraph)
 
- Private Member Functions inherited from CRaptorInit
 CRaptorInit ()
 
 ~CRaptorInit ()
 

Private Attributes

CRDFGraphmpGraph
 
raptor_serializer * mpWriter
 

Additional Inherited Members

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

Detailed Description

Definition at line 26 of file CRDFWriter.h.

Constructor & Destructor Documentation

CRDFWriter::CRDFWriter ( )

Constructor

Definition at line 43 of file CRDFWriter.cpp.

References mpWriter.

43  :
44  CRaptorInit(),
45  mpWriter(NULL),
46  mpGraph(NULL)
47 {
48  mpWriter = raptor_new_serializer("rdfxml-abbrev");
49 
50  // We do not want <?xml version="1.0" encoding="utf-8"?>
51  raptor_serializer_set_feature(mpWriter, RAPTOR_FEATURE_WRITER_XML_DECLARATION, 0);
52 
53  // Remove any empty elements
54  raptor_serializer_set_feature(mpWriter, RAPTOR_FEATURE_WRITER_AUTO_EMPTY, 1);
55 }
CRDFGraph * mpGraph
Definition: CRDFWriter.h:73
raptor_serializer * mpWriter
Definition: CRDFWriter.h:68
CRDFWriter::~CRDFWriter ( )

Destructor

Definition at line 57 of file CRDFWriter.cpp.

References mpWriter.

58 {
59  if (mpWriter != NULL)
60  {
61  raptor_free_serializer(mpWriter);
62  mpWriter = NULL;
63  }
64 }
raptor_serializer * mpWriter
Definition: CRDFWriter.h:68

Member Function Documentation

void CRDFWriter::initNamespaces ( const CRDFGraph pGraph)
private

This adds the default name spaces to the writer

Parameters
constCRDFGraph * pGraph

Definition at line 66 of file CRDFWriter.cpp.

References CRDFGraph::getNameSpaceMap(), mpWriter, and pRaptorFreeUri.

Referenced by write().

67 {
68  std::map< std::string, std::string >::const_iterator it =
69  pGraph->getNameSpaceMap().begin();
70  std::map< std::string, std::string >::const_iterator end =
71  pGraph->getNameSpaceMap().end();
72 
73  raptor_uri* pURI = NULL;
74 
75  for (; it != end; ++it)
76  {
77  pURI = raptor_new_uri((const unsigned char *) it->second.c_str());
78  raptor_serialize_set_namespace(mpWriter, pURI, (const unsigned char *) it->first.c_str());
79  pRaptorFreeUri(pURI);
80  }
81 }
raptor_serializer * mpWriter
Definition: CRDFWriter.h:68
const std::map< std::string, std::string > & getNameSpaceMap() const
Definition: CRDFGraph.cpp:85
#define pRaptorFreeUri(p)
Definition: CRaptorInit.h:47
char * CRDFWriter::write ( const CRDFGraph pGraph)

Convert an RDF graph to an XML string. It is the repsonsibility of user to free the allocate memory for the string. If the conversion fails a NULL pointer is returned.

Parameters
constCRDFGraph * pGraph
Returns
char * XML

Definition at line 83 of file CRDFWriter.cpp.

References CRDFSubject::BLANK_NODE, CRDFObject::BLANK_NODE, fatalError, CRDFSubject::getBlankNodeID(), CRDFObject::getBlankNodeID(), CRDFLiteral::getDataType(), CRDFLiteral::getLanguage(), CRDFLiteral::getLexicalData(), CRDFObject::getLiteral(), CRDFSubject::getResource(), CRDFObject::getResource(), CRDFGraph::getTriplets(), CRDFLiteral::getType(), CRDFSubject::getType(), CRDFObject::getType(), initNamespaces(), CRDFObject::LITERAL, mpWriter, CRDFLiteral::PLAIN, pRaptorFreeUri, CRDFSubject::RESOURCE, CRDFObject::RESOURCE, and CRDFLiteral::TYPED.

84 {
85  bool success = true;
86 
87  void * XML; /* destination for string */
88  size_t Length; /* length of constructed string */
89 
90  initNamespaces(pGraph);
91 
92  raptor_uri * pURI = raptor_new_uri((const unsigned char *) "");
93 
94  if (raptor_serialize_start_to_string(mpWriter, pURI, & XML, & Length))
95  fatalError();
96 
97  // We need to add all triplets
98  std::set< CRDFTriplet >::const_iterator it = pGraph->getTriplets().begin();
99  std::set< CRDFTriplet >::const_iterator end = pGraph->getTriplets().end();
100 
101  raptor_uri * pSubjectURI = NULL;
102  raptor_uri * pPredicateURI = NULL;
103  raptor_uri * pObjectURI = NULL;
104  raptor_uri * pLiteralDataTypeURI = NULL;
105 
106  raptor_statement Triplet;
107 
108  for (; it != end; ++it)
109  {
110  // Set the subject of the triplet
111  const CRDFSubject & Subject = it->pSubject->getSubject();
112 
113  switch (Subject.getType())
114  {
116  Triplet.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
117  pSubjectURI = raptor_new_uri((const unsigned char *) Subject.getResource().c_str());
118  Triplet.subject = pSubjectURI;
119  break;
120 
122  Triplet.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
123  Triplet.subject = Subject.getBlankNodeID().c_str();
124  break;
125  }
126 
127  // Set the predicate of the triplet
128  Triplet.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
129  pPredicateURI = raptor_new_uri((const unsigned char *) it->Predicate.getURI().c_str());
130  Triplet.predicate = pPredicateURI;
131 
132  // Set the object of the triplet
133  const CRDFObject & Object = it->pObject->getObject();
134 
135  switch (Object.getType())
136  {
138  Triplet.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
139  pObjectURI = raptor_new_uri((const unsigned char *) Object.getResource().c_str());
140  Triplet.object = pObjectURI;
141  break;
142 
144  Triplet.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
145  Triplet.object = Object.getBlankNodeID().c_str();
146  break;
147 
148  case CRDFObject::LITERAL:
149  Triplet.object_type = RAPTOR_IDENTIFIER_TYPE_LITERAL;
150  const CRDFLiteral & Literal = Object.getLiteral();
151 
152  switch (Literal.getType())
153  {
154  case CRDFLiteral::TYPED:
155  pLiteralDataTypeURI = raptor_new_uri((const unsigned char *) Literal.getDataType().c_str());
156  Triplet.object_literal_datatype = pLiteralDataTypeURI;
157  Triplet.object_literal_language = NULL;
158  break;
159 
160  case CRDFLiteral::PLAIN:
161  Triplet.object_literal_datatype = NULL;
162 
163  if (Literal.getLanguage() != "")
164  Triplet.object_literal_language = (const unsigned char *) Literal.getLanguage().c_str();
165  else
166  Triplet.object_literal_language = NULL;
167 
168  break;
169  }
170 
171  Triplet.object = Literal.getLexicalData().c_str();
172  break;
173  }
174 
175  if (raptor_serialize_statement(mpWriter, &Triplet))
176  success = false;
177 
178  pRaptorFreeUri(pPredicateURI);
179  pRaptorFreeUri(pObjectURI);
180  pRaptorFreeUri(pLiteralDataTypeURI);
181  pRaptorFreeUri(pSubjectURI);
182  }
183 
184  if (raptor_serialize_end(mpWriter))
185  fatalError();
186 
187  return static_cast< char * >(XML);
188 }
const std::string & getResource() const
Definition: CRDFSubject.cpp:82
const std::string & getDataType() const
Definition: CRDFLiteral.cpp:54
const eObjectType & getType() const
Definition: CRDFObject.cpp:85
const std::string & getBlankNodeID() const
Definition: CRDFObject.cpp:103
const std::string & getBlankNodeID() const
Definition: CRDFSubject.cpp:91
#define fatalError()
raptor_serializer * mpWriter
Definition: CRDFWriter.h:68
const std::set< CRDFTriplet > & getTriplets() const
Definition: CRDFGraph.cpp:434
const std::string & getLanguage() const
Definition: CRDFLiteral.cpp:48
const eLiteralType & getType() const
Definition: CRDFLiteral.cpp:42
void initNamespaces(const CRDFGraph *pGraph)
Definition: CRDFWriter.cpp:66
const std::string & getLexicalData() const
Definition: CRDFLiteral.cpp:60
#define pRaptorFreeUri(p)
Definition: CRaptorInit.h:47
const std::string & getResource() const
Definition: CRDFObject.cpp:94
const eSubjectType & getType() const
Definition: CRDFSubject.cpp:73
CRDFLiteral & getLiteral()
Definition: CRDFObject.cpp:112
std::string CRDFWriter::xmlFromGraph ( const CRDFGraph pGraph)
static

Convert an RDF graph to an XML string

Parameters
constCRDFGraph * pGraph
Returns
std::string

Definition at line 26 of file CRDFWriter.cpp.

Referenced by CMIRIAMInfo::save(), and CRDFGraphConverter::SBML2Copasi().

27 {
28  CRDFWriter Writer;
29 
30  char * pXML = Writer.CRDFWriter::write(pGraph);
31 
32  if (pXML != NULL)
33  {
34  std::string XML = pXML;
35  free(pXML);
36 
37  return XML;
38  }
39 
40  return "";
41 }

Member Data Documentation

CRDFGraph* CRDFWriter::mpGraph
private

A pointer to the created RDF graph

Definition at line 73 of file CRDFWriter.h.

raptor_serializer* CRDFWriter::mpWriter
private

Pointer to the underlying C serializer from the Raptor library

Definition at line 68 of file CRDFWriter.h.

Referenced by CRDFWriter(), initNamespaces(), write(), and ~CRDFWriter().


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