|
| 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 | +] |
0 commit comments