From 6e3e4aeef383536c8d6b41c25bcabf7d34f8d5c0 Mon Sep 17 00:00:00 2001 From: Jan Bliznicenko Date: Thu, 6 Feb 2020 18:51:38 +0100 Subject: [PATCH] no need to use readStream on aStream (does not work with Pharo 8) --- repository/OP-XMI/OPXMIAttribute.class.st | 56 +- repository/OP-XMI/OPXMIElement.class.st | 56 +- repository/OP-XMI/OPXMIInfoItems.class.st | 110 ++-- repository/OP-XMI/OPXMIObjectElement.class.st | 210 ++++---- .../OP-XMI/OPXMIObjectElementTest.class.st | 236 ++++----- repository/OP-XMI/OPXMIReader.class.st | 142 ++--- repository/OP-XMI/OPXMIReaderTest.class.st | 186 +++---- repository/OP-XMI/OPXMIReaderVisitor.class.st | 300 +++++------ .../OPXMIReaderVisitorExtraRulesTest.class.st | 126 ++--- .../OP-XMI/OPXMIReaderVisitorTest.class.st | 486 +++++++++--------- .../OP-XMI/OPXMIReferenceAttribute.class.st | 138 ++--- .../OP-XMI/OPXMIReferenceElement.class.st | 144 +++--- .../OP-XMI/OPXMIReferenceItemTest.class.st | 42 +- repository/OP-XMI/OPXMITest.class.st | 62 +-- .../OP-XMI/OPXMIValueAttribute.class.st | 130 ++--- repository/OP-XMI/OPXMIValueElement.class.st | 78 +-- repository/OP-XMI/OPXMIValueItemTest.class.st | 42 +- repository/OP-XMI/OPXMIWriter.class.st | 46 +- repository/OP-XMI/OPXMIWriterVisitor.class.st | 194 +++---- .../OP-XMI/OPXMIWriterVisitorTest.class.st | 242 ++++----- repository/OP-XMI/package.st | 2 +- 21 files changed, 1514 insertions(+), 1514 deletions(-) diff --git a/repository/OP-XMI/OPXMIAttribute.class.st b/repository/OP-XMI/OPXMIAttribute.class.st index 5e0d385..ef14459 100644 --- a/repository/OP-XMI/OPXMIAttribute.class.st +++ b/repository/OP-XMI/OPXMIAttribute.class.st @@ -1,28 +1,28 @@ -Class { - #name : #OPXMIAttribute, - #superclass : #OPXMIInfoItems, - #instVars : [ - 'xmiName' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #testing } -OPXMIAttribute class >> isAbstract [ - ^ self = OPXMIAttribute -] - -{ #category : #testing } -OPXMIAttribute >> isAttribute [ - ^ true -] - -{ #category : #accessing } -OPXMIAttribute >> xmiName [ - ^ xmiName -] - -{ #category : #accessing } -OPXMIAttribute >> xmiName: aString [ - xmiName := aString -] +Class { + #name : #OPXMIAttribute, + #superclass : #OPXMIInfoItems, + #instVars : [ + 'xmiName' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #testing } +OPXMIAttribute class >> isAbstract [ + ^ self = OPXMIAttribute +] + +{ #category : #testing } +OPXMIAttribute >> isAttribute [ + ^ true +] + +{ #category : #accessing } +OPXMIAttribute >> xmiName [ + ^ xmiName +] + +{ #category : #accessing } +OPXMIAttribute >> xmiName: aString [ + xmiName := aString +] diff --git a/repository/OP-XMI/OPXMIElement.class.st b/repository/OP-XMI/OPXMIElement.class.st index d957e4a..c2e5640 100644 --- a/repository/OP-XMI/OPXMIElement.class.st +++ b/repository/OP-XMI/OPXMIElement.class.st @@ -1,28 +1,28 @@ -Class { - #name : #OPXMIElement, - #superclass : #OPXMIInfoItems, - #instVars : [ - 'xmiName' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #testing } -OPXMIElement class >> isAbstract [ - ^ self = OPXMIElement -] - -{ #category : #testing } -OPXMIElement >> isElement [ - ^ true -] - -{ #category : #accessing } -OPXMIElement >> xmiName [ - ^ xmiName -] - -{ #category : #accessing } -OPXMIElement >> xmiName: aString [ - xmiName := aString -] +Class { + #name : #OPXMIElement, + #superclass : #OPXMIInfoItems, + #instVars : [ + 'xmiName' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #testing } +OPXMIElement class >> isAbstract [ + ^ self = OPXMIElement +] + +{ #category : #testing } +OPXMIElement >> isElement [ + ^ true +] + +{ #category : #accessing } +OPXMIElement >> xmiName [ + ^ xmiName +] + +{ #category : #accessing } +OPXMIElement >> xmiName: aString [ + xmiName := aString +] diff --git a/repository/OP-XMI/OPXMIInfoItems.class.st b/repository/OP-XMI/OPXMIInfoItems.class.st index c3016a0..526e394 100644 --- a/repository/OP-XMI/OPXMIInfoItems.class.st +++ b/repository/OP-XMI/OPXMIInfoItems.class.st @@ -1,55 +1,55 @@ -Class { - #name : #OPXMIInfoItems, - #superclass : #Object, - #category : 'OP-XMI-DOM' -} - -{ #category : #testing } -OPXMIInfoItems class >> isAbstract [ - ^ self = OPXMIInfoItems -] - -{ #category : #testing } -OPXMIInfoItems >> isAttribute [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isElement [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isObjectElement [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isReferenceAttribute [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isReferenceElement [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isReferenceItem [ - ^ self isReferenceAttribute | self isReferenceElement -] - -{ #category : #testing } -OPXMIInfoItems >> isValueAttribute [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isValueElement [ - ^ false -] - -{ #category : #testing } -OPXMIInfoItems >> isValueItem [ - ^ self isValueAttribute | self isValueElement -] +Class { + #name : #OPXMIInfoItems, + #superclass : #Object, + #category : 'OP-XMI-DOM' +} + +{ #category : #testing } +OPXMIInfoItems class >> isAbstract [ + ^ self = OPXMIInfoItems +] + +{ #category : #testing } +OPXMIInfoItems >> isAttribute [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isElement [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isObjectElement [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isReferenceAttribute [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isReferenceElement [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isReferenceItem [ + ^ self isReferenceAttribute | self isReferenceElement +] + +{ #category : #testing } +OPXMIInfoItems >> isValueAttribute [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isValueElement [ + ^ false +] + +{ #category : #testing } +OPXMIInfoItems >> isValueItem [ + ^ self isValueAttribute | self isValueElement +] diff --git a/repository/OP-XMI/OPXMIObjectElement.class.st b/repository/OP-XMI/OPXMIObjectElement.class.st index 1ef5d34..5a7ef2e 100644 --- a/repository/OP-XMI/OPXMIObjectElement.class.st +++ b/repository/OP-XMI/OPXMIObjectElement.class.st @@ -1,105 +1,105 @@ -" -XMIObjectElement is an XML element that can contain other information items (XML elements and attributes). -" -Class { - #name : #OPXMIObjectElement, - #superclass : #OPXMIElement, - #instVars : [ - 'containedItems' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #visiting } -OPXMIObjectElement >> accept: aVisitor [ - aVisitor visitObjectElement: self -] - -{ #category : #accessing } -OPXMIObjectElement >> containedItem [ - ^ containedItems ifEmpty: [ ^ nil ] ifNotEmpty: #first -] - -{ #category : #accessing } -OPXMIObjectElement >> containedItems [ - ^ containedItems -] - -{ #category : #accessing } -OPXMIObjectElement >> containedItems: aCollection [ - containedItems := aCollection -] - -{ #category : #accessing } -OPXMIObjectElement >> elementNamed: aName [ - "return the first element with the given name" - - ^ self containedItems - detect: [ :each | each xmiName = aName ] - ifFound: #yourself - ifNone: [ nil ] -] - -{ #category : #searching } -OPXMIObjectElement >> findElementWithXmiId: aXmiId [ - self xmiId = aXmiId - ifTrue: [ ^ self ]. - self containedItems - select: #isObjectElement - thenDo: [ :each | (each findElementWithXmiId: aXmiId) ifNotNil: [ :match | ^ match ] ]. - ^ nil -] - -{ #category : #initialization } -OPXMIObjectElement >> initialize [ - super initialize. - containedItems := OrderedCollection new -] - -{ #category : #testing } -OPXMIObjectElement >> isObjectElement [ - ^ true -] - -{ #category : #'accessing - shortcuts' } -OPXMIObjectElement >> nameValue [ - "Retrieve the value of child element. Throw up if none is present." - - ^ (self containedItems detect: [ :each | each xmiName = #name ]) value -] - -{ #category : #printing } -OPXMIObjectElement >> printOn: aStream [ - aStream - << 'XMIObjectElement('; - << xmiName. - self xmiId - ifNotNil: [ :id | - aStream - << ' xmi:id="'; - << id; - << '"' ]. - self xmiType - ifNotNil: [ :type | - aStream - << ' xmi:type="'; - << type; - << '"' ]. - aStream << ')' -] - -{ #category : #'accessing - shortcuts' } -OPXMIObjectElement >> xmiId [ - ^ self containedItems - detect: [ :each | each xmiName = #xmi:id ] - ifFound: [ :each | each value ] - ifNone: [ nil ] -] - -{ #category : #'accessing - shortcuts' } -OPXMIObjectElement >> xmiType [ - ^ self containedItems - detect: [ :each | each xmiName = #xmi:type ] - ifFound: [ :each | each value ] - ifNone: [ nil ] -] +" +XMIObjectElement is an XML element that can contain other information items (XML elements and attributes). +" +Class { + #name : #OPXMIObjectElement, + #superclass : #OPXMIElement, + #instVars : [ + 'containedItems' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #visiting } +OPXMIObjectElement >> accept: aVisitor [ + aVisitor visitObjectElement: self +] + +{ #category : #accessing } +OPXMIObjectElement >> containedItem [ + ^ containedItems ifEmpty: [ ^ nil ] ifNotEmpty: #first +] + +{ #category : #accessing } +OPXMIObjectElement >> containedItems [ + ^ containedItems +] + +{ #category : #accessing } +OPXMIObjectElement >> containedItems: aCollection [ + containedItems := aCollection +] + +{ #category : #accessing } +OPXMIObjectElement >> elementNamed: aName [ + "return the first element with the given name" + + ^ self containedItems + detect: [ :each | each xmiName = aName ] + ifFound: #yourself + ifNone: [ nil ] +] + +{ #category : #searching } +OPXMIObjectElement >> findElementWithXmiId: aXmiId [ + self xmiId = aXmiId + ifTrue: [ ^ self ]. + self containedItems + select: #isObjectElement + thenDo: [ :each | (each findElementWithXmiId: aXmiId) ifNotNil: [ :match | ^ match ] ]. + ^ nil +] + +{ #category : #initialization } +OPXMIObjectElement >> initialize [ + super initialize. + containedItems := OrderedCollection new +] + +{ #category : #testing } +OPXMIObjectElement >> isObjectElement [ + ^ true +] + +{ #category : #'accessing - shortcuts' } +OPXMIObjectElement >> nameValue [ + "Retrieve the value of child element. Throw up if none is present." + + ^ (self containedItems detect: [ :each | each xmiName = #name ]) value +] + +{ #category : #printing } +OPXMIObjectElement >> printOn: aStream [ + aStream + << 'XMIObjectElement('; + << xmiName. + self xmiId + ifNotNil: [ :id | + aStream + << ' xmi:id="'; + << id; + << '"' ]. + self xmiType + ifNotNil: [ :type | + aStream + << ' xmi:type="'; + << type; + << '"' ]. + aStream << ')' +] + +{ #category : #'accessing - shortcuts' } +OPXMIObjectElement >> xmiId [ + ^ self containedItems + detect: [ :each | each xmiName = #xmi:id ] + ifFound: [ :each | each value ] + ifNone: [ nil ] +] + +{ #category : #'accessing - shortcuts' } +OPXMIObjectElement >> xmiType [ + ^ self containedItems + detect: [ :each | each xmiName = #xmi:type ] + ifFound: [ :each | each value ] + ifNone: [ nil ] +] diff --git a/repository/OP-XMI/OPXMIObjectElementTest.class.st b/repository/OP-XMI/OPXMIObjectElementTest.class.st index a228c00..d518ad7 100644 --- a/repository/OP-XMI/OPXMIObjectElementTest.class.st +++ b/repository/OP-XMI/OPXMIObjectElementTest.class.st @@ -1,118 +1,118 @@ -Class { - #name : #OPXMIObjectElementTest, - #superclass : #TestCase, - #category : 'OP-XMI-Tests' -} - -{ #category : #'instance creation' } -OPXMIObjectElementTest >> nestedObjects [ - ^ OPXMIObjectElement new - xmiName: 'aaa'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'AAA'. - OPXMIObjectElement new - xmiName: 'bbb'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'BBB'}. - OPXMIObjectElement new - xmiName: 'ccc'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'CCC'. - OPXMIObjectElement new - xmiName: 'ddd'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'DDD'}}} -] - -{ #category : #tests } -OPXMIObjectElementTest >> testElementNamed [ - | el | - (el := OPXMIObjectElement new) - xmiName: 'container'; - containedItems: {OPXMIObjectElement new xmiName: 'element'}. - self assert: (el elementNamed: 'element') xmiName equals: 'element'. - self assert: (el elementNamed: 'none') equals: nil -] - -{ #category : #tests } -OPXMIObjectElementTest >> testFindElementWithXmiId1 [ - | root result | - root := self nestedObjects. - result := root findElementWithXmiId: 'AAA'. - self assert: result equals: root -] - -{ #category : #tests } -OPXMIObjectElementTest >> testFindElementWithXmiId2 [ - | root result | - root := self nestedObjects. - result := root findElementWithXmiId: 'BBB'. - self assert: result xmiName equals: 'bbb' -] - -{ #category : #tests } -OPXMIObjectElementTest >> testFindElementWithXmiId3 [ - | root result | - root := self nestedObjects. - result := root findElementWithXmiId: 'CCC'. - self assert: result xmiName equals: 'ccc' -] - -{ #category : #tests } -OPXMIObjectElementTest >> testFindElementWithXmiId4 [ - | root result | - root := self nestedObjects. - result := root findElementWithXmiId: 'DDD'. - self assert: result xmiName equals: 'ddd' -] - -{ #category : #tests } -OPXMIObjectElementTest >> testFindElementWithXmiId5 [ - | root result | - root := self nestedObjects. - result := root findElementWithXmiId: 'EEE'. - self assert: result equals: nil -] - -{ #category : #tests } -OPXMIObjectElementTest >> testId [ - | element | - element := OPXMIObjectElement new. - self assert: element xmiId equals: nil. - element - containedItems: - {OPXMIValueAttribute new - xmiName: #xmi:id; - value: 'ID'}. - self assert: element xmiId equals: 'ID' -] - -{ #category : #tests } -OPXMIObjectElementTest >> testNameValue [ - | element | - element := OPXMIObjectElement new - containedItems: {OPXMIValueElement xmiName: 'name' value: 'hello'}. - self assert: element nameValue equals: 'hello'. - self should: [ OPXMIObjectElement new nameValue ] raise: NotFound -] - -{ #category : #tests } -OPXMIObjectElementTest >> testType [ - | element | - element := OPXMIObjectElement new. - self assert: element xmiType equals: nil. - element - containedItems: - {OPXMIValueAttribute new - xmiName: #xmi:type; - value: 'TYPE'}. - self assert: element xmiType equals: 'TYPE' -] +Class { + #name : #OPXMIObjectElementTest, + #superclass : #TestCase, + #category : 'OP-XMI-Tests' +} + +{ #category : #'instance creation' } +OPXMIObjectElementTest >> nestedObjects [ + ^ OPXMIObjectElement new + xmiName: 'aaa'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'AAA'. + OPXMIObjectElement new + xmiName: 'bbb'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'BBB'}. + OPXMIObjectElement new + xmiName: 'ccc'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'CCC'. + OPXMIObjectElement new + xmiName: 'ddd'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'DDD'}}} +] + +{ #category : #tests } +OPXMIObjectElementTest >> testElementNamed [ + | el | + (el := OPXMIObjectElement new) + xmiName: 'container'; + containedItems: {OPXMIObjectElement new xmiName: 'element'}. + self assert: (el elementNamed: 'element') xmiName equals: 'element'. + self assert: (el elementNamed: 'none') equals: nil +] + +{ #category : #tests } +OPXMIObjectElementTest >> testFindElementWithXmiId1 [ + | root result | + root := self nestedObjects. + result := root findElementWithXmiId: 'AAA'. + self assert: result equals: root +] + +{ #category : #tests } +OPXMIObjectElementTest >> testFindElementWithXmiId2 [ + | root result | + root := self nestedObjects. + result := root findElementWithXmiId: 'BBB'. + self assert: result xmiName equals: 'bbb' +] + +{ #category : #tests } +OPXMIObjectElementTest >> testFindElementWithXmiId3 [ + | root result | + root := self nestedObjects. + result := root findElementWithXmiId: 'CCC'. + self assert: result xmiName equals: 'ccc' +] + +{ #category : #tests } +OPXMIObjectElementTest >> testFindElementWithXmiId4 [ + | root result | + root := self nestedObjects. + result := root findElementWithXmiId: 'DDD'. + self assert: result xmiName equals: 'ddd' +] + +{ #category : #tests } +OPXMIObjectElementTest >> testFindElementWithXmiId5 [ + | root result | + root := self nestedObjects. + result := root findElementWithXmiId: 'EEE'. + self assert: result equals: nil +] + +{ #category : #tests } +OPXMIObjectElementTest >> testId [ + | element | + element := OPXMIObjectElement new. + self assert: element xmiId equals: nil. + element + containedItems: + {OPXMIValueAttribute new + xmiName: #xmi:id; + value: 'ID'}. + self assert: element xmiId equals: 'ID' +] + +{ #category : #tests } +OPXMIObjectElementTest >> testNameValue [ + | element | + element := OPXMIObjectElement new + containedItems: {OPXMIValueElement xmiName: 'name' value: 'hello'}. + self assert: element nameValue equals: 'hello'. + self should: [ OPXMIObjectElement new nameValue ] raise: NotFound +] + +{ #category : #tests } +OPXMIObjectElementTest >> testType [ + | element | + element := OPXMIObjectElement new. + self assert: element xmiType equals: nil. + element + containedItems: + {OPXMIValueAttribute new + xmiName: #xmi:type; + value: 'TYPE'}. + self assert: element xmiType equals: 'TYPE' +] diff --git a/repository/OP-XMI/OPXMIReader.class.st b/repository/OP-XMI/OPXMIReader.class.st index 39a482a..a522ac9 100644 --- a/repository/OP-XMI/OPXMIReader.class.st +++ b/repository/OP-XMI/OPXMIReader.class.st @@ -1,71 +1,71 @@ -Class { - #name : #OPXMIReader, - #superclass : #Object, - #instVars : [ - 'document' - ], - #category : 'OP-XMI-Reader' -} - -{ #category : #'instance creation' } -OPXMIReader class >> read: aStream mapping: aMapping [ - aMapping at: 'result' put: aStream contents. - ^ (self readFromMapping: aMapping) at: 'result' -] - -{ #category : #'instance creation' } -OPXMIReader class >> readFrom: aStream [ - ^ self new readFrom: aStream -] - -{ #category : #'instance creation' } -OPXMIReader class >> readFromMapping: aMapping [ - "aMapping is a Dictionary of HREF URI -> XMI contents" - - ^ self new readFromMapping: aMapping -] - -{ #category : #'instance creation' } -OPXMIReader >> readFrom: aStream [ - | dom root | - dom := XMLDOMParser parse: aStream readStream usingNamespaces: false. - root := OPXMIReaderVisitor new visitDocument: dom. - ^ root -] - -{ #category : #'instance creation' } -OPXMIReader >> readFromMapping: aMapping [ - | graphs | - graphs := OrderedDictionary new. - aMapping keysAndValuesDo: [ :key :value | graphs at: key put: (self readFrom: value) ]. - graphs valuesDo: [ :root | self resolveHrefsIn: root with: graphs ]. - ^ graphs -] - -{ #category : #'instance creation' } -OPXMIReader >> resolveHrefIn: aReferenceItem with: aMapping [ - | parts ref | - aReferenceItem href ifNil: [ ^ self ]. - parts := aReferenceItem href splitOn: '#'. - self flag: 'hack to circumvent package imports which are not properly resolved yet'. - aMapping - at: parts first - ifPresent: [ :root | ref := root findElementWithXmiId: parts second ]. - (ref isNil - and: - [ aReferenceItem isReferenceElement and: [ aReferenceItem xmiType = 'uml:PrimitiveType' ] ]) - ifTrue: [ aMapping values - detect: [ :root | (root findElementWithXmiId: parts second) isNotNil ] - ifFound: [ :root | ref := root findElementWithXmiId: parts second ] ]. - " ref ifNil: [ self error: 'Unable to resolve a reference.' ]." - ref ifNotNil: [ aReferenceItem referencedElements: {ref} ] -] - -{ #category : #'instance creation' } -OPXMIReader >> resolveHrefsIn: anElement with: aMapping [ - anElement isReferenceElement - ifTrue: [ ^ self resolveHrefIn: anElement with: aMapping ]. - anElement isObjectElement - ifFalse: [ ^ self ]. - anElement containedItems do: [ :each | self resolveHrefsIn: each with: aMapping ] -] +Class { + #name : #OPXMIReader, + #superclass : #Object, + #instVars : [ + 'document' + ], + #category : #'OP-XMI-Reader' +} + +{ #category : #'instance creation' } +OPXMIReader class >> read: aStream mapping: aMapping [ + aMapping at: 'result' put: aStream contents. + ^ (self readFromMapping: aMapping) at: 'result' +] + +{ #category : #'instance creation' } +OPXMIReader class >> readFrom: aStream [ + ^ self new readFrom: aStream +] + +{ #category : #'instance creation' } +OPXMIReader class >> readFromMapping: aMapping [ + "aMapping is a Dictionary of HREF URI -> XMI contents" + + ^ self new readFromMapping: aMapping +] + +{ #category : #'instance creation' } +OPXMIReader >> readFrom: aStream [ + | dom root | + dom := XMLDOMParser parse: aStream usingNamespaces: false. + root := OPXMIReaderVisitor new visitDocument: dom. + ^ root +] + +{ #category : #'instance creation' } +OPXMIReader >> readFromMapping: aMapping [ + | graphs | + graphs := OrderedDictionary new. + aMapping keysAndValuesDo: [ :key :value | graphs at: key put: (self readFrom: value) ]. + graphs valuesDo: [ :root | self resolveHrefsIn: root with: graphs ]. + ^ graphs +] + +{ #category : #'instance creation' } +OPXMIReader >> resolveHrefIn: aReferenceItem with: aMapping [ + | parts ref | + aReferenceItem href ifNil: [ ^ self ]. + parts := aReferenceItem href splitOn: '#'. + self flag: 'hack to circumvent package imports which are not properly resolved yet'. + aMapping + at: parts first + ifPresent: [ :root | ref := root findElementWithXmiId: parts second ]. + (ref isNil + and: + [ aReferenceItem isReferenceElement and: [ aReferenceItem xmiType = 'uml:PrimitiveType' ] ]) + ifTrue: [ aMapping values + detect: [ :root | (root findElementWithXmiId: parts second) isNotNil ] + ifFound: [ :root | ref := root findElementWithXmiId: parts second ] ]. + " ref ifNil: [ self error: 'Unable to resolve a reference.' ]." + ref ifNotNil: [ aReferenceItem referencedElements: {ref} ] +] + +{ #category : #'instance creation' } +OPXMIReader >> resolveHrefsIn: anElement with: aMapping [ + anElement isReferenceElement + ifTrue: [ ^ self resolveHrefIn: anElement with: aMapping ]. + anElement isObjectElement + ifFalse: [ ^ self ]. + anElement containedItems do: [ :each | self resolveHrefsIn: each with: aMapping ] +] diff --git a/repository/OP-XMI/OPXMIReaderTest.class.st b/repository/OP-XMI/OPXMIReaderTest.class.st index d36d06c..3979873 100644 --- a/repository/OP-XMI/OPXMIReaderTest.class.st +++ b/repository/OP-XMI/OPXMIReaderTest.class.st @@ -1,93 +1,93 @@ -Class { - #name : #OPXMIReaderTest, - #superclass : #TestCase, - #category : 'OP-XMI-Tests' -} - -{ #category : #'instance creation' } -OPXMIReaderTest >> fileAAContents [ - ^ ' - - - -' -] - -{ #category : #'instance creation' } -OPXMIReaderTest >> fileBBContents [ - ^ ' - - - -' -] - -{ #category : #'instance creation' } -OPXMIReaderTest >> fileCCContents [ - ^ ' - -' -] - -{ #category : #tests } -OPXMIReaderTest >> testReadFromMapping [ - | mapping result aaItem bbItem ccItem | - "ordered to have both forward (B->C) and backward (B<-A) lookup" - mapping := OrderedDictionary - with: 'BB.xmi' -> self fileBBContents - with: 'AA.xmi' -> self fileAAContents - with: 'CC.xmi' -> self fileCCContents. - result := OPXMIReader readFromMapping: mapping. - aaItem := (result at: 'AA.xmi') containedItems first. - bbItem := (result at: 'BB.xmi') containedItems first. - ccItem := (result at: 'CC.xmi') containedItems first. - self assert: aaItem containedItems second referencedElement equals: bbItem. - self assert: bbItem containedItems second referencedElement equals: ccItem -] - -{ #category : #tests } -OPXMIReaderTest >> testReadFromTypedMapping [ - | mapping result uml type | - mapping := OrderedDictionary - with: 'UML.xmi' -> self umlTypedElementContents - with: 'PrimitiveTypes.xmi' -> self umlTypesContents. - result := OPXMIReader readFromMapping: mapping. - uml := (result at: 'UML.xmi') containedItems first. - type := (result at: 'PrimitiveTypes.xmi') containedItems first. - self assert: uml containedItems first referencedElement equals: type -] - -{ #category : #tests } -OPXMIReaderTest >> testResolveHref [ - | reference map someFileXmi | - reference := OPXMIReferenceElement new - xmiName: 'reference'; - href: 'someFile.xmi#AAA'. - someFileXmi := OPXMIObjectElement new - xmiName: 'container'; - containedItems: - {OPXMIObjectElement new - xmiName: 'item'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'AAA'}}. - map := Dictionary with: 'someFile.xmi' -> someFileXmi. - OPXMIReader new resolveHrefIn: reference with: map. - self assert: reference referencedElements size equals: 1. - self assert: reference referencedElement equals: someFileXmi containedItem -] - -{ #category : #'instance creation' } -OPXMIReaderTest >> umlTypedElementContents [ - ^ ' - -' -] - -{ #category : #'instance creation' } -OPXMIReaderTest >> umlTypesContents [ - ^ ' - -' -] +Class { + #name : #OPXMIReaderTest, + #superclass : #TestCase, + #category : 'OP-XMI-Tests' +} + +{ #category : #'instance creation' } +OPXMIReaderTest >> fileAAContents [ + ^ ' + + + +' +] + +{ #category : #'instance creation' } +OPXMIReaderTest >> fileBBContents [ + ^ ' + + + +' +] + +{ #category : #'instance creation' } +OPXMIReaderTest >> fileCCContents [ + ^ ' + +' +] + +{ #category : #tests } +OPXMIReaderTest >> testReadFromMapping [ + | mapping result aaItem bbItem ccItem | + "ordered to have both forward (B->C) and backward (B<-A) lookup" + mapping := OrderedDictionary + with: 'BB.xmi' -> self fileBBContents + with: 'AA.xmi' -> self fileAAContents + with: 'CC.xmi' -> self fileCCContents. + result := OPXMIReader readFromMapping: mapping. + aaItem := (result at: 'AA.xmi') containedItems first. + bbItem := (result at: 'BB.xmi') containedItems first. + ccItem := (result at: 'CC.xmi') containedItems first. + self assert: aaItem containedItems second referencedElement equals: bbItem. + self assert: bbItem containedItems second referencedElement equals: ccItem +] + +{ #category : #tests } +OPXMIReaderTest >> testReadFromTypedMapping [ + | mapping result uml type | + mapping := OrderedDictionary + with: 'UML.xmi' -> self umlTypedElementContents + with: 'PrimitiveTypes.xmi' -> self umlTypesContents. + result := OPXMIReader readFromMapping: mapping. + uml := (result at: 'UML.xmi') containedItems first. + type := (result at: 'PrimitiveTypes.xmi') containedItems first. + self assert: uml containedItems first referencedElement equals: type +] + +{ #category : #tests } +OPXMIReaderTest >> testResolveHref [ + | reference map someFileXmi | + reference := OPXMIReferenceElement new + xmiName: 'reference'; + href: 'someFile.xmi#AAA'. + someFileXmi := OPXMIObjectElement new + xmiName: 'container'; + containedItems: + {OPXMIObjectElement new + xmiName: 'item'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'AAA'}}. + map := Dictionary with: 'someFile.xmi' -> someFileXmi. + OPXMIReader new resolveHrefIn: reference with: map. + self assert: reference referencedElements size equals: 1. + self assert: reference referencedElement equals: someFileXmi containedItem +] + +{ #category : #'instance creation' } +OPXMIReaderTest >> umlTypedElementContents [ + ^ ' + +' +] + +{ #category : #'instance creation' } +OPXMIReaderTest >> umlTypesContents [ + ^ ' + +' +] diff --git a/repository/OP-XMI/OPXMIReaderVisitor.class.st b/repository/OP-XMI/OPXMIReaderVisitor.class.st index 475dbdc..7f460fa 100644 --- a/repository/OP-XMI/OPXMIReaderVisitor.class.st +++ b/repository/OP-XMI/OPXMIReaderVisitor.class.st @@ -1,150 +1,150 @@ -Class { - #name : #OPXMIReaderVisitor, - #superclass : #XMLDOMVisitor, - #instVars : [ - 'document', - 'root', - 'mapping', - 'unresolved', - 'refIdMapping' - ], - #category : 'OP-XMI-Reader' -} - -{ #category : #factory } -OPXMIReaderVisitor >> attributeFor: aXMLAttribute [ - (self isReferenceAttribute: aXMLAttribute) - ifTrue: [ ^ self referenceAttributeFor: aXMLAttribute ]. - ^ self valueAttributeFor: aXMLAttribute -] - -{ #category : #accessing } -OPXMIReaderVisitor >> elementAt: aXMLElement [ - ^ mapping at: aXMLElement -] - -{ #category : #factory } -OPXMIReaderVisitor >> elementFor: aXMLElement [ - (self isValueElement: aXMLElement) - ifTrue: [ ^ self valueElementFor: aXMLElement ]. - (self isReferenceElement: aXMLElement) - ifTrue: [ ^ self referenceElementFor: aXMLElement ]. - ^ self objectElementFor: aXMLElement -] - -{ #category : #initialization } -OPXMIReaderVisitor >> initialize [ - super initialize. - mapping := Dictionary new. - refIdMapping := Dictionary new. - unresolved := OrderedCollection new -] - -{ #category : #factory } -OPXMIReaderVisitor >> isReferenceAttribute: aXMLAttribute [ - (aXMLAttribute parent isNotNil - and: [ aXMLAttribute value = (self elementAt: aXMLAttribute parent) xmiId ]) - ifTrue: [ ^ false ]. - aXMLAttribute prefix = 'xmi' - ifTrue: [ ^ false ]. - ^ (aXMLAttribute value splitOn: ' ') - allSatisfy: [ :each | refIdMapping includesKey: each ] -] - -{ #category : #testing } -OPXMIReaderVisitor >> isReferenceElement: aXMLElement [ - | names | - aXMLElement hasAttributes not | aXMLElement hasElements - ifTrue: [ ^ false ]. - names := aXMLElement attributeNodes collect: #name. - ^ names allSatisfy: [ :each | #('xmi:idref' 'href' 'xmi:type') includes: each ] -] - -{ #category : #testing } -OPXMIReaderVisitor >> isValueElement: aXMLElement [ - aXMLElement hasAttributes - ifTrue: [ ^ false ]. - aXMLElement nodes size = 0 - ifTrue: [ ^ true ]. - ^ aXMLElement nodes size = 1 and: [ aXMLElement nodes first isStringNode ] -] - -{ #category : #factory } -OPXMIReaderVisitor >> objectElementFor: aXMLElement [ - | element | - element := OPXMIObjectElement new - xmiName: aXMLElement localName; - yourself. - element - containedItems: - (aXMLElement attributeNodes collect: [ :attr | self valueAttributeFor: attr ]) - asOrderedCollection. - unresolved - add: [ element - containedItems: - (aXMLElement attributeNodes collect: [ :attr | self attributeFor: attr ]) - asOrderedCollection, (element containedItems select: #isElement) ]. - ^ element -] - -{ #category : #factory } -OPXMIReaderVisitor >> referenceAttributeFor: aXMLAttribute [ - ^ OPXMIReferenceAttribute new - xmiName: aXMLAttribute name; - referencedElements: ((aXMLAttribute value splitOn: ' ') collect: [ :each | refIdMapping at: each ]); - yourself -] - -{ #category : #factory } -OPXMIReaderVisitor >> referenceElementFor: aXMLElement [ - | el attribute | - (el := OPXMIReferenceElement new) xmiName: aXMLElement localName. - attribute := aXMLElement attributeNodes - detect: [ :each | each name = 'href' | (each name = 'xmi:idref') ]. - attribute name = 'href' - ifTrue: [ el href: attribute value ] - ifFalse: [ unresolved add: [ el referencedElements: {refIdMapping at: attribute value} ] ]. - aXMLElement attributeNodes - detect: [ :each | each name = 'xmi:type' ] - ifFound: [ :typeAttrib | el xmiType: typeAttrib value ]. - ^ el -] - -{ #category : #actions } -OPXMIReaderVisitor >> resolveAll [ - unresolved do: #value -] - -{ #category : #factory } -OPXMIReaderVisitor >> valueAttributeFor: aXMLAttribute [ - ^ OPXMIValueAttribute new - xmiName: aXMLAttribute name; - value: aXMLAttribute value; - yourself -] - -{ #category : #factory } -OPXMIReaderVisitor >> valueElementFor: aXMLElement [ - ^ OPXMIValueElement new - xmiName: aXMLElement localName; - value: (aXMLElement stringNodes ifEmpty: [ '' ] ifNotEmpty: [ :nodes | nodes first string ]); - yourself -] - -{ #category : #visiting } -OPXMIReaderVisitor >> visitDocument: aDocument [ - self visit: aDocument root. - self resolveAll. - ^ self elementAt: aDocument root -] - -{ #category : #visiting } -OPXMIReaderVisitor >> visitElement: aXMLElement [ - | element | - element := self elementFor: aXMLElement. - mapping at: aXMLElement put: element. - mapping at: aXMLElement parent ifPresent: [ :parent | parent containedItems add: element ]. - element isObjectElement - ifTrue: [ refIdMapping at: element xmiId put: element ]. - super visitElement: aXMLElement -] +Class { + #name : #OPXMIReaderVisitor, + #superclass : #XMLDOMVisitor, + #instVars : [ + 'document', + 'root', + 'mapping', + 'unresolved', + 'refIdMapping' + ], + #category : 'OP-XMI-Reader' +} + +{ #category : #factory } +OPXMIReaderVisitor >> attributeFor: aXMLAttribute [ + (self isReferenceAttribute: aXMLAttribute) + ifTrue: [ ^ self referenceAttributeFor: aXMLAttribute ]. + ^ self valueAttributeFor: aXMLAttribute +] + +{ #category : #accessing } +OPXMIReaderVisitor >> elementAt: aXMLElement [ + ^ mapping at: aXMLElement +] + +{ #category : #factory } +OPXMIReaderVisitor >> elementFor: aXMLElement [ + (self isValueElement: aXMLElement) + ifTrue: [ ^ self valueElementFor: aXMLElement ]. + (self isReferenceElement: aXMLElement) + ifTrue: [ ^ self referenceElementFor: aXMLElement ]. + ^ self objectElementFor: aXMLElement +] + +{ #category : #initialization } +OPXMIReaderVisitor >> initialize [ + super initialize. + mapping := Dictionary new. + refIdMapping := Dictionary new. + unresolved := OrderedCollection new +] + +{ #category : #factory } +OPXMIReaderVisitor >> isReferenceAttribute: aXMLAttribute [ + (aXMLAttribute parent isNotNil + and: [ aXMLAttribute value = (self elementAt: aXMLAttribute parent) xmiId ]) + ifTrue: [ ^ false ]. + aXMLAttribute prefix = 'xmi' + ifTrue: [ ^ false ]. + ^ (aXMLAttribute value splitOn: ' ') + allSatisfy: [ :each | refIdMapping includesKey: each ] +] + +{ #category : #testing } +OPXMIReaderVisitor >> isReferenceElement: aXMLElement [ + | names | + aXMLElement hasAttributes not | aXMLElement hasElements + ifTrue: [ ^ false ]. + names := aXMLElement attributeNodes collect: #name. + ^ names allSatisfy: [ :each | #('xmi:idref' 'href' 'xmi:type') includes: each ] +] + +{ #category : #testing } +OPXMIReaderVisitor >> isValueElement: aXMLElement [ + aXMLElement hasAttributes + ifTrue: [ ^ false ]. + aXMLElement nodes size = 0 + ifTrue: [ ^ true ]. + ^ aXMLElement nodes size = 1 and: [ aXMLElement nodes first isStringNode ] +] + +{ #category : #factory } +OPXMIReaderVisitor >> objectElementFor: aXMLElement [ + | element | + element := OPXMIObjectElement new + xmiName: aXMLElement localName; + yourself. + element + containedItems: + (aXMLElement attributeNodes collect: [ :attr | self valueAttributeFor: attr ]) + asOrderedCollection. + unresolved + add: [ element + containedItems: + (aXMLElement attributeNodes collect: [ :attr | self attributeFor: attr ]) + asOrderedCollection, (element containedItems select: #isElement) ]. + ^ element +] + +{ #category : #factory } +OPXMIReaderVisitor >> referenceAttributeFor: aXMLAttribute [ + ^ OPXMIReferenceAttribute new + xmiName: aXMLAttribute name; + referencedElements: ((aXMLAttribute value splitOn: ' ') collect: [ :each | refIdMapping at: each ]); + yourself +] + +{ #category : #factory } +OPXMIReaderVisitor >> referenceElementFor: aXMLElement [ + | el attribute | + (el := OPXMIReferenceElement new) xmiName: aXMLElement localName. + attribute := aXMLElement attributeNodes + detect: [ :each | each name = 'href' | (each name = 'xmi:idref') ]. + attribute name = 'href' + ifTrue: [ el href: attribute value ] + ifFalse: [ unresolved add: [ el referencedElements: {refIdMapping at: attribute value} ] ]. + aXMLElement attributeNodes + detect: [ :each | each name = 'xmi:type' ] + ifFound: [ :typeAttrib | el xmiType: typeAttrib value ]. + ^ el +] + +{ #category : #actions } +OPXMIReaderVisitor >> resolveAll [ + unresolved do: #value +] + +{ #category : #factory } +OPXMIReaderVisitor >> valueAttributeFor: aXMLAttribute [ + ^ OPXMIValueAttribute new + xmiName: aXMLAttribute name; + value: aXMLAttribute value; + yourself +] + +{ #category : #factory } +OPXMIReaderVisitor >> valueElementFor: aXMLElement [ + ^ OPXMIValueElement new + xmiName: aXMLElement localName; + value: (aXMLElement stringNodes ifEmpty: [ '' ] ifNotEmpty: [ :nodes | nodes first string ]); + yourself +] + +{ #category : #visiting } +OPXMIReaderVisitor >> visitDocument: aDocument [ + self visit: aDocument root. + self resolveAll. + ^ self elementAt: aDocument root +] + +{ #category : #visiting } +OPXMIReaderVisitor >> visitElement: aXMLElement [ + | element | + element := self elementFor: aXMLElement. + mapping at: aXMLElement put: element. + mapping at: aXMLElement parent ifPresent: [ :parent | parent containedItems add: element ]. + element isObjectElement + ifTrue: [ refIdMapping at: element xmiId put: element ]. + super visitElement: aXMLElement +] diff --git a/repository/OP-XMI/OPXMIReaderVisitorExtraRulesTest.class.st b/repository/OP-XMI/OPXMIReaderVisitorExtraRulesTest.class.st index 25d3a58..393a046 100644 --- a/repository/OP-XMI/OPXMIReaderVisitorExtraRulesTest.class.st +++ b/repository/OP-XMI/OPXMIReaderVisitorExtraRulesTest.class.st @@ -1,63 +1,63 @@ -Class { - #name : #OPXMIReaderVisitorExtraRulesTest, - #superclass : #TestCase, - #instVars : [ - 'visitor', - 'result' - ], - #category : 'OP-XMI-Tests' -} - -{ #category : #'instance creation' } -OPXMIReaderVisitorExtraRulesTest >> elementNamed: aName [ - ^ XMLElement named: aName -] - -{ #category : #'instance creation' } -OPXMIReaderVisitorExtraRulesTest >> elementNamed: aName attributes: aCollection [ - ^ (XMLElement named: aName) - usesNamespaces: false; - setAttributes: aCollection; - yourself -] - -{ #category : #running } -OPXMIReaderVisitorExtraRulesTest >> setUp [ - super setUp. - visitor := OPXMIReaderVisitor new -] - -{ #category : #tests } -OPXMIReaderVisitorExtraRulesTest >> testNoSelfReference [ - " -the name attribute should be ValueAttribute because it refers to itself" - - | element | - element := self - elementNamed: 'packagedElement' - attributes: - {#xmi:type -> 'uml:Package'. - #xmi:id -> 'Classification'. - #name -> 'Classification'}. - visitor visitElement: element. - result := visitor elementAt: element. - self assert: result containedItems last isValueAttribute. - visitor resolveAll. - self assert: result containedItems last isValueAttribute -] - -{ #category : #tests } -OPXMIReaderVisitorExtraRulesTest >> testNoSelfReferenceIsReferenceAttributeNot [ - "goes with testNoSelfReference" - - | element | - element := self - elementNamed: 'whatever' - attributes: - {#xmi:id -> 'NAME'. - #name -> 'NAME'}. - self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'NAME')). - visitor visitElement: element. - self assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'NAME')). - self deny: (visitor isReferenceAttribute: (element attributeNodeAt: 'name')) -] +Class { + #name : #OPXMIReaderVisitorExtraRulesTest, + #superclass : #TestCase, + #instVars : [ + 'visitor', + 'result' + ], + #category : 'OP-XMI-Tests' +} + +{ #category : #'instance creation' } +OPXMIReaderVisitorExtraRulesTest >> elementNamed: aName [ + ^ XMLElement named: aName +] + +{ #category : #'instance creation' } +OPXMIReaderVisitorExtraRulesTest >> elementNamed: aName attributes: aCollection [ + ^ (XMLElement named: aName) + usesNamespaces: false; + setAttributes: aCollection; + yourself +] + +{ #category : #running } +OPXMIReaderVisitorExtraRulesTest >> setUp [ + super setUp. + visitor := OPXMIReaderVisitor new +] + +{ #category : #tests } +OPXMIReaderVisitorExtraRulesTest >> testNoSelfReference [ + " +the name attribute should be ValueAttribute because it refers to itself" + + | element | + element := self + elementNamed: 'packagedElement' + attributes: + {#xmi:type -> 'uml:Package'. + #xmi:id -> 'Classification'. + #name -> 'Classification'}. + visitor visitElement: element. + result := visitor elementAt: element. + self assert: result containedItems last isValueAttribute. + visitor resolveAll. + self assert: result containedItems last isValueAttribute +] + +{ #category : #tests } +OPXMIReaderVisitorExtraRulesTest >> testNoSelfReferenceIsReferenceAttributeNot [ + "goes with testNoSelfReference" + + | element | + element := self + elementNamed: 'whatever' + attributes: + {#xmi:id -> 'NAME'. + #name -> 'NAME'}. + self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'NAME')). + visitor visitElement: element. + self assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'NAME')). + self deny: (visitor isReferenceAttribute: (element attributeNodeAt: 'name')) +] diff --git a/repository/OP-XMI/OPXMIReaderVisitorTest.class.st b/repository/OP-XMI/OPXMIReaderVisitorTest.class.st index 44bd667..f77060c 100644 --- a/repository/OP-XMI/OPXMIReaderVisitorTest.class.st +++ b/repository/OP-XMI/OPXMIReaderVisitorTest.class.st @@ -1,243 +1,243 @@ -Class { - #name : #OPXMIReaderVisitorTest, - #superclass : #TestCase, - #instVars : [ - 'visitor', - 'result' - ], - #category : 'OP-XMI-Tests' -} - -{ #category : #'instance creation' } -OPXMIReaderVisitorTest >> elementNamed: aName [ - ^ XMLElement named: aName -] - -{ #category : #'instance creation' } -OPXMIReaderVisitorTest >> elementNamed: aName attributes: aCollection [ - ^ (XMLElement named: aName) - usesNamespaces: false; - setAttributes: aCollection; - yourself -] - -{ #category : #running } -OPXMIReaderVisitorTest >> setUp [ - super setUp. - visitor := OPXMIReaderVisitor new -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceAttribute [ - self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID')). - visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID'}). - self assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID')) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceAttributeMulti [ - self - deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID1 ID2')). - visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID1'}). - visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID2'}). - self - assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID1 ID2')) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceAttributeNotXmi [ - self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'xmi:idref' value: 'value')). - self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'xmi:id' value: 'value')) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceElementHref [ - self - assert: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#href -> #content})) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceElementHref2 [ - self - assert: - (visitor - isReferenceElement: - (self - elementNamed: 'el' - attributes: - {#xmi:type -> 'type'. - #href -> #content})) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceElementIdRef [ - self - assert: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#xmi:idref -> #value})) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsReferenceElementNot [ - self - deny: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#other -> #sth})) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsValueElement [ - self - assert: - (visitor - isValueElement: - (XMLElement new - name: 'el'; - addString: 'content'; - yourself)) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsValueElementNot [ - self deny: (visitor isValueElement: (XMLElement name: 'el' attributes: {#key -> #value})) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testIsValueElementNot2 [ - self - deny: - (visitor - isValueElement: - ((XMLElement name: 'el') - addNode: (XMLElement name: 'child'); - yourself)) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testObjectElement [ - result := visitor objectElementFor: (self elementNamed: 'el'). - self assert: result isObjectElement. - self assert: result xmiName equals: 'el' -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testObjectElementWithAttributes [ - result := visitor - objectElementFor: (self elementNamed: 'el' attributes: {#name -> #value}). - self assert: result xmiName equals: 'el'. - self assert: result containedItems size equals: 1. - self assert: result containedItems first isValueAttribute. - self assert: result containedItems first xmiName equals: 'name'. - self assert: result containedItems first value equals: 'value' -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testReferenceAttribute [ - | target reference attribute | - target := self elementNamed: 'Target' attributes: {#xmi:id -> #target}. - reference := self elementNamed: 'Reference' attributes: {#object -> #target}. - visitor visitElement: reference. - visitor visitElement: target. - visitor resolveAll. - result := visitor elementAt: reference. - self assert: result containedItems size equals: 1. - attribute := result containedItem. - self assert: attribute isReferenceAttribute. - self assert: attribute referencedElements size equals: 1. - self assert: attribute referencedElement equals: (visitor elementAt: target) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testReferenceAttributeMulti [ - | target1 target2 reference attribute | - target1 := self elementNamed: 'Target' attributes: {#xmi:id -> #target1}. - target2 := self elementNamed: 'Target' attributes: {#xmi:id -> #target2}. - reference := self elementNamed: 'Reference' attributes: {#object -> 'target1 target2'}. - visitor visitElement: reference. - visitor visitElement: target1. - visitor visitElement: target2. - visitor resolveAll. - result := visitor elementAt: reference. - self assert: result containedItems size equals: 1. - attribute := result containedItem. - self assert: attribute isReferenceAttribute. - self assert: attribute referencedElements size equals: 2. - self assert: attribute referencedElements first equals: (visitor elementAt: target1). - self assert: attribute referencedElements second equals: (visitor elementAt: target2) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testReferenceElement [ - | target reference | - target := self elementNamed: 'Target' attributes: {#xmi:id -> #target}. - reference := self elementNamed: 'Reference' attributes: {#xmi:idref -> #target}. - visitor visitElement: target. - visitor visitElement: reference. - visitor resolveAll. - result := visitor elementAt: reference. - self assert: result isReferenceElement. - self assert: result referencedElements size equals: 1. - self assert: result referencedElement equals: (visitor elementAt: target) -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testReferenceElementHrefUnresolved [ - | reference | - reference := self - elementNamed: 'importedPackage' - attributes: {#href -> 'http://www.omg.org/spec/DD/20131001/DI.xmi#_0'}. - visitor visitElement: reference. - visitor resolveAll. - result := visitor elementAt: reference. - self assert: result isReferenceElement. - self assert: result href equals: 'http://www.omg.org/spec/DD/20131001/DI.xmi#_0'. - self assert: result referencedElements size equals: 0 -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testReferenceElementHrefUnresolvedWithType [ - | reference | - reference := self - elementNamed: 'type' - attributes: - {#xmi:type -> 'uml:PrimitiveType'. - #href -> 'PrimitiveTypes.xmi#String'}. - visitor visitElement: reference. - visitor resolveAll. - result := visitor elementAt: reference. - self assert: result isReferenceElement. - self assert: result href equals: 'PrimitiveTypes.xmi#String'. - self assert: result xmiType equals: 'uml:PrimitiveType'. - self assert: result referencedElements size equals: 0 -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testValueAttribute [ - result := visitor valueAttributeFor: (XMLAttribute name: 'name' value: 'value'). - self assert: result isValueAttribute. - self assert: result xmiName equals: 'name'. - self assert: result value equals: 'value' -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testValueElement [ - result := visitor - valueElementFor: - (XMLElement new - name: 'el'; - addString: 'content'; - yourself). - self assert: result isValueElement. - self assert: result xmiName equals: 'el'. - self assert: result value equals: 'content' -] - -{ #category : #tests } -OPXMIReaderVisitorTest >> testVisitObjectElementWithChild [ - | node | - node := (self elementNamed: 'parent' attributes: {#xmi:type -> 'type'}) - addNode: (self elementNamed: 'child'); - yourself. - visitor visitElement: node. - result := visitor elementAt: node. - self assert: result xmiName equals: 'parent'. - self assert: result containedItems size equals: 2. - self assert: result containedItems second isElement. - self assert: result containedItems second xmiName equals: 'child' -] +Class { + #name : #OPXMIReaderVisitorTest, + #superclass : #TestCase, + #instVars : [ + 'visitor', + 'result' + ], + #category : 'OP-XMI-Tests' +} + +{ #category : #'instance creation' } +OPXMIReaderVisitorTest >> elementNamed: aName [ + ^ XMLElement named: aName +] + +{ #category : #'instance creation' } +OPXMIReaderVisitorTest >> elementNamed: aName attributes: aCollection [ + ^ (XMLElement named: aName) + usesNamespaces: false; + setAttributes: aCollection; + yourself +] + +{ #category : #running } +OPXMIReaderVisitorTest >> setUp [ + super setUp. + visitor := OPXMIReaderVisitor new +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceAttribute [ + self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID')). + visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID'}). + self assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID')) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceAttributeMulti [ + self + deny: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID1 ID2')). + visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID1'}). + visitor visitElement: (self elementNamed: 'whatever' attributes: {#xmi:id -> 'ID2'}). + self + assert: (visitor isReferenceAttribute: (XMLAttribute name: 'name' value: 'ID1 ID2')) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceAttributeNotXmi [ + self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'xmi:idref' value: 'value')). + self deny: (visitor isReferenceAttribute: (XMLAttribute name: 'xmi:id' value: 'value')) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceElementHref [ + self + assert: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#href -> #content})) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceElementHref2 [ + self + assert: + (visitor + isReferenceElement: + (self + elementNamed: 'el' + attributes: + {#xmi:type -> 'type'. + #href -> #content})) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceElementIdRef [ + self + assert: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#xmi:idref -> #value})) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsReferenceElementNot [ + self + deny: (visitor isReferenceElement: (self elementNamed: 'el' attributes: {#other -> #sth})) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsValueElement [ + self + assert: + (visitor + isValueElement: + (XMLElement new + name: 'el'; + addString: 'content'; + yourself)) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsValueElementNot [ + self deny: (visitor isValueElement: (XMLElement name: 'el' attributes: {#key -> #value})) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testIsValueElementNot2 [ + self + deny: + (visitor + isValueElement: + ((XMLElement name: 'el') + addNode: (XMLElement name: 'child'); + yourself)) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testObjectElement [ + result := visitor objectElementFor: (self elementNamed: 'el'). + self assert: result isObjectElement. + self assert: result xmiName equals: 'el' +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testObjectElementWithAttributes [ + result := visitor + objectElementFor: (self elementNamed: 'el' attributes: {#name -> #value}). + self assert: result xmiName equals: 'el'. + self assert: result containedItems size equals: 1. + self assert: result containedItems first isValueAttribute. + self assert: result containedItems first xmiName equals: 'name'. + self assert: result containedItems first value equals: 'value' +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testReferenceAttribute [ + | target reference attribute | + target := self elementNamed: 'Target' attributes: {#xmi:id -> #target}. + reference := self elementNamed: 'Reference' attributes: {#object -> #target}. + visitor visitElement: reference. + visitor visitElement: target. + visitor resolveAll. + result := visitor elementAt: reference. + self assert: result containedItems size equals: 1. + attribute := result containedItem. + self assert: attribute isReferenceAttribute. + self assert: attribute referencedElements size equals: 1. + self assert: attribute referencedElement equals: (visitor elementAt: target) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testReferenceAttributeMulti [ + | target1 target2 reference attribute | + target1 := self elementNamed: 'Target' attributes: {#xmi:id -> #target1}. + target2 := self elementNamed: 'Target' attributes: {#xmi:id -> #target2}. + reference := self elementNamed: 'Reference' attributes: {#object -> 'target1 target2'}. + visitor visitElement: reference. + visitor visitElement: target1. + visitor visitElement: target2. + visitor resolveAll. + result := visitor elementAt: reference. + self assert: result containedItems size equals: 1. + attribute := result containedItem. + self assert: attribute isReferenceAttribute. + self assert: attribute referencedElements size equals: 2. + self assert: attribute referencedElements first equals: (visitor elementAt: target1). + self assert: attribute referencedElements second equals: (visitor elementAt: target2) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testReferenceElement [ + | target reference | + target := self elementNamed: 'Target' attributes: {#xmi:id -> #target}. + reference := self elementNamed: 'Reference' attributes: {#xmi:idref -> #target}. + visitor visitElement: target. + visitor visitElement: reference. + visitor resolveAll. + result := visitor elementAt: reference. + self assert: result isReferenceElement. + self assert: result referencedElements size equals: 1. + self assert: result referencedElement equals: (visitor elementAt: target) +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testReferenceElementHrefUnresolved [ + | reference | + reference := self + elementNamed: 'importedPackage' + attributes: {#href -> 'http://www.omg.org/spec/DD/20131001/DI.xmi#_0'}. + visitor visitElement: reference. + visitor resolveAll. + result := visitor elementAt: reference. + self assert: result isReferenceElement. + self assert: result href equals: 'http://www.omg.org/spec/DD/20131001/DI.xmi#_0'. + self assert: result referencedElements size equals: 0 +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testReferenceElementHrefUnresolvedWithType [ + | reference | + reference := self + elementNamed: 'type' + attributes: + {#xmi:type -> 'uml:PrimitiveType'. + #href -> 'PrimitiveTypes.xmi#String'}. + visitor visitElement: reference. + visitor resolveAll. + result := visitor elementAt: reference. + self assert: result isReferenceElement. + self assert: result href equals: 'PrimitiveTypes.xmi#String'. + self assert: result xmiType equals: 'uml:PrimitiveType'. + self assert: result referencedElements size equals: 0 +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testValueAttribute [ + result := visitor valueAttributeFor: (XMLAttribute name: 'name' value: 'value'). + self assert: result isValueAttribute. + self assert: result xmiName equals: 'name'. + self assert: result value equals: 'value' +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testValueElement [ + result := visitor + valueElementFor: + (XMLElement new + name: 'el'; + addString: 'content'; + yourself). + self assert: result isValueElement. + self assert: result xmiName equals: 'el'. + self assert: result value equals: 'content' +] + +{ #category : #tests } +OPXMIReaderVisitorTest >> testVisitObjectElementWithChild [ + | node | + node := (self elementNamed: 'parent' attributes: {#xmi:type -> 'type'}) + addNode: (self elementNamed: 'child'); + yourself. + visitor visitElement: node. + result := visitor elementAt: node. + self assert: result xmiName equals: 'parent'. + self assert: result containedItems size equals: 2. + self assert: result containedItems second isElement. + self assert: result containedItems second xmiName equals: 'child' +] diff --git a/repository/OP-XMI/OPXMIReferenceAttribute.class.st b/repository/OP-XMI/OPXMIReferenceAttribute.class.st index ea2eb5c..1e95d61 100644 --- a/repository/OP-XMI/OPXMIReferenceAttribute.class.st +++ b/repository/OP-XMI/OPXMIReferenceAttribute.class.st @@ -1,69 +1,69 @@ -" -XMIReferenceAttribute is an XML attribute that references an XMIElement by id. -" -Class { - #name : #OPXMIReferenceAttribute, - #superclass : #OPXMIAttribute, - #instVars : [ - 'referencedElements', - 'href' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #'instance creation' } -OPXMIReferenceAttribute class >> xmiName: aName referencedElements: aCollection [ - ^ self new - xmiName: aName; - referencedElements: aCollection; - yourself -] - -{ #category : #visiting } -OPXMIReferenceAttribute >> accept: aVisitor [ - aVisitor visitReferenceAttribute: self -] - -{ #category : #accessing } -OPXMIReferenceAttribute >> href [ - ^ href -] - -{ #category : #accessing } -OPXMIReferenceAttribute >> href: anObject [ - href := anObject -] - -{ #category : #initialization } -OPXMIReferenceAttribute >> initialize [ - super initialize. - referencedElements := OrderedCollection new -] - -{ #category : #testing } -OPXMIReferenceAttribute >> isReferenceAttribute [ - ^ true -] - -{ #category : #printing } -OPXMIReferenceAttribute >> printOn: aStream [ - aStream - << 'XMIReferenceAttribute('; - << xmiName; - << '")' -] - -{ #category : #accessing } -OPXMIReferenceAttribute >> referencedElement [ - ^ referencedElements ifEmpty: [ nil ] ifNotEmpty: [ referencedElements first ] -] - -{ #category : #accessing } -OPXMIReferenceAttribute >> referencedElements [ - ^ referencedElements -] - -{ #category : #accessing } -OPXMIReferenceAttribute >> referencedElements: aCollection [ - referencedElements := aCollection -] +" +XMIReferenceAttribute is an XML attribute that references an XMIElement by id. +" +Class { + #name : #OPXMIReferenceAttribute, + #superclass : #OPXMIAttribute, + #instVars : [ + 'referencedElements', + 'href' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #'instance creation' } +OPXMIReferenceAttribute class >> xmiName: aName referencedElements: aCollection [ + ^ self new + xmiName: aName; + referencedElements: aCollection; + yourself +] + +{ #category : #visiting } +OPXMIReferenceAttribute >> accept: aVisitor [ + aVisitor visitReferenceAttribute: self +] + +{ #category : #accessing } +OPXMIReferenceAttribute >> href [ + ^ href +] + +{ #category : #accessing } +OPXMIReferenceAttribute >> href: anObject [ + href := anObject +] + +{ #category : #initialization } +OPXMIReferenceAttribute >> initialize [ + super initialize. + referencedElements := OrderedCollection new +] + +{ #category : #testing } +OPXMIReferenceAttribute >> isReferenceAttribute [ + ^ true +] + +{ #category : #printing } +OPXMIReferenceAttribute >> printOn: aStream [ + aStream + << 'XMIReferenceAttribute('; + << xmiName; + << '")' +] + +{ #category : #accessing } +OPXMIReferenceAttribute >> referencedElement [ + ^ referencedElements ifEmpty: [ nil ] ifNotEmpty: [ referencedElements first ] +] + +{ #category : #accessing } +OPXMIReferenceAttribute >> referencedElements [ + ^ referencedElements +] + +{ #category : #accessing } +OPXMIReferenceAttribute >> referencedElements: aCollection [ + referencedElements := aCollection +] diff --git a/repository/OP-XMI/OPXMIReferenceElement.class.st b/repository/OP-XMI/OPXMIReferenceElement.class.st index d70d1fa..9c478ab 100644 --- a/repository/OP-XMI/OPXMIReferenceElement.class.st +++ b/repository/OP-XMI/OPXMIReferenceElement.class.st @@ -1,72 +1,72 @@ -" -XMIReferenceElement is an XML element with an idref or href attribute that references another XMIElement, by id, URI, or URI and XPointer. -" -Class { - #name : #OPXMIReferenceElement, - #superclass : #OPXMIElement, - #instVars : [ - 'referencedElements', - 'href', - 'xmiType' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #'instance creation' } -OPXMIReferenceElement class >> xmiName: aName referencedElements: aCollection [ - ^ self new - xmiName: aName; - referencedElements: aCollection; - yourself -] - -{ #category : #visiting } -OPXMIReferenceElement >> accept: aVisitor [ - aVisitor visitReferenceElement: self -] - -{ #category : #accessing } -OPXMIReferenceElement >> href [ - ^ href -] - -{ #category : #accessing } -OPXMIReferenceElement >> href: anObject [ - href := anObject -] - -{ #category : #initialization } -OPXMIReferenceElement >> initialize [ - super initialize. - referencedElements := OrderedCollection new -] - -{ #category : #testing } -OPXMIReferenceElement >> isReferenceElement [ - ^ true -] - -{ #category : #accessing } -OPXMIReferenceElement >> referencedElement [ - ^ referencedElements ifEmpty: [ nil ] ifNotEmpty: [ referencedElements first ] -] - -{ #category : #accessing } -OPXMIReferenceElement >> referencedElements [ - ^ referencedElements -] - -{ #category : #accessing } -OPXMIReferenceElement >> referencedElements: aCollection [ - referencedElements := aCollection -] - -{ #category : #accessing } -OPXMIReferenceElement >> xmiType [ - ^ xmiType -] - -{ #category : #accessing } -OPXMIReferenceElement >> xmiType: anObject [ - xmiType := anObject -] +" +XMIReferenceElement is an XML element with an idref or href attribute that references another XMIElement, by id, URI, or URI and XPointer. +" +Class { + #name : #OPXMIReferenceElement, + #superclass : #OPXMIElement, + #instVars : [ + 'referencedElements', + 'href', + 'xmiType' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #'instance creation' } +OPXMIReferenceElement class >> xmiName: aName referencedElements: aCollection [ + ^ self new + xmiName: aName; + referencedElements: aCollection; + yourself +] + +{ #category : #visiting } +OPXMIReferenceElement >> accept: aVisitor [ + aVisitor visitReferenceElement: self +] + +{ #category : #accessing } +OPXMIReferenceElement >> href [ + ^ href +] + +{ #category : #accessing } +OPXMIReferenceElement >> href: anObject [ + href := anObject +] + +{ #category : #initialization } +OPXMIReferenceElement >> initialize [ + super initialize. + referencedElements := OrderedCollection new +] + +{ #category : #testing } +OPXMIReferenceElement >> isReferenceElement [ + ^ true +] + +{ #category : #accessing } +OPXMIReferenceElement >> referencedElement [ + ^ referencedElements ifEmpty: [ nil ] ifNotEmpty: [ referencedElements first ] +] + +{ #category : #accessing } +OPXMIReferenceElement >> referencedElements [ + ^ referencedElements +] + +{ #category : #accessing } +OPXMIReferenceElement >> referencedElements: aCollection [ + referencedElements := aCollection +] + +{ #category : #accessing } +OPXMIReferenceElement >> xmiType [ + ^ xmiType +] + +{ #category : #accessing } +OPXMIReferenceElement >> xmiType: anObject [ + xmiType := anObject +] diff --git a/repository/OP-XMI/OPXMIReferenceItemTest.class.st b/repository/OP-XMI/OPXMIReferenceItemTest.class.st index 5b17ed2..fede46e 100644 --- a/repository/OP-XMI/OPXMIReferenceItemTest.class.st +++ b/repository/OP-XMI/OPXMIReferenceItemTest.class.st @@ -1,21 +1,21 @@ -Class { - #name : #OPXMIReferenceItemTest, - #superclass : #TestCase, - #category : 'OP-XMI-Tests' -} - -{ #category : #tests } -OPXMIReferenceItemTest >> testConstructorAttribute [ - | element | - element := OPXMIReferenceAttribute xmiName: 'NAME' referencedElements: #(1). - self assert: element xmiName equals: 'NAME'. - self assert: element referencedElements equals: #(1) -] - -{ #category : #tests } -OPXMIReferenceItemTest >> testConstructorElement [ - | element | - element := OPXMIReferenceElement xmiName: 'NAME' referencedElements: #(1). - self assert: element xmiName equals: 'NAME'. - self assert: element referencedElements equals: #(1) -] +Class { + #name : #OPXMIReferenceItemTest, + #superclass : #TestCase, + #category : 'OP-XMI-Tests' +} + +{ #category : #tests } +OPXMIReferenceItemTest >> testConstructorAttribute [ + | element | + element := OPXMIReferenceAttribute xmiName: 'NAME' referencedElements: #(1). + self assert: element xmiName equals: 'NAME'. + self assert: element referencedElements equals: #(1) +] + +{ #category : #tests } +OPXMIReferenceItemTest >> testConstructorElement [ + | element | + element := OPXMIReferenceElement xmiName: 'NAME' referencedElements: #(1). + self assert: element xmiName equals: 'NAME'. + self assert: element referencedElements equals: #(1) +] diff --git a/repository/OP-XMI/OPXMITest.class.st b/repository/OP-XMI/OPXMITest.class.st index a7fc8e9..689d6b2 100644 --- a/repository/OP-XMI/OPXMITest.class.st +++ b/repository/OP-XMI/OPXMITest.class.st @@ -1,31 +1,31 @@ -Class { - #name : #OPXMITest, - #superclass : #TestCase, - #category : 'OP-XMI-Tests' -} - -{ #category : #tests } -OPXMITest >> normalized: aString [ - ^ (aString copyReplaceAll: String cr with: String lf) - copyReplaceAll: String tab - with: ' ' -] - -{ #category : #tests } -OPXMITest >> testReadWrite [ - | in root out | - in := ' - - - - - - - Content - - -'. - root := OPXMIReader readFrom: in readStream. - out := OPXMIWriter writeToString: root. - self assert: (self normalized: in) equals: (self normalized: out) -] +Class { + #name : #OPXMITest, + #superclass : #TestCase, + #category : 'OP-XMI-Tests' +} + +{ #category : #tests } +OPXMITest >> normalized: aString [ + ^ (aString copyReplaceAll: String cr with: String lf) + copyReplaceAll: String tab + with: ' ' +] + +{ #category : #tests } +OPXMITest >> testReadWrite [ + | in root out | + in := ' + + + + + + + Content + + +'. + root := OPXMIReader readFrom: in readStream. + out := OPXMIWriter writeToString: root. + self assert: (self normalized: in) equals: (self normalized: out) +] diff --git a/repository/OP-XMI/OPXMIValueAttribute.class.st b/repository/OP-XMI/OPXMIValueAttribute.class.st index 705096f..c3c5e59 100644 --- a/repository/OP-XMI/OPXMIValueAttribute.class.st +++ b/repository/OP-XMI/OPXMIValueAttribute.class.st @@ -1,65 +1,65 @@ -" -XMIValueAttribute is simply an XML attribute with a value. -" -Class { - #name : #OPXMIValueAttribute, - #superclass : #OPXMIAttribute, - #instVars : [ - 'value' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #'instance creation' } -OPXMIValueAttribute class >> xmiName: aName value: aValue [ - ^ self new - xmiName: aName; - value: aValue; - yourself -] - -{ #category : #comparing } -OPXMIValueAttribute >> = aValueAttribute [ - self species = aValueAttribute species - ifFalse: [ ^ false ]. - self xmiName = aValueAttribute xmiName - ifFalse: [ ^ false ]. - self value = aValueAttribute value - ifFalse: [ ^ false ]. - ^ true -] - -{ #category : #visiting } -OPXMIValueAttribute >> accept: aVisitor [ - aVisitor visitValueAttribute: self -] - -{ #category : #comparing } -OPXMIValueAttribute >> hash [ - ^ self species hash bitXor: (self xmiName hash bitXor: self value hash) -] - -{ #category : #testing } -OPXMIValueAttribute >> isValueAttribute [ - ^ true -] - -{ #category : #printing } -OPXMIValueAttribute >> printOn: aStream [ - aStream - << 'XMIValueAttribute('; - << xmiName; - << '="'; - << value; - << '")' -] - -{ #category : #accessing } -OPXMIValueAttribute >> value [ - ^ value -] - -{ #category : #accessing } -OPXMIValueAttribute >> value: aString [ - value := aString -] +" +XMIValueAttribute is simply an XML attribute with a value. +" +Class { + #name : #OPXMIValueAttribute, + #superclass : #OPXMIAttribute, + #instVars : [ + 'value' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #'instance creation' } +OPXMIValueAttribute class >> xmiName: aName value: aValue [ + ^ self new + xmiName: aName; + value: aValue; + yourself +] + +{ #category : #comparing } +OPXMIValueAttribute >> = aValueAttribute [ + self species = aValueAttribute species + ifFalse: [ ^ false ]. + self xmiName = aValueAttribute xmiName + ifFalse: [ ^ false ]. + self value = aValueAttribute value + ifFalse: [ ^ false ]. + ^ true +] + +{ #category : #visiting } +OPXMIValueAttribute >> accept: aVisitor [ + aVisitor visitValueAttribute: self +] + +{ #category : #comparing } +OPXMIValueAttribute >> hash [ + ^ self species hash bitXor: (self xmiName hash bitXor: self value hash) +] + +{ #category : #testing } +OPXMIValueAttribute >> isValueAttribute [ + ^ true +] + +{ #category : #printing } +OPXMIValueAttribute >> printOn: aStream [ + aStream + << 'XMIValueAttribute('; + << xmiName; + << '="'; + << value; + << '")' +] + +{ #category : #accessing } +OPXMIValueAttribute >> value [ + ^ value +] + +{ #category : #accessing } +OPXMIValueAttribute >> value: aString [ + value := aString +] diff --git a/repository/OP-XMI/OPXMIValueElement.class.st b/repository/OP-XMI/OPXMIValueElement.class.st index b603e78..b19edbd 100644 --- a/repository/OP-XMI/OPXMIValueElement.class.st +++ b/repository/OP-XMI/OPXMIValueElement.class.st @@ -1,39 +1,39 @@ -" -XMIValueElement is an XML element that can have a value, but cannot contain other XML elements or attributes. -" -Class { - #name : #OPXMIValueElement, - #superclass : #OPXMIElement, - #instVars : [ - 'value' - ], - #category : 'OP-XMI-DOM' -} - -{ #category : #'instance creation' } -OPXMIValueElement class >> xmiName: aName value: aValue [ - ^ self new - xmiName: aName; - value: aValue; - yourself -] - -{ #category : #visiting } -OPXMIValueElement >> accept: aVisitor [ - aVisitor visitValueElement: self -] - -{ #category : #testing } -OPXMIValueElement >> isValueElement [ - ^ true -] - -{ #category : #accessing } -OPXMIValueElement >> value [ - ^ value -] - -{ #category : #accessing } -OPXMIValueElement >> value: aString [ - value := aString -] +" +XMIValueElement is an XML element that can have a value, but cannot contain other XML elements or attributes. +" +Class { + #name : #OPXMIValueElement, + #superclass : #OPXMIElement, + #instVars : [ + 'value' + ], + #category : 'OP-XMI-DOM' +} + +{ #category : #'instance creation' } +OPXMIValueElement class >> xmiName: aName value: aValue [ + ^ self new + xmiName: aName; + value: aValue; + yourself +] + +{ #category : #visiting } +OPXMIValueElement >> accept: aVisitor [ + aVisitor visitValueElement: self +] + +{ #category : #testing } +OPXMIValueElement >> isValueElement [ + ^ true +] + +{ #category : #accessing } +OPXMIValueElement >> value [ + ^ value +] + +{ #category : #accessing } +OPXMIValueElement >> value: aString [ + value := aString +] diff --git a/repository/OP-XMI/OPXMIValueItemTest.class.st b/repository/OP-XMI/OPXMIValueItemTest.class.st index 39442ca..0ba93be 100644 --- a/repository/OP-XMI/OPXMIValueItemTest.class.st +++ b/repository/OP-XMI/OPXMIValueItemTest.class.st @@ -1,21 +1,21 @@ -Class { - #name : #OPXMIValueItemTest, - #superclass : #TestCase, - #category : 'OP-XMI-Tests' -} - -{ #category : #tests } -OPXMIValueItemTest >> testConstructorAttribute [ - | element | - element := OPXMIValueAttribute xmiName: 'NAME' value: 'VALUE'. - self assert: element xmiName equals: 'NAME'. - self assert: element value equals: 'VALUE' -] - -{ #category : #tests } -OPXMIValueItemTest >> testConstructorElement [ - |element| - element := OPXMIValueElement xmiName: 'NAME' value: 'VALUE'. - self assert: element xmiName equals: 'NAME'. - self assert: element value equals: 'VALUE' -] +Class { + #name : #OPXMIValueItemTest, + #superclass : #TestCase, + #category : 'OP-XMI-Tests' +} + +{ #category : #tests } +OPXMIValueItemTest >> testConstructorAttribute [ + | element | + element := OPXMIValueAttribute xmiName: 'NAME' value: 'VALUE'. + self assert: element xmiName equals: 'NAME'. + self assert: element value equals: 'VALUE' +] + +{ #category : #tests } +OPXMIValueItemTest >> testConstructorElement [ + |element| + element := OPXMIValueElement xmiName: 'NAME' value: 'VALUE'. + self assert: element xmiName equals: 'NAME'. + self assert: element value equals: 'VALUE' +] diff --git a/repository/OP-XMI/OPXMIWriter.class.st b/repository/OP-XMI/OPXMIWriter.class.st index 50280c6..2c3fde0 100644 --- a/repository/OP-XMI/OPXMIWriter.class.st +++ b/repository/OP-XMI/OPXMIWriter.class.st @@ -1,23 +1,23 @@ -Class { - #name : #OPXMIWriter, - #superclass : #Object, - #category : 'OP-XMI-Writer' -} - -{ #category : #writing } -OPXMIWriter class >> write: anXMI to: aStream [ - ^ self new write: anXMI to: aStream -] - -{ #category : #writing } -OPXMIWriter class >> writeToString: anXMI [ - ^ String streamContents: [ :stream | self write: anXMI to: stream ] -] - -{ #category : #writing } -OPXMIWriter >> write: anXMI to: aStream [ - | visitor | - visitor := OPXMIWriterVisitor new. - visitor visit: anXMI. - aStream << visitor contents -] +Class { + #name : #OPXMIWriter, + #superclass : #Object, + #category : 'OP-XMI-Writer' +} + +{ #category : #writing } +OPXMIWriter class >> write: anXMI to: aStream [ + ^ self new write: anXMI to: aStream +] + +{ #category : #writing } +OPXMIWriter class >> writeToString: anXMI [ + ^ String streamContents: [ :stream | self write: anXMI to: stream ] +] + +{ #category : #writing } +OPXMIWriter >> write: anXMI to: aStream [ + | visitor | + visitor := OPXMIWriterVisitor new. + visitor visit: anXMI. + aStream << visitor contents +] diff --git a/repository/OP-XMI/OPXMIWriterVisitor.class.st b/repository/OP-XMI/OPXMIWriterVisitor.class.st index 2ddd811..7f1367b 100644 --- a/repository/OP-XMI/OPXMIWriterVisitor.class.st +++ b/repository/OP-XMI/OPXMIWriterVisitor.class.st @@ -1,97 +1,97 @@ -Class { - #name : #OPXMIWriterVisitor, - #superclass : #Object, - #instVars : [ - 'writer' - ], - #category : 'OP-XMI-Writer' -} - -{ #category : #accessing } -OPXMIWriterVisitor >> contents [ - ^ writer contents -] - -{ #category : #initialization } -OPXMIWriterVisitor >> initialize [ - super initialize. - writer := XMLWriter new. - writer enablePrettyPrinting. - writer xml encoding: 'UTF-8' -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visit: anElement [ - anElement accept: self -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visitObjectElement: anObjectElement [ - "2a:XMIObjectElement ::= ( ""<"" 2k:QName 2d:XMIAttributes ""/>"" ) -| ( ""<"" 2k:QName 2d:XMIAttributes "">"" -(2:XMIElement)* -"""" )" - -"NOTE: I am not handling namespaces properly... thus I explicitly change XMI to xmi:XMI" - - writer - tag: - (anObjectElement xmiName = 'XMI' - ifTrue: [ 'xmi:XMI' ] - ifFalse: [ anObjectElement xmiName ]) - with: [ anObjectElement containedItems - select: #isAttribute - thenDo: [ :each | self visit: each ]. - anObjectElement containedItems select: #isElement thenDo: [ :each | self visit: each ] ] -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visitReferenceAttribute: aReferenceAttribute [ - "2j:XMIReferenceAttribute ::= //xmiName// ""=’"" ( //refId// )+ ""’" - - writer writeAttributeName: aReferenceAttribute xmiName value: ((aReferenceAttribute referencedElements collect: #xmiId) joinUsing: ' ') -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visitReferenceElement: aReferenceElement [ - " -2c:XMIReferenceElement::= ""<"" //xmiName// 2l:LinkAttribs ""/>"" -2l:LinkAttribs ::= ""xmi:idref=’"" //refId// ""’"" | 2m:Link -2m:Link ::= ""href='"" 2n:URIref ""'"" -2n:URIref ::= (2k:QName)? //URI reference// - -Additionally every modeling tool adds xmi:type information, even though the specs do not seem to allow it. -" - - writer - tag: aReferenceElement xmiName - with: [ aReferenceElement xmiType - ifNotNil: [ writer writeAttributeName: #xmi:type value: aReferenceElement xmiType ]. - aReferenceElement href - ifNil: - [ "idref" writer writeAttributeName: #xmi:idref value: aReferenceElement referencedElement xmiId ] - ifNotNil: [ "href" writer writeAttributeName: 'href' value: aReferenceElement href ] ] -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visitValueAttribute: aValueAttribute [ - "2i:XMIValueAttribute ::= //xmiName// ""=’"" value ""’""" - - writer writeAttributeName: aValueAttribute xmiName value: aValueAttribute value -] - -{ #category : #visiting } -OPXMIWriterVisitor >> visitValueElement: aValueElement [ - "2b:XMIValueElement ::= ( ""<"" //xmiName// "">"" //value// -"""" ) -| ( """" )" - - "xsi:nil shouldBeImplement" - - writer tag: aValueElement xmiName with: aValueElement value -] - -{ #category : #accessing } -OPXMIWriterVisitor >> writer [ - ^ writer -] +Class { + #name : #OPXMIWriterVisitor, + #superclass : #Object, + #instVars : [ + 'writer' + ], + #category : 'OP-XMI-Writer' +} + +{ #category : #accessing } +OPXMIWriterVisitor >> contents [ + ^ writer contents +] + +{ #category : #initialization } +OPXMIWriterVisitor >> initialize [ + super initialize. + writer := XMLWriter new. + writer enablePrettyPrinting. + writer xml encoding: 'UTF-8' +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visit: anElement [ + anElement accept: self +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visitObjectElement: anObjectElement [ + "2a:XMIObjectElement ::= ( ""<"" 2k:QName 2d:XMIAttributes ""/>"" ) +| ( ""<"" 2k:QName 2d:XMIAttributes "">"" +(2:XMIElement)* +"""" )" + +"NOTE: I am not handling namespaces properly... thus I explicitly change XMI to xmi:XMI" + + writer + tag: + (anObjectElement xmiName = 'XMI' + ifTrue: [ 'xmi:XMI' ] + ifFalse: [ anObjectElement xmiName ]) + with: [ anObjectElement containedItems + select: #isAttribute + thenDo: [ :each | self visit: each ]. + anObjectElement containedItems select: #isElement thenDo: [ :each | self visit: each ] ] +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visitReferenceAttribute: aReferenceAttribute [ + "2j:XMIReferenceAttribute ::= //xmiName// ""=’"" ( //refId// )+ ""’" + + writer writeAttributeName: aReferenceAttribute xmiName value: ((aReferenceAttribute referencedElements collect: #xmiId) joinUsing: ' ') +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visitReferenceElement: aReferenceElement [ + " +2c:XMIReferenceElement::= ""<"" //xmiName// 2l:LinkAttribs ""/>"" +2l:LinkAttribs ::= ""xmi:idref=’"" //refId// ""’"" | 2m:Link +2m:Link ::= ""href='"" 2n:URIref ""'"" +2n:URIref ::= (2k:QName)? //URI reference// + +Additionally every modeling tool adds xmi:type information, even though the specs do not seem to allow it. +" + + writer + tag: aReferenceElement xmiName + with: [ aReferenceElement xmiType + ifNotNil: [ writer writeAttributeName: #xmi:type value: aReferenceElement xmiType ]. + aReferenceElement href + ifNil: + [ "idref" writer writeAttributeName: #xmi:idref value: aReferenceElement referencedElement xmiId ] + ifNotNil: [ "href" writer writeAttributeName: 'href' value: aReferenceElement href ] ] +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visitValueAttribute: aValueAttribute [ + "2i:XMIValueAttribute ::= //xmiName// ""=’"" value ""’""" + + writer writeAttributeName: aValueAttribute xmiName value: aValueAttribute value +] + +{ #category : #visiting } +OPXMIWriterVisitor >> visitValueElement: aValueElement [ + "2b:XMIValueElement ::= ( ""<"" //xmiName// "">"" //value// +"""" ) +| ( """" )" + + "xsi:nil shouldBeImplement" + + writer tag: aValueElement xmiName with: aValueElement value +] + +{ #category : #accessing } +OPXMIWriterVisitor >> writer [ + ^ writer +] diff --git a/repository/OP-XMI/OPXMIWriterVisitorTest.class.st b/repository/OP-XMI/OPXMIWriterVisitorTest.class.st index ca31251..c640091 100644 --- a/repository/OP-XMI/OPXMIWriterVisitorTest.class.st +++ b/repository/OP-XMI/OPXMIWriterVisitorTest.class.st @@ -1,121 +1,121 @@ -Class { - #name : #OPXMIWriterVisitorTest, - #superclass : #TestCase, - #instVars : [ - 'visitor' - ], - #category : 'OP-XMI-Tests' -} - -{ #category : #running } -OPXMIWriterVisitorTest >> setUp [ - super setUp. - visitor := OPXMIWriterVisitor new -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitObjectElement [ - visitor - visitObjectElement: - (OPXMIObjectElement new - xmiName: 'Object'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'ID'. - OPXMIValueElement new - xmiName: 'body'; - value: 'CONTENT'}). - self - assert: visitor contents - equals: - ' - - CONTENT -' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitReferenceAttribute [ - visitor writer - tag: 'outer' - with: [ visitor - visitReferenceAttribute: - (OPXMIReferenceAttribute new - xmiName: 'key'; - referencedElements: - {OPXMIObjectElement new - xmiName: 'whatever'; - containedItems: - {OPXMIValueAttribute new - xmiName: 'xmi:id'; - value: 'AAA'}}) ]. - self assert: visitor contents lines last equals: '' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitReferenceElement [ - visitor - visitReferenceElement: - (OPXMIReferenceElement new - xmiName: 'name'; - referencedElements: - {OPXMIObjectElement new - xmiName: 'whatever'; - containedItems: - {OPXMIValueElement new - xmiName: 'xmi:id'; - value: 'AAA'}}). - self assert: visitor contents lines last equals: '' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitReferenceElementHref [ - visitor - visitReferenceElement: - (OPXMIReferenceElement new - xmiName: 'name'; - href: 'http://UNIVERSE'). - self assert: visitor contents lines last equals: '' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitReferenceElementHrefType [ - visitor - visitReferenceElement: - (OPXMIReferenceElement new - xmiName: 'name'; - xmiType: 'uml:PrimitiveType'; - href: 'http://UNIVERSE'). - self - assert: visitor contents lines last - equals: '' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitValueAttribute [ - visitor writer - tag: 'outer' - with: [ visitor - visitValueAttribute: - (OPXMIValueAttribute new - xmiName: 'key'; - value: 'VALUE') ]. - self assert: visitor contents lines last equals: '' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitValueElement [ - visitor visitValueElement: (OPXMIValueElement new xmiName: 'name'; value: 'VALUE'). - self assert: visitor contents lines last equals: 'VALUE' -] - -{ #category : #tests } -OPXMIWriterVisitorTest >> testVisitXmiElement [ - visitor visitObjectElement: (OPXMIObjectElement new xmiName: 'XMI'). - self - assert: visitor contents - equals: - ' -' -] +Class { + #name : #OPXMIWriterVisitorTest, + #superclass : #TestCase, + #instVars : [ + 'visitor' + ], + #category : 'OP-XMI-Tests' +} + +{ #category : #running } +OPXMIWriterVisitorTest >> setUp [ + super setUp. + visitor := OPXMIWriterVisitor new +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitObjectElement [ + visitor + visitObjectElement: + (OPXMIObjectElement new + xmiName: 'Object'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'ID'. + OPXMIValueElement new + xmiName: 'body'; + value: 'CONTENT'}). + self + assert: visitor contents + equals: + ' + + CONTENT +' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitReferenceAttribute [ + visitor writer + tag: 'outer' + with: [ visitor + visitReferenceAttribute: + (OPXMIReferenceAttribute new + xmiName: 'key'; + referencedElements: + {OPXMIObjectElement new + xmiName: 'whatever'; + containedItems: + {OPXMIValueAttribute new + xmiName: 'xmi:id'; + value: 'AAA'}}) ]. + self assert: visitor contents lines last equals: '' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitReferenceElement [ + visitor + visitReferenceElement: + (OPXMIReferenceElement new + xmiName: 'name'; + referencedElements: + {OPXMIObjectElement new + xmiName: 'whatever'; + containedItems: + {OPXMIValueElement new + xmiName: 'xmi:id'; + value: 'AAA'}}). + self assert: visitor contents lines last equals: '' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitReferenceElementHref [ + visitor + visitReferenceElement: + (OPXMIReferenceElement new + xmiName: 'name'; + href: 'http://UNIVERSE'). + self assert: visitor contents lines last equals: '' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitReferenceElementHrefType [ + visitor + visitReferenceElement: + (OPXMIReferenceElement new + xmiName: 'name'; + xmiType: 'uml:PrimitiveType'; + href: 'http://UNIVERSE'). + self + assert: visitor contents lines last + equals: '' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitValueAttribute [ + visitor writer + tag: 'outer' + with: [ visitor + visitValueAttribute: + (OPXMIValueAttribute new + xmiName: 'key'; + value: 'VALUE') ]. + self assert: visitor contents lines last equals: '' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitValueElement [ + visitor visitValueElement: (OPXMIValueElement new xmiName: 'name'; value: 'VALUE'). + self assert: visitor contents lines last equals: 'VALUE' +] + +{ #category : #tests } +OPXMIWriterVisitorTest >> testVisitXmiElement [ + visitor visitObjectElement: (OPXMIObjectElement new xmiName: 'XMI'). + self + assert: visitor contents + equals: + ' +' +] diff --git a/repository/OP-XMI/package.st b/repository/OP-XMI/package.st index 4ac17b7..69d16c5 100644 --- a/repository/OP-XMI/package.st +++ b/repository/OP-XMI/package.st @@ -1 +1 @@ -Package { #name : #'OP-XMI' } +Package { #name : #'OP-XMI' }