Skip to content

Instantly share code, notes, and snippets.

@ebot
Created November 17, 2011 14:09
Show Gist options
  • Save ebot/1373217 to your computer and use it in GitHub Desktop.
Save ebot/1373217 to your computer and use it in GitHub Desktop.
Format Statement Messages
<xsl:template name="p1">
<xsl:variable name="message" select="string(./messages/element[purpose='1']/text)"/>
<text><xsl:text>$message-p1$</xsl:text><xsl:value-of select="$message"/></text>
</xsl:template>
<xsl:template name="p2">
<xsl:variable name="message" select="string(./messages/element[purpose='2']/text)"/>
<text><xsl:text>$message-p2$</xsl:text><xsl:value-of select="$message"/></text>
</xsl:template>
<xsl:template name="p3">
<xsl:variable name="message" select="string(./messages/element[purpose='3']/text)"/>
<text><xsl:text>$message-p3$</xsl:text><xsl:value-of select="$message"/></text>
</xsl:template>
<xsl:template name="p15">
<xsl:variable name="message" select="string(./messages/element[purpose='15']/text)"/>
<text><xsl:text>$message-p15$</xsl:text><xsl:value-of select="$message"/></text>
</xsl:template>
#!/usr/bin/python
#
# Soarian Conversion Script
# Created By Ed Botzum
#
# This script replaces the UTF xml declaration with an IBM one and then
# runs the resulting xml through a series of xsl conversions. Finally,
# formats the plain text message statements.
import sys
import subprocess
print( 'Generating...' )
input_file = open( sys.argv[1], 'r' )
hospital = sys.argv[2]
region = sys.argv[3]
xsl_1 = sys.argv[4]
xsl_2 = sys.argv[5]
hosp_enc = hospital + "_" + region + "_encoded.xml"
hosp_xml = hospital + "_" + region + "_final.xml"
hosp_txt = hospital + "_" + region + ".txt"
hosp_rpt = hospital + "_" + region + ".smt"
# Encode the xml for the mainframe
output_file = open( hosp_enc, 'w' )
xml = input_file.read()
xml = xml.replace( 'utf-8', 'IBM-1047' )
xml = xml.replace( 'UTF-8', 'IBM-1047' )
output_file.write( xml )
input_file.close()
output_file.close()
# Run the encoded xml through some xsl conversions to convert the statements
# to plain text
subprocess.call( [ "java", "-cp",
"$CLASSPATH/xml.jar", "org.apache.xalan.xslt.Process",
"-in", hosp_enc, "-xsl", xsl_1, "-out", hosp_xml ] )
subprocess.call( [ "java", "-cp",
"$CLASSPATH/xml.jar", "org.apache.xalan.xslt.Process",
"-in", hosp_xml, "-xsl", xsl_2, "-out", hosp_txt,
"-param", "hospital", hospital, "-param", "region", region ] )
# Format the messages to finalize the report.
hosp_file = open( hosp_txt, 'r' )
hosp_stmt = open( hosp_rpt, 'w' )
statement = ''
message_lengths = { 'p1' : { 'width' : 68, 'length' : 2 },
'p2' : { 'width' : 121, 'length' : 4 },
'p3' : { 'width' : 121, 'length' : 4 },
'p5' : { 'width' : 121, 'length' : 7 },
'p6' : { 'width' : 121, 'length' : 7 },
'p8' : { 'width' : 121, 'length' : 4 },
'p9' : { 'width' : 121, 'length' : 4 },
'p10' : { 'width' : 63, 'length' : 3 },
'p11' : { 'width' : 121, 'length' : 5 },
'p11a' : { 'width' : 121, 'length' : 4 },
'p13' : { 'width' : 121, 'length' : 1 },
'p14' : { 'width' : 121, 'length' : 22 },
'p15' : { 'width' : 115, 'length' : 51 } }
for line in hosp_file:
if line.find( '$message-' ) >= 0:
line = line.rstrip().strip()
msg = line.split( '$' )[1].split( '-' )[1]
line = line.split( '$message-' + msg + '$' )[1]
line_width = message_lengths[msg]['width']
line_length = message_lengths[msg]['length']
words = line.split( ' ' )
line = ''
width = 0
for word in words:
if word.find( '\\line' ) >= 0:
word = word.replace( '\\line', '\n')
width += len( word ) # Don't include the space b/c there is a line feed
if width > line_width: # Add line feeds if this is a word with a \line
line += '\n '
width = len( word ) + 1
line += word
width = 0
word = ''
width += ( len( word ) + 1 )
if width > line_width:
line += '\n '
width = len( word ) + 1
line += word + ' '
# Add a space to the beginning of the reformatted line and remove the last
# space after the last word. Finally and a line feed unless it's empty.
line = ' ' + line.strip()
if len(line) != 0: line += '\n'
# Add lines based on the message level and lines used already
lines_to_add = line_length - ( len( line.split( '\n' ) ) - 1 )
if lines_to_add > 0:
for i in range( 1, lines_to_add + 1 ): line += ' \n'
elif lines_to_add < 0:
print( ' WARNING: Removing ' + str(-1 * lines_to_add) + ' line(s) from message level ' + msg + '.' )
sublines = line.split( '\n' )
line = ''
for i in range( 0, line_length ): line += sublines[i] + '\n'
statement += line
hosp_stmt.write( statement )
hosp_stmt.close()
hosp_file.close()
print( 'Done!' )
python soar_1-1_encode.py test.xml HH RR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment