Book a Demo

Author Topic: API method to store Base64 encoded shapescripts in repository.  (Read 4328 times)

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
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
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #1 on: August 31, 2020, 05:13:47 pm »
The API takes whatever it is. You need to supply some
Code: [Select]
<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.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #2 on: August 31, 2020, 05:20:42 pm »
The API takes whatever it is. You need to supply some
Code: [Select]
<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

Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #3 on: August 31, 2020, 05:34:14 pm »
A total bummer but yes.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #4 on: August 31, 2020, 06:13:49 pm »
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

Code: [Select]
'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

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #5 on: August 31, 2020, 09:22:25 pm »
Thanks, guys,

I'll have a play and see what's the best way forward for what we are trying to do.

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: API method to store Base64 encoded shapescripts in repository.
« Reply #6 on: September 01, 2020, 08:03:53 pm »
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
« Last Edit: September 01, 2020, 08:07:28 pm by Paolo F Cantoni »
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!