1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
6require_once("./Services/Xml/classes/class.ilSaxParser.php");
7
8/**
9* Style Import Parser
10*
11* @author Alex Killing <alex.killing@gmx.de>
12* @version $Id$
13*
14* @extends ilSaxParser
15*/
16class ilStyleImportParser extends ilSaxParser
17{
18    /**
19     * @var ilTree
20     */
21    protected $tree;
22
23
24    /**
25    * Constructor
26    *
27    * @param	string		$a_xml_file		xml file
28    * @param	int			$a_mode			IL_EXTRACT_ROLES | IL_USER_IMPORT
29    *
30    * @access	public
31    */
32    public function __construct($a_xml_file, &$a_style_obj)
33    {
34        global $DIC;
35
36        $this->lng = $DIC->language();
37        $this->tree = $DIC->repositoryTree();
38        $lng = $DIC->language();
39        $tree = $DIC->repositoryTree();
40
41        $this->style_obj = $a_style_obj;
42
43        parent::__construct($a_xml_file);
44    }
45
46
47    /**
48    * set event handler
49    * should be overwritten by inherited class
50    * @access	private
51    */
52    public function setHandlers($a_xml_parser)
53    {
54        xml_set_object($a_xml_parser, $this);
55        xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
56        xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
57    }
58
59    /**
60    * start the parser
61    */
62    public function startParsing()
63    {
64        $this->styles = array();
65        parent::startParsing();
66        $this->style_obj->setStyle($this->styles);
67        $this->style_obj->setCharacteristics($this->chars);
68    }
69
70
71    /**
72    * handler for begin of element
73    */
74    public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
75    {
76        switch ($a_name) {
77            case "Style":
78                $this->current_tag = $a_attribs["Tag"];
79                $this->current_class = $a_attribs["Class"];
80                $this->current_type = $a_attribs["Type"];
81                if ($this->current_class == "PageTitle" && $this->current_type == "page_title" && $this->current_tag == "div") {
82                    $this->current_tag = "h1";
83                }
84                if ($this->current_class == "Headline1" && $this->current_tag == "div") {
85                    $this->current_tag = "h1";
86                    $this->current_type = "heading1";
87                }
88                if ($this->current_class == "Headline2" && $this->current_tag == "div") {
89                    $this->current_tag = "h2";
90                    $this->current_type = "heading2";
91                }
92                if ($this->current_class == "Headline3" && $this->current_tag == "div") {
93                    $this->current_tag = "h3";
94                    $this->current_type = "heading3";
95                }
96                $this->current_tags = array();
97                $this->chars[] = array("type" => $this->current_type,
98                    "class" => $this->current_class);
99                break;
100
101            case "StyleParameter":
102                $this->current_tags[] = array(
103                    "tag" => $this->current_tag,
104                    "class" => $this->current_class,
105                    "parameter" => $a_attribs["Name"],
106                    "type" => $this->current_type,
107                    "value" => $a_attribs["Value"],
108                    "custom" => $a_attribs["Custom"]);
109                break;
110
111            case "StyleColor":
112                $this->style_obj->addColor($a_attribs["Name"], $a_attribs["Code"]);
113                break;
114
115            case "StyleTemplate":
116                $this->cur_template = array("type" => $a_attribs["Type"],
117                    "name" => $a_attribs["Name"]);
118                $this->cur_template_classes = array();
119                break;
120
121            case "StyleTemplateClass":
122                $this->cur_template_classes[$a_attribs["ClassType"]] =
123                    $a_attribs["Class"];
124                break;
125
126        }
127        $this->cdata = "";
128    }
129
130
131    /**
132    * handler for end of element
133    */
134    public function handlerEndTag($a_xml_parser, $a_name)
135    {
136        switch ($a_name) {
137            case "Title":
138                $this->style_obj->setTitle($this->cdata);
139                break;
140
141            case "Description":
142                $this->style_obj->setDescription($this->cdata);
143                break;
144
145            case "Style":
146                $this->styles[] = $this->current_tags;
147                break;
148
149            case "StyleTemplate":
150                $this->style_obj->addTemplate(
151                    $this->cur_template["type"],
152                    $this->cur_template["name"],
153                    $this->cur_template_classes
154                );
155                break;
156
157        }
158    }
159
160    /**
161    * handler for character data
162    */
163    public function handlerCharacterData($a_xml_parser, $a_data)
164    {
165        // i don't know why this is necessary, but
166        // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
167        // in character data, but we don't want that, because it's the
168        // way we mask user html in our content, so we convert back...
169        $a_data = str_replace("<", "&lt;", $a_data);
170        $a_data = str_replace(">", "&gt;", $a_data);
171
172        // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
173        $a_data = preg_replace("/\n/", "", $a_data);
174        $a_data = preg_replace("/\t+/", "", $a_data);
175        if (!empty($a_data)) {
176            $this->cdata .= $a_data;
177        }
178    }
179}
180