C:\xampp2_not used\php\pear\PHP\UML\Output\Xmi\AbstractBuilder.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
 * PHP_UML (PHP_UML_Output_Xmi_AbstractBuilder)
 *
 * 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: 166 $
 * @link     http://pear.php.net/package/PHP_UML
 * @since    $Date: 2011-09-07 23:20:01 +0200 (mer., 07 sept. 2011) $
 */

/**
 * Abstract class to generate UML elements in XMI code.
 * 
 * To deal with the two different versions of XMI (1.4 and 2.1), you must use one of
 * the two specialized versions:
 * PHP_UML_Output_Xmi_BuilderImpl1, or PHP_UML_Output_Xmi_BuilderImpl2
 *
 * @category   PHP
 * @package    PHP_UML
 * @subpackage Output
 * @subpackage Xmi
 * @author     Baptiste Autin <ohlesbeauxjours@yahoo.fr> 
 * @license    http://www.gnu.org/licenses/lgpl.html LGPL License 3
 */
abstract class PHP_UML_Output_Xmi_AbstractBuilder implements PHP_UML_Output_Xmi_Builder
{
    const 
EXPORTER_NAME 'PEAR\PHP_UML';
    const 
PHP_FILE      'PHP File';

    static protected 
$allStereotypes = array('File'self::PHP_FILE);
    static protected 
$allExtensions  = array(''=>'File''php'=>self::PHP_FILE);

    protected 
$xmiVersion 2;
    
    protected 
$encoding 'iso-8859-1';
    protected 
$logicalView true;
    protected 
$deploymentView false;
    protected 
$componentView false;
    protected 
$stereotypes false;
    
    
/**
     * Sets the XML encoding
     *
     * @param string $encoding Encoding
     */
    
public function setEncoding($encoding)
    {
        
$this->encoding $encoding;
    }
    
    
/**
     * Enables the inclusion of a logical view in the serialized model
     *
     * @param boolean $value True/False
     */
    
public function setLogicalView($value)
    {
        
$this->logicalView $value;
    }
    
    
/**
     * Enables the inclusion of a deployment view in the serialized model
     *
     * @param boolean $value True/False
     */
    
public function setDeploymentView($value)
    {
        
$this->deploymentView $value;
    }
    
    
/**
     * Enables the inclusion of a component view in the serialized model
     *
     * @param boolean $value True/False
     */
    
public function setComponentView($value)
    {
        
$this->componentView $value;
    }
    
    
/**
     * Enables the inclusion of the stereotypes in the serialized model
     *
     * @param boolean $value True/False
     */
    
public function setStereotypes($value)
    {
        
$this->stereotypes $value;
    }
    
    
    
/**
     * Generates an ID for an element. A partial identifier can be provided
     * (used for classes and their idrefs)
     *
     * @param string $prefix Prefix
     * 
     * @return string ID
     */
    
static protected function getUID($prefix null)
    {
        if (
is_null($prefix))
            return 
PHP_UML_SimpleUID::getUID();
        else
            return 
md5(self::EXPORTER_NAME.'#'.$prefix);
    }

    
/**
     * Gets an XML header for the XMI file
     *
     * @return string
     */
    
protected function getXmlHeader()
    {
        return 
'<?xml version="1.0" encoding="'.$this->encoding.'"?>'
    }

    
/**
     * Factory method. Retrieves a proper implementation class,
     * matching the XMI version.
     *
     * @param float $version XMI version
     * 
     * @return PHP_UML_Output_Xmi_Builder An XMI builder object 
     */
    
static function getInstance($version)
    {
        if (
$version 2)
            return new 
PHP_UML_Output_Xmi_BuilderImpl1();
        else
            return new 
PHP_UML_Output_Xmi_BuilderImpl2();
    }
    
   

    
/**
     * Get all packages, recursively, with all the objects they contain
     * Initially called by generateXmi() on the root package
     * 
     * @param PHP_UML_Metamodel_Package $package Base package
     * 
     * @return string XMI code
     */
    
protected function getAllPackages(PHP_UML_Metamodel_Package $package)
    {
        
$str $this->getPackageOpen($package);

        
$str .= $this->getNamespaceOpen();

        
$str .= $this->getOwnedElements($package);

        foreach (
$package->nestedPackage as $pkg)
            
$str .= $this->getAllPackages($pkgfalse);

        
$str .= $this->getNamespaceClose();
        
$str .= $this->getPackageClose();
       
        return 
$str;
    }

    
/**
     * Get the different types owned by a package
     *
     * @param PHP_UML_Metamodel_Package $package Package to get the types of
     * 
     * @return string XMI code
     */
    
protected function getOwnedElements(PHP_UML_Metamodel_Package $package)
    {
        
$str '';
        foreach (
$package->ownedType as &$elt) {
            switch(
get_class($elt)) {
            case 
'PHP_UML_Metamodel_Interface':
                
$str .= $this->getInterface($elt);
                break;
            case 
'PHP_UML_Metamodel_Datatype':
                
$str .= $this->getDatatype($elt);
                break;
            case 
'PHP_UML_Metamodel_Artifact':
                
$str .= $this->getArtifact($elt$elt->manifested);
                break;
            default:
                
$str .= $this->getClass($elt);
            }
        }
        
// we will finally not use this since it leads to unvalid XMI:
        /*foreach ($package->ownedOperation as &$elt) {
            $str .= $this->getOperation($elt);
        }
        foreach ($package->ownedAttribute as &$elt) {
            $str .= $this->getProperty($elt);
        }*/
        
return $str;
    }

    
/**
     * Get all components, with its provided classes
     * PHP_UML considers each logical package as a component, and each owned class
     * as a provided class.
     *
     * @param PHP_UML_Metamodel_Package $package Package to map to a component
     * 
     * @return string XMI code
     */
    
protected function getAllComponents(PHP_UML_Metamodel_Package $package)
    {
        
$str      '';
        
$cv       = new PHP_UML_Metamodel_Package;
        
$cv->id   self::getUID();
        
$cv->name $package->name;

        
$classes = array();
        foreach (
$package->ownedType as &$elt) {
            
$classes[] = $elt;
        }
        
$str .= $this->getComponentOpen($cv$classes, array());

        foreach (
$package->nestedPackage as $pkg)
            
$str .= $this->getAllComponents($pkg);

        
$str .= $this->getComponentClose();
        return 
$str;
    }
    
    
/**
     * Return the full serialized XMI metamodel of a given Superstructure.
     *
     * @param PHP_UML_Metamodel_Superstructure $structure Full metamodel
     * 
     * @return PHP_UML_Output_XmiDocument The serialized metamodel, as an XmiDocument
     *
     */
    
public function getXmiDocument(PHP_UML_Metamodel_Superstructure $structure)
    {
        if (empty(
$structure) || empty($structure->packages)) {
            throw new 
PHP_UML_Exception('No model given');
        }
        
        
$xmi  $this->getXmlHeader();
        
$xmi .= $this->getXmiHeaderOpen();

        
$_root $structure->packages;

        
$xmi .= $this->getModelOpen($_root);
        
$xmi .= $this->getNamespaceOpen();
 
        if (
$this->logicalView) {
            
$xmi .= $this->addLogicalView($_root);
        }

        if (
$this->componentView) {
            
$xmi .= $this->addComponentView($_root);
        }

        if (
$this->deploymentView) {
            
$xmi .= $this->addDeploymentView($structure->deploymentPackages);
        }

        
$xmi .= $this->getNamespaceClose();
        
$xmi .= $this->getModelClose();

        if (
$this->stereotypes) {    // = XML metadata only for the moment
            
$xmi .= $this->addStereotypeInstances($structure);
        }

        
$xmi .= $this->getXmiHeaderClose();

        if (
strcasecmp($this->encoding'utf-8')==0) {
            
$xmi utf8_encode($xmi);
        }
        
        
$xmiDocument = new PHP_UML_Output_XmiDocument($xmi);
        
        return 
$xmiDocument;
    }
       
    
/**
     * Return the logical view of the model
     *
     * @param PHP_UML_Metamodel_Package $package Package
     */
    
private function addLogicalView(PHP_UML_Metamodel_Package $package)
    {
        
$xmi $this->getStereotypes();

        
$xmi .= $this->getOwnedElements($package);
        foreach (
$package->nestedPackage as $pkg)
            
$xmi .= $this->getAllPackages($pkgfalse);
            
        return 
$xmi;
    }
    
    
/**
     * Return a component view of the logical system
     *
     * @param PHP_UML_Metamodel_Package $package Root package to browse into
     */
    
private function addComponentView(PHP_UML_Metamodel_Package $package)
    {
        return 
$this->getAllComponents($package);
    }

    
/**
     * Return a deployment view of the scanned files.
     * A file is viewed as an artifact (artifacts exist from UML 1.4)
     * Filesystem's folders are treated as packages.
     * TODO: use a package-tree, like with logical packages 
     *
     * @param PHP_UML_Metamodel_Package $package The root deployment package
     */
    
private function addDeploymentView(PHP_UML_Metamodel_Package $package)
    {
        return 
$this->getAllPackages($package);
    }

    
/**
     * Return the instances of stereotypes
     * At the current time, there are only XML metadata, not real UML stereotypes
     *
     * @param PHP_UML_Metamodel_Superstructure $structure Metamodel
     */
    
private function addStereotypeInstances(PHP_UML_Metamodel_Superstructure $structure)
    {
        
$xmi '';
        foreach (
$structure->stereotypes as $s) {
            
$xmi .= $this->getStereotypeInstance($s);
        }
        return 
$xmi;
    }
}
?>
x

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