3 * ARC2 RDF Parser (generic)
5 * @author Benjamin Nowack <bnowack@semsol.com>
6 * @license http://arc.semsol.org/license
7 * @homepage <http://arc.semsol.org/>
14 class ARC2_RDFParser extends ARC2_Class {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_RDFParser($a = '', &$caller) {
21 $this->__construct($a, $caller);
24 function __init() {/* proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects, reader, skip_dupes */
26 $this->a['format'] = $this->v('format', false, $this->a);
27 $this->keep_time_limit = $this->v('keep_time_limit', 0, $this->a);
28 $this->triples = array();
30 $this->added_triples = array();
31 $this->skip_dupes = $this->v('skip_dupes', false, $this->a);
32 $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a);
39 function setReader(&$reader) {
40 $this->reader =& $reader;
43 function parse($path, $data = '') {
45 if (!isset($this->reader)) {
47 $this->reader = & new ARC2_Reader($this->a, $this);
49 $this->reader->activate($path, $data) ;
50 /* format detection */
54 'sparqlxml' => 'SPOG',
55 'ntriples' => 'Turtle',
59 'sgajson' => 'SGAJSON',
62 $format = $this->reader->getFormat();
63 if (!$format || !isset($mappings[$format])) {
64 return $this->addError('No parser available for "' . $format . '".');
66 $this->format = $format;
68 $suffix = $mappings[$format] . 'Parser';
70 $cls = 'ARC2_' . $suffix;
71 $this->parser =& new $cls($this->a, $this);
72 $this->parser->setReader($this->reader);
73 return $this->parser->parse($path, $data);
76 function parseData($data) {
77 return $this->parse(ARC2::getScriptURI(), $data);
87 function createBnodeID(){
89 return '_:' . $this->bnode_prefix . $this->bnode_id;
92 function getTriples() {
93 return $this->v('parser') ? $this->m('getTriples', false, array(), $this->v('parser')) : array();
96 function countTriples() {
97 return $this->v('parser') ? $this->m('countTriples', false, 0, $this->v('parser')) : 0;
100 function getSimpleIndex($flatten_objects = 1, $vals = '') {
101 return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
106 if (isset($this->reader)) unset($this->reader);
107 if (isset($this->parser)) {
108 $this->parser->__init();
109 unset($this->parser);
115 function extractRDF($formats = '') {
116 if (method_exists($this->parser, 'extractRDF')) {
117 return $this->parser->extractRDF($formats);
123 function getEncoding($src = 'config') {
124 if (method_exists($this->parser, 'getEncoding')) {
125 return $this->parser->getEncoding($src);
130 * returns the array of namespace prefixes encountered during parsing
131 * @return array (keys = namespace URI / values = prefix used)
134 function getParsedNamespacePrefixes() {
135 if (isset($this->parser)) {
136 return $this->v('nsp', array(), $this->parser);
138 return $this->v('nsp', array());