Source

Writing Object to XML using XmlSerializer C# Source Code

How to write an object to a file with XML format? There is a class from .NET System.Xml.Serialization.XmlSerializer which can help to completely serialize the objects from program to a XML formatted files.

Suppose that we want to save the data of a person which has attributes:

  1. Name
  2. Age
  3. Height
  4. Weight

And the Person class:

public class Person
{
    public string name;
    public int age;
    public int height;
    public int weight;

    public Person() { }

    public Person(string name, int age, int h, int w)
    {
        this.name   = name;
        this.age    = age;
        this.height = h;
        this.weight = w;
    }
}

Read More →

Rail-Fence Cipher C# Source Code

The rail-fence cipher algorithm has been posted before so this post just show how to implement the algorithm to programming language in C#. First of all always the encryption method:

public static string Encrypt(int rail, string plainText)
{
    List<string> railFence = new List<string>();
    for (int i = 0; i < rail; i++)
    {
        railFence.Add("");
    }

    int number = 0;
    int increment = 1;
    foreach (char c in plainText)
    {
        if (number + increment == rail)
        {
            increment = -1;
        }
        else if (number + increment == -1)
        {
            increment = 1;
        }
        railFence[number] += c;
        number += increment;
    }

    string buffer = "";
    foreach (string s in railFence)
    {
        buffer += s;
    }
    return buffer;
}

Read More →

NHibernate for MySQL C# Source Code

Now we’re going to make a database connection using ORM Framework that was discussed before, NHibernate.

What the requirement of using NHibernate for our projects? First, add some reference like i’ve explained on previous post (Using NHibernate ORM for .NET Framework), NHibernate.dll, NHibernate.ByteCode.LinFu.dll, and MySql.Data.dll

Second step, create the app.config file and this step also have been explained on my previous post. And for this project the app.config content is :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">server=127.0.0.1;User Id=nhibernate;password=nhibernate;Persist Security Info=True;database=nhibernate</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <mapping assembly="SimpleNHibernate"/>
    </session-factory>
  </hibernate-configuration>
</configuration>

Then the most important step, creating a persistant class that representing the database table. Suppose my database just have 1 table, user and the structure like this :

NHibernate MySQL Structure

namespace SimpleNHibernate
{
    class User
    {
        private int userId;
        private string userName;

        public User() { }

        public virtual int UserId
        {
            set { this.userId = value; }
            get { return this.userId; }
        }

        public virtual string UserName
        {
            set { this.userName = value; }
            get { return this.userName; }
        }

    }
}

The important thing to remember is make the property “public virtual” because that NHibernate need

And the second important step, create a mapping xml which tells NHibernate how to make relation between persistent objects with relational database table. Now, create a xml file named user.hbm.xml then change the Build Action propery to “Embedded Resource” and the content like this :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SimpleNHibernate" assembly="SimpleNHibernate">
  <class name="User" table="user">
    <id name="UserId">
      <column name="user_id" sql-type="int(32)" not-null="true" />
      <generator class="increment" />
    </id>
    <property name="UserName">
      <column name="user_name" sql-type="varchar(128)" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

For primary key we use <id> and the other column we use <property>. And inside those tag there is <column> tag that’s the mapping for the <id> or <property> attribute from persistant class to the databasae column. And for auto_increment function from MySQL Database use <generator class=”increment” /> on the id.

Okay the persistant class and it’s mapping done now, next step is creating a helper class or connector class to make our job easier. This step is optional if you’re prefer to use NHibernate directly and repeat some code. And here’s my code for creating the helper class.

using NHibernate;
using NHibernate.Cfg;

namespace SimpleNHibernate
{
    class NHibernateConnector
    {
        private ISessionFactory sessionFactory;
        private ITransaction transaction;
        private ISession session;

        public NHibernateConnector()
        {
            this.sessionFactory = new Configuration().Configure().BuildSessionFactory();
        }

        public void OpenConnection()
        {
            this.session = sessionFactory.OpenSession();
            this.transaction = this.session.BeginTransaction();
        }

        public void CloseConnection()
        {
            this.session.Close();
        }

        public void Commit()
        {
            this.transaction.Commit();
        }

        public void Rollback()
        {
            this.transaction.Rollback();
        }

        public IQuery CreateQuery(string hql)
        {
            return this.session.CreateQuery(hql);
        }

        public ISession Session
        {
            get { return this.session; }
        }

        public ITransaction Transaction
        {
            get { return this.transaction; }
        }

    }
}

The preparation step is done.. now look at the example for inserting data to the MySQL DBMS using NHibernate Framework.

namespace SimpleNHibernate
{
    class Program
    {
        static void Main(string[] args)
        {
            NHibernateConnector conn = new NHibernateConnector();
            conn.OpenConnection();

            User imKrisna = new User();
            imKrisna.UserName = "I Made Krisna Widhiastra";

            conn.Session.Save(imKrisna);

            conn.Commit();
            conn.CloseConnection();
        }
    }
}

CAM, TS, SCR, DVDRip, etc Video Types

Hi, when i’m going to choose some videos to download, there is some label that shown in various files, such as “CAM”, “TS”, “SCR”, and “DVDRip”. What is it? Does it mean something? I’m curious about that and start searching about this topic. Finally i found it! that’s the type of videos from what or how the videos created. Let me expain about those types with my own word from combining from many sources and references.

  1. CAM
    CAM is a rip from digital video camera. Usually the videos was recorded on movie theatres with tripod or something but because that’s recording on movie theatre, there is a very small chance the record is clean. It may come with camera shake, shouting people or laughing, and a someone’s shadow blocking the camera. The quality also very low the audio isn’t clear because of much other sounds and the video is dark because recorded on movie theatre which is on dark condition when the movie playing.
  2. TS or TELESYNC
    TELESYNC is slight better than CAM, TS also use digital camera for recording the video or movies but the TS video commonly use other audio source, it maybe directly taken from the original output so TS will be better at audio quality than CAM because there are smaller chance to get audio noise.
  3. TC or TELECINE
    TELECINE is a rare method. TC will copy directly from movie reels and make it to video format. This form of video is rarely found when we want to download some video.
  4. SCR or SCREENER
    SCREENER is generated from the movie which was sent to someone or somewhere in order to get review or promotional reason. On SCREENER videos, we may find a running text or some text that prohibit the copying of this video or showing the copyright of the video. SCREENER usually transferred to VCD form or SVCD for better result.
  5. DVDscr or DVD-SCREENER
    Same as SCREENER but DVDscr is transferred to DVD form.
  6. DVDRip
    DVDRip is getting from the original release of DVD so the quality of the result is quite good. The original release of DVD is ripped by some software and transferred to video form and stored to hard disk. DVDRip also can be found with DivX or XviD name.

Source and References :

  1. http://uneasysilence.com/archive/2006/08/7331/
  2. http://en.wikipedia.org/wiki/Cam_(bootleg)
  3. http://en.wikipedia.org/wiki/Telesync
  4. http://en.wikipedia.org/wiki/Telecine
  5. http://en.wikipedia.org/wiki/Screener
  6. http://en.wikipedia.org/wiki/DVDRip

Binary Search Tree Java Source Code

Binary Search Tree is a kind of Binary Tree. Let’s start with Tree. Tree is a data structure model which looks like a reversed tree, the root placed on top of the tree and the leaf placed on the bottom of the tree. The model will be looks like pyramid shape. Binary Tree is a tree which every leaves / nodes just have maximum two child leaves, that’s why we call it binary (base 2). Binary Search Tree… this kind of binary tree that have the position of leaves “sorted”. All of elements on the left of a leaf must be smaller than the leaf and all of elements on the right of a leaf must be bigger than the leaf. What about same value? that’s up to you to place it where.

Let’s look this example from wikipedia, “F” is the root of the tree. “B” is a left child of “F”. “G” is a right child of “F”. “B” is a parent of “A”. “B” also a parent of “D”.

Binary Search Tree

Creating Binary Search Tree can’t use the Node which is used for Linked List, Stack, and Queue. Honestly, the Node can be used but the context will be different because the pointer to other Node we will named child. I also give additional pointer, “parent” for pointing the parent of the Node. Let’s see the implementation on Java Programming Language.

public class BinaryTreeNode {

    public BinaryTreeNode parent;
    public BinaryTreeNode leftChild;
    public BinaryTreeNode rightChild;

    private String info;

    public BinaryTreeNode(String info){
        this.parent = null;
        this.leftChild = null;
        this.rightChild = null;
        this.info = info;
    }

    public String getInfo(){
        return this.info;
    }

    public void setInfo(String info){
        this.info = info;
    }

}

Then after we have the Nodes we can construct the Binary Search Tree. Which the most important method is inserting Nodes. If the tree root is null (the tree is new) we just make the node as root. But if the tree is not empty, we must track or find the right place of that node. The concepts is to find an empty place that meet the rules of binary search tree.

  1. If the value of the new node less than current node
    1. If the left child of current node is not empty, move current node to left child
    2. else, the left child of current node is new node
  2. If the value of the new node equal or more than current node
    1. If the right child of current node is not empty, move the current node to right child
    2. else, the right child of current node is new node
public class BinarySearchTree {

    private BinaryTreeNode root;

    public BinarySearchTree(){
        this.root = null;
    }

    public void insertNode(BinaryTreeNode node){
        if (this.root == null){
            this.root = node;
        }
        else{
            trackPosition(node, this.root);
        }
    }

    private void trackPosition(BinaryTreeNode node, BinaryTreeNode start){
        String sInfo = start.getInfo();
        if (sInfo.compareTo(node.getInfo()) > 0){
            if (start.leftChild == null){
                start.leftChild = node;
                node.parent = start;
            }
            else{
                trackPosition(node, start.leftChild);
            }
        }
        else{
            if (start.rightChild == null){
                start.rightChild = node;
                node.parent = start;
            }
            else{
                trackPosition(node, start.rightChild);
            }
        }
    }

}