#!/usr/bin/php -qC * * The current version of this xsl stylesheet can be found at http://tizac.com. * * NOTE: Edit the path on the first line above to point to your php executable. */ // }}} // {{{ Script /** * This script creates a php class skeleton from a MagicDraw XML project file, * the md_to_php.xsl file and a class name. * * @author Charlie Killian * */ // {{{ transform() /** * Apply a xsl template to an xml file and return the result. * * @param source_xml String. Path and name to the xml file. * @param source_xsl String. Path and name to the xsl file. * @param params Key based array of parameters that will be passed * into the xsl stylesheet. * * @return String. The transformed xml. * @author Charlie Killian */ function transform($source_xml, $source_xsl, $params) { $arguments = array(); $xslt = xslt_create(); if (!$result = @xslt_process($xslt, $source_xml, $source_xsl, NULL, $arguments, $params)) { print("Error: ".xslt_error($xslt) ."\nCode: ". xslt_errno($xslt)); xslt_free($xslt); exit(0); } xslt_free($xslt); return($result); } // }}} // {{{ transform() /** * Replace and wraps the comments in the transformed xml file to 75 chars. * * @param src String. Source path and name file to copy. * * @return String. The camment wrapped to 75 chars. * @author Charlie Killian */ function comment_callback($matches) { $length = 75 - strlen($matches[1]); $comment = wordwrap($matches[2], $length); $comment = preg_replace("/^(.)/", "$matches[1]\\1", $comment); // Remove ending whitespace. $comment = rtrim($comment); $comment = preg_replace("/(\n)/", "\n$matches[1]", $comment); return($comment); } // }}} // }}} // Check the command line parameters. if ($_SERVER["argc"] != 4 OR in_array($_SERVER["argv"][1], array('--help', '-help', '-h', '-?'))) { print("Usage:\n".$_SERVER["argv"][0]." XMLFile XSLFile ClassName"); } else { // Set ClassName parameter. $params = array( 'class_name' => $_SERVER["argv"][3] ); // Transform the xml. $result = transform($_SERVER["argv"][1], $_SERVER["argv"][2], $params); // Wrap the comments. $result = preg_replace_callback("/(.*?)__START__((.|\s)*?)__END__/", 'comment_callback', $result); // Print out the result. print($result); } /* * Local Variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * End: */ ?>