1 |
<? |
2 |
|
3 |
// ================================================================== |
4 |
// parses pdl-file and stores result to a passed-in reference to a "PDLModel"-object |
5 |
class PDLReader { |
6 |
|
7 |
// our local reference to the "PDLModel"-object |
8 |
var $ref_PDLModel; |
9 |
|
10 |
var $variabletypes_patternpart; |
11 |
|
12 |
|
13 |
// ------------------------------------------------------------------ |
14 |
// parses a given pdl-file |
15 |
function parsePdlFile($filename, &$ref_pdlmodel) { |
16 |
|
17 |
global $benchmark; |
18 |
if (isset($benchmark)) { $benchmark->setMarker('parsePdlFile - begin'); } |
19 |
|
20 |
// debug |
21 |
slog("PDLModel: parsePdlFile($filename)"); |
22 |
|
23 |
// store reference to passed-in "PDLModel"-object in classvariable |
24 |
$this->ref_PDLModel =& $ref_pdlmodel; |
25 |
|
26 |
// clear all "PDLModelObject"-objects |
27 |
$this->ref_PDLModel->pdlModelObjects = array(); |
28 |
|
29 |
// trigger error if pdl-file cannot be found |
30 |
if (!file_exists($filename)) { |
31 |
trigger_error("could not load pdl-file \"$filename\"", E_USER_ERROR); |
32 |
} |
33 |
|
34 |
// build PCRE-pattern from defined variable-types in "PDLModel" |
35 |
// for matching against these variable-types when parsing the PDL-file later |
36 |
$this->variabletypes_patternpart = join('|', $this->ref_PDLModel->meta_VariableTypes); |
37 |
|
38 |
// --- |
39 |
// pdl-parsing starts here |
40 |
|
41 |
// get file into array (line by line) |
42 |
$pdl_raw = file($filename); |
43 |
|
44 |
// initialize array for storing temporary parsing informations |
45 |
$ptmp = array('curlycount' => 0); |
46 |
|
47 |
// parse file by iterating through each line |
48 |
while ($line = current($pdl_raw)) { |
49 |
|
50 |
// strip bounding whitespaces and stuff from current line |
51 |
$line = trim($line); |
52 |
|
53 |
// strip comments from line |
54 |
$line = preg_replace("/(\/\/.*)/", "", $line); |
55 |
|
56 |
// strip bounding whitespaces and stuff from current line |
57 |
$line = trim($line); |
58 |
|
59 |
// if nothing's left (perhaps empty line), we move on to the next line |
60 |
if (!$line) { |
61 |
next($pdl_raw); |
62 |
continue; |
63 |
} |
64 |
|
65 |
// curlycount (counting "{"s and "}"s) |
66 |
if ( strstr($line, '}') ) { $ptmp['curlycount']--; } |
67 |
|
68 |
|
69 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
70 |
// 1. "main-entity" detection and parsing |
71 |
|
72 |
// - - - - - - - - - - - - - - - |
73 |
// 1.1. entity "object" |
74 |
|
75 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
76 |
// object ready |
77 |
if ( (isset($ptmp['modelobject'])) && ($ptmp['modelobject']['cc_start'] == $ptmp['curlycount']) ) { |
78 |
$this->ref_PDLModel->addObject($ptmp['modelobject']['obj']->type, &$ptmp['modelobject']['obj']); |
79 |
unset($ptmp['modelobject']); |
80 |
} |
81 |
|
82 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
83 |
// object begin |
84 |
if ( preg_match('/object type.(.+).{/', $line, $regs) ) { |
85 |
$objecttype = $regs[1]; |
86 |
$ptmp['modelobject']['obj'] =& new PDLModelObject($objecttype); |
87 |
$ptmp['modelobject']['cc_start'] = $ptmp['curlycount']; |
88 |
} |
89 |
|
90 |
|
91 |
// - - - - - - - - - - - - - - - |
92 |
// 1.2. entity "data-operation" |
93 |
|
94 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
95 |
// data-operation ready |
96 |
if ( (isset($ptmp['pc']['inDataOperation'])) && ($ptmp['pc']['inDataOperation']['cc_start'] == $ptmp['curlycount']) ) { |
97 |
$parse_result = $this->_parseEvent($ptmp['pc']['inDataOperation']['name'], $ptmp['pc']['inDataOperation']['content']); |
98 |
$do =& new PDLModelDataOperation($parse_result[0], $parse_result[1]); |
99 |
$this->ref_PDLModel->addDataOperation($do->name, &$do); |
100 |
unset($ptmp['pc']['inDataOperation']); |
101 |
} |
102 |
|
103 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
104 |
// inside data-operation |
105 |
if (isset($ptmp['pc']['inDataOperation'])) { |
106 |
$ptmp['pc']['inDataOperation']['content'] .= $line . ' '; |
107 |
} |
108 |
|
109 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
110 |
// data-operation begin |
111 |
if ( preg_match('/data operation.(.+).{/', $line, $regs) ) { |
112 |
$operationname = $regs[1]; |
113 |
$ptmp['pc']['inDataOperation']['flag'] = 1; |
114 |
$ptmp['pc']['inDataOperation']['name'] = $operationname; |
115 |
$ptmp['pc']['inDataOperation']['cc_start'] = $ptmp['curlycount']; |
116 |
$ptmp['pc']['inDataOperation']['content'] = ""; |
117 |
} |
118 |
|
119 |
|
120 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
121 |
// 2. attribute detection and parsing |
122 |
|
123 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
124 |
// attribute ready |
125 |
if ( (isset($ptmp['pc']['inAttribute'])) && ($ptmp['pc']['inAttribute']['cc_start'] == $ptmp['curlycount']) ) { |
126 |
// $ptmp['modelobject']['obj']->addAttribute($this->_parseAttribute($ptmp['pc']['inAttribute']['content'])); |
127 |
// unset($ptmp['pc']['inAttribute']); |
128 |
} |
129 |
|
130 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
131 |
// attribute begin |
132 |
if (preg_match('/^(' . $this->variabletypes_patternpart . ') (.+);$/', $line, $regs)) { |
133 |
/* |
134 |
$ptmp['pc']['inAttribute']['flag'] = 1; |
135 |
$ptmp['pc']['inAttribute']['name'] = $regs[2]; |
136 |
$ptmp['pc']['inAttribute']['cc_start'] = $ptmp['curlycount']; |
137 |
$ptmp['pc']['inAttribute']['content'] = $line; |
138 |
*/ |
139 |
$ptmp['modelobject']['obj']->addAttribute($this->_parseAttribute($line)); |
140 |
} |
141 |
|
142 |
|
143 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
144 |
// 3. "object key (<attributename>);" detection and parsing |
145 |
|
146 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
147 |
// object_key ready |
148 |
if (preg_match('/^object key \((.+)\);$/', $line, $regs)) { |
149 |
$ptmp['modelobject']['obj']->setObjectKey($regs[1]); |
150 |
} |
151 |
|
152 |
|
153 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
154 |
// 4. event detection and parsing (also maps in events) |
155 |
|
156 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
157 |
// event ready |
158 |
if ( (isset($ptmp['pc']['inEvent'])) && ($ptmp['pc']['inEvent']['cc_start'] == $ptmp['curlycount']) ) { |
159 |
$ptmp['modelobject']['obj']->addEventAndOrMap($this->_parseEvent($ptmp['pc']['inEvent']['name'], $ptmp['pc']['inEvent']['content'])); |
160 |
unset($ptmp['pc']['inEvent']); |
161 |
} |
162 |
|
163 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
164 |
// inside event |
165 |
if (isset($ptmp['pc']['inEvent'])) { |
166 |
$ptmp['pc']['inEvent']['content'] .= $line . ' '; |
167 |
} |
168 |
|
169 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . |
170 |
// event begin |
171 |
if ( preg_match('/(retrieve |add|update|remove)(.*).{/', $line, $regs) ) { |
172 |
$ptmp['pc']['inEvent']['flag'] = 1; |
173 |
$ptmp['pc']['inEvent']['name'] = $regs[1] . $regs[2]; |
174 |
$ptmp['pc']['inEvent']['cc_start'] = $ptmp['curlycount']; |
175 |
$ptmp['pc']['inEvent']['content'] = ""; |
176 |
} |
177 |
|
178 |
|
179 |
// curlycount (counting "{"s and "}"s) |
180 |
if (stristr($line, '{')) { $ptmp['curlycount']++; } |
181 |
|
182 |
// next line in pdl-file |
183 |
next($pdl_raw); |
184 |
} |
185 |
|
186 |
if (isset($benchmark)) { $benchmark->setMarker('parsePdlFile - end'); } |
187 |
|
188 |
} |
189 |
|
190 |
|
191 |
// ------------------------------------------------------------------ |
192 |
// handle different kinds of attribute-declarations here: |
193 |
// a) "normal" ones |
194 |
// b) including column-mapping-info |
195 |
// c) including column-mapping- and association-info (TODO) |
196 |
function _parseAttribute($content_raw) { |
197 |
|
198 |
// TODO: enhance! |
199 |
if (preg_match('/^(' . $this->variabletypes_patternpart . ') (.+?) = (?:(.*)|(.*)\s(.*));$/', $content_raw, $regs)) { |
200 |
if (stristr($regs[3], ' ')) { |
201 |
$tmp_arr = split(' ', $regs[3]); |
202 |
$regs[3] = $tmp_arr[0]; |
203 |
$regs[4] = $tmp_arr[1]; |
204 |
} else { |
205 |
$regs[4] = ''; |
206 |
} |
207 |
} else { |
208 |
preg_match('/^(' . $this->variabletypes_patternpart . ') (.+)()();$/', $content_raw, $regs); |
209 |
} |
210 |
|
211 |
return array($regs[2], $regs[1], $regs[3], $regs[4]); |
212 |
|
213 |
} |
214 |
|
215 |
|
216 |
// ------------------------------------------------------------------ |
217 |
// this is intended to create column-mapping-info |
218 |
// based on "do { ... } map { ... }" - declarations from the PDL-file |
219 |
function _parseEvent($name, $content_raw) { |
220 |
|
221 |
// the "unready" sql-statement which is declared inside the "do... map..."-block |
222 |
$return_unreadySql = ''; |
223 |
|
224 |
// column-mapping-info which will be built here |
225 |
$return_map = array(); |
226 |
|
227 |
// examine "content_raw", get "unreadySql"- and "mapping"-info |
228 |
if (!preg_match('/do.{.(.+).}.map.{.(.+).}./', $content_raw, $regs)) { |
229 |
preg_match('/do.{.(.+).}./', $content_raw, $regs); |
230 |
} |
231 |
|
232 |
// strip out "unready" sql-statement |
233 |
$return_unreadySql = ''; |
234 |
(isset($regs[1])) && ($return_unreadySql = trim($regs[1])); |
235 |
|
236 |
// strip out raw-content of column-mapping-info |
237 |
$map_raw = ''; |
238 |
(isset($regs[2])) && ($map_raw = trim($regs[2])); |
239 |
// create mapping-hash |
240 |
$return_map = $this->_parseEvent_Map($map_raw); |
241 |
|
242 |
return array($name, $return_unreadySql, $return_map); |
243 |
|
244 |
} |
245 |
|
246 |
|
247 |
// ------------------------------------------------------------------ |
248 |
// create the mapping-hash from "map { ... }" - declarations in the PDL-file (raw-content of column-mapping-info) |
249 |
function _parseEvent_Map($map_raw = '') { |
250 |
|
251 |
// column-mapping-info which will be built here |
252 |
$return_map = array(); |
253 |
|
254 |
// an array of mapping-entries |
255 |
$map_entries = split(';', $map_raw); |
256 |
|
257 |
// iterate through each mapping-entry and create hash |
258 |
// from attribute-name (as key) and column-name (as value) |
259 |
// with both strings stripped from bounding whitespaces |
260 |
while ($map_entry = current($map_entries)) { |
261 |
list($attributename, $mapname) = split(' = ', $map_entry); |
262 |
$attributename = trim($attributename); |
263 |
$mapname = trim($mapname); |
264 |
$return_map[$attributename] = $mapname; |
265 |
next($map_entries); |
266 |
} |
267 |
|
268 |
return $return_map; |
269 |
|
270 |
} |
271 |
|
272 |
|
273 |
} |
274 |
|
275 |
?> |