Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Paolo F Cantoni on August 31, 2020, 04:54:08 pm
-
I recently asked about Base64 functionality for scripting. Eve pointed me in a specific direction. I know I could spend a ton of time experimenting (and probably getting frustrated) but I'm somewhat "under the pump" and to save myself time and energy I thought I'd ask first.
Can anyone confirm that the API is NOT smart enough to figure out that if I send a text (shapescript) as an attribute.Default for an attribute named _image EA will automatically save it as a Base64 encoded XML value?
I presume I'd have to create the XML Base64 encoded string first from the shapescript text itself and then set the attribute.Default and attribute.Update?
Does anybody have a code snippet they're willing to share that does this? Any language...
TIA,
Paolo
-
The API takes whatever it is. You need to supply some
<Image type="EAShapeScript 1.0" xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64">base64 suff==</Image>in order to make it recognize a shape script. Else it will be just some string representing the default (yes, broken EA design as usual; and pretty pointless to zip something in order to store it bloated as base64 right after that). Since I'm working with Python on these I can't tell anything about that VB stuff.
q.
-
The API takes whatever it is. You need to supply some <Image type="EAShapeScript 1.0" xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64">base64 suff==</Image>in order to make it recognize a shape script. Else it will be just some string representing the default (yes, broken EA design as usual; and pretty pointless to zip something in order to store it bloated as base64 right after that). Since I'm working with Python on these I can't tell anything about that VB stuff.
q.
That's what I thought... But just to be clear. I need to take the "raw" Shapescript text, zip it up and convert the zipped string into Base64. Yes?
Paolo
-
A total bummer but yes.
q.
-
I've only ever done it the other way, but here the code in VBScript used by this script to extract shapescripts from MDG xml files
https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Tools/Script%20Management/ExtractShapescriptsFromMDG.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Tools/Script%20Management/ExtractShapescriptsFromMDG.vbs)
'returns the string encoded int he base64 zipped contents of the given xml string
public function decodeBase64zippedXML(xmlString,nodeName)
Dim xDoc
Set xDoc = CreateObject( "MSXML2.DOMDocument" )
decodeBase64zippedXML = ""
'get the stereotype
'load the resultset in the xml document
If xDoc.LoadXML(xmlString) Then
dim contentsNode
set contentsNode = xDoc.SelectSingleNode("//" & nodeName)
if not contentsNode is nothing then
dim contentsDecoded
contentsDecoded = contentsNode.nodeTypedValue
'save as temp zip file
dim tempZipFile
set tempZipFile = new BinaryFile
tempZipFile.FullPath = replace(getTempFilename, ".tmp",".zip")
tempZipFile.Contents = contentsDecoded
tempZipFile.Save
'unzip
dim tempFolderPath
tempfolderPath = unzip(tempZipFile.FullPath)
'get the text file
dim tempFolder
set tempFolder = new FileSystemFolder
tempFolder.FullPath = tempfolderPath
dim contentsFile
For each contentsFile in tempfolder.TextFiles
decodeBase64zippedXML = contentsFile.Contents
'there should be only one file
exit for
next
'delete the temp folder and temp file name
tempfolder.Delete
tempZipFile.Delete
end if
end if
end function
Geert
-
Thanks, guys,
I'll have a play and see what's the best way forward for what we are trying to do.
Paolo
-
So I spent ALL day trying to get this to work! It shouldn't be this hard, BUT not all the issues are Sparx related.
For various reasons, I opted to use external mechanisms to create the Zip and Base64 encoded files. For those that are interested, I used the Windows native "send to compressed file" to zip things up and UltraStudio to perform the Base64 encoding/decoding.
Because it was external, I could investigate issues at each stage. I was first able to round-trip an external shapescript file through zipping, Base64 encoding, decoding then unzipping. But for the life of me, I could NOT get EA to open the _image.Default value as a shapescript in the shapescript editor.
After many hours investigation, I "tumbled" that what EA was generating (as _image.Default) and I was inputting were NOT the same character encoding.
EA was generating a str.dat which was UTF-16 and I WASNT! This may have been because the shapescript text files have been created on various machines at various times. Anyway, once I made the initial shapescript text file UTF-16, I could Zip it up, encode it and (eventually) insert it (correctly XML enveloped) into the _image.Default and EA would correct recognise it as an encoded shapescript!
I now have a reproducible process; which I need to automate to update all my shapescripts in an orderly manner.
I provide this explanation in case someone else is silly enough to attempt what I'm doing!!!
Paolo