22 throw new Exception(
"ODS convert needs an ODS path and a CSV path");
28 if ($isWrited ===
false) {
29 throw new Exception(sprintf(
"Unable to convert ODS to CSV fo %s",
$odsfile));
43 throw new Exception(
"file $odsfile not found");
45 $cibledir = uniqid(
"/var/tmp/ods");
47 $cmd = sprintf(
"unzip -j %s content.xml -d %s >/dev/null",
$odsfile, $cibledir);
50 $contentxml = $cibledir .
"/content.xml";
51 if (file_exists($contentxml)) {
52 $content = file_get_contents($contentxml);
55 throw new Exception(
"unable to extract $odsfile");
71 $xml_parser = xml_parser_create();
73 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING,
true);
74 xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
75 xml_set_element_handler($xml_parser, array($this,
"startElement"), array($this,
"endElement"));
76 xml_set_character_data_handler($xml_parser, array($this,
"characterData"));
78 if (!xml_parse($xml_parser, $xmlcontent)) {
79 throw new Exception(sprintf(
"Unable to parse XML : %s line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
82 xml_parser_free($xml_parser);
83 foreach ($rows as $row) {
92 $parser, $name, $attrs)
94 global $rows,
$nrow, $inrow, $incell, $ncol, $colrepeat, $celldata;
95 if ($name ==
"TABLE:TABLE-ROW") {
97 if (isset($rows[$nrow])) {
100 foreach ($rows[$nrow] as $k => $v) {
101 if (!isset($rows[$nrow][$idx])) {
102 $rows[
$nrow][$idx] =
'';
106 ksort($rows[$nrow], SORT_NUMERIC);
110 $rows[
$nrow] = array();
113 if ($name ==
"TABLE:TABLE-CELL") {
116 if (!empty($attrs[
"TABLE:NUMBER-COLUMNS-REPEATED"])) {
117 $colrepeat = intval($attrs[
"TABLE:NUMBER-COLUMNS-REPEATED"]);
120 if ($name ==
"TEXT:P") {
121 if (isset($rows[$nrow][$ncol])) {
122 if (strlen($rows[$nrow][$ncol]) > 0) {
123 $rows[
$nrow][$ncol] .=
'\n';
133 global $rows,
$nrow, $inrow, $incell, $ncol, $colrepeat, $celldata;
134 if ($name ==
"TABLE:TABLE-ROW") {
138 if (strlen($rows[$nrow][$i]) > 0) {
143 array_splice($rows[$nrow], $i + 1);
147 if ($name ==
"TABLE:TABLE-CELL") {
150 $rows[
$nrow][$ncol] = $celldata;
152 if ($colrepeat > 1) {
153 $rval = $rows[
$nrow][$ncol];
154 for ($i = 1; $i < $colrepeat; $i++) {
156 $rows[
$nrow][$ncol] = $rval;
168 global $inrow, $incell, $celldata;
169 if ($inrow && $incell) {
176 if (preg_match(
'/\.ods$/',
$filename))
return true;
177 $sys = trim(shell_exec(sprintf(
"file -bi %s", escapeshellarg(
$filename))));
178 if ($sys ==
"application/x-zip")
return true;
179 if ($sys ==
"application/vnd.oasis.opendocument.spreadsheet")
return true;
194 $this->files[] =
$file;
201 if ($convert->seemsODS(
$file)) {
202 $csvFile = tempnam(
"/tmp",
"FOO");
203 $convert->convertOds2csv(
$file, $csvFile);
204 $needUnlink = $csvFile;
213 if (($handle = fopen($csvFile,
"r")) !== FALSE) {
214 while ((
$data = fgetcsv($handle, 1000,
";")) !== FALSE) {
217 if (isset(
$data[5])) {
218 $famName = strtolower(
$data[5]);
219 if (isset($this->content[$famName])) {
222 = $this->content[$famName][
"className"];
224 = $this->content[$famName][
"fromName"];
225 $famId = $this->content[$famName][
"id"];
226 $name = $this->content[$famName][
"name"];
232 $fromName = (
$data[1] ==
'--') ?
''
239 $famTitle =
$data[2];
244 $fromName = (
$data[1] ==
'--') ?
'' :
$data[1];
246 $famTitle =
$data[2];
249 if (!isset($this->attr[$famName])) {
250 $this->attr[$famName] = array();
259 $attrid = strtolower(
$data[1]);
260 $this->attr[$famName][$attrid] = array(
264 "famName" => $famName);
267 $this->content[$famName] = array(
268 "famName" => $famName,
272 "title" => $famTitle,
273 "fromName" => $fromName);
288 foreach ($this->content as $k =>
$info) {
289 $fromName =
$info[
"fromName"];
290 if ($fromName and is_numeric($fromName)) {
291 foreach ($this->content as $famName => $info2) {
292 if ($info2[
"id"] == $fromName) {
293 $this->content[$k][
"fromName"] = $famName;
302 $phpContent =
"<?php\n";
303 $phpContent .=
"namespace Dcp\\Family {\n";
304 foreach ($this->content as
$famId => $famInfo) {
305 $phpContent .=
"\t" . $this->
getPhpPart($famInfo) .
"\n";
307 $phpContent .=
"}\n";
313 $famName = sprintf(
'%s', ucfirst(strtolower($info[
"famName"])));
314 if ($info[
"className"]) {
315 $parentClass =
'\\' . $info[
"className"];
316 } elseif ($info[
"fromName"]) {
317 $parentClass = sprintf(
'%s', ucfirst(strtolower($info[
"fromName"])));
319 $parentClass =
'Document';
321 $comment = sprintf(
'/** %s */', $info[
"title"]);
322 $template = sprintf(
'class %s extends %s { const familyName="%s";}', $famName, $parentClass, $info[
"name"]);
323 return $comment .
"\n\t" . $template;
330 $phpContent =
"namespace Dcp\\AttributeIdentifiers {\n";
331 foreach ($this->attr as $famName => $attrInfo) {
332 $phpContent .=
"\t" . $this->
getPhpAttrPart($famName, $attrInfo) .
"\n";
334 $phpContent .=
"}\n";
340 $famInfo = $this->content[$famName];
341 if ($famInfo[
"fromName"]) {
342 $parentClass = sprintf(
'%s', ucfirst(strtolower($famInfo[
"fromName"])));
346 $comment = sprintf(
'/** %s */', $famInfo[
"title"]);
348 $template = sprintf(
"class %s extends %s {\n", ucwords($famName), $parentClass);
350 $template = sprintf(
"class %s {\n", ucwords($famName));
353 foreach ($info as $attrid => $attrInfo) {
354 $template .= sprintf(
"\t\t/** [%s] %s */\n", str_replace(
'*',
' ', $attrInfo[
"type"]),str_replace(
'*',
' ',$attrInfo[
"label"]));
355 $template .= sprintf(
"\t\tconst %s='%s';\n", $attrInfo[
"id"], $attrInfo[
"id"]);
358 return $comment .
"\n\t" . $template;
363 foreach ($argv as $k => $aFile) {
365 $s->getSignifiantContent($aFile);
369 print(
$s->generateStubAttrFile());
static startElement($parser, $name, $attrs)
convertOds2csv($odsfile, $csvfile)
static endElement($parser, $name)
foreach($argv as $arg) $cmd
getSignifiantContent($file)
getPhpAttrPart($famName, array $info)
static characterData($parser, $data)
xmlcontent2csv($xmlcontent)