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

About this user

James Robertson http://www.r0bertson.co.uk

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

Generate short URLs using Ruby

This Ruby code generates shortened URLs. It has the added feature that it will not allow words to be created accidentally eg. swear words.

#!/usr/bin/ruby

class ShortUrl
  def initialize()
    @chars = ('a'..'z').to_a + ('1'..'9').to_a + ('A'..'Z').to_a
    @array_size = @chars.size
    @h = Hash.new
    @chars.each {|c| @h[c] = '0'}
    vowels = %w(a e i o u A E I O U 4 3 1 0)
    vowels.each {|v| @h[v] = '1'}
    nums = %w(2 5 6 7 8 9)
    nums.each {|n| @h[n] = '2'}
    @count = 0
    @a = Array.new(7, -1)
    @k = 0
  end 

  def iterate_chars(array_size)
    (0..array_size).each {|i| 
      increment_index(@k)
      convert_to_chars()
    }
  end
  
  def convert_to_chars()
    buffer = ''
    @a.each {|i|
      buffer << @chars[i] if i >= 0
    }
    a = buffer.reverse.scan(/./)
    k = a.length  
    if  (k > 1)  
      if ((@h[a[k-2]] + @h[a[k-1]]) != '10')
        puts buffer.reverse 
      end
    else
      puts buffer.reverse
    end
  end

  def get_short_url(count)
    if count >  @array_size
      new_count = count - @array_size
      iterate_chars(@array_size)
      get_short_url(new_count)
    else
      iterate_chars(count)
    end
  end
  
  def increment_a(i)
    if @a[i] < @array_size - 1
      @a[i] = @a[i] + 1
      return i 
    else
      @a[i] = 0
      return i += 1
    end 
  end

  def increment_index(k)
    old_k = k
    k = increment_a(k)
    
    if k != old_k
      increment_index(k)
    else
      k = 0
    end
    k
  end

end

if __FILE__ == $0
  su = ShortUrl.new  
  su.get_short_url(222761)
end


Note: You will most likely see the " `convert_to_chars': stack level too deep (SystemStackError)" error message after around 253950 iterations, I'm presuming this is expected due to the memory constraints of the language. However 253k short urls is enough for linking my website to itself.

Generate an XSL file for a simple XML file

Using Ruby and XSLT this code creates an xsl file for a corresponding xml file. Note: It only reproduces xml elements in the xsl file, not element attributes.
#!/usr/bin/ruby
# file: xml2xsl.rb

# author: jrobertson
# created: 31-Dec-07
# description: 
#   Converts an xml file into another xml file in 'xml2xsl' format.
#   This new xml file can then be transformed with the stylesheet 
#   xml2xsl.xsl into a multi-purpose xsl file template for the original 
#   xml file.

 
require 'rexml/document'
include REXML

class PreXml2Xsl

  def pre_xml2xsl(doc)
    puts '<xsl>'
    puts "  <template name='" + doc.root.name + "'>"
    build_xml(doc.root)
    puts '    </template>'
    puts '</xsl>'
  end
  
  def build_xml(node)
    previous_name = ''
    node.each_child do |child| 
      if child.length > 1 
        build_template(child.name)
        build_xml(child) 
      else
        build_value(child.name) if child.name != previous_name
        previous_name = child.name
      end
    end
  end
      
  def build_template(element)
    puts '  ' + "<select name=\"#{element}\" />"
    puts "</template>"
    puts "<template name=\"#{element}\">"
  end

  def build_value(element)
    puts '  ' + "<value name=\"#{element}\" />"
  end

  if __FILE__ == $0
    xml_language = '<languages><programming><name>C++</name><name>C Sharp</name
><name>Ruby</name></programming></languages>'
    doc = Document.new(xml_language)
    px = PreXml2Xsl.new
    px.pre_xml2xsl(doc)
  end
end

file: xml2xsl.xml (output from xml2xsl.rb)
<xsl>
  <template name="languages">
    <select name="programming"/>
  </template>
  <template name="programming">
    <value name="name"/>
  </template>
</xsl>

file: xml2xsl.xsl (used with xml2xsl.xml)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="xsl">
  <xsl:variable name="colon"><xsl:text>:</xsl:text></xsl:variable>
    <xsl:element name="xsl:stylesheet">
      <xsl:attribute name="xmlns{$colon}xsl">
        <xsl:text>http://www.w3.org/1999/XSL/Transform</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="version">
        <xsl:text>1.0</xsl:text>
      </xsl:attribute>
      
      <xsl:apply-templates select="*" />
    
  </xsl:element>

  </xsl:template>
  
  <xsl:template match="template">
  <xsl:text>
  </xsl:text>
        <xsl:element name="xsl:template">
        <xsl:attribute name="match">
          <xsl:value-of select="@name"/>
        </xsl:attribute>

        <xsl:apply-templates select="*" />
  <xsl:text>
  </xsl:text>
    </xsl:element>
<xsl:text>
</xsl:text>

  </xsl:template>
  
  <xsl:template match="select">
    <xsl:text>
    </xsl:text>
  <xsl:element name="xsl:element">
    <xsl:attribute name="name">
      <xsl:value-of select="@name"/>
    </xsl:attribute>
    <xsl:text>
    </xsl:text>
    <xsl:text>  </xsl:text><xsl:element name="xsl:apply-templates">
      <xsl:attribute name="select">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:element>
    <xsl:text>
    </xsl:text>
  </xsl:element>


  </xsl:template>
  
  <xsl:template match="value">
    <xsl:text>
    </xsl:text>
  <xsl:element name="xsl:element">
    <xsl:attribute name="name">
      <xsl:value-of select="@name"/>
    </xsl:attribute>
      <xsl:text>
      </xsl:text>
    <xsl:element name="xsl:value-of">
      <xsl:attribute name="select">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:element>
    <xsl:text>
    </xsl:text>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

output from xml2xsl.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="languages">
    <xsl:element name="programming">
      <xsl:apply-templates select="programming"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="programming">
    <xsl:element name="name">
      <xsl:value-of select="name"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS