Skip navigation


Adding an XSLT Action

Its sometimes useful to apply an XSLT transform on a message, for example during development and testing. If you select one or more TextMessages from a queue or topic, this action lets you select a stylesheet from the filesystem and then will perform that transform on the contents of each message. Finally, it will place those messages a back on the clipboard so you can paste the messages onto another queue or topic.

from hermes.browser import HermesBrowser
from hermes.browser import IconCache

from javax.xml.transform import TransformerFactory
from javax.xml.transform.stream import StreamSource
from javax.xml.transform.stream import StreamResult

from java.io import File
from java.io import StringWriter
from java.io import StringReader

from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
from java.awt.event import ActionEvent

from javax.swing import JFileChooser
from javax.swing import Action
from javax.swing import AbstractAction

from java.lang import System
from java.lang import Throwable

from java.util import ArrayList

browser = HermesBrowser.getBrowser() 

class XSLAction(AbstractAction):
	def __init__(self, browser, xsl):		
		self.putValue(Action.NAME, "XSL/T")
		self.putValue(Action.SHORT_DESCRIPTION, "Perform transform on the selected message(s)")
		self.putValue(Action.SMALL_ICON, IconCache.getIcon("python"))
		self.browser = browser
		self.xsl = xsl 
		self.lastDirectory = File(System.getProperty("user.home"))	
			
	def actionPerformed(self, event):
		numMessages = browser.getSelectedMessages().size()
		if numMessages == 0:
			browser.showInformationDialog("No messages selected")
		else:
			try:				
				if self.xsl == None:
					chooser = JFileChooser(self.lastDirectory)
					if chooser.showDialog(browser, "Open XSL/T document") == JFileChooser.APPROVE_OPTION:
						self.lastDirectory = chooser.getSelectedFile().getParentFile()
						transformer = TransformerFactory.newInstance().newTransformer(StreamSource(chooser.getSelectedFile()))
					else:						
						return
	
				else:
					transformer = TransformerFactory.newInstance().newTransformer(StreamSource(xsl))
				
				rval = ArrayList()
 
				for m in browser.getSelectedMessages():
					hermes = browser.getBrowserTree().getSelectedHermesNode().getHermes()
					transformed = StringWriter()
					transformer.transform(StreamSource(StringReader(m.getText())), StreamResult(transformed))	
					mOut = hermes.createTextMessage(transformed.toString())
					rval.add(mOut)
			
					Toolkit.getDefaultToolkit().getSystemClipboard().setContents(StringSelection(hermes.toXML(rval)), None)

					if (rval.size() > 1):
						browser.showInformationDialog("Transformed messages are on the clipboard") ;	
					else:
						browser.showInformationDialog("Transformed message is on the clipboard") ;	

			except (Throwable), e:
				browser.showInformationDialog(e.getMessage())


commandBar = browser.getDockableBarManager().getDockableBar("XML")

if commandBar == None:
	commandBar = browser.createDockableBar("XML")
	browser.getDockableBarManager().addDockableBar(commandBar)

commandBar.add(XSLAction(browser, None))

Adaptavist Theme Builder Powered by Atlassian Confluence