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