スキップしてメイン コンテンツに移動

投稿

C#: Download All Image Links from Html Page

I know this is not perfect way. But I would like to give hints to anyone who would like to download all links in html page. Example usage is something like this. // only download "jpg" files HttpUtils.SaveFirstLevelLinksToFile("", Encoding.UTF8, "c:/temp/", link => link => ".jpg".Equals(Path.GetExtension(link.Link), StringComparison.OrdinalIgnoreCase) ); Main code is below. // main entry point method public static void SaveFirstLevelLinksToFile(string baseUri, Encoding enc, string dir, Func<LinkAttr, bool> filter) { ProcessAllExtractedLinksInHtmlText(GetPage(baseUri, enc), link => { try { if (!filter(link)) return; Uri uri = ConvertToAbsoluteURL(baseUri, link.Link); var filePath = dir + uri.AbsoluteUri.GetFileName().Replace("?", ""); uri.AbsoluteUri.GetAndSaveToFile(filePath);

ActionScript 3.0: Loading Bytes from URL

I wrote a utility class for loading bytes from URL because the official method needs a lot of preparation. Here is the usage. var loader:ByteLoadHelper = new ByteLoadHelper(); loader.addEventListener(ByteLoadEvent.COMPLETE, completeHandler); loader.load(url); function completeHandler(evt:ByteLoadEvent):void { loader.removeEventListener(ByteLoadEvent.COMPLETE, completeHandler); var bytes:ByteArray = evt.data; // do something nice :D } Here is the core code. package utils.tool { import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.utils.ByteArray; public class ByteLoadHelper extends EventDispatcher { public function ByteLoadHelper() { } public function load(url:String):void { var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoa

ActionScript 3.0: Dictionary Utility Methods (Delete All Entries, Count All Entries, etc.))

public class DictionaryUtils { public function DictionaryUtils() { } public static function forEachBySortedValue(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractValues(dict); v.sort(compare); for each(var value:Object in v) { callback(value); } } public static function forEachBySortedKey(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractKeys(dict); v.sort(compare); for each(var key:Object in v) { callback(key, dict[key]); } } public static function extractValues(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for each(var value:Object in dict) { v.push(value); } return v; } public static function extractKeys(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for (var key:Object in dict) { v.push(key); } return v; } pu

ActionScript 3.0: Digging Directory Recursively And Process Each File

This class aims to parse directory and do something for each file. You should just override proper methods, then call process method like this. new DirectoryDigger().process("c:/temp"); package utils.file { import flash.filesystem.File; public class DirectoryDigger { public function DirectoryDigger() { } public function process(path:String):Object { var file:File = new File(path); preProcess(file); processBody(file); return postProcess(file); } protected function processBody(file:File):void { if (file.isDirectory) { preProcessDir(file); var files : Array = file.getDirectoryListing(); for (var i : int = 0; i < files.length; i++) { processBody(files[i] as File); } postProcessDir(file);

C#: Capture Screen

Here is the code for capturing current screen in given Rectangle region. public static Bitmap CaptureFromScreen(Rectangle rc) { var bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy); } return bmp; } You can give something like below as a Rectangle argument // full screen area Screen.PrimaryScreen.Bounds; // 640 x 480 rectamgle from left side corner new Rectangle(0, 0, 640, 480); // working area Screen.PrimaryScreen.WorkingArea; The following links are helpful. http://homepage1.nifty.com/yasunari/VB/VB2005/DrawToScreen.htm : Sorry this page is written in Japanese... http://www.bobpowell.net/capture.htm http://msdn.microsoft.com/en-us/library/dd144871(VS.85).aspx http://msdn.microsoft.com/en-us/library/dd162920(VS.85).aspx

Solving Unimodal Function by Brent's Method

Introduction I faced a mathematical problem for solving a root from a unimodal function like quadratic function. Of course we can solve the root if the function is mono decreasing or mono increasing in the target range. However if the function is in potentially there are 2 solutions or no solutions in the target range. I know this article is not perfect yet. I will keep improving this article :) Main Requirements There are mainly 2 requirements when solving the root. Minimize the function call as much as possible: because the function needs heavy calculation Robustness against oscillating: Maybe the below figure helps your understanding. If we solve the root strictly, there can be a lot of solutions in the range because of the small oscillating. However we should choose reasonable accrual solution from candidate points. Solution I have tried Newton method, but it didn’t work well because of the above bullet point 2. I have also tried fitting 3 dimensional function

C#: Extract A Href Links from Html Text

// I know 1, 2, 3 is bad grouping name X( private readonly static Regex LINK_REGEX = new Regex( @"<a\s+[^>]*href\s*=\s*(?:(?<3>'|"")(?<1>[^\3>]*?)\3|(?<1>[^\s>]+))[^>]*>(?<2>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled ); public static void ExtractLinks(string text, ICollection<string> links) { LINK_REGEX.ApplyAllMatched(text, (m) => links.Add(m.Groups[1].Value)); } Helper method which processes matched string for each. public static void ApplyAllMatched(this Regex regex, string text, Action apply) { for (var m = regex.Match(text); m.Success; ) { apply(m); m = m.NextMatch(); } }

C#: Save Image as JPEG or PNG format

Just a simple code tips for saving image as JPEG or PNG format in C#. Save image as JPEG with Quality parameter public static readonly ImageCodecInfo JPEG_CODEC = GetEncoderInfo("image/jpeg"); public static void SaveImageAsJpeg(this Image src, string fileName, int quality) { var eps = new EncoderParameters(1); var ep = new EncoderParameter(Encoder.Quality, quality); eps.Param[0] = ep; src.Save(fileName.ChangeExtension("jpg"), JPEG_CODEC, eps); } public static ImageCodecInfo GetEncoderInfo(string mineType) { return ImageCodecInfo.GetImageEncoders().First(enc => enc.MimeType == mineType); } Save image as PNG public static void SaveImageAsPng(this Image src, string fileName) { src.Save(fileName.ChangeExtension("png"), ImageFormat.Png); }

C#: Resize Image

// Default parameters are almost the highest quality setting. public static Bitmap Resize(this Image src, int w, int h, SmoothingMode smoothingMode = SmoothingMode.AntiAlias, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic, PixelOffsetMode pixelOffsetMode = PixelOffsetMode.HighQuality) { var newImage = new Bitmap(w, h); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = smoothingMode; gr.InterpolationMode = interpolationMode; gr.PixelOffsetMode = pixelOffsetMode; gr.DrawImage(src, 0, 0, w, h); } return newImage; } // resize image based on given percentage public static Bitmap ResizedByPercentage(this Image src, double percent) { var rW = (int)Math.Round(src.Width * percent, 0); var rH = (int)Math.Round(src.Height * percent, 0); return src.Resize(rW, rH); } // resize image limited in given w, h parameter public static Bitmap ResizeImageIn(this Image src, int w, i

C#: Extract Charset from Html Meta Tag

private static readonly Regex META_TAG_CHARSET_REGEX = new Regex(@"<META\s+http-equiv\s*=\s*Content-Type\s+content=\s*""[^""]*\s+charset\s*=\s*(?<charset>[^""\s]*).*""\s*>", RegexOptions.IgnoreCase | RegexOptions.Compiled); public static string ExtractCharset(string htmlText) { string result = null; var m = META_TAG_CHARSET_REGEX.Match(htmlText); if (m.Success) { result = m.Groups["charset"].Value; } return result; } Note: System.Text.RegularExpressions.Regex class is thread safe according to this msdn doc .

ActionScript 3.0: Load SWF, JPG, PNG

Hey guys! I wrote small utility method for loading swf, jpg, png etc. in ActionScript 3.0. public static function loadFromURL(url:String):Loader { var img:Loader = new Loader(); img.load(new URLRequest(url)); return img; } public static function loadFromFile(path:String):Loader { var img:Loader = new Loader(); img.loadBytes(IOUtils.readByteData(path)); return img; } public static function load(url:String, format:String):URLLoader { var loader:URLLoader = new URLLoader(); loader.dataFormat = format; loader.addEventListener(Event.COMPLETE, completeHandler); loader.load(new URLRequest(url)); function completeHandler(event:Event):void { loader.removeEventListener(Event.COMPLETE, completeHandler); //trace(event.target.data); } return loader; }

C#: XPath and HtmlTextWriter Example

Hey guys!! This post show you how to use XPath in C#. I wrote program for formatting rss feed (xml) to html for its example. XPath in C# The simplest way is just creating XmlDocument and call SelectNodes method. var xmlString = "some xml string...." // create XmlDocment var doc = new XmlDocument(); doc.LoadXml(xmlString); // selecting nodes by xpath string doc.SelectNodes("/rss/channel/item"); RSS Xml to Html Example Okay here is a simple XPath example - converting rss xml to html. using System.IO; using System.Web.UI; using System.Web; namespace Utility { public class RSStoHtmlWriter : HTMLWriteHelper { private readonly string url; public RSStoHtmlWriter(string url) { this.url = url; } public override void WriteBody(HtmlTextWriter htmlWriter) { using (var reader = new XmlTextReader(url)) { // **** using XPath!! **** // As you n

PHP: Extract Values on Specific Key in Array

I wrote tiny utility method - which is for extracting values on specific key in array. Code function extractValuesForSpecificKey(array& $sourceArray, $key){ $retArray = array(); foreach ($sourceArray as $sourceElem){ $retArray[] = $sourceElem[$key]; } return $retArray; } Example $source = array( array('id' => 1, 'name' =>'Joe'), array('id' => 2, 'name' =>'Chris'), ); $result = extractValuesForSpecificKey($source, 'id'); var_dump($result); The result should be: array(2) { [0]=> int(1) [1]=> int(2) }

CMSL (Connected Multilayer Spring Layout)

I finally announce Java Web Start Demo for CMSL (Connected Multilayer Spring Layout) is available!! 2013/11 Sorry, looks like jnlp not works very well. I stopped make the demo public. I developed the CMSL originally in 2008. Screenshots Source Code The source code is available on CMSL on sourceforge. Future Plan I know I need to do a lot of refactoring for the code, put functionality for playing music, support more flexible data structure, etc.... X( I will keep updates on this blog if something of CMSL is improved. Currently In order to improve the calculation speed for layout of elements, am working on following tasks Cell division algorithm Multi-threading The limit of number of elements frame rate which meet achieving 30fps is about 1000. Am targeting to achieve 10000 elements. Not sure people can see 1000 elements at a time though :P

JDK7 + EMMA

I recently upgraded my JDK from 6 to 7. When I ran my JUnit tests with EMMA, all tests suddenly became failed with the following error: java.lang.ClassFormatError: Illegal local variable table length 11 in method ..... I have googled and found that the solution from here . What I have done is adding -XX:-UseSplitVerifier option when JUnit runs. After I added this option, everything works fine. (The cause looks like new try-catch resource feature according to the above stackoverflow trace but I have not confirmed yet.)

JavaでGUIアプリケーション開発: Swing, SWT, Java3D, Animation

JavaでもリッチなUIやアニメーションを作りたいという方向けにリンクと書籍を紹介します。 今は、JQueryやTwitter Bootstrapなどのjavascript、HTML5でリッチなUIを作る人が多いと思いますが、Javaも捨てたものじゃないということで参考にしていただければと思います。 Swing JavaのGUIといえばSwing! 2D系の処理は手軽に何でもできるので、非常に便利です。 Swing Tutorial Native Swing JGoodies Filthy Rich Clients アニメーションとグラフィカルエフェクトを使ったデスクトップJavaアプリケーション (The Java Series) JavaのSwingでColl & Richなアプリケーションを開発しようと思ったら、 絶対 にはずせない書籍です。 私にとってのJavaのSwing開発のバイブルです :) 特に画像のスケーリングについては、画像の補間方法、性能比較なども含め本当に詳しく紹介されています。 またアニメーションのタイミングフレームワークについても深く掘り下げて説明されています。 個人的にはもっと注目されてもいい書籍だと思うのですが。。。かなりお値段は張りますが、購入する価値の十分ある書籍です。 書籍中で出てくるソースコードは、下記リンクですべて公開されています。 http://filthyrichclients.org/ java.net上の http://java.net/projects/filthyrichclients/ からもチェックアウトできます。 Java Swing Hacks ―今日から使える驚きのGUIプログラミング集 2006年刊行の本ですが、Swingをここまで使いこなせている人は、未だに少ないのではないでしょうか。 テキストボックスのオートコンプリートの作成、ドラッグアンドロップ、サムネイル付JFileChooserの作成などなど。。すばらしい Hack が満載です!! SWT Eclipseで使われているライブラリです。良くも悪くもOSに依存したNativeなライブラリです。 私はJavaからIEのWebブラウザが使いたかったことがあり、触った

Taking Diff bewteen Two Repositories

I wrote a bash script for taking diffs between two repositories. #!/bin/bash SVN_USERNAME=dukesoftware SVN_PASSWORD=some_password SVN_COMMON_COMMAND="svn --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive --ignore-externals --quiet" SVN_COMMON_UPDATE_COMMAND="$SVN_COMMON_COMMAND update --set-depth infinity" SVN_COMMON_CHECKOUT_COMMAND="$SVN_COMMON_COMMAND co --depth immediates" SVN_BASE_URL=http://svn.repository.dummy.com/projects/ # comparison svn urls SVN_URL1=$SVN_BASE_URL/url1 SVN_URL2=$SVN_BASE_URL/url2 DIR1=target1 DIR2=target2 OUTPUT_DIR=diff_out EXCLUDE_FILES=(".svn" "jquery-*.min.js") # To minimize number of checkout files, set below constants CHECKOUT_DIRS=("dir1" "dir2" "lib") DIFF_TARGETS=("dir1/sub1" "dir1/sub2" "dir2" "lib") # function definitions build_base_diff_command(){ DIFF_BASE_COMMAND="diff -E -B -b -u -r" fo

C#: Generate Integer Sequence

This is quick tips. If you would like to generate sequence in C#, you can do like below: // generate 1, 2, 3,...., 24 foreach (var i in Enumerable.Range(1, 25)){ // do somthing nice :D } In my opinion, C# (.Net implementation) has nicer built-in libraries and syntax than Java...

JsTestDriver Trouble and Tips

I am looking for testing tools for Javascript and reached at This Stackoverflow page . The page contains bunch of useful information :) After reading the page, I decided to use JsTestDriver for the following reason. having high affinity with CI server providing plugin for codecoverage - ( lcov format) JUnit format report To run Javascript test, you need to start up JsTestDriver server. The command is simple. java -jar JsTestDriver-1.3.4.b.jar --port 9876 One thing you should be careful about is if you run the above command in the directory which includes jsTesDriver.conf, the jar automatically picked up the jsTestDriver.conf and and it starts as server mode. Ok now you need to run client side (actual tests) against the server. You will use FireFox + Xvfb or PhantomJS as headless browser. java -jar JsTestDriver-1.3.4.b.jar --browser {path to browser} --tests all --config jsTestDriver.conf --testOutput testResult Also jsTestDriver.conf should something like this: server:

Selenium IDEの使い勝手について調査

Selenium IDEの概要 FireFoxのPlugin 公式ドキュメント 独断と偏見に満ちた長所・短所 長所 作成したテストを様々なプログラミング言語で出力可能 (File->Export Test Suite As...) ブラウザ上での操作をSelenium IDE Commandとして記録するマクロ機能あり 短所 正直、このpluginを導入するより、普通のSeleniumを使って、システムの実装言語だけでテストが完結している方が、プロジェクトの運用コストや学習コストが低くてすむ可能性大。 スクリプトを書いてみた感じだと'''面倒くさい'''。後々のテストのメンテナンスに難あり。 その他、メモ Html内の要素の選択方法 XPath, CSS, DOM, id属性, name属性などが使える Selenium CoreのLocatorsを参照 Assertion or Verification? Assert: テストに失敗すると、それ以降のテストコマンドは実行されない Verify: テストに失敗すると、結果はFailとして表示されるが、Verify以降のテストコマンドは引き続き実行される waitForコマンドは特定の要素が出現するまで待って、出現したら次のテストコマンドを実行する。Ajax系のテストに最適。

Phalanger

I didn't know that there is a PHP compiler - Phalanger It compiles PHP on .Net/Mono platform. The aim of Phalanger is improving performance of PHP  Web application. I am surprised that PHP compiled by Phalanger is four times faster than offical PHP 5.4. Will try it when I have a free time.

SWF Investigator

Not sure everyone is interested in Flash technology but I found an interesting powerful tool - Adobe SWF Investigator . It can analyze SWF file by useful provided functionality like disassembler, SWF Viewer etc. Here is the screenshot. I think this kind of tool should be released much more earlier... I think flash is already fade-out phase. But still the tool is useful for flash developer.

Get Useful SVN info in Ant

Hello there! I am trying to get svn info from Ant. I think there are two ways. SvnAnt Execute SVN command and extract information on Ant I've tried first option - SvnAnt but there is an issue for me. I need to pick up proper version of JavaHL based on Subversion client version installed on machine. Since I would like to shared the ant script between Windows and Linux box, it's a bit pain for me to switch the JavaHL in my Ant script. So I decided to go for second option. This method is really straight forward but a bit hack. The advantage of this method is it works at least svn command is available on your machine and it doesn't depend on svn client version. The disadvantage is it really depends on svn info output string e.g. it's design for human readability, not designed for API. Anyway, here is the example for getting svn revision and set it to build.current.revision property. What this target do is redirecting svn info output and extracting revision li

Setup Headless Browser

Recently I was trying to setup Selenium Tests on CI Server on Linux (Cent OS) box in order to test my web project. I need headless browser for Selenium Tests on CI Server. There are 2 options: PhantomJS FireFox + Xvfb PhantomJS is really cool and I confirmed it worked fine on my Windows XP. I would have liked to use PhantomJS on my Linux box, too. However looks the latest binary build is unable to run on my Linux box because the required libc and libstdc++ are missing in the box: $ phantomjs/bin/phantomjs phantomjs/bin/phantomjs: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by phantomjs/bin/phantomjs) phantomjs/bin/phantomjs: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by phantomjs/bin/phantomjs) phantomjs/bin/phantomjs: /lib64/libc.so.6: version `GLIBC_2.7' not found (required by phantomjs/bin/phantomjs) phantomjs/bin/phantomjs: /lib64/libc.so.6: version `GLIBC_2.7' not found (required by /home/duke/bin/ph

PMD and CPD for ECMAScript (Javascript)

Recently I have tried to use PMD and CPD for Javascript. I found that the offical PMD release says PMD 5.0.0 supports Javascript :D We have strong code inspection tools like Sonar etc. But I think still helpful for setting up compact ant task. PMD PMD ant task setup for javascript is easy, almost same as Java. Only you have to remember is including rhino jar in the classpath. Here is the example. <property name="buildscripts.path" location="${basedir}/generic-buildscripts"/> <property name="pmd.jar" value="pmd-5.0.0.jar"/> <property location="rhino-1.7R3.jar" name="rhino.jar"/> <path id="pmd.classpath"> <fileset dir="${buildscripts.path}/pmd/"> <include name="asm-3.2.jar"> <include name="jaxen-1.1.1.jar"> <include name="${pmd.jar}"> <include pa

「特定の文字から始まらない文字列」にマッチする正規表現

「特定の文字から始まらない文字列」 にマッチする正規表現の例です。  以下の例では、Aから始まらない文字列にマッチする正規表現を示しています。 ^(?!A).*$ 私も正規表現の組み方で四苦八苦することがあります。以下の書籍は実践的に様々な正規表現のパターンを例示してくれているので、重宝しています。

Continuous Integration

最近、継続的インテグレーション(CI:Continuous Integration)の環境の構築に励んでいるので、CIツールを紹介したいと思います。 TeamCity JetBrains 社が販売している分散ビルド管理(Distributed Build Management)と継続的インテグレーション(Continuous Integration)のためのサーバフレームワークです。 メリット とにかくRemoteRunが非常に便利、コミット前にCIServerでテストできるのはありがたいです。 UIがかっこいい(個人的な感想ですが。)  Jenkins 恐らく、2012年現在、CIサーバはJenkinsとTeamCityとの一騎打ち感じではないでしょうか? メリット 豊富なプラグイン! フリー!  2011年末に以下の書籍が発売されたので最近購入しました。初めてCI環境を経験、構築する人にはお勧めできます。私はTeamCityで既に実践(実戦?)経験があり、知っていた部分も多かったので、ちょっと物足りなかったです。ただプラグインの紹介やプラグインの開発の章は参考になりました。CIを初めて体験する方にお勧めします。 O'reillyからもJenkinsの書籍が出ているようです。 CruiseControl フリーの継続的インテグレーションツールです。 恥ずかしながら使ったことはありません。。。ちなみに.Net版の CruiseControl.NET もあるようです。 Sonar このツール自体は、CIサーバではありませんが、PMD、CPDなどのあらゆるCode Inspectionの機能を持ったサーバです。CIとは非常に親和性が高いので、導入をお勧めします。 特にコードの状態の可視化機能は圧倒的に優れていますので、大きなプロジェクトでは、是非導入したいところです。

C++の書籍(原著 vs 邦訳)

以下は、私が実際に購入した書籍です。原著と邦訳版を対で掲載します。 私が購入したのはすべて邦訳版です。英語力のある方は是非、原著を読むことをお勧めします! 書籍の画像をクリックするとAmazonへ飛びます。(欲張ってたくさん載せたら、リンクの画像が表示されない場合があるようです。。。申し訳ありません。) 基礎 C++ from the Ground Up, 4th Edition 色々見比べてこの書籍を購入しました。基本的な部分はすべて網羅されていますので、ちょっとしたC++のプログラムを書き始めるのであれば、十分な書籍だと思います。 中級 Effective C++: 55 Specific Ways to Improve Your Programs and Designs (Addison-Wesley Professional Computing Series) C++ Coding Standards: 101 Rules, Guidelines, and Best Practices (C++ In-Depth Series) C++だけでなく他の言語でも適用出るようなPracticeもあるので、私のようなJava中心のDeveloperでも役に立ちました。ただ分量の割りにお値段が。。。 More Effective C++: 35 New Ways to Improve Your Programs and Designs (Addison-Wesley Professional Computing Series) Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions (C++ In-Depth Series) Modern C++ Design: Generic Programming and Design Patterns Applied (C++ In-Depth Series) Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and So