Skip to content

Instantly share code, notes, and snippets.

@lonniev
Created January 3, 2025 17:16
Show Gist options
  • Save lonniev/4a3a9d197ca8034fd372484709ee6767 to your computer and use it in GitHub Desktop.
Save lonniev/4a3a9d197ca8034fd372484709ee6767 to your computer and use it in GitHub Desktop.
A simple groovy script for MagicDraw StructuredExpressions that will replace any HTML Text tag value with suitable Plaintext
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype
import com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper
import com.nomagic.magicdraw.core.Application
import com.nomagic.magicdraw.openapi.uml.SessionManager
def elided = { str ->
if (str.length() > 20) {
return str[0..9] + '...' + str[-10..-1]
}
return str
}
def decodeHtmlEntities = { text ->
return text.replaceAll(/&#(\d+);/) { match, code ->
int charCode = code.toInteger()
return (charCode in 32..126) ? (char) charCode : ' '
}
}
def element = arg1
// Start a session to be able to modify the model
SessionManager.getInstance().createSession("Set Text to plaintext")
try {
List<Stereotype> appliedStereotypes = StereotypesHelper.getStereotypes(element)
for (Stereotype stereotype : appliedStereotypes) {
def tagValue = StereotypesHelper.getTaggedValue(element, stereotype, 'Text')
if (tagValue != null) {
plaintext = decodeHtmlEntities( tagValue.getValue().get(0).replaceAll(/<[^>]*>/, '') ).trim()
StereotypesHelper.setStereotypePropertyValue(element, stereotype, 'Text', plaintext )
Application.getInstance().getGUILog().log("Updated 'Text' tag value to ${elided(plaintext)}'.")
// leave after changing the first Text tag
return plaintext
}
}
}
finally
{
SessionManager.getInstance().closeSession()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment