PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/arc/parsers/ARC2_RDFParser.php

https://github.com/damz/foafssl-drupal
PHP | 129 lines | 89 code | 23 blank | 17 comment | 7 complexity | 766fcc13ecab8fb6397a96903dbdeebc MD5 | raw file
  1. <?php
  2. /*
  3. homepage: http://arc.semsol.org/
  4. license: http://arc.semsol.org/license
  5. class: ARC2 RDF Parser (generic)
  6. author: Benjamin Nowack
  7. version: 2008-09-30 (Addition: Support for Atom)
  8. */
  9. ARC2::inc('Class');
  10. class ARC2_RDFParser extends ARC2_Class {
  11. function __construct($a = '', &$caller) {
  12. parent::__construct($a, $caller);
  13. }
  14. function ARC2_RDFParser($a = '', &$caller) {
  15. $this->__construct($a, $caller);
  16. }
  17. function __init() {/* proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects, reader, skip_dupes */
  18. parent::__init();
  19. $this->a['format'] = $this->v('format', false, $this->a);
  20. $this->triples = array();
  21. $this->t_count = 0;
  22. $this->added_triples = array();
  23. $this->skip_dupes = $this->v('skip_dupes', false, $this->a);
  24. $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a);
  25. $this->bnode_id = 0;
  26. $this->format = '';
  27. }
  28. /* */
  29. function setReader(&$reader) {
  30. $this->reader =& $reader;
  31. }
  32. function parse($path, $data = '') {
  33. /* reader */
  34. if (!isset($this->reader)) {
  35. ARC2::inc('Reader');
  36. $this->reader = & new ARC2_Reader($this->a, $this);
  37. }
  38. $this->reader->activate($path, $data) ;
  39. /* format detection */
  40. $mappings = array(
  41. 'rdfxml' => 'RDFXML',
  42. 'turtle' => 'Turtle',
  43. 'sparqlxml' => 'SPOG',
  44. 'ntriples' => 'Turtle',
  45. 'html' => 'SemHTML',
  46. 'rss' => 'RSS',
  47. 'atom' => 'Atom',
  48. 'sgajson' => 'SGAJSON',
  49. 'cbjson' => 'CBJSON'
  50. );
  51. $format = $this->reader->getFormat();
  52. if (!$format || !isset($mappings[$format])) {
  53. return $this->addError('No parser available for "' . $format . '".');
  54. }
  55. $this->format = $format;
  56. /* format parser */
  57. $suffix = $mappings[$format] . 'Parser';
  58. ARC2::inc($suffix);
  59. $cls = 'ARC2_' . $suffix;
  60. $this->parser =& new $cls($this->a, $this);
  61. $this->parser->setReader($this->reader);
  62. return $this->parser->parse($path, $data);
  63. }
  64. function parseData($data) {
  65. return $this->parse(ARC2::getScriptURI(), $data);
  66. }
  67. /* */
  68. function done() {
  69. }
  70. /* */
  71. function createBnodeID(){
  72. $this->bnode_id++;
  73. return '_:' . $this->bnode_prefix . $this->bnode_id;
  74. }
  75. function getTriples() {
  76. return $this->v('parser') ? $this->m('getTriples', false, array(), $this->v('parser')) : array();
  77. }
  78. function countTriples() {
  79. return $this->v('parser') ? $this->m('countTriples', false, 0, $this->v('parser')) : 0;
  80. }
  81. function getSimpleIndex($flatten_objects = 1, $vals = '') {
  82. return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
  83. }
  84. function reset() {
  85. $this->__init();
  86. if (isset($this->reader)) unset($this->reader);
  87. if (isset($this->parser)) {
  88. $this->parser->__init();
  89. unset($this->parser);
  90. }
  91. }
  92. /* */
  93. function extractRDF($formats = '') {
  94. if (method_exists($this->parser, 'extractRDF')) {
  95. return $this->parser->extractRDF($formats);
  96. }
  97. }
  98. /* */
  99. function getEncoding($src = 'config') {
  100. if (method_exists($this->parser, 'getEncoding')) {
  101. return $this->parser->getEncoding($src);
  102. }
  103. }
  104. /* */
  105. }