showing code snippets in blogs

0
drunken programmer has been complaining that his blog posts can't include his code properly. So I dug out some research I did a few months ago on how to set up your blog to include code snippets. Of course, some smart cookie has thought of how to do this already.

Here's a quick step by step...

1) add the syntax highlighter css and js to the < head> section of your site.

<link href="http://alexgorbatchev.com/pub/sh/2.0.278/styles/shCore.css" rel="stylesheet" type="text/css">
<link href="http://alexgorbatchev.com/pub/sh/2.0.278/styles/shThemeDefault.css" rel="stylesheet" type="text/css">
<script src="http://alexgorbatchev.com/pub/sh/2.0.278/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/2.0.278/scripts/shBrushJScript.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/2.0.278/scripts/shBrushCSharp.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/2.0.278/scripts/shBrushSql.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/2.0.278/scripts/shBrushXml.js" type="text/javascript"></script>
<script type="text/javascript">
SyntaxHighlighter.all();
</script>


2) add the required css to the <head> section of your site

.syntaxhighlighter .line .content .block
{
background: none !important;
}


3) that's it!!! (some ppl say that you need to set the convert line break option in blogger OFF, but mine is running ok wih it ON)

4) now for actually using the syntax highlighter. make sure that your code is surrounded by <pre> tags. Here is an example

<pre class="brush:js">
alert("Hello world");
</pre>


5) note that the brush type specified in this case is "js" so that it will do syntax highlighting appropriate for javascript. The list of all supported brush types can be found here. Note that the brush list matches the javascript import statements you added to your <head> section in step 1.

6) finally, make sure that any content you put in between the <pre> tags are bracket escaped using something similar to this


Add a custom Spring Security filter/handler

2
What I am trying to do is to write a log to the database when a user logout and we are using Spring Security 2.x + CAS. So I guess the best place to implement this will be along the Spring Security filter.

http://static.springsource.org/spring-security/site/docs/2.0.x/reference/ns-config.html#ns-custom-filters

We can replace a filter by extending org.springframework.security.ui.SpringSecurityFilter and add that into the filter chain





But instead of extending the filter a better way is to add a handler to the default logout filter.










public class LogLogoutHandler implements LogoutHandler{
public void logout(HttpServletRequest arg0, HttpServletResponse arg1, Authentication arg2) {
// Log info to DB
}
}


Sidenote: if you are using sec:custom-filter, make sure you do not have any reference to the same namespace which will cause a conflict. (i.e. you cannot do sec:logout if you are using a custom logout filter)

localize database

0
I am working on a project which I have to write a help module and it has to be l10n (coz we have Japanese and Chinese customers). And because I want the content to be editable TTW, everything has to be in the DB. Another requirement is that if the content hasn't been translated, the default content (in English will be displayed).

The following is the DB schema (mysql dialect)

CREATE TABLE `help_manager`.`helps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic` varchar(512) NOT NULL,
`content` varchar(16384) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

CREATE TABLE IF NOT EXISTS `help_manager`.`helps_l10n` (
`id` INT NOT NULL ,
`locale_id` INT NOT NULL ,
`topic` VARCHAR(512) NOT NULL ,
`content` VARCHAR(16384) NULL ,
PRIMARY KEY (`id`, `locale_id`) ,
INDEX `helps_fk` (`id` ASC) ,
INDEX `locale_fk` (`locale_id` ASC) ,
CONSTRAINT `helps_fk`
FOREIGN KEY (`id` )
REFERENCES `help_manager`.`helps` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `locale_fk`
FOREIGN KEY (`locale_id` )
REFERENCES `help_manager`.`locale` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci

CREATE TABLE IF NOT EXISTS `help_manager`.`locale` (
`id` INT NOT NULL AUTO_INCREMENT ,
`locale` VARCHAR(8) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB


So here is how you select contents from the table with a specific locale

SELECT COALESCE (hl.topic,h.topic) , COALESCE(hl.content, h.content) FROM helps h LEFT JOIN (helps_l10n hl, `locale` l) ON (h.id=hl.id AND l.id=hl.locale_id) AND l.locale = 'ja_JP'

Side note
mysql UTF8 encoding:
utf8_bin: compare strings by the binary value of each character in the string

utf8_general_ci: compare strings using general language rules and using case-insensitive comparisons

utf8_general_cs: compare strings using general language rules and using case-sensitive comparisons


for example:
Ä == A => True
Ü == U => True