class PlantUmlEncode64

Encode the uml code into a string to provide for building the image with a remote provider.

This code is inspired by the page of plantuml website plantuml.sourceforge.net/codephp.html

Public Class Methods

append3bytes(b1, b2, b3) click to toggle source
# File lib/plantuml-encode64.rb, line 79
def self.append3bytes(b1, b2, b3)
    c1 = (b1 >> 2)
    c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
    c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
    c4 = b3 & 0x3F;
    r = encode6bit(c1 & 0x3F)
    r += encode6bit(c2 & 0x3F);
    r += encode6bit(c3 & 0x3F);
    return r + encode6bit(c4 & 0x3F);
end
encode64(input) click to toggle source
Internal : Encode is some special base 64.

 @param a deflate string

Returns a encoded string
# File lib/plantuml-encode64.rb, line 45
def self.encode64(input)
    len, i, out =  input.length, 0, "";
    while i < len do
        i1 = (i+1 < len) ? input[i+1].ord : 0;
        i2 = (i+2 < len) ? input[i+2].ord : 0;
        out += append3bytes(input[i].ord, i1, i2);
        i += 3;
    end
    return out
end
encode6bit(b) click to toggle source
# File lib/plantuml-encode64.rb, line 56
def self.encode6bit(b)
    if b < 10 then
    return (48 + b).chr;
    end

    b -= 10;
    if b < 26 then
    return (65 + b).chr;
    end

    b -= 26;
    if b < 26 then
    return (97 + b).chr;
    end

    b -= 26;
    if b == 0 then
        return '-';
    end

    return (b == 1) ? '_' : '?';
end
new(input) click to toggle source
# File lib/plantuml-encode64.rb, line 27
def initialize(input)
    @input = input;
end

Public Instance Methods

encode() click to toggle source

Public : proceed to the encoding for plantuml servlet

Returns the encoded uml to send to the servlet

# File lib/plantuml-encode64.rb, line 34
def encode()
    require 'zlib';
    o = @input.force_encoding("utf-8");
    o = Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(o, Zlib::FINISH)
    return PlantUmlEncode64.encode64(o);
end