<?php require 'lang.base.php'; uses('util.cmd.Console', 'util.cmd.ParamString', 'org.dia.DiaUnmarshaller', 'io.File'); // TODO: => unittest! $Param = new ParamString(); $diagram = $Param->value(1); try { $Dia = DiaUnmarshaller::unmarshal($diagram); } catch (Exception $e) { $e->printStackTrace(); exit(-1); } $Dia->saveTo('test_out.dia', FALSE);
* */ require 'lang.base.php'; uses('util.cmd.Console', 'io.File', 'io.ZipFile', 'xml.dom.Document', 'org.dia.DiaDiagram', 'org.dia.DiaText', 'org.dia.DiaMarshaller', 'org.dia.DiaUnmarshaller'); // create new empty diagram: $Dia = new DiaDiagram(); $BgLayer = $Dia->getChild('Background'); $BgLayer->addObject(new DiaText()); Console::writeLine($Dia->getSource()); exit(0); //$Dia= &DiaMarshaller::marshal(array('object.TdocVertrag'), $recurse= 1, $depend= FALSE); //print($Dia->getSource()); // read an existing diagram $file = getcwd() . '/test-read.dia'; Console::writeLine("Using file: {$file}"); DiaUnmarshaller::unmarshal($file, array('org.dia.DiaDiagram', 'lang.Object')); // try opening (zipped) file // hint: ZipFile also handles unzipped files... /* try (); { $dia_file= &new ZipFile($file) && $dia_file->open(FILE_MODE_READ); } if (catch('Exception', $e)) { $e->printStackTrace(); exit(-1); } // get the encoding from file //$xml_encoding= $dia_file->readLine(); $dia_xml= $dia_file->read($dia_file->size()); // parse the dia file
/** * Process XML with the annotated methods of the given class * * @param xml.XPath XPath Instance of xml.XPath * @param php.DomNode Context The XML node to recurse from * @param string classname Fully qualified class name */ public static function recurse($XPath, $Context, $classname) { if (DIA_UNM_DEBUG) { Console::writeLine('recurse: ' . $Context->tagname() . "-> {$classname}"); } $Class = XPClass::forName($classname); $Instance = $Class->newInstance(); if (DIA_UNM_DEBUG) { Console::writeLine('Instance: ' . $Instance->getClassName()); } // loop over class methods with annotation 'fromDia' $methods = $Class->getMethods(); foreach (array_keys($methods) as $key) { $Method = $methods[$key]; if (!$Method->hasAnnotation('fromDia', 'xpath')) { continue; } $name = $Method->getName(); $xpath = $Method->getAnnotation('fromDia', 'xpath'); if (DIA_UNM_DEBUG) { Console::writeLine("--> Method: {$name} Xpath: {$xpath}"); } // continue if this fails: some expressions don't work on WRONG nodes... try { $result = $XPath->query($xpath, $Context); } catch (Exception $e) { if (DIA_UNM_DEBUG) { $e->printStackTrace(); } $nodename = $Context->tagname(); if ($Context->has_attribute('type')) { $type = $Context->get_attribute('type'); } else { $type = '<unknown>'; } if (DIA_UNM_DEBUG) { Console::writeLine("Warn: XPath= '{$xpath}' failed on Node= '{$nodename}' Type= '{$type}'"); } // skip this method/expression continue; } if (DIA_UNM_DEBUG) { Console::writeLine(' + Size of nodeset: ' . sizeof($result->nodeset)); } // loop over nodeset foreach (array_keys($result->nodeset) as $key) { $Node = $result->nodeset[$key]; // key 'value' (simple value) if ($Method->hasAnnotation('fromDia', 'value')) { $type = $Method->getAnnotation('fromDia', 'value'); if (DIA_UNM_DEBUG) { Console::writeLine(" + Value-type: '{$type}'"); } // get node value depending on nod type if (is('domattribute', $Node)) { $nodevalue = $Node->value(); } elseif (is('domelement', $Node)) { // remove whitespace and leading/trailing '#' character $content = $Node->get_content(); if (isset($content) and $content !== '') { $nodevalue = substr(trim($Node->get_content()), 1, -1); } else { $nodevalue = ''; } } elseif (is('domnamespace', $Node)) { $nodevalue = array($Node->prefix() => $Node->namespace_uri()); } else { Console::writeLine('Unknown node class: ' . xp::stringOf($Node)); exit(-1); } switch ($type) { case 'namespace': if (!is_array($nodevalue) or empty($nodevalue)) { Console::writeLine("Nodevalue is no array or is empty: {$nodevalue}"); } $value = $nodevalue; break; case 'enum': case 'int': $nodevalue = cast($nodevalue, 'int'); if (!is_int($nodevalue)) { Console::writeLine("Nodevalue is no integer: {$nodevalue}"); } $value = $nodevalue; break; case 'real': $nodevalue = cast($nodevalue, 'float'); if (!is_real($nodevalue) and !is_int($nodevalue)) { Console::writeLine("Nodevalue is no float/integer: {$nodevalue}"); } $value = $nodevalue; break; case 'string': $nodevalue = cast($nodevalue, 'string'); if (!is_string($nodevalue)) { Console::writeLine("Nodevalue is no string: {$nodevalue}"); } $value = $nodevalue; break; case 'boolean': $value = NULL; if ($nodevalue === 'false') { $value = FALSE; } if ($nodevalue === 'true') { $value = TRUE; } if (!is_bool($value)) { Console::writeLine("Nodevalue is no boolean: {$nodevalue}"); } break; case 'point': case 'array': // comma separated list of values $value = explode(',', $nodevalue); break; case 'rectangle': case 'arrayarray': // semicolon and comma separated list of values $points = explode(';', $nodevalue); $value = array(); foreach ($points as $point) { $value[] = explode(',', $point); } break; case 'font': if (is('domelement', $Node)) { $value = array(); $value['family'] = $Node->get_attribute('family'); $value['style'] = $Node->get_attribute('style'); $value['name'] = $Node->get_attribute('name'); } else { Console::writeLine('Wrong font node: ' . xp::stringOf($Node)); exit(-1); } break; default: Console::writeLine("!!!Unknown 'value' type: '{$type}'!"); } $Method->invoke($Instance, array($value)); } // key 'class' if ($Method->hasAnnotation('fromDia', 'class')) { $classname = $Method->getAnnotation('fromDia', 'class'); if (DIA_UNM_DEBUG) { Console::writeLine(" + Class: {$classname}"); } // recurse with given classname $Obj = DiaUnmarshaller::recurse($XPath, $Node, $classname); // hand results over to the method $Method->invoke($Instance, array($Obj)); } } } return $Instance; }
$Dia = DiaMarshaller::marshal($classes, $recurse, $depend); $Dia->saveTo($file, FALSE); } else { // else: update diagram Console::writeLine('Diagram file found: updating classes...'); // initialize objects try { // Visitor checks if the given classes exist $Visitor = new UpdateVisitor($classes, TRUE, TRUE); } catch (Exception $e) { $e->printStackTrace(); exit(-1); } Console::writeLine('Parsing XML diagram file...'); try { $Dia = DiaUnmarshaller::unmarshal($file); } catch (Exception $e) { $e->printStackTrace(); exit(-1); } // do the updates Console::writeLine('Running visitor...'); $Dia->accept($Visitor); Console::writeLine('Finalize visitor...'); $Visitor->finalize(); // write changes back to file if the visitor has changed something if ($Visitor->changedClasses()) { Console::writeLine('Writing changes back to diagram file...'); $Dia->saveTo($file, FALSE); } }
<?php require 'lang.base.php'; uses('org.dia.DiaUnmarshaller', 'org.dia.UpdateVisitor'); // TODO: => unittest... Console::writeLine('============= Unmarshaller =========='); $Dia = DiaUnmarshaller::unmarshal('DiaClasses.dia'); $Dia->saveTo('DiaClasses_parsed.dia', FALSE); Console::writeLine('============= Visitor ==============='); $Visitor = new UpdateVisitor(array(), FALSE, TRUE); $Dia->accept($Visitor); $Visitor->finalize(); $Dia->saveTo('DiaClasses_updated.dia', FALSE);