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