ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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  $a_attribs = $this->trimAndStripAttribs($a_attribs);
127 
128  if (!$this->getMDParsingStatus()) {
129  return;
130  }
131 
132  switch ($a_name) {
133  case 'MetaData':
134  $this->md_in_md = true;
135  $this->__pushParent($this->md);
136  break;
137 
138  case 'General':
139  $this->md_gen = &$this->md->addGeneral();
140  $this->md_gen->setStructure($a_attribs['Structure']);
141  $this->md_gen->save();
142  $this->__pushParent($this->md_gen);
143  break;
144 
145  case 'Identifier':
146  $par = &$this->__getParent();
147  $this->md_ide = &$par->addIdentifier();
148  $this->md_ide->setCatalog($a_attribs['Catalog']);
149  $this->md_ide->setEntry($a_attribs['Entry']);
150  $this->md_ide->save();
151  $this->__pushParent($this->md_ide);
152  break;
153 
154  case 'Title':
155  $par = &$this->__getParent();
156  $par->setTitleLanguage(new ilMDLanguageItem($a_attribs['Language']));
157  break;
158 
159  case 'Language':
160  $par = &$this->__getParent();
161  $this->md_lan = &$par->addLanguage();
162  $this->md_lan->setLanguage(new ilMDLanguageItem($a_attribs['Language']));
163  $this->md_lan->save();
164  $this->__pushParent($this->md_lan);
165  break;
166 
167  case 'Description':
168  $par = &$this->__getParent();
169 
170  if (strtolower(get_class($par)) == 'ilmdrights' or
171  strtolower(get_class($par)) == 'ilmdannotation' or
172  strtolower(get_class($par)) == 'ilmdclassification') {
173  $par->setDescriptionLanguage(new ilMDLanguageItem($a_attribs['Language']));
174  break;
175  } else {
176  $this->md_des = &$par->addDescription();
177  $this->md_des->setDescriptionLanguage(new ilMDLanguageItem($a_attribs['Language']));
178  $this->md_des->save();
179  $this->__pushParent($this->md_des);
180  break;
181  }
182 
183  // no break
184  case 'Keyword':
185  $par = &$this->__getParent();
186  if (!in_array(get_class($par), ["ilMD"])) {
187  $this->md_key = &$par->addKeyword();
188  $this->md_key->setKeywordLanguage(new ilMDLanguageItem($a_attribs['Language']));
189  $this->md_key->save();
190  $this->__pushParent($this->md_key);
191  }
192  break;
193 
194  case 'Coverage':
195  $par = &$this->__getParent();
196  $par->setCoverageLanguage(new ilMDLanguageItem($a_attribs['Language']));
197  break;
198 
199  case 'Lifecycle':
200  $par = &$this->__getParent();
201  $this->md_lif = &$par->addLifecycle();
202  $this->md_lif->setStatus($a_attribs['Status']);
203  $this->md_lif->save();
204  $this->__pushParent($this->md_lif);
205  break;
206 
207  case 'Version':
208  $par = &$this->__getParent();
209  $par->setVersionLanguage(new ilMDLanguageItem($a_attribs['Language']));
210  break;
211 
212  case 'Contribute':
213  $par = &$this->__getParent();
214  $this->md_con = &$par->addContribute();
215  $this->md_con->setRole($a_attribs['Role']);
216  $this->md_con->save();
217  $this->__pushParent($this->md_con);
218  break;
219 
220  case 'Entity':
221  $par = &$this->__getParent();
222 
223  if (strtolower(get_class($par)) == 'ilmdcontribute') {
224  $this->md_ent = &$par->addEntity();
225  $this->md_ent->save();
226  $this->__pushParent($this->md_ent);
227  break;
228  } else {
229  // single element in 'Annotation'
230  break;
231  }
232  // no break
233  case 'Date':
234  break;
235 
236  case 'Meta-Metadata':
237  $par = &$this->__getParent();
238  $this->md_met = &$par->addMetaMetadata();
239  $this->md_met->setMetaDataScheme($a_attribs['MetadataScheme']);
240  $this->md_met->setLanguage(new ilMDLanguageItem($a_attribs['Language']));
241  $this->md_met->save();
242  $this->__pushParent($this->md_met);
243  break;
244 
245  case 'Technical':
246  $par = &$this->__getParent();
247  $this->md_tec = &$par->addTechnical();
248  $this->md_tec->save();
249  $this->__pushParent($this->md_tec);
250  break;
251 
252  case 'Format':
253  $par = &$this->__getParent();
254  $this->md_for = &$par->addFormat();
255  $this->md_for->save();
256  $this->__pushParent($this->md_for);
257  break;
258 
259  case 'Size':
260  break;
261 
262  case 'Location':
263  $par = &$this->__getParent();
264  $this->md_loc = &$par->addLocation();
265  $this->md_loc->setLocationType($a_attribs['Type']);
266  $this->md_loc->save();
267  $this->__pushParent($this->md_loc);
268  break;
269 
270  case 'Requirement':
271  $par = &$this->__getParent();
272  $this->md_req = &$par->addRequirement();
273  $this->md_req->save();
274  $this->__pushParent($this->md_req);
275  break;
276 
277  case 'OrComposite':
278  $par = &$this->__getParent();
279  $this->md_orc = &$par->addOrComposite();
280  $this->__pushParent($this->md_orc);
281  break;
282 
283  case 'Type':
284  break;
285 
286  case 'OperatingSystem':
287  $par = &$this->__getParent();
288  $par->setOperatingSystemName($a_attribs['Name']);
289  $par->setOperatingSystemMinimumVersion($a_attribs['MinimumVersion']);
290  $par->setOperatingSystemMaximumVersion($a_attribs['MaximumVersion']);
291  break;
292 
293  case 'Browser':
294  $par = &$this->__getParent();
295  $par->setBrowserName($a_attribs['Name']);
296  $par->setBrowserMinimumVersion($a_attribs['MinimumVersion']);
297  $par->setBrowserMaximumVersion($a_attribs['MaximumVersion']);
298  break;
299 
300  case 'InstallationRemarks':
301  $par = &$this->__getParent();
302  $par->setInstallationRemarksLanguage(new ilMDLanguageItem($a_attribs['Language']));
303  break;
304 
305  case 'OtherPlatformRequirements':
306  $par = &$this->__getParent();
307  $par->setOtherPlatformRequirementsLanguage(new ilMDLanguageItem($a_attribs['Language']));
308  break;
309 
310  case 'Duration':
311  break;
312 
313  case 'Educational':
314  $par = &$this->__getParent();
315  $this->md_edu = &$par->addEducational();
316  $this->md_edu->setInteractivityType($a_attribs['InteractivityType']);
317  $this->md_edu->setLearningResourceType($a_attribs['LearningResourceType']);
318  $this->md_edu->setInteractivityLevel($a_attribs['InteractivityLevel']);
319  $this->md_edu->setSemanticDensity($a_attribs['SemanticDensity']);
320  $this->md_edu->setIntendedEndUserRole($a_attribs['IntendedEndUserRole']);
321  $this->md_edu->setContext($a_attribs['Context']);
322  $this->md_edu->setDifficulty($a_attribs['Difficulty']);
323  $this->md_edu->save();
324  $this->__pushParent($this->md_edu);
325  break;
326 
327  case 'TypicalAgeRange':
328  $par = &$this->__getParent();
329  $this->md_typ = &$par->addTypicalAgeRange();
330  $this->md_typ->setTypicalAgeRangeLanguage(new ilMDLanguageItem($a_attribs['Language']));
331  $this->md_typ->save();
332  $this->__pushParent($this->md_typ);
333  break;
334 
335  case 'TypicalLearningTime':
336  break;
337 
338  case 'Rights':
339  $par = &$this->__getParent();
340  $this->md_rig = &$par->addRights();
341  $this->md_rig->setCosts($a_attribs['Cost']);
342  $this->md_rig->setCopyrightAndOtherRestrictions($a_attribs['CopyrightAndOtherRestrictions']);
343  $this->md_rig->save();
344  $this->__pushParent($this->md_rig);
345  break;
346 
347  case 'Relation':
348  $par = &$this->__getParent();
349  $this->md_rel = &$par->addRelation();
350  $this->md_rel->setKind($a_attribs['Kind']);
351  $this->md_rel->save();
352  $this->__pushParent($this->md_rel);
353  break;
354 
355  case 'Resource':
356  break;
357 
358  case 'Identifier_':
359  $par = &$this->__getParent();
360  $this->md_ide_ = &$par->addIdentifier_();
361  $this->md_ide_->setCatalog($a_attribs['Catalog']);
362  $this->md_ide_->setEntry($a_attribs['Entry']);
363  $this->md_ide_->save();
364  $this->__pushParent($this->md_ide_);
365  break;
366 
367  case 'Annotation':
368  $par = &$this->__getParent();
369  $this->md_ann = &$par->addAnnotation();
370  $this->md_ann->save();
371  $this->__pushParent($this->md_ann);
372  break;
373 
374  case 'Classification':
375  $par = &$this->__getParent();
376  $this->md_cla = &$par->addClassification();
377  $this->md_cla->setPurpose($a_attribs['Purpose']);
378  $this->md_cla->save();
379  $this->__pushParent($this->md_cla);
380  break;
381 
382  case 'TaxonPath':
383  $par = &$this->__getParent();
384  $this->md_taxp = &$par->addTaxonPath();
385  $this->md_taxp->save();
386  $this->__pushParent($this->md_taxp);
387  break;
388 
389  case 'Source':
390  $par = &$this->__getParent();
391  $par->setSourceLanguage(new ilMDLanguageItem($a_attribs['Language']));
392  break;
393 
394  case 'Taxon':
395  $par = &$this->__getParent();
396  $this->md_tax = &$par->addTaxon();
397  $this->md_tax->setTaxonLanguage(new ilMDLanguageItem($a_attribs['Language']));
398  $this->md_tax->setTaxonId($a_attribs['Id']);
399  $this->md_tax->save();
400  $this->__pushParent($this->md_tax);
401  break;
402  }
403  }
404 
408  public function handlerEndTag($a_xml_parser, $a_name)
409  {
410  if (!$this->getMDParsingStatus()) {
411  return;
412  }
413 
414  switch ($a_name) {
415  case 'MetaData':
416  $this->md_parent = array();
417  $this->md_in_md = false;
418  break;
419 
420  case 'General':
421  $par = &$this->__getParent();
422  $par->update();
423  $this->__popParent();
424  break;
425 
426  case 'Identifier':
427  $par = &$this->__getParent();
428  $par->update();
429  $this->__popParent();
430  break;
431 
432  case 'Title':
433  $par = &$this->__getParent();
434  $par->setTitle($this->__getCharacterData());
435  break;
436 
437  case 'Language':
438  $par = &$this->__getParent();
439  $par->update();
440  $this->__popParent();
441  break;
442 
443  case 'Description':
444  $par = $this->__getParent();
445  if ($par instanceof ilMDRights) {
446  $par->parseDescriptionFromImport(
447  $this->__getCharacterData()
448  );
449  } else {
450  $par->setDescription($this->__getCharacterData());
451  }
452  $par->update();
453  if ($par instanceof ilMDDescription) {
454  $this->__popParent();
455  }
456  break;
457 
458  case 'Keyword':
459  $par = &$this->__getParent();
460  if (!in_array(get_class($par), ["ilMD"])) {
461  $par->setKeyword($this->__getCharacterData());
462  $this->meta_log->debug("Keyword: " . $this->__getCharacterData());
463  $par->update();
464  $this->__popParent();
465  }
466  break;
467 
468  case 'Coverage':
469  $par = &$this->__getParent();
470  $par->setCoverage($this->__getCharacterData());
471  break;
472 
473  case 'Lifecycle':
474  $par = &$this->__getParent();
475  $par->update();
476  $this->__popParent();
477  break;
478 
479  case 'Version':
480  $par = &$this->__getParent();
481  $par->setVersion($this->__getCharacterData());
482  break;
483 
484  case 'Contribute':
485  $par = &$this->__getParent();
486  $par->update();
487  $this->__popParent();
488  break;
489 
490  case 'Entity':
491  $par = &$this->__getParent();
492 
493  if (strtolower(get_class($par)) == 'ilmdentity') {
494  $par->setEntity($this->__getCharacterData());
495  $par->update();
496  $this->__popParent();
497  } else {
498  // Single element in 'Annotation'
499  $par->setEntity($this->__getCharacterData());
500  }
501  break;
502 
503  case 'Date':
504  $par = &$this->__getParent();
505  $par->setDate($this->__getCharacterData());
506  break;
507 
508  case 'Meta-Metadata':
509  $par = &$this->__getParent();
510  $par->update();
511  $this->__popParent();
512  break;
513 
514  case 'Technical':
515  $par = &$this->__getParent();
516  $par->update();
517  $this->__popParent();
518  break;
519 
520  case 'Format':
521  $par = &$this->__getParent();
522  $par->setFormat($this->__getCharacterData());
523  $par->update();
524  $this->__popParent();
525  break;
526 
527  case 'Size':
528  $par = &$this->__getParent();
529  $par->setSize($this->__getCharacterData());
530  break;
531 
532  case 'Location':
533  $par = &$this->__getParent();
534  $par->setLocation($this->__getCharacterData());
535  $par->update();
536  $this->__popParent();
537  break;
538 
539  case 'Requirement':
540  $par = &$this->__getParent();
541  $par->update();
542  $this->__popParent();
543  break;
544 
545  case 'OrComposite':
546  $this->__popParent();
547  break;
548 
549  case 'Type':
550  break;
551 
552  case 'OperatingSystem':
553  break;
554 
555  case 'Browser':
556  break;
557 
558  case 'InstallationRemarks':
559  $par = &$this->__getParent();
560  $par->setInstallationRemarks($this->__getCharacterData());
561  break;
562 
563  case 'OtherPlatformRequirements':
564  $par = &$this->__getParent();
565  $par->setOtherPlatformRequirements($this->__getCharacterData());
566  break;
567 
568  case 'Duration':
569  $par = &$this->__getParent();
570  $par->setDuration($this->__getCharacterData());
571  break;
572 
573  case 'Educational':
574  $par = &$this->__getParent();
575  $par->update();
576  $this->__popParent();
577  break;
578 
579  case 'TypicalAgeRange':
580  $par = &$this->__getParent();
581  $par->setTypicalAgeRange($this->__getCharacterData());
582  $par->update();
583  $this->__popParent();
584  break;
585 
586  case 'TypicalLearningTime':
587  $par = &$this->__getParent();
588  $par->setTypicalLearningTime($this->__getCharacterData());
589  break;
590 
591  case 'Rights':
592  $par = &$this->__getParent();
593  $par->update();
594  $this->__popParent();
595  break;
596 
597  case 'Relation':
598  $par = &$this->__getParent();
599  $par->update();
600  $this->__popParent();
601  break;
602 
603  case 'Resource':
604  break;
605 
606  case 'Identifier_':
607  $par = &$this->__getParent();
608  $par->update();
609  $this->__popParent();
610  break;
611 
612  case 'Annotation':
613  $par = &$this->__getParent();
614  $par->update();
615  $this->__popParent();
616  break;
617 
618  case 'Classification':
619  $par = &$this->__getParent();
620  $par->update();
621  $this->__popParent();
622  break;
623 
624  case 'TaxonPath':
625  $par = &$this->__getParent();
626  $par->update();
627  $this->__popParent();
628  break;
629 
630  case 'Taxon':
631  $par = &$this->__getParent();
632  $par->setTaxon($this->__getCharacterData());
633  $par->update();
634  $this->__popParent();
635  break;
636 
637  case 'Source':
638  $par = &$this->__getParent();
639  $par->setSource($this->__getCharacterData());
640  break;
641 
642  }
643  $this->md_chr_data = '';
644  }
645 
649  public function handlerCharacterData($a_xml_parser, $a_data)
650  {
651  if (!$this->getMDParsingStatus()) {
652  return;
653  }
654 
655  if ($this->inMetaData() and $a_data != "\n") {
656  // Replace multiple tabs with one space
657  $a_data = preg_replace("/\t+/", " ", $a_data);
658 
659  $this->md_chr_data .= $a_data;
660  }
661  }
662 
663 
664 
665  // PRIVATE
666  public function __getCharacterData()
667  {
668  return $this->trimAndStrip($this->md_chr_data);
669  }
670 
671  public function __pushParent(&$md_obj)
672  {
673  $this->md_parent[] = &$md_obj;
674  $this->meta_log->debug('New parent stack (push)...');
675  foreach ($this->md_parent as $class) {
676  $this->meta_log->debug(get_class($class));
677  }
678  }
679  public function __popParent()
680  {
681  $this->meta_log->debug('New parent stack (pop)....');
682  $class = array_pop($this->md_parent);
683  foreach ((array) $this->md_parent as $class) {
684  $this->meta_log->debug(get_class($class));
685  }
686  $this->meta_log->debug(is_object($class) ? get_class($class) : 'null');
687  unset($class);
688  }
689  public function __getParent()
690  {
691  return $this->md_parent[count($this->md_parent) - 1];
692  }
693 
694  protected function trimAndStripAttribs(array $attribs) : array
695  {
696  $ret = [];
697  foreach ($attribs as $k => $v) {
698  $ret[$k] = $this->trimAndStrip((string) $v);
699  }
700  return $ret;
701  }
702 
703  protected function trimAndStrip(string $input) : string
704  {
705  return ilUtil::stripSlashes(trim($input));
706  }
707 }
__construct($a_xml_file='')
Constructor.
trimAndStrip(string $input)
enableMDParsing($a_status)
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
trimAndStripAttribs(array $attribs)
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
global $DIC
Definition: goto.php:24
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
__construct(Container $dic, ilPlugin $plugin)
$ret
Definition: parser.php:6