3 homepage: http://arc.semsol.org/
4 license: http://arc.semsol.org/license
6 class: ARC2 RDF/XML Parser
7 author: Benjamin Nowack
8 version: 2009-02-09 (Addition: Support for RDFa detection via typeof/property/about)
11 ARC2::inc('LegacyXMLParser');
13 class ARC2_SemHTMLParser extends ARC2_LegacyXMLParser {
15 function __construct($a = '', &$caller) {
16 parent::__construct($a, $caller);
19 function ARC2_SemHTMLParser($a = '', &$caller) {
20 $this->__construct($a, $caller);
23 function __init() {/* reader */
25 $this->default_sem_html_formats = 'dc openid erdf rdfa posh-rdf microformats';
26 $this->triples = array();
27 $this->target_encoding = '';
29 $this->added_triples = array();
30 $this->skip_dupes = false;
31 $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a);
33 $this->auto_extract = $this->v('auto_extract', 1, $this->a);
34 $this->extracted_formats = array();
35 $this->cache = array();
36 $this->detected_formats = array();
37 $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a);
42 function x($re, $v, $options = 'si', $keep_ws = 0) {
43 list($ws, $v) = preg_match('/^(\s*)(.*)$/s', $v, $m) ? array($m[1], $m[2]) : array('', $v);
44 if (preg_match("/^" . $re . "(.*)$/" . $options, $v, $m)) {
45 if ($keep_ws) $m[1] = $ws . $m[1];
51 function camelCase($v) {
53 while (preg_match('/^(.*)[\-\_ ](.*)$/', $r, $m)) {
54 $r = $m[1] . ucfirst($m[2]);
61 function setReader(&$reader) {
62 $this->reader =& $reader;
65 function createBnodeID(){
67 return '_:' . $this->bnode_prefix . $this->bnode_id;
71 if (function_exists('html_entity_decode')) {
72 $t['o'] = html_entity_decode($t['o']);
74 if ($this->skip_dupes) {
75 $h = md5(serialize($t));
76 if (!isset($this->added_triples[$h])) {
77 $this->triples[$this->t_count] = $t;
79 $this->added_triples[$h] = true;
83 $this->triples[$this->t_count] = $t;
88 function getTriples() {
89 return $this->v('triples', array());
92 function countTriples() {
93 return $this->t_count;
96 function getSimpleIndex($flatten_objects = 1, $vals = '') {
97 return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
102 function parse($path, $data = '') {
103 $this->nodes = array();
104 $this->node_count = 0;
107 if (!$this->v('reader')) {
109 $this->reader = & new ARC2_Reader($this->a, $this);
111 $this->reader->setAcceptHeader('Accept: text/html, application/xhtml, */*; q=0.9');
112 $this->reader->activate($path, $data);
113 $this->target_encoding = $this->reader->getEncoding(false);
114 $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base;
115 $this->base = $this->x_base;
116 $this->doc_url = $this->reader->base;
120 while ($d = $this->reader->readStream(1)) {
121 $rest = $this->processData($rest . $d);
123 $this->reader->closeStream();
124 unset($this->reader);
125 return $this->done();
130 function getEncoding() {
131 return $this->target_encoding;
137 if ($this->auto_extract) {
144 function processData($v) {
148 if ((list($sub_r, $sub_v) = $this->xComment($sub_v)) && $sub_r) {
149 $this->open(0, 'comment', array('value' => $sub_r));
150 $this->close(0, 'comment');
153 if ((list($sub_r, $sub_v) = $this->xDoctype($sub_v)) && $sub_r) {
154 $this->open(0, 'doctype', array('value' => $sub_r));
155 $this->close(0, 'doctype');
157 if (preg_match('/rdfa /i', $sub_r)) $this->detected_formats['rdfa'] = 1;
160 if ($this->level && ((list($sub_r, $sub_v) = $this->xWS($sub_v)) && $sub_r)) {
161 $this->cData(0, $sub_r);
163 elseif ((list($sub_r, $sub_v) = $this->xOpen($sub_v)) && $sub_r) {
164 $this->open(0, $sub_r['tag'], $sub_r['a']);
165 $this->cur_tag = $sub_r['tag'];
166 if ($sub_r['empty']) {
167 $this->close(0, $sub_r['tag'], 1);
171 if (!isset($this->detected_formats['erdf']) && isset($sub_r['a']['profile m']) && in_array('http://purl.org/NET/erdf/profile', $sub_r['a']['profile m'])) $this->detected_formats['erdf'] = 1;
172 /* poshRDF detection */
173 if (!isset($this->detected_formats['posh-rdf']) && isset($sub_r['a']['class m']) && in_array('rdf-p', $sub_r['a']['class m'])) $this->detected_formats['posh-rdf'] = 1;
175 if (!isset($this->detected_formats['rdfa']) && ($this->cur_tag == 'html') && isset($sub_r['a']['version m']) && in_array('XHTML+RDFa', $sub_r['a']['version m'])) $this->detected_formats['rdfa'] = 1;
176 if (!isset($this->detected_formats['rdfa']) && isset($sub_r['a']['xmlns']) && $sub_r['a']['xmlns'] && $this->isRDFNSDecl($sub_r['a']['xmlns'])) $this->detected_formats['rdfa'] = 1;
177 if (!isset($this->detected_formats['rdfa']) && array_intersect(array('about', 'typeof', 'property'), array_keys($sub_r['a']))) $this->detected_formats['rdfa'] = 1;
179 elseif ((list($sub_r, $sub_v) = $this->xClose($sub_v)) && $sub_r) {
180 if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $sub_r['tag'])) {
181 /* already implicitly closed */
184 $this->close(0, $sub_r['tag']);
188 elseif ((list($sub_r, $sub_v) = $this->xCData($sub_v)) && $sub_r) {
189 $this->cData(0, $sub_r);
200 function isRDFNSDecl($ns) {
201 foreach ($ns as $k => $v) {
209 function xComment($v) {
210 if ($r = $this->x('\<\!\-\-', $v)) {
211 if ($sub_r = $this->x('(.*)\-\-\>', $r[1], 'Us')) {
212 return array($sub_r[1], $sub_r[2]);
218 function xDoctype($v) {
219 if ($r = $this->x('\<\!DOCTYPE', $v)) {
220 if ($sub_r = $this->x('([^\>]+)\>', $r[1])) {
221 return array($sub_r[1], $sub_r[2]);
228 if ($r = ARC2::x('(\s+)', $v)) {
229 return array($r[1], $r[2]);
237 if ($r = $this->x('\<([^\s\/\>]+)([^\>]*)\>', $v)) {
238 list($sub_r, $sub_v) = $this->xAttributes($r[2]);
239 return array(array('tag' => strtolower($r[1]), 'a' => $sub_r, 'empty' => $this->isEmpty($r[1], $r[2])), $r[3]);
246 function xAttributes($v) {
248 while ((list($sub_r, $v) = $this->xAttribute($v)) && $sub_r) {
249 if ($sub_sub_r = $this->x('xmlns\:?(.*)', $sub_r['k'])) {
250 $this->nsDecl(0, $sub_sub_r[1], $sub_r['value']);
251 $r['xmlns'][$sub_sub_r[1]] = $sub_r['value'];
254 $r[$sub_r['k']] = $sub_r['value'];
255 $r[$sub_r['k'] . ' m'] = $sub_r['values'];
258 return array($r, $v);
263 function xAttribute($v) {
264 if ($r = $this->x('([^\s\=]+)\s*(\=)?\s*([\'\"]?)', $v)) {
265 if (!$r[2]) {/* no '=' */
267 return array(0, $r[4]);
269 return array(array('k' => $r[1], 'value' => 1, 'values' => array(1)), $r[4]);
271 if (!$r[3]) {/* no quots */
272 if ($sub_r = $this->x('([^\s]+)', $r[4])) {
273 return array(array('k' => $r[1], 'value' => $sub_r[1], 'values' => array($sub_r[1])), $sub_r[2]);
275 return array(array('k' => $r[1], 'value' => '', 'values' => array()), $r[4]);
280 while ($sub_v && (!$sub_r = $this->x('(\x5c\\' .$r[3]. '|\\' .$r[3]. ')', $sub_v))) {
281 $val .= substr($sub_v, 0, 1);
282 $sub_v = substr($sub_v, 1);
284 $sub_v = $sub_v ? $sub_r[2] : $sub_v;
285 $vals = split(' ', $val);
286 return array(array('k' => $r[1], 'value' => $val, 'values' => $vals), $sub_v);
293 function isEmpty($t, $v) {
294 if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $t)) {
297 if (preg_match('/\/$/', $v)) {
305 function xClose($v) {
306 if ($r = $this->x('\<\/([^\s\>]+)\>', $v)) {
307 return array(array('tag' => strtolower($r[1])), $r[2]);
314 function xCData($v) {
315 if (preg_match('/(script|style)/i', $this->cur_tag)) {
316 if ($r = $this->x('(.+)(\<\/' . $this->cur_tag . '\>)', $v, 'Uis')) {
317 return array($r[1], $r[2] . $r[3]);
320 elseif ($r = $this->x('([^\<]+)', $v, 'si', $this->keep_cdata_ws)) {
321 return array($r[1], $r[2]);
328 function extractRDF($formats = '') {
329 $this->node_index = $this->getNodeIndex();
330 $formats = !$formats ? $this->v1('sem_html_formats', $this->default_sem_html_formats, $this->a) : $formats;
331 $formats = split(' ', $formats);
332 foreach ($formats as $format) {
333 if (!in_array($format, $this->extracted_formats)) {
334 $comp = $this->camelCase($format) . 'Extractor';
335 if (ARC2::inc($comp)) {
336 $cls = 'ARC2_' . $comp;
337 $e = new $cls($this->a, $this);
340 $this->extracted_formats[] = $format;
345 function getNode($id) {
346 return isset($this->nodes[$id]) ? $this->nodes[$id] : 0;