Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Automate the insertion of text into a file.

This example updates an xsl file with a new xsl:include declaration, and with a javascript header declaration. It reads 'guide.xsl' as a text file, and reads the template 'guide_ptemplate.xml' as a rexml document.

#!/usr/bin/ruby
#file: create add2guide_xsl.rb

require 'rexml/document'
include REXML

class Add2GuideTxt
  def initialize
    @guide = 'guide.xsl'
    @guide_template = 'guide_ptemplate.xml'
  end
  
  def insertText(project)
    # read guide.txt and return the buffer
    buffer = readGuide(@guide)    
    replaceBuffer(project, buffer, @guide) if not buffer.match("<xsl:include href='/xsl/#{project}.xsl'/>")
  end
  
  def replaceBuffer(project, buffer, output_file)
    # read guide_ptemplate.xsl and return the doc
    doc = readGuideTemplate(@guide_template)    
    # read the xsl:include
    xsl_include = buildIncludeTemplate(project, doc)
    # read the xsl:if
    xsl_if = buildIfTemplate(project,doc)
    
    eoi = "<!-- </xsl_includes> -->"
    buffer = buffer.gsub(eoi, "#{xsl_include}\n" + eoi)
    eoj = "<!--</xsl_javascript> -->"
    buffer = buffer.gsub(eoj, "\n#{xsl_if}\n" + eoj)
    
    file = File.new(output_file,'w')
    file.puts buffer.gsub("&quot;","'")
    file.close
  end
  
  def buildIncludeTemplate(project, doc)
    xsl_include = doc.root.elements['xsl:include']
    xsl_include.attributes['href'] = '/xsl/' + project + '.xsl'
    xsl_include
  end
  
  def buildIfTemplate(project, doc)
    xsl_if = doc.root.elements['xsl:if']
    xsl_if.attributes['test'] = project
    xsl_if.elements['script[3]'].text = 'const ProjectX = "' + project + '";'
    xsl_if
  end
  
  def readGuide(filename) 
    file = File.new(filename,'r')
    buffer = file.read
    file.close
    buffer
  end
  
  def readGuideTemplate(filename) 
    file = File.new(filename)
    Document.new(file)
  end
  
end

if __FILE__ == $0
  a2g = Add2GuideTxt.new
  #add the new project to the guide.xsl file  
  a2g.insertText('tasks')
end


file: guide.xsl (target file)
<!-- <xsl_includes> -->

<!-- <xsl_car_log.xsl/> -->
<xsl:include href="/xsl/car_log.xsl"/>
<!-- </xsl_includes> -->

...

<xsl:if test="car_logpage|car_log">
  <script type="text/javascript" src="/p/js/edit_recordx.js"></script>
  <script type="text/javascript" src="/p/js/itasks5.js"></script>
  <script type="text/javascript">const ProjectX = "car_log";</script>
</xsl:if>
<!--</xsl_javascript> -->

file: guide_ptemplate.xml (template used to insert blocks of code into guide.xsl)
<template>
  <xsl:include href="[x]"/>
  <xsl:if test="[x]">
    <script type="text/javascript" src="/p/js/edit_recordx.js"></script>
    <script type="text/javascript" src="/p/js/itasks5.js"></script>
    <script type="text/javascript">[x]</script>
  </xsl:if>
</template>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS