ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMDSaxParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once './Services/Xml/classes/class.ilSaxParser.php';
25 
35 {
36  public $md_in_md = false;
37  public $md_chr_data = '';
38 
39  public $md_cur_el = null;
40 
41  /*
42  * @var boolean enable/disable parsing status.
43  */
44  public $md_parsing_enabled = null;
45  /*
46  * @var object ilMD
47  */
48  public $md = null;
49 
50  /*
51  * @var object ilMDGeneral
52  */
53  public $md_gen;
54 
58  protected $meta_log;
59 
65  public function __construct($a_xml_file = '')
66  {
67  global $DIC;
68 
69  $lng = $DIC['lng'];
70  $tree = $DIC['tree'];
71 
72  $this->meta_log = $DIC->logger()->meta();
73 
74 
75  // Enable parsing. E.g qpl' s will set this value to false
76  $this->md_parsing_enabled = true;
77 
78  parent::__construct($a_xml_file);
79  }
80 
81  public function enableMDParsing($a_status)
82  {
83  $this->md_parsing_enabled = (bool) $a_status;
84  }
85  public function getMDParsingStatus()
86  {
87  return (bool) $this->md_parsing_enabled;
88  }
89 
90  public function setMDObject(&$md)
91  {
92  $this->md = &$md;
93  }
94  public function &getMDObject()
95  {
96  return is_object($this->md) ? $this->md : false;
97  }
98 
99  public function inMetaData()
100  {
101  return $this->md_in_md;
102  }
103 
110  public function setHandlers($a_xml_parser)
111  {
112  xml_set_object($a_xml_parser, $this);
113  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
114  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
115  }
116 
117 
118 
122  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
123  {
124  include_once 'Services/MetaData/classes/class.ilMDLanguageItem.php';
125 
126  if (!$this->getMDParsingStatus()) {
127  return;
128  }
129 
130  switch ($a_name) {
131  case 'MetaData':
132  $this->md_in_md = true;
133  $this->__pushParent($this->md);
134  break;
135 
136  case 'General':
137  $this->md_gen = &$this->md->addGeneral();
138  $this->md_gen->setStructure($a_attribs['Structure']);
139  $this->md_gen->save();
140  $this->__pushParent($this->md_gen);
141  break;
142 
143  case 'Identifier':
144  $par = &$this->__getParent();
145  $this->md_ide = &$par->addIdentifier();
146  $this->md_ide->setCatalog($a_attribs['Catalog']);
147  $this->md_ide->setEntry($a_attribs['Entry']);
148  $this->md_ide->save();
149  $this->__pushParent($this->md_ide);
150  break;
151 
152  case 'Title':
153  $par = &$this->__getParent();
154  $par->setTitleLanguage(new ilMDLanguageItem($a_attribs['Language']));
155  break;
156 
157  case 'Language':
158  $par = &$this->__getParent();
159  $this->md_lan = &$par->addLanguage();
160  $this->md_lan->setLanguage(new ilMDLanguageItem($a_attribs['Language']));
161  $this->md_lan->save();
162  $this->__pushParent($this->md_lan);
163  break;
164 
165  case 'Description':
166  $par = &$this->__getParent();
167 
168  if (strtolower(get_class($par)) == 'ilmdrights' or
169  strtolower(get_class($par)) == 'ilmdannotation' or
170  strtolower(get_class($par)) == 'ilmdclassification') {
171  $par->setDescriptionLanguage(new ilMDLanguageItem($a_attribs['Language']));
172  break;
173  } else {
174  $this->md_des = &$par->addDescription();
175  $this->md_des->setDescriptionLanguage(new ilMDLanguageItem($a_attribs['Language']));
176  $this->md_des->save();
177  $this->__pushParent($this->md_des);
178  break;
179  }
180 
181  // no break
182  case 'Keyword':
183  $par = &$this->__getParent();
184  if (!in_array(get_class($par), ["ilMD"])) {
185  $this->md_key = &$par->addKeyword();
186  $this->md_key->setKeywordLanguage(new ilMDLanguageItem($a_attribs['Language']));
187  $this->md_key->save();
188  $this->__pushParent($this->md_key);
189  }
190  break;
191 
192  case 'Coverage':
193  $par = &$this->__getParent();
194  $par->setCoverageLanguage(new ilMDLanguageItem($a_attribs['Language']));
195  break;
196 
197  case 'Lifecycle':
198  $par = &$this->__getParent();
199  $this->md_lif = &$par->addLifecycle();
200  $this->md_lif->setStatus($a_attribs['Status']);
201  $this->md_lif->save();
202  $this->__pushParent($this->md_lif);
203  break;
204 
205  case 'Version':
206  $par = &$this->__getParent();
207  $par->setVersionLanguage(new ilMDLanguageItem($a_attribs['Language']));
208  break;
209 
210  case 'Contribute':
211  $par = &$this->__getParent();
212  $this->md_con = &$par->addContribute();
213  $this->md_con->setRole($a_attribs['Role']);
214  $this->md_con->save();
215  $this->__pushParent($this->md_con);
216  break;
217 
218  case 'Entity':
219  $par = &$this->__getParent();
220 
221  if (strtolower(get_class($par)) == 'ilmdcontribute') {
222  $this->md_ent = &$par->addEntity();
223  $this->md_ent->save();
224  $this->__pushParent($this->md_ent);
225  break;
226  } else {
227  // single element in 'Annotation'
228  break;
229  }
230  // no break
231  case 'Date':
232  break;
233 
234  case 'Meta-Metadata':
235  $par = &$this->__getParent();
236  $this->md_met = &$par->addMetaMetadata();
237  $this->md_met->setMetaDataScheme($a_attribs['MetadataScheme']);
238  $this->md_met->setLanguage(new ilMDLanguageItem($a_attribs['Language']));
239  $this->md_met->save();
240  $this->__pushParent($this->md_met);
241  break;
242 
243  case 'Technical':
244  $par = &$this->__getParent();
245  $this->md_tec = &$par->addTechnical();
246  $this->md_tec->save();
247  $this->__pushParent($this->md_tec);
248  break;
249 
250  case 'Format':
251  $par = &$this->__getParent();
252  $this->md_for = &$par->addFormat();
253  $this->md_for->save();
254  $this->__pushParent($this->md_for);
255  break;
256 
257  case 'Size':
258  break;
259 
260  case 'Location':
261  $par = &$this->__getParent();
262  $this->md_loc = &$par->addLocation();
263  $this->md_loc->setLocationType($a_attribs['Type']);
264  $this->md_loc->save();
265  $this->__pushParent($this->md_loc);
266  break;
267 
268  case 'Requirement':
269  $par = &$this->__getParent();
270  $this->md_req = &$par->addRequirement();
271  $this->md_req->save();
272  $this->__pushParent($this->md_req);
273  break;
274 
275  case 'OrComposite':
276  $par = &$this->__getParent();
277  $this->md_orc = &$par->addOrComposite();
278  $this->__pushParent($this->md_orc);
279  break;
280 
281  case 'Type':
282  break;
283 
284  case 'OperatingSystem':
285  $par = &$this->__getParent();
286  $par->setOperatingSystemName($a_attribs['Name']);
287  $par->setOperatingSystemMinimumVersion($a_attribs['MinimumVersion']);
288  $par->setOperatingSystemMaximumVersion($a_attribs['MaximumVersion']);
289  break;
290 
291  case 'Browser':
292  $par = &$this->__getParent();
293  $par->setBrowserName($a_attribs['Name']);
294  $par->setBrowserMinimumVersion($a_attribs['MinimumVersion']);
295  $par->setBrowserMaximumVersion($a_attribs['MaximumVersion']);
296  break;
297 
298  case 'InstallationRemarks':
299  $par = &$this->__getParent();
300  $par->setInstallationRemarksLanguage(new ilMDLanguageItem($a_attribs['Language']));
301  break;
302 
303  case 'OtherPlatformRequirements':
304  $par = &$this->__getParent();
305  $par->setOtherPlatformRequirementsLanguage(new ilMDLanguageItem($a_attribs['Language']));
306  break;
307 
308  case 'Duration':
309  break;
310 
311  case 'Educational':
312  $par = &$this->__getParent();
313  $this->md_edu = &$par->addEducational();
314  $this->md_edu->setInteractivityType($a_attribs['InteractivityType']);
315  $this->md_edu->setLearningResourceType($a_attribs['LearningResourceType']);
316  $this->md_edu->setInteractivityLevel($a_attribs['InteractivityLevel']);
317  $this->md_edu->setSemanticDensity($a_attribs['SemanticDensity']);
318  $this->md_edu->setIntendedEndUserRole($a_attribs['IntendedEndUserRole']);
319  $this->md_edu->setContext($a_attribs['Context']);
320  $this->md_edu->setDifficulty($a_attribs['Difficulty']);
321  $this->md_edu->save();
322  $this->__pushParent($this->md_edu);
323  break;
324 
325  case 'TypicalAgeRange':
326  $par = &$this->__getParent();
327  $this->md_typ = &$par->addTypicalAgeRange();
328  $this->md_typ->setTypicalAgeRangeLanguage(new ilMDLanguageItem($a_attribs['Language']));
329  $this->md_typ->save();
330  $this->__pushParent($this->md_typ);
331  break;
332 
333  case 'TypicalLearningTime':
334  break;
335 
336  case 'Rights':
337  $par = &$this->__getParent();
338  $this->md_rig = &$par->addRights();
339  $this->md_rig->setCosts($a_attribs['Cost']);
340  $this->md_rig->setCopyrightAndOtherRestrictions($a_attribs['CopyrightAndOtherRestrictions']);
341  $this->md_rig->save();
342  $this->__pushParent($this->md_rig);
343  break;
344 
345  case 'Relation':
346  $par = &$this->__getParent();
347  $this->md_rel = &$par->addRelation();
348  $this->md_rel->setKind($a_attribs['Kind']);
349  $this->md_rel->save();
350  $this->__pushParent($this->md_rel);
351  break;
352 
353  case 'Resource':
354  break;
355 
356  case 'Identifier_':
357  $par = &$this->__getParent();
358  $this->md_ide_ = &$par->addIdentifier_();
359  $this->md_ide_->setCatalog($a_attribs['Catalog']);
360  $this->md_ide_->setEntry($a_attribs['Entry']);
361  $this->md_ide_->save();
362  $this->__pushParent($this->md_ide_);
363  break;
364 
365  case 'Annotation':
366  $par = &$this->__getParent();
367  $this->md_ann = &$par->addAnnotation();
368  $this->md_ann->save();
369  $this->__pushParent($this->md_ann);
370  break;
371 
372  case 'Classification':
373  $par = &$this->__getParent();
374  $this->md_cla = &$par->addClassification();
375  $this->md_cla->setPurpose($a_attribs['Purpose']);
376  $this->md_cla->save();
377  $this->__pushParent($this->md_cla);
378  break;
379 
380  case 'TaxonPath':
381  $par = &$this->__getParent();
382  $this->md_taxp = &$par->addTaxonPath();
383  $this->md_taxp->save();
384  $this->__pushParent($this->md_taxp);
385  break;
386 
387  case 'Source':
388  $par = &$this->__getParent();
389  $par->setSourceLanguage(new ilMDLanguageItem($a_attribs['Language']));
390  break;
391 
392  case 'Taxon':
393  $par = &$this->__getParent();
394  $this->md_tax = &$par->addTaxon();
395  $this->md_tax->setTaxonLanguage(new ilMDLanguageItem($a_attribs['Language']));
396  $this->md_tax->setTaxonId($a_attribs['Id']);
397  $this->md_tax->save();
398  $this->__pushParent($this->md_tax);
399  break;
400  }
401  }
402 
406  public function handlerEndTag($a_xml_parser, $a_name)
407  {
408  if (!$this->getMDParsingStatus()) {
409  return;
410  }
411 
412  switch ($a_name) {
413  case 'MetaData':
414  $this->md_parent = array();
415  $this->md_in_md = false;
416  break;
417 
418  case 'General':
419  $par = &$this->__getParent();
420  $par->update();
421  $this->__popParent();
422  break;
423 
424  case 'Identifier':
425  $par = &$this->__getParent();
426  $par->update();
427  $this->__popParent();
428  break;
429 
430  case 'Title':
431  $par = &$this->__getParent();
432  $par->setTitle($this->__getCharacterData());
433  break;
434 
435  case 'Language':
436  $par = &$this->__getParent();
437  $par->update();
438  $this->__popParent();
439  break;
440 
441  case 'Description':
442  $par = $this->__getParent();
443  if ($par instanceof ilMDRights) {
444  $par->parseDescriptionFromImport(
445  $this->__getCharacterData()
446  );
447  } else {
448  $par->setDescription($this->__getCharacterData());
449  }
450  $par->update();
451  if ($par instanceof ilMDDescription) {
452  $this->__popParent();
453  }
454  break;
455 
456  case 'Keyword':
457  $par = &$this->__getParent();
458  if (!in_array(get_class($par), ["ilMD"])) {
459  $par->setKeyword($this->__getCharacterData());
460  $this->meta_log->debug("Keyword: " . $this->__getCharacterData());
461  $par->update();
462  $this->__popParent();
463  }
464  break;
465 
466  case 'Coverage':
467  $par = &$this->__getParent();
468  $par->setCoverage($this->__getCharacterData());
469  break;
470 
471  case 'Lifecycle':
472  $par = &$this->__getParent();
473  $par->update();
474  $this->__popParent();
475  break;
476 
477  case 'Version':
478  $par = &$this->__getParent();
479  $par->setVersion($this->__getCharacterData());
480  break;
481 
482  case 'Contribute':
483  $par = &$this->__getParent();
484  $par->update();
485  $this->__popParent();
486  break;
487 
488  case 'Entity':
489  $par = &$this->__getParent();
490 
491  if (strtolower(get_class($par)) == 'ilmdentity') {
492  $par->setEntity($this->__getCharacterData());
493  $par->update();
494  $this->__popParent();
495  } else {
496  // Single element in 'Annotation'
497  $par->setEntity($this->__getCharacterData());
498  }
499  break;
500 
501  case 'Date':
502  $par = &$this->__getParent();
503  $par->setDate($this->__getCharacterData());
504  break;
505 
506  case 'Meta-Metadata':
507  $par = &$this->__getParent();
508  $par->update();
509  $this->__popParent();
510  break;
511 
512  case 'Technical':
513  $par = &$this->__getParent();
514  $par->update();
515  $this->__popParent();
516  break;
517 
518  case 'Format':
519  $par = &$this->__getParent();
520  $par->setFormat($this->__getCharacterData());
521  $par->update();
522  $this->__popParent();
523  break;
524 
525  case 'Size':
526  $par = &$this->__getParent();
527  $par->setSize($this->__getCharacterData());
528  break;
529 
530  case 'Location':
531  $par = &$this->__getParent();
532  $par->setLocation($this->__getCharacterData());
533  $par->update();
534  $this->__popParent();
535  break;
536 
537  case 'Requirement':
538  $par = &$this->__getParent();
539  $par->update();
540  $this->__popParent();
541  break;
542 
543  case 'OrComposite':
544  $this->__popParent();
545  break;
546 
547  case 'Type':
548  break;
549 
550  case 'OperatingSystem':
551  break;
552 
553  case 'Browser':
554  break;
555 
556  case 'InstallationRemarks':
557  $par = &$this->__getParent();
558  $par->setInstallationRemarks($this->__getCharacterData());
559  break;
560 
561  case 'OtherPlatformRequirements':
562  $par = &$this->__getParent();
563  $par->setOtherPlatformRequirements($this->__getCharacterData());
564  break;
565 
566  case 'Duration':
567  $par = &$this->__getParent();
568  $par->setDuration($this->__getCharacterData());
569  break;
570 
571  case 'Educational':
572  $par = &$this->__getParent();
573  $par->update();
574  $this->__popParent();
575  break;
576 
577  case 'TypicalAgeRange':
578  $par = &$this->__getParent();
579  $par->setTypicalAgeRange($this->__getCharacterData());
580  $par->update();
581  $this->__popParent();
582  break;
583 
584  case 'TypicalLearningTime':
585  $par = &$this->__getParent();
586  $par->setTypicalLearningTime($this->__getCharacterData());
587  break;
588 
589  case 'Rights':
590  $par = &$this->__getParent();
591  $par->update();
592  $this->__popParent();
593  break;
594 
595  case 'Relation':
596  $par = &$this->__getParent();
597  $par->update();
598  $this->__popParent();
599  break;
600 
601  case 'Resource':
602  break;
603 
604  case 'Identifier_':
605  $par = &$this->__getParent();
606  $par->update();
607  $this->__popParent();
608  break;
609 
610  case 'Annotation':
611  $par = &$this->__getParent();
612  $par->update();
613  $this->__popParent();
614  break;
615 
616  case 'Classification':
617  $par = &$this->__getParent();
618  $par->update();
619  $this->__popParent();
620  break;
621 
622  case 'TaxonPath':
623  $par = &$this->__getParent();
624  $par->update();
625  $this->__popParent();
626  break;
627 
628  case 'Taxon':
629  $par = &$this->__getParent();
630  $par->setTaxon($this->__getCharacterData());
631  $par->update();
632  $this->__popParent();
633  break;
634 
635  case 'Source':
636  $par = &$this->__getParent();
637  $par->setSource($this->__getCharacterData());
638  break;
639 
640  }
641  $this->md_chr_data = '';
642  }
643 
647  public function handlerCharacterData($a_xml_parser, $a_data)
648  {
649  if (!$this->getMDParsingStatus()) {
650  return;
651  }
652 
653  if ($this->inMetaData() and $a_data != "\n") {
654  // Replace multiple tabs with one space
655  $a_data = preg_replace("/\t+/", " ", $a_data);
656 
657  $this->md_chr_data .= $a_data;
658  }
659  }
660 
661 
662 
663  // PRIVATE
664  public function __getCharacterData()
665  {
666  return trim($this->md_chr_data);
667  }
668 
669  public function __pushParent(&$md_obj)
670  {
671  $this->md_parent[] = &$md_obj;
672  $this->meta_log->debug('New parent stack (push)...');
673  foreach ($this->md_parent as $class) {
674  $this->meta_log->debug(get_class($class));
675  }
676  }
677  public function __popParent()
678  {
679  $this->meta_log->debug('New parent stack (pop)....');
680  $class = array_pop($this->md_parent);
681  foreach ((array) $this->md_parent as $class) {
682  $this->meta_log->debug(get_class($class));
683  }
684  $this->meta_log->debug(is_object($class) ? get_class($class) : 'null');
685  unset($class);
686  }
687  public function __getParent()
688  {
689  return $this->md_parent[count($this->md_parent) - 1];
690  }
691 }
__construct($a_xml_file='')
Constructor.
global $DIC
Definition: saml.php:7
enableMDParsing($a_status)
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
setHandlers($a_xml_parser)
set event handlers
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element