2015年8月18日 星期二

字元編碼 Unicode


1.  編碼最早是  ASCII 有 1 byte , 8 bits

2. 微軟發展出 Big 5 支援其他語系, 有 2 bytes, 16bits
    缺點是:  a. 有些文字是 2 bytes, 但此時英文語系還是 1 byte
                 b. 固定每個國家不同
                 c. 會有 許功蓋 問題, 因為跳脫字元


3.  Unicode 出現
    a.  支援所有國家的字元, 用 2 bytes 表示所有字元
    b.  是一個 Table 來存所有國家字元


4. UTF8
    a. 是個編碼方法,為了動態決定 Unicode 長度
    b. 在記憶體的字元是 Unicode, 但寫到檔案, 網路傳輸 將字元編碼成 UTF8
    c. 在讀取檔案時, 指定 UTF8 才會解碼 UTF8 到 Unicode string

2015年8月12日 星期三

Eclipse install Python PyDev



1. Eclipse
Eclipse toolbar -> help -> install new software

2. Install pydev
work with type: https://dl.bintray.com/fabioz/pydev/all
search: pydev


3. Setting python version
Eclipse toolbar -> Windows -> preference -> PyDev -> Python Interpretor



Reference:
Chinese python-eclipsepydev
Official PyDev


python 3 source install On Centos

1. Download tar source
 https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz

2.  Untar
tar -xzf   Python-3.4.3.tgz

3. Compiler
./configure --prefix=/home/Andy/IBMS/Software/python/Python-3.4.1/bin
make
make test
make install

2015年8月3日 星期一

Raspberry pi NOOBS install on linux

Main -> http://qdosmsq.dunbar-it.co.uk/blog/2013/06/noobs-for-raspberry-pi/
https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained
https://github.com/raspberrypi/noobs
http://blogger.gtwang.org/2014/12/raspberry-pi-b-plus-noobs-linux-installation.html

# unmount the sdcard
umount /media/3466-6661/

# check sd card
fdisk -l

# format sdcard
fdisk /dev/sdc
// detail
----------
# delete them using the ‘d’ command repeat until you have deleted them all.
Command (m for help): d
Selected partition 1

# Listing the existing partitions afterwards should show the following:
Command (m for help): p

# create a single new partition
Command (m for help): n
Select (default p): p
Partition number (1-4, default 1): 1
First sector (1-15523839, default 2048): 1
Last sector (1-15523839, default 2048): 15523839

# The partition type is FAT32
Command (m for help): t
Hex code (type L to list codes): b

# Print content
Command (m for help): p
Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1936    15550888+   b  W95 FAT32


# All that remains now is to write the new partition table to the SD card
Command (m for help): w
The partition table has been altered!

------

# finally format the particial
mkfs.vfat /dev/sdc1



# check noobs boot integraty
>sha1sum NOOBS_v1_4_1.zip
279cdeb50861d2ef2681b4d1f5e98c40581f48b1  NOOBS_v1_4_1.zip



# mount the sdcadrd
mount -t vfat  /dev/sdc1 /media/pi


# cp the boot file
cd /media/pi/
unzip /home/Andy/IBMS/Software/pi/NOOBS_v1_4_1.zip


# exit
cd ~
umount /media/pi

2015年8月2日 星期日

First For Backbone.js of simple hello world


Backbone.js provide the MVC pattern for web development

1. HTML code

  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
  </head>
  <body>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>

  <script src="1.js"></script>


  </body>
  </html>


2. JS code
// This example illustrates the declaration and instantiation of a minimalist View.
 (function($){
          var ListView = Backbone.View.extend({
//                 el: $('body'), // attaches `this.el` to an existing element.
                 el: 'body', // attaches `this.el` to an existing element.

                initialize: function(){
                        _.bindAll(this, 'render'); // fixes loss of context for 'this' within methods
                       this.render(); // not all views are self-rendering. This one is.
                },
                render: function(){
                      $(this.el).append("<ul> <li> cool hello world</li> </ul>");
                }
          });
         var listView = new ListView();
})(jQuery);


Reference
1. hello-backbonejs_example
2. tutorials_eample
3. backbonejs_doc

2015年7月21日 星期二

Apache shiro ldap multiple OU

Problem:

Shiro provide the LDAP access, but the accessing links is only complete CN.
such as
ldapRealm.userDnTemplate = cn={0},ou=eee,dc=gp,dc=mycompany

If you want to access the different ou such as
ldapRealm.userDnTemplate = cn={0},ou=eee,dc=gp,dc=mycompany
ldapRealm.userDnTemplate = cn={0},ou=aaa,dc=gp,dc=mycompany

It will use the ou=aaa, how to I access the two ou in same ldap content ?


Solution:

I define the access multiple ou format in shiro and inheritance the JndiLdapRealm

a. Shiro format  in shiro.ini
  - Define handle class
     >  ldapRealm = package.LdapMultiOU
  - Using the piple as sepearte
     > ldapRealm.userDnTemplate = cn={0},[cn=aaa|ou=eee],dc=gp,dc=mycompany


b.  The code

public class LdapMultiOU extends JndiLdapRealm {

protected AuthenticationInfo queryForAuthenticationInfo(
AuthenticationToken token, LdapContextFactory ldapContextFactory)
throws NamingException {

Object principal = token.getPrincipal();
Object credentials = token.getCredentials();

// coding
AuthenticationInfo info = null;
NamingException e = null;

principal = getLdapPrincipal(token);
System.out.println("Before ldap cn was " + principal);
String[] CNs= principal.toString().split(",");
StringTokenizer OUs = new StringTokenizer(CNs[1].replaceAll("(\\[|\\])", ""), "|"); 
while (OUs.hasMoreTokens()) {
         principal = CNs[0] + "," + OUs.nextToken() + "," +  CNs[2] + "," + CNs[3];
System.out.println("After ldap cn was " + principal);
       LdapContext ctx = null;
    try {
ctx = ldapContextFactory.getLdapContext(principal, credentials);
// context was opened successfully, which means their credentials
// were valid. Return the AuthenticationInfo:
info = createAuthenticationInfo(token, principal, credentials, ctx);
} catch (NamingException eNam) {
e = eNam;
    } finally {
LdapUtils.closeContext(ctx);
}
    }

if (info != null)  return info;
else throw e;
}
}


Reference:
http://stackoverflow.com/questions/9273631/apache-shiro-ldap-multiple-ous
http://blog.stratio.com/supporting-service-based-multi-realm-authentication-and-authorization/

2015年7月19日 星期日

Javascript Regular Expression

Description

Javascript provides two  regular function.

exec():
- Goal: This method will testing the string and return the match pattern into array.
- Example:
var  tabName = /#(.*)/g.exec(window.location.href)[1];

test():
- Goal: only testing the string is having the pattern and return true or false




Reference:
Chinese introduction
More detail explanation





2015年7月8日 星期三

Jquery event handler

1. You can define your own event using trigger() method

 $(document).trigger('reveal.facebox').trigger('afterReveal.facebox')



2. Then to receive the event by bind() or on() method

 $(document).bind('reveal.facebox', function() {

}

2015年5月18日 星期一

Multiple CSS and jquery

Problem Description

How does the meaning in class="errorMessage active"
HTML Code
<span class="errorMessage active">Invalid</span>


CSS Code and why the .active after the .errorMesage
.errorMessage{
display: none;
color: red;
margin-left: 10px;
}
.errorMessage.active {
display: inline-block;
border: 1px solid #e6e6e6;
padding: 0 5px;
background: #ffdae0;
border-radius: 3px;
}

Jquery code
$('.IncludeSnRNA').on('change', function(){
    if($(this).is(":checked")){
    $('.errorMessage').addClass("active");
    $('.IncludeSnRNA').val('t');
    }else {
    $('.errorMessage').removeClass("active");
    $('.IncludeSnRNA').val('f');
    }
    });



Solution

Target an element that has all of multiple classes
and means the active class of errorMessage class is use as constraint to that the subject is errorMessage

2015年5月4日 星期一

Jquery Deffer method

Introduction Basic content

Ajax: Asynchronous Java script and XML
JSON: Using string representation for an object 
jqXHR object:  is jquery XML HTTP object which represent as $.ajax
JSONP: Give a strategy to access cross domain data
 
                    Subject of Deffer function
Goal: 
It will let programmer make a asyn-like calling mechanism
and often use in nested ajax calling to avoid to nested content.

Event trigger:
In the process, declare the deffer start .deffer().
Then, you should write deffer.resolve() mean it success, or deffer.reject() mean fail in handle process.
Finally, you return it by .promise();

Event Handle:
Client code can handle it by .then(callback1, [callback2]) method after it use the Event trigger method,


Ref:
ajax-deferreds tutorial
jquery deferred-object function
jquery-deferred-object chinese








2015年4月24日 星期五

JAVA script template Using Handlebars.js


1. What is Handlebars.js ?
It is using javascript to process the MVC pattern in view process.

You can Learn start from getting-started-with-handlebars-js
Official site api from Official sites
code From git

2015年4月12日 星期日

Abstract How browser work

This is a abstract content from how browsers work


Main structure

User interface 
   this includes the address bar, back/forward button, bookmarking menu etc.
 
Browser engine
   the interface for querying and manipulating the rendering engine.
 
Rendering engine
responsible for displaying the requested content. For example if the requested content is HTML, it is responsible for parsing the HTML and CSS and displaying the parsed content on the screen.

Networking
 used for network calls, like HTTP requests. It has platform independent interface and underneath implementations for each platform.

UI backend 
 used for drawing basic widgets like combo boxes and windows. It exposes a generic interface that is not platform specific. Underneath it uses the operating system user interface methods.

JavaScript interpreter. 
Used to parse and execute the JavaScript code.

Data storage.
his is a persistence layer. The browser needs to save all sorts of data on the hard disk, for examples, cookies.


The main flow


I Parse STEP

1. Parse HTML  to DOM tree

2. Parse CSS to css tree

3. Parse javascript.
  • javascript will execute when it encounter and this the html may be not all in standby.

II Build the Render tree

A render tree will combine the DOM NODE and CSS attribution

III  Layout 

Layout is output content the width and height in the screen, and show the color etc. The viewport mean the browser default position









2015年4月8日 星期三

V8 and NodeJS

1. What are them?

V8 is such as a interpreter and a platform that run javascript code and glue the C++ .

Node.js is framework that provides  the basic API such the buffer. It will be extend to a lot of modules such as network, server and etc.

2. Introduction of V8
Feature:

  • V8 is Google's open source JavaScript engine.
  • V8 implements ECMAScript as specified in ECMA-262.
  • V8 is written in C++ and is used in Google Chrome, the open source browser from Google.
  • V8 can run standalone, or can be embedded into any C++ application.
Download:
      You can download the code from git v8-git-mirror

How-does-a-JavaScript-engine-work:

V8 Component:
  • JS object and compiler
  • Share library
  • Runtime enviroment


3. Introduction of Node.JS
Node js provides many programming API and you can write the js on server side.
Following is the API snip from official site and you cand



















4. Advance topic about Custom your API
This article teach how to design a API using V8 API how-to-roll-out-your-own-javascript-api-with/



REF:
V8 build
How V8 work
google v8

2015年4月6日 星期一

Javascript loop

JavaScript supports different kinds of loops:
  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object


the for/in used in self defined property;

var person = {fname:"John", lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
    text += person[x];
}

Javascript function

1. What is it? 


It has Function Expression

//anonymous function expression
var a = function() {
    return 3;
}
a();
 
//self invoking function expression
(function sayHello() {
    alert("hello!");
})();
 


and  Function Declaration
function bar() {
    return 3;
}
 
 
2.  JavaScript Closures
 JavaScript variables can belong to the local or global scope.Private variables can be made possible with closures.
// Inner class
 function add() {
    var counter = 0;
    function plus() {counter += 1;}
    plus();   
    return counter;
}





REF
 W3C 
IF HEMINGWAY WROTE JAVASCrIPT

Javascript - Let description


1. What is it?

The let statement declares a block scope local variable, optionally initializing it to a value. and use in ECMA 5.

2. How to use it



if (x > y) {
  let gamma = 12.7 + y;
  i = gamma * x;
}


3. Compare

I: Local variable
Variables declared within a JavaScript function,
become LOCAL to the function.

Local variables have local scope: They can only be accessed within the function
// Example 



II: Global variable
A variable declared outside a function, becomes GLOBAL

// Example 
var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!'




 REF
MSDN



2015年3月20日 星期五

source code block using css

I found two tools to beautiful the code

A. using javascript SyntaxHighlighter
B. online web change source beautiful

java 8 lambda


1.  What is lambda ?
 * lambda is like a mathematics function f(x) = x +2, but it doesn't the name.
 * lambda in java is called function interface
 * lambda is anonymous interface and only one function

2. Lambda style

(input) -> {body}

3. example

Runnable runnbale = new Runnable() {
     public void run() {
      System.out.println("run me!");
     }
 }; 
 
 




Runnable runnbale = () -> System.out.println("run me!"); 
 
 
 
REF
Chinese: http://magiclen.org/java-8-lambda/
English more detail: more detail 
Explanation visual: visual
oracle function interface
oracle comsumer face





2015年3月9日 星期一

Authentication vs. Authorization

Authentication mean 


  • Who is user ?
  • And his password is hser?





Authorization mean

  • Is the user can get the file?
  • Is the user have write power?

2015年2月24日 星期二

LOG4j Simple start and slf4j start


Log4j 

1. Log4j setting explaination Good ExplainationOverview2

2. Example code Simple Example


For Setting in tomcat discussion
Stackup
apache office
Discussion2
Non-web

Slf4j Start

1. Overview ,  overview 2
2. It need the dependence of
  • slf4j-api-1.7.2.jar
  • slf4j-simple
  • logback-classic
  • logback-core
3.If you using log4j in the after you should setting in run command
-Dlog4j.configuration="/home/Andy/workspace/ShiroSource/WebContent/WEB-INF/log4j.properties"





Complete the slf4j and log4j in apache shiro

Summary File



From here

a. Library using 



b.  log4j.properties file  and put in the WEB-INF/






c. Setting the web.xml


d. Create web context listen at web start to load property files






2015年2月23日 星期一

Eclipse Add javax library




Using Eclipse:
  1. Right click your project folder, select Properties at the bottom of the context menu.
  2. Select "Java Build Path"
  3. Click Libraries" tab
  4. Click "Add Library..." button on right (about halfway down)
  5. Select "Server Runtime" click "Next"
  6. Select your Tomcat version from the list
  7. Click Finish

Reference from forumla