Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created June 5, 2016 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jechlin/eea390104f609c4b9b011854007db62f to your computer and use it in GitHub Desktop.
Save jechlin/eea390104f609c4b9b011854007db62f to your computer and use it in GitHub Desktop.
package examples.docs.util
import java.util.regex.Pattern
// modify path to workflow to clean
def workflowIn = new File("/var/tmp/my-workflow.xml")
// and path of cleaned file to write
def workflowOut = new File("/var/tmp/cleaned-workflow.xml")
def parser = new XmlParser()
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
parser.setFeature("http://xml.org/sax/features/validation", false)
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
def xml = parser.parse(workflowIn)
String BASE_64_CANARY = "`!`"
String BASE_64_REGEX = Pattern.compile('^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$')
def decodeBase64EncodedString = { String str ->
if (str ==~ BASE_64_REGEX) {
def decoded = new String(str.decodeBase64())
if (decoded.startsWith(BASE_64_CANARY)) {
return decoded.substring(BASE_64_CANARY.size())
}
}
return str
}
xml.'**'.arg.each {
def encoded = it.value
it.value = decodeBase64EncodedString(it.text())
}
workflowOut.withWriter { w ->
w.write('''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE workflow PUBLIC "-//OpenSymphony Group//DTD OSWorkflow 2.8//EN" "http://www.opensymphony.com/osworkflow/workflow_2_8.dtd">
''')
def printer = new XmlNodePrinter(new PrintWriter(w))
printer.preserveWhitespace = true
printer.print(xml)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment