COPASI API  4.16.103
CRDFWriter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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 <sstream>
14 
15 #include "copasi.h"
16 
17 #include "CRDFWriter.h"
18 #include "CRDFGraph.h"
19 #include "CRDFSubject.h"
20 #include "CRDFObject.h"
21 #include "CRDFLiteral.h"
22 
24 
25 //static
26 std::string CRDFWriter::xmlFromGraph(const CRDFGraph * pGraph)
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 }
42 
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 }
56 
58 {
59  if (mpWriter != NULL)
60  {
61  raptor_free_serializer(mpWriter);
62  mpWriter = NULL;
63  }
64 }
65 
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 }
82 
83 char * CRDFWriter::write(const CRDFGraph * pGraph)
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()
char * write(const CRDFGraph *pGraph)
Definition: CRDFWriter.cpp:83
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 std::map< std::string, std::string > & getNameSpaceMap() const
Definition: CRDFGraph.cpp:85
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
static std::string xmlFromGraph(const CRDFGraph *pGraph)
Definition: CRDFWriter.cpp:26
CRDFLiteral & getLiteral()
Definition: CRDFObject.cpp:112