C:\xampp\php\pear\PHP\UML\Output\ExporterXSL.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
 * PHP_UML
 *
 * PHP version 5
 * 
 * @category PHP
 * @package  PHP_UML
 * @author   Baptiste Autin <ohlesbeauxjours@yahoo.fr> 
 * @license  http://www.gnu.org/licenses/lgpl.html LGPL License 3
 * @version  SVN: $Revision: 176 $
 * @link     http://pear.php.net/package/PHP_UML
 * @since    $Date: 2011-09-19 00:03:11 +0200 (lun., 19 sept. 2011) $
 */
 
/**
 * An exportation class, that relies on XSL transformations.
 * 
 * It does its export by transforming the XMI data contained in the object
 * $xmiDocument.
 * Various transformations are available: they are all stored in separate subfolders
 * (php, html). To get an ExporterXSL for a given format, call the constructor
 * with the name of the format as first parameter:
 * new PHP_UML_Output_ExporterXSL("php")
 * This format name must be equal to the folder name where the desired XSL files are.
 * 
 * @category   PHP
 * @package    PHP_UML
 * @subpackage Output
 * @author     Baptiste Autin <ohlesbeauxjours@yahoo.fr>
 * @license    http://www.gnu.org/licenses/lgpl.html LGPL License 3
 * @link       http://pear.php.net/package/PHP_UML
 */
abstract class PHP_UML_Output_ExporterXSL extends PHP_UML_Output_Exporter
{
    const 
RESOURCES_FOLDER 'resources';
    const 
STARTING_TPL     'main.xsl';
   
    protected 
$saveToFileCallback;
    
    protected 
$createFolderCallback;
    
    
/**
     * XmiDocument object containing the XMI to transform
     * 
     * @var PHP_UML_Output_XmiDocument
     */
    
protected $xmiDocument;
    
   
    public function 
__construct()
    {
        
parent::__construct();
        
$this->saveToFileCallback   __CLASS__.'::saveToFile';
        
$this->createFolderCallback __CLASS__.'::createFolder';
    }
    
    public function 
setSaveToFileCallback($function)
    {
        
$this->saveToFileCallback $function;
    }
    
    public function 
setCreateFolderCallback($function)
    {
        
$this->createFolderCallback $function;
    }
    
    
/**
     * Generates output data by applying a transformation on a given XMI file
     *
     * @param string $outputDir Output directory
     * @param string $xslFile   XSL file (template file)
     * @param string $xmlFile   XMI file  
     */
    
protected function exportFromFile($outputDir$xslFile$xmlFile)
    {
        if (
is_file($xmlFile) && is_readable($xmlFile)) {
            
$xmlDom = new DomDocument;
            
$xmlDom->load($xmlFile);
            return 
$this->transform($outputDir$xslFile$xmlDom);
        } else {
            throw new 
PHP_UML_Exception(
                
'Could not read file '.$xmlFile
            
);
        }
    }
    
    
/**
     * Generates ouput data by applying a transformation on some provided XMI
     *
     * @param string $outputDir Output directory
     * @param string $xslFile   XSL file (template file)
     * @param string $xmlData   The XMI data
     */
    
protected function exportFromXml($outputDir$xslFile$xmlData)
    {
        if (empty(
$xmlData))
            throw new 
PHP_UML_Exception(
                
'No XMI data was available for transformation.'
            
);
        
$xmlDom = new DomDocument;
        
$xmlDom->loadXML($xmlData);
        return 
$this->transform($outputDir$xslFile$xmlDom);
    }
    
    
/**
     * Return the absolute location of the XSL file
     * 
     * @return string Filepath
     */
    
abstract function getXslFilepath();
    
    
/**
     * Generates output data by applying a transformation on the XMI stored in the
     * property $xmi
     *
     * @param string $outputDir Output folder
     * 
     * @return DOMDocument A DOM document containing the result of the XSLT
     */
    
public function export($outputDir)
    {
        
$xslFile $this->getXslFilepath();

        if (empty(
$this->xmiDocument)) {
            
            
$temp = new PHP_UML_Output_Xmi_Exporter();
            
/*
             * Component views and deployment views don't mean anything
             * for output formats other than XMI. Yet, since ExporterXSL is
             * a subclass of ExportXMI, we must force these options to
             * false, otherwise they will appear in the output produced by 
             * the XSL transformation:
             */
            
$temp->setComponentView(false);
            
$temp->setDeploymentView(false);
            
$temp->setModel($this->structure);
            
$temp->generateXmi();
            
$this->xmiDocument $temp->getXmiDocument();
        }
             
        if (
file_exists($xslFile))
            return 
$this->exportFromXml($outputDir$xslFile$this->xmiDocument->dump());
        else {
            throw new 
PHP_UML_Exception(
                
'Could not find the XSL template file. It must be named '.
                
self::STARTING_TPL.', under Format/YourTemplate/'
            
);
        }
    }
    
    
/**
     * Generates output data by applying a transformation on a Dom Document
     *
     * @param string      $outputDir Output folder
     * @param string      $xslFile   XSL file (template file)
     * @param DomDocument $xmlDom    XMI data (Dom)
     *
     * @return string (possible) messages raised during XSLT
     */
    
private function transform($outputDir$xslFileDomDocument $xmlDom)
    {
        
clearstatcache();

        
$userCurrentDir getcwd();    // we memorize the current working dir
        
chdir(dirname(__FILE__));      // ... swap to the /Output dir  
        
if ($xslFile=='')
            
$xslFile 'html/'.self::STARTING_TPL;
        
        if (!(
file_exists($xslFile) && is_readable($xslFile)))
            throw new 
PHP_UML_Exception(
                
'Could not read file '.$xslFile
            
);    
        
$xslDom = new DomDocument;
        
$xslDom->load($xslFile);       // ... load the XSLT starting file
        
        
chdir($userCurrentDir);        // ... and change back to the user dir
 
        // so that we can check the output dir (counted from the user dir)
        
if ($outputDir=='')
            throw new 
PHP_UML_Exception(
                
'No output directory was given.'
            
);
        if (!
file_exists($outputDir))
            throw new 
PHP_UML_Exception(
                
'Directory '.$outputDir.' does not exist.'
            
);
        
chdir($outputDir);

        
self::copyResources(dirname($xslFile));

        
$xslProc = new XSLTProcessor;
        
$xslProc->registerPHPFunctions();
        
$xslProc->importStylesheet($xslDom);

        
$xslProc->setParameter('''phpSaveToFile'$this->saveToFileCallback);
        
$xslProc->setParameter('''phpCreateFolder'$this->createFolderCallback);
        
$xslProc->setParameter('''appName'self::APP_NAME);
        
$xslProc->setParameter('''genDate'date('r'));

        
$out $xslProc->transformToDoc($xmlDom);

        
chdir($userCurrentDir); // ... and back again to the original user dir
        
        
return $out;
    }

    const 
XMLNS_UML1 'http://www.omg.org/spec/UML/1.4';

    
/**
     * XMI converter
     * 
     * @param string $xmi XMI (in version 1)
     * 
     * @return string XMI (in version 2)
     */
    
static public function convertTo2($xmi)
    {
        return 
self::simpleTransform('xmi1to2.xsl'$xmi);
    }
    
    
/**
     * Applies a simple transformation on XML data
     * Used by the UML 1->2 conversion
     *
     * @param string $xsl XSL file
     * @param string $xmi XML data
     *
     * @return string XML
     */
    
static private function simpleTransform($xsl$xmi)
    {   
        
//we must set the xmlns:UML to the same value as the XSLT stylesheet
        //(otherwise transfomations don't work)
        
$xmi preg_replace('/xmlns:(UML)\s*=\s*["\'](.*)["\']/i''xmlns:$1="'.self::XMLNS_UML1.'"'$xmi1);

        
$xslDom = new DomDocument;
        
$xslDom->load(dirname(__FILE__).DIRECTORY_SEPARATOR.$xsl);

        
$xmlDom = new DomDocument;
        
$xmlDom->loadXML($xmi);
 
        
/* $xmiTag = &$xmlDom->getElementsByTagName('XMI')->item(0);
        if ($xmiTag->getAttribute('xmlns:UML') != '') {
            $xmlDom->getElementsByTagName('XMI')->item(0)->setAttribute('verified', 'http://www.omg.org/spec/UML/1.4');
        } */

        
$xslProc = new XSLTProcessor;
        
$xslProc->importStylesheet($xslDom);

        
$xslProc->setParameter('''appName'self::APP_NAME);

        return 
$xslProc->transformToXML($xmlDom);
    }
    
    
/**
     * Copy the "resources" folder
     *
     * @param string $path Path to the folder that contains the XSL templates
     */
    
static private function copyResources($path)
    {
        
$dir $path.DIRECTORY_SEPARATOR.self::RESOURCES_FOLDER;

        if (
file_exists($dir)) {
            
$iterator = new DirectoryIterator($dir);
            foreach (
$iterator as $file) {
                if(
$file->isFile())
                    
copy($file->getPathname(), $file->getFilename());
            }
        }
    }


    
/**
     * Create a folder. This is a callback function for the XSL templates
     * (that's why it is public), and you should not have to use it.
     *
     * @param string $path Folder name
     */
    
static public function createFolder($path)
    {   
        if (
substr(getcwd(), -1)==DIRECTORY_SEPARATOR)
            
$k getcwd().utf8_decode($path);
        else
            
$k getcwd().DIRECTORY_SEPARATOR.utf8_decode($path);
        if (!
file_exists($k)) {
            
mkdir($k);
        }
        
chdir($k);
    }
   
    
/**
     * Save a content to a file. This is a callback function for the XSL templates
     * (that's why it is public), and you should not have to use it.
     *
     * @param string $name    File name
     * @param mixed  $content Content (can be either a string, or a node-set)
     */
    
static public function saveToFile($name$content)
    {
        
$file fopen(utf8_decode($name), 'w');
 
        if (
is_string($content)) {
            
fwrite($fileutf8_decode($content));
        } else {
            
$dom  = new DomDocument();
            
$node $dom->importNode($content[0], true);
            
$dom->appendChild($node);
    
            
fwrite($file$dom->saveHTML());
        }
        
fclose($file);
    }
}

?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586