Skip to content

Commit 83038d2

Browse files
authored
Merge pull request #731 from moosetechnology/move-importers
Move FileImporters to Famix
2 parents dbd50ae + 16844f7 commit 83038d2

6 files changed

+249
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Class {
2+
#name : #MiAbstractFileImporter,
3+
#superclass : #FamixAbstractFileImporter,
4+
#category : #'Famix-Deprecated'
5+
}
6+
7+
{ #category : #testing }
8+
MiAbstractFileImporter class >> acceptFile: aFileReference [
9+
10+
^ super acceptFile: aFileReference
11+
]
12+
13+
{ #category : #'model detection' }
14+
MiAbstractFileImporter class >> canImport: inputSample importer: importer metaModel: tmpModel [
15+
16+
^ super canImport: inputSample importer: importer metaModel: tmpModel
17+
]
18+
19+
{ #category : #'model detection' }
20+
MiAbstractFileImporter class >> findPossibleModelClassIn: aCollectionOfModels forFile: aFile [
21+
22+
^ super findPossibleModelClassIn: aCollectionOfModels forFile: aFile
23+
]
24+
25+
{ #category : #executing }
26+
MiAbstractFileImporter class >> importerFor: aFileReference [
27+
28+
^ super importerFor: aFileReference
29+
]
30+
31+
{ #category : #testing }
32+
MiAbstractFileImporter class >> isDeprecated [
33+
34+
^ true
35+
]
36+
37+
{ #category : #executing }
38+
MiAbstractFileImporter class >> knownExtensions [
39+
40+
^ super knownExtensions
41+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Class {
2+
#name : #MiJSONFileImporter,
3+
#superclass : #FamixJSONFileImporter,
4+
#category : #'Famix-Deprecated'
5+
}
6+
7+
{ #category : #testing }
8+
MiJSONFileImporter class >> isDeprecated [
9+
10+
^ true
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Class {
2+
#name : #MiMSEFileImporter,
3+
#superclass : #FamixMSEFileImporter,
4+
#category : #'Famix-Deprecated'
5+
}
6+
7+
{ #category : #testing }
8+
MiMSEFileImporter class >> isDeprecated [
9+
10+
^ true
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"
2+
Abstract class for file importers
3+
4+
Responsabilities:
5+
- able to check that a filename matches an extension that the file importer recognize
6+
- able to import a given readStream into their `model`
7+
8+
"
9+
Class {
10+
#name : #FamixAbstractFileImporter,
11+
#superclass : #Object,
12+
#instVars : [
13+
'model',
14+
'inputStream'
15+
],
16+
#category : #'Moose-Importers'
17+
}
18+
19+
{ #category : #testing }
20+
FamixAbstractFileImporter class >> acceptFile: aFileReference [
21+
"this FileImporter can deal with the given FileReference"
22+
^(aFileReference extension = self fileExtension)
23+
]
24+
25+
{ #category : #'model detection' }
26+
FamixAbstractFileImporter class >> canImport: inputSample importer: importer metaModel: tmpModel [
27+
28+
"try import with the metamodel
29+
- on FMElementNotFound, some class was not found in the metamodel, it's a failure
30+
- on FMSyntaxError, we reached end of the stream (which is an excerpt of a file) so it is a success
31+
- on other Error, not sure what it is but assume failure
32+
- on no Error, could import the whole stream that was less than default sample size, so it is a success"
33+
34+
[
35+
importer
36+
model: tmpModel;
37+
inputStream: inputSample readStream;
38+
run ]
39+
on: Error
40+
do: [ :err |
41+
err class = FMElementNotFound ifTrue: [ ^ false ].
42+
err class = FMSyntaxError ifTrue: [ ^ true ].
43+
^ false ].
44+
^ true
45+
]
46+
47+
{ #category : #executing }
48+
FamixAbstractFileImporter class >> fileExtension [
49+
"file extension for the files that the fileImporter can accept"
50+
^nil
51+
]
52+
53+
{ #category : #'model detection' }
54+
FamixAbstractFileImporter class >> findPossibleModelClassIn: aCollectionOfModels forFile: aFile [
55+
"I get provided with a collection of Moose models and a file containing a model I can parse and I'll return the first model that does not crash when trying to import the model in it."
56+
57+
| importer inputSample |
58+
importer := self new.
59+
inputSample := self inputSampleFrom: aFile.
60+
61+
^ aCollectionOfModels
62+
detect: [ :mmClass | self canImport: inputSample importer: importer metaModel: mmClass new ]
63+
ifNone: [ Error signal: 'No metamodel to import file' ]
64+
]
65+
66+
{ #category : #executing }
67+
FamixAbstractFileImporter class >> importerFor: aFileReference [
68+
^self withAllSubclasses
69+
detect: [ :clazz | clazz acceptFile: aFileReference ]
70+
ifNone: [ nil ]
71+
]
72+
73+
{ #category : #'model detection' }
74+
FamixAbstractFileImporter class >> inputSampleFrom: aFile [
75+
76+
^ aFile readStreamDo: [ :inputStream |
77+
| firstChar |
78+
firstChar := inputStream peek.
79+
inputStream size < self inputSampleLength
80+
ifTrue: [ inputStream contents ]
81+
ifFalse: [
82+
(inputStream next: self inputSampleLength) , (inputStream upTo: (firstChar = $(
83+
ifTrue: [ "mse" $) ]
84+
ifFalse: [ "json" $} ])) ] ]
85+
]
86+
87+
{ #category : #accessing }
88+
FamixAbstractFileImporter class >> inputSampleLength [
89+
90+
^ 1000
91+
]
92+
93+
{ #category : #executing }
94+
FamixAbstractFileImporter class >> knownExtensions [
95+
^self withAllSubclasses
96+
collect: [ :clazz | clazz fileExtension ]
97+
thenReject: [ :ext | ext isNil ]
98+
]
99+
100+
{ #category : #accessing }
101+
FamixAbstractFileImporter >> inputFile: aFileReference [
102+
103+
inputStream := aFileReference readStream
104+
]
105+
106+
{ #category : #accessing }
107+
FamixAbstractFileImporter >> inputStream: aStream [
108+
inputStream := aStream
109+
]
110+
111+
{ #category : #accessing }
112+
FamixAbstractFileImporter >> model [
113+
114+
^ model
115+
]
116+
117+
{ #category : #accessing }
118+
FamixAbstractFileImporter >> model: anObject [
119+
120+
model := anObject
121+
]
122+
123+
{ #category : #running }
124+
FamixAbstractFileImporter >> run [
125+
self subclassResponsibility
126+
]
127+
128+
{ #category : #running }
129+
FamixAbstractFileImporter >> runFilteredBy: anImportingContext [
130+
131+
self subclassResponsibility
132+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"
2+
Importer for JSON file
3+
4+
"
5+
Class {
6+
#name : #FamixJSONFileImporter,
7+
#superclass : #FamixAbstractFileImporter,
8+
#category : #'Moose-Importers'
9+
}
10+
11+
{ #category : #executing }
12+
FamixJSONFileImporter class >> fileExtension [
13+
^'json'
14+
]
15+
16+
{ #category : #running }
17+
FamixJSONFileImporter >> run [
18+
[ model importFromJSONStream: inputStream ]
19+
ensure: [ inputStream close ]
20+
]
21+
22+
{ #category : #running }
23+
FamixJSONFileImporter >> runFilteredBy: anImportingContext [
24+
25+
[ model
26+
importFromJSONStream: inputStream
27+
filteredBy: anImportingContext ] ensure: [ inputStream close ]
28+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"
2+
Importer for MSE files
3+
"
4+
Class {
5+
#name : #FamixMSEFileImporter,
6+
#superclass : #FamixAbstractFileImporter,
7+
#category : #'Moose-Importers'
8+
}
9+
10+
{ #category : #executing }
11+
FamixMSEFileImporter class >> fileExtension [
12+
^'mse'
13+
]
14+
15+
{ #category : #running }
16+
FamixMSEFileImporter >> run [
17+
[ model importFromMSEStream: inputStream ]
18+
ensure: [ inputStream close ]
19+
]
20+
21+
{ #category : #'as yet unclassified' }
22+
FamixMSEFileImporter >> runFilteredBy: anImportingContext [
23+
24+
[ model importFromMSEStream: inputStream filteredBy: anImportingContext ]
25+
ensure: [ inputStream close ]
26+
]

0 commit comments

Comments
 (0)