ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
37include_once './Services/Xml/classes/class.ilSaxParser.php';
38
40{
41 public $md_in_md = false;
42 public $md_chr_data = '';
43
44 public $md_cur_el = null;
45
46 /*
47 * @var boolean enable/disable parsing status.
48 */
49 public $md_parsing_enabled = null;
50 /*
51 * @var object ilMD
52 */
53 public $md = null;
54
55 /*
56 * @var object ilMDGeneral
57 */
58 public $md_gen;
59
63 protected $meta_log;
64
70 public 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 public function enableMDParsing($a_status)
84 {
85 $this->md_parsing_enabled = (bool) $a_status;
86 }
87 public function getMDParsingStatus()
88 {
89 return (bool) $this->md_parsing_enabled;
90 }
91
92 public function setMDObject(&$md)
93 {
94 $this->md =&$md;
95 }
96 public function &getMDObject()
97 {
98 return is_object($this->md) ? $this->md : false;
99 }
100
101 public function inMetaData()
102 {
103 return $this->md_in_md;
104 }
105
112 public 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 public 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 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 $this->md_key =&$par->addKeyword();
187 $this->md_key->setKeywordLanguage(new ilMDLanguageItem($a_attribs['Language']));
188 $this->md_key->save();
189 $this->__pushParent($this->md_key);
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 (strtolower(get_class($par)) == 'ilmddescription') {
444 $par->setDescription($this->__getCharacterData());
445 $par->update();
446 $this->__popParent();
447 break;
448 } else {
449 $par->setDescription($this->__getCharacterData());
450 break;
451 }
452
453 // no break
454 case 'Keyword':
455 $par =&$this->__getParent();
456 $par->setKeyword($this->__getCharacterData());
457 $this->meta_log->debug("Keyword: " . $this->__getCharacterData());
458 $par->update();
459 $this->__popParent();
460 break;
461
462 case 'Coverage':
463 $par =&$this->__getParent();
464 $par->setCoverage($this->__getCharacterData());
465 break;
466
467 case 'Lifecycle':
468 $par =&$this->__getParent();
469 $par->update();
470 $this->__popParent();
471 break;
472
473 case 'Version':
474 $par =&$this->__getParent();
475 $par->setVersion($this->__getCharacterData());
476 break;
477
478 case 'Contribute':
479 $par =&$this->__getParent();
480 $par->update();
481 $this->__popParent();
482 break;
483
484 case 'Entity':
485 $par =&$this->__getParent();
486
487 if (strtolower(get_class($par)) == 'ilmdentity') {
488 $par->setEntity($this->__getCharacterData());
489 $par->update();
490 $this->__popParent();
491 } else {
492 // Single element in 'Annotation'
493 $par->setEntity($this->__getCharacterData());
494 }
495 break;
496
497 case 'Date':
498 $par =&$this->__getParent();
499 $par->setDate($this->__getCharacterData());
500 break;
501
502 case 'Meta-Metadata':
503 $par =&$this->__getParent();
504 $par->update();
505 $this->__popParent();
506 break;
507
508 case 'Technical':
509 $par =&$this->__getParent();
510 $par->update();
511 $this->__popParent();
512 break;
513
514 case 'Format':
515 $par =&$this->__getParent();
516 $par->setFormat($this->__getCharacterData());
517 $par->update();
518 $this->__popParent();
519 break;
520
521 case 'Size':
522 $par =&$this->__getParent();
523 $par->setSize($this->__getCharacterData());
524 break;
525
526 case 'Location':
527 $par =&$this->__getParent();
528 $par->setLocation($this->__getCharacterData());
529 $par->update();
530 $this->__popParent();
531 break;
532
533 case 'Requirement':
534 $par =&$this->__getParent();
535 $par->update();
536 $this->__popParent();
537 break;
538
539 case 'OrComposite':
540 $this->__popParent();
541 break;
542
543 case 'Type':
544 break;
545
546 case 'OperatingSystem':
547 break;
548
549 case 'Browser':
550 break;
551
552 case 'InstallationRemarks':
553 $par =&$this->__getParent();
554 $par->setInstallationRemarks($this->__getCharacterData());
555 break;
556
557 case 'OtherPlatformRequirements':
558 $par =&$this->__getParent();
559 $par->setOtherPlatformRequirements($this->__getCharacterData());
560 break;
561
562 case 'Duration':
563 $par =&$this->__getParent();
564 $par->setDuration($this->__getCharacterData());
565 break;
566
567 case 'Educational':
568 $par =&$this->__getParent();
569 $par->update();
570 $this->__popParent();
571 break;
572
573 case 'TypicalAgeRange':
574 $par =&$this->__getParent();
575 $par->setTypicalAgeRange($this->__getCharacterData());
576 $par->update();
577 $this->__popParent();
578 break;
579
580 case 'TypicalLearningTime':
581 $par =&$this->__getParent();
582 $par->setTypicalLearningTime($this->__getCharacterData());
583 break;
584
585 case 'Rights':
586 $par =&$this->__getParent();
587 $par->update();
588 $this->__popParent();
589 break;
590
591 case 'Relation':
592 $par =&$this->__getParent();
593 $par->update();
594 $this->__popParent();
595 break;
596
597 case 'Resource':
598 break;
599
600 case 'Identifier_':
601 $par =&$this->__getParent();
602 $par->update();
603 $this->__popParent();
604 break;
605
606 case 'Annotation':
607 $par =&$this->__getParent();
608 $par->update();
609 $this->__popParent();
610 break;
611
612 case 'Classification':
613 $par =&$this->__getParent();
614 $par->update();
615 $this->__popParent();
616 break;
617
618 case 'TaxonPath':
619 $par =&$this->__getParent();
620 $par->update();
621 $this->__popParent();
622 break;
623
624 case 'Taxon':
625 $par =&$this->__getParent();
626 $par->setTaxon($this->__getCharacterData());
627 $par->update();
628 $this->__popParent();
629 break;
630
631 case 'Source':
632 $par =&$this->__getParent();
633 $par->setSource($this->__getCharacterData());
634 break;
635
636 }
637 $this->md_chr_data = '';
638 }
639
643 public function handlerCharacterData($a_xml_parser, $a_data)
644 {
645 if (!$this->getMDParsingStatus()) {
646 return;
647 }
648
649 if ($this->inMetaData() and $a_data != "\n") {
650 // Replace multiple tabs with one space
651 $a_data = preg_replace("/\t+/", " ", $a_data);
652
653 $this->md_chr_data .= $a_data;
654 }
655 }
656
657
658
659 // PRIVATE
660 public function __getCharacterData()
661 {
662 return trim($this->md_chr_data);
663 }
664
665 public function __pushParent(&$md_obj)
666 {
667 $this->md_parent[] =&$md_obj;
668 #echo '<br />';
669 foreach ($this->md_parent as $class) {
670 #echo get_class($class).' -> ';
671 }
672 }
673 public function &__popParent()
674 {
675 $class = array_pop($this->md_parent);
676 $this->meta_log->debug(is_object($class) ? get_class($class) : 'null');
677 unset($class);
678 }
679 public function &__getParent()
680 {
681 return $this->md_parent[count($this->md_parent) - 1];
682 }
683}
An exception for terminatinating execution or to throw for unit testing.
static getLogger($a_component_id)
Get component logger.
enableMDParsing($a_status)
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
setHandlers($a_xml_parser)
set event handlers
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
__construct($a_xml_file='')
Constructor.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...