<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stanković Vladimir</title>
	<atom:link href="http://www.svlada.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.svlada.com/blog</link>
	<description>Sit down, relax, mix yourself a drink and enjoy the show ...</description>
	<lastBuildDate>Mon, 05 Nov 2012 14:06:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Publish/Subscribe design pattern in Javascript using EventBroker/Mediator</title>
		<link>http://www.svlada.com/blog/2012/08/13/publish-subcribe-pattern-javascript/</link>
		<comments>http://www.svlada.com/blog/2012/08/13/publish-subcribe-pattern-javascript/#comments</comments>
		<pubDate>Mon, 13 Aug 2012 09:06:20 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[design-patterns]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript aop]]></category>
		<category><![CDATA[javascript delegate]]></category>
		<category><![CDATA[pub/sub]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=343</guid>
		<description><![CDATA[To follow me on Twitter click here. Visit Sargarepa (My efforts to better understand Javascript patterns) on github. Caution Sargarepa is in early phase of development and serves as a learning resource. Before we begin checkout working Pub/Sub tutorial from github or download complete source code as zip archive . After obtaining the source, open [...]]]></description>
			<content:encoded><![CDATA[<div style="font-size: 11px; background-color: #eee;">
To follow me on Twitter <a target="_blank" href="https://twitter.com/#!/svlada">click here.</a><br />
Visit <a target="_blank" href="https://github.com/svlada/Sargarepa">Sargarepa</a> (My efforts to better understand Javascript patterns) on <a target="_blank" href="https://github.com/svlada/Sargarepa">github</a>. <span style="font-size:10px;"><em>Caution Sargarepa is in early phase of development and serves as a learning resource.</em></span>
</div>
<hr/>
Before we begin checkout working Pub/Sub tutorial from <a target="_blank" href="https://github.com/svlada/Sargarepa">github</a> or download complete source code as <a href="https://github.com/svlada/Sargarepa/zipball/master">zip archive </a>.<br />
After obtaining the source, open <em>&#8220;/Sargarepa/pubsub.html&#8221;</em> in your browser.</p>
<h3>Table of contents:</h2>
<p><a href="#t0">Introduction</a><br />
<a href="#t1">Publish/Subcribe design pattern in javascript</a><br />
<a href="#t2">Source code</a><br />
<a href="#t3">References</a></p>
<h1><a name="t0" id="t0">Introduction</a></h1>
<p>Publish / Subcribe design pattern is built around simple concept. Essentially, we need three different entities: Publishers, Subscribers and EventBroker / Mediator.</p>
<p><em>Subcribers</em> can subscribe to one or multiple channels / topics, and receive messages from multiple sources without the knowledge about publishers.</p>
<p><em>Publisher</em> can publish messages / topics, without any knowledge about subscribers.</p>
<p><em>EventBroker / Mediator</em> provides decoupling of publishers and subscribers.</p>
<h1><a name="t1" id="t1">Publish / Subcribe design pattern in javascript</a></h1>
<p>I am going to explain implementation from top to bottom, revealing implementation details as we go.</p>
<p>We want to create one publisher and two subscriber objects for this example. EventBroker has responsability to manage subcriber objects and route messages from publisher to subscribers.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">pubSub</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Mediator-EventBroker</span><br />
&nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> inputStream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> InputStream<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Publisher</span><br />
&nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> mailBox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;online&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Subcriber 1</span><br />
<span style="color: #003366; font-weight: bold;">var</span> mobileInbox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mobile inbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Subcriber 2</span></div></div>
<p>Please note that we have placed EventBroker on global namespace. You should attach EventBroker to your application namespace. What other options do we have?</p>
<p>1) We could inject EventBroker into every Publisher instance through setter / constructor.<br />
2) Publisher object can extend EventBroker and inherit the publish method.</p>
<p>How to publish message to message bus / channel?</p>
<p>InputStream is object that will serve as Publisher. After InputStream receives message, it should be able to send message to message bus / channel. Event Broker will broadcast received message to all subscribers subscribed to that channel.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;First message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Let&#8217;s see how our Publisher object look.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> InputStream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> message <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
InputStream.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">receiveMessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>We could alter <strong>[InputStream.prototype.receiveMessage]</strong> method in the following way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">InputStream.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">receiveMessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; pubSub.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This approach will work. But we don&#8217;t want to couple publish logic with Publisher source code. Nicer solution would be to mark arbitary Publisher method as &#8220;publishable&#8221; dynamically at runtime. Goal is to register callback function that will be called every time after execution of any &#8220;publishable&#8221; method, so we basically need to mimic AOP for javascript <strong>[PubSub.prototype.publishAfter]</strong>.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">publishAfter</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> targetFn<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> target<span style="color: #009900;">&#91;</span>targetFn<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; target<span style="color: #009900;">&#91;</span>targetFn<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; temp.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>How to use <strong>[PubSub.prototype.publishAfter]</strong> method? </p>
<p>You need to pass following arguments to [PubSub.prototype.publishAfter] method:<br />
1. target object/context &#8211; Object that contain publishable method.<br />
2. targetFn &#8211; This is name of the function we want to call. This parameter is passed as string value.<br />
3. callback &#8211; Function that will be called everytime after execution of publishable method.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">pubSub</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Mediator-EventBroker </span><br />
<span style="color: #003366; font-weight: bold;">var</span> inputStream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> InputStream<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Publisher</span><br />
&nbsp; &nbsp;<br />
pubSub.<span style="color: #660066;">publishAfter</span><span style="color: #009900;">&#40;</span>inputStream<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;receiveMessage&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; pubSub.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;First message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Second message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Third message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can make publishable any method in global / application namespace. Just provide correct context.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">fnPublisher</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I am publisher from namespace&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
pubSub.<span style="color: #660066;">publishAfter</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fnPublisher&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; pubSub.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>At this point we have completed logic needed on Publisher side. Now we are going to add one Subcriber object.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> MailBox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
MailBox.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getInbox</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
MailBox.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setInbox</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hey I am updated: '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Subcriber doesnt have any knowledge about Publisher. We are going to add subscribe method onto the EventBroker prototype.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">pubSub</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Mediator-EventBroker</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> mailBox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;online&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Subscriber 1</span><br />
<span style="color: #003366; font-weight: bold;">var</span> mobileInbox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mobile inbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Subscriber 2</span><br />
<br />
pubSub.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mailBox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mailBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
pubSub.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mobileInbox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mobileInbox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Method <strong>[PubSub.prototype.subscribe]</strong> receives three arguments:</p>
<p>1. topic/channel &#8211; First argument is channel name. Subscriber can subscribe to one or multiple topics.<br />
2. callback &#8211; Subscriber register callback function that will be called when new message arrives to message bus / channel<br />
3. target &#8211; Subscriber object</p>
<p>EventBroker(PubSub) maintain list of channels. Every channel has a name and an associated collection of subscribers.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Event Broker</span><br />
<span style="color: #003366; font-weight: bold;">var</span> PubSub <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Most imporant part of EventBroker are publish and subscribe methods. In order to understand better the structure of Event broker let&#8217;s see what the collection of channels may look at runtime when it is populated with topics and subscribers.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;new message&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span> callback<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> target<span style="color: #339933;">:</span> MailBox <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>callback<span style="color: #339933;">:</span> fnSubscriber<span style="color: #339933;">,</span> target<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;channel-1&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;channel-2&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>We can register method from global / application namespace or object method to be called on subscriber&#8217;s part.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">subscribe</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> callback<span style="color: #339933;">:</span> callback<span style="color: #339933;">,</span> target<span style="color: #339933;">:</span> target <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Publish method traverses through subscriber collection and fires registered callbacks.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">publish</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createDelegate<span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">callback</span><span style="color: #339933;">,</span> obj.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span style="color: #660066;">callback</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In order to bind context(this) to callback function we are going to create createDelegate factory function. Javascript doesn&#8217;t have built-in delegate objects that can have function and &#8220;this&#8221; parameter. This behavior is easy to emulate. We pass function and target object as arguments to our createDelegate function, and as a result new function that will call original function with correct context is returned.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> createDelegate<span style="color: #009900;">&#40;</span>fn<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Subscriber can unsubscribe from any topic at any time by removing its callback from subscriber&#8217;s queue.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">unsubscribe</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target <span style="color: #339933;">&amp;&amp;</span> obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;target&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> target <span style="color: #339933;">&amp;&amp;</span> obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;callback&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Removing delegate function'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;callback&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Removing regular function'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Download complete source code as <a href="https://github.com/svlada/Sargarepa/zipball/master">zip archive</a> or checkout source from <a target="_blank" href="https://github.com/svlada/Sargarepa">github</a>.<br />
After you have obtained source, open <em>&#8220;/Sargarepa/pubsub.html&#8221;</em> in your browser.</p>
<p>Final notice:<br />
Working implementation uses <a href="http://requirejs.org/">RequireJS</a> in order to organize project in smaller modules. I strongly encourage you to use <a href="http://requirejs.org/">RequireJS</a> for your javascript project.</p>
<h1><a name="t2" id="t2">Source code listing</a></h1>
<p><strong>EventBroker</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;use strict&quot;</span><br />
define<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'app/util'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>Util<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> PubSub <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">subscribe</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> callback<span style="color: #339933;">:</span> callback<span style="color: #339933;">,</span> target<span style="color: #339933;">:</span> target <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">publish</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.<span style="color: #660066;">createDelegate</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">callback</span><span style="color: #339933;">,</span> obj.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span style="color: #660066;">callback</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">unsubscribe</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>topic<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target <span style="color: #339933;">&amp;&amp;</span> obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;target&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> target <span style="color: #339933;">&amp;&amp;</span> obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;callback&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Removing delegate function'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;callback&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Removing regular function'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">channels</span><span style="color: #009900;">&#91;</span>topic<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; PubSub.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">publishAfter</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> targetFn<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> target<span style="color: #009900;">&#91;</span>targetFn<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; target<span style="color: #009900;">&#91;</span>targetFn<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>PubSub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Publisher</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;use strict&quot;</span><br />
define<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> InputStream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> message <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; InputStream.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">receiveMessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// pubSub.publish('new message', { message: this.message });</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>InputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Subscriber</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;use strict&quot;</span><br />
define<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> MailBox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; MailBox.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getInbox</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; MailBox.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setInbox</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hey I am updated: '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inbox</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>MailBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Util</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;use strict&quot;</span><br />
define<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> createDelegate<span style="color: #009900;">&#40;</span>fn<span style="color: #339933;">,</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; createDelegate<span style="color: #339933;">:</span> createDelegate<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Code for testing EventBroker, Publisher and Subscriber</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'./common'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>common<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'app/pubsub'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'app/util'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'app/mailbox'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'app/inputstream'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PubSub<span style="color: #339933;">,</span> Util<span style="color: #339933;">,</span> MailBox<span style="color: #339933;">,</span> InputStream<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span style="color: #660066;">pubSub</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> inputStream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> InputStream<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> mailBox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;online&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> mobileInbox <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MailBox<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mobile inbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mailBox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mailBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mobileInbox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mobileInbox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">publishAfter</span><span style="color: #009900;">&#40;</span>inputStream<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;receiveMessage&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;First message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mailBox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mailBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> mobileInbox.<span style="color: #660066;">setInbox</span><span style="color: #339933;">,</span> mobileInbox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>pubSub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> fnSubscriber<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hey I have recieved that message too'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Test Global function as subscriber</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> fnSubscriber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; inputStream.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Druga poruka&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// pubSub.unsubscribe('new message', fnSubscriber);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>pubSub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Test aop on function on global namespace</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span style="color: #660066;">fnPublisher</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I am publisher from namespace&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">publishAfter</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fnPublisher&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pubSub.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new message'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">message</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; fnPublisher<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;aop on global namespace published message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h1><a name="t3" id="t3">References</a></h1>
<p><a href="http://msdn.microsoft.com/en-us/magazine//hh201955.aspx">Understanding the Publish/Subscribe Pattern for Greater JavaScript Scalability</a><br />
<a href="http://addyosmani.com/largescalejavascript/">Patterns For Large-Scale JavaScript Application Architecture</a><br />
<a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/">Learning JavaScript Design Patterns</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2012/08/13/publish-subcribe-pattern-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Require.js Optimization – part2</title>
		<link>http://www.svlada.com/blog/2012/07/02/require-js-optimization-part2/</link>
		<comments>http://www.svlada.com/blog/2012/07/02/require-js-optimization-part2/#comments</comments>
		<pubDate>Mon, 02 Jul 2012 09:36:15 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=324</guid>
		<description><![CDATA[Follow me on Twitter click here Jump to complete source code source code. Visit Keleraba (Backbone, Require, Handlebars) boilerplate project on github. Caution Keleraba is in early phase of development and serves as a learning resource. This is part2 tutorial in RequireJs series(part1, part2). Reading material about r.js and RequireJS is in references section of [...]]]></description>
			<content:encoded><![CDATA[<p>Follow me on Twitter <a target="_blank" href="https://twitter.com/#!/svlada">click here </a><br />
Jump to complete source code <a href="#source">source code</a>.<br />
Visit Keleraba (Backbone, Require, Handlebars) boilerplate project on <a target="_blank" href="https://github.com/svlada/Keleraba">github</a>. <span style="font-size:10px;"><em>Caution Keleraba is in early phase of development and serves as a learning resource.</em></span></p>
<p>This is part2 tutorial in RequireJs series(<a target="_blank" href="http://www.svlada.com/blog/2012/06/29/require-js-dependency-management-part1/">part1</a>, <a target="_blank"  href="http://www.svlada.com/blog/2012/07/02/require-js-optimization-part2/">part2</a>). Reading material about r.js and RequireJS is in references section of article.</p>
<h3>Table of contents:</h2>
<p><a href="#t0">Introduction</a><br />
<a href="#t1">Require.js optimizer</a><br />
<a href="#t2">Require.js optimizer dump dependencies to single file</a></p>
<h1><a name="t0" id="t0">Introduction</a></h1>
<p>This is short article in which I will explain you how to build and optimize your javascript code with ReqireJS optimization tool r.js.</p>
<p>James Burke <a target="_blank" href="http://requirejs.org/docs/optimization.html">recommends</a> using node.js for optimizing and building your code. Make sure you have node.js installed on your machine. You can download node.js from <a target="_blank" href="http://nodejs.org/#download">here</a>.</p>
<h1><a name="t1" id="t0">Require.js optimizer</a></h1>
<p>Create your build script file on following location <strong>[webapp/build/build.js]</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">|-</span><span style="color: #009900;">&#91;</span>wepapp<span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">|---</span> <span style="color: #009900;">&#91;</span>build<span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">|-----</span> r.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">|-----</span> build.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">|-----</span> build.<span style="color: #660066;">single</span>.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">|---</span> <span style="color: #009900;">&#91;</span>js<span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">|------</span> <span style="color: #009900;">&#91;</span>app<span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">|------</span> <span style="color: #009900;">&#91;</span>lib<span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">|---------</span> jquery.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">|-----</span> app.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">|-----</span> app<span style="color: #339933;">-</span>built.<span style="color: #660066;">js</span><span style="color: #339933;">*</span><br />
<span style="color: #339933;">|-</span>index.<span style="color: #660066;">html</span><br />
<span style="color: #339933;">|-</span>readme.<span style="color: #660066;">md</span></div></div>
<p>Build your application with node.js and r.js executing following command</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">node r.<span style="color: #660066;">js</span> <span style="color: #339933;">-</span>o build.<span style="color: #660066;">js</span></div></div>
<p>For demonstration purposes I have made 3 examples of building application with RequireJS.</p>
<h4>Example 1: Optimize modules</h4>
<p>Main module app.js has many dependencies and we want to bundle all that dependencies into single file. Mark modules for optimization in modules array of configuration object inside build.js. First example will contain only one module for optimization <strong>[webapp/js/app.js]</strong>.</p>
<p>Paste following code to your build.js file.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;js/lib&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; appDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;..&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; dir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dist&quot;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; modules<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app&quot;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>appDir</strong> &#8211; Telling us where webapp root directory is located relative to build.js script.<br />
<strong>dir</strong> &#8211; Output directory relative to build.js</p>
<p>Console output of build.js</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>r.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>category.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>jquery.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>require.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>text.<span style="color: #660066;">js</span><br />
<br />
js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">----------------</span><br />
js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>jquery.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>category.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span></div></div>
<p>All <strong>[webapp/js/app.js]</strong> dependencies are now minified with Uglify.js and concatenated into single file. But what happened with individual modules? Module <strong>[webapp/js/app/category/specialItem.js]</strong> has a dependency <strong>[webapp/js/app/category/Item.js]</strong>. Open your optimized <strong>[webapp/js/app/category/specialItem.js]</strong> and pass it to beatifier. As you can see from output <strong>[js/app/category/item.js]</strong> is not bundled with our <strong>[js/app/category/specialItem.js]</strong>.</p>
<p>Optimized module:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">define<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;./item&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">Item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> SpecialItem <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>itemName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Default color&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">weigth</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Default weigth&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">Item</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> itemName<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> SpecialItem.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">Item</span><span style="color: #339933;">,</span> SpecialItem.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">=</span> SpecialItem<span style="color: #339933;">,</span> SpecialItem<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></div>
<h4>Example 2: Optimize additional modules</h4>
<p>In this example we will add <strong>[webapp/js/app/category/specialItem.js]</strong> to modules array of build.js configuration object.</p>
<p>Paste following code to your build.js file.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;js/lib&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; appDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;..&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; dir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dist&quot;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; modules<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app/category/specialItem&quot;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Console output of build.js</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>r.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>category.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>jquery.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>require.<span style="color: #660066;">js</span><br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>dist<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>text.<span style="color: #660066;">js</span><br />
<br />
js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">----------------</span><br />
js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>jquery.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>category.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span><br />
<br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">----------------</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span></div></div>
<p>Now <strong>[js/app/category/item.js]</strong> is bundled with <strong>[js/app/category/specialItem.js]</strong> module. </p>
<p>Optimized module:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app/category/item&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">Item</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>itemName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> itemName<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">Item</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getItemName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">Item</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app/category/specialItem&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;./item&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">Item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> SpecialItem <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>itemName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Default color&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">weigth</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Default weigth&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">Item</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> itemName<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> SpecialItem.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">Item</span><span style="color: #339933;">,</span> SpecialItem.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">=</span> SpecialItem<span style="color: #339933;">,</span> SpecialItem<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></div>
<h1><a name="t2" id="t0">Require.js optimizer compile dependencies to single file</a></h1>
<p>Paste following code to your build.single.js file.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;../js/lib&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;../app&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; out<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app-built.js&quot;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>name</strong> &#8211; Location of module you want to export as a single file with all dependencies.<br />
<strong>out</strong> &#8211; File &#8220;app-built.js&#8221; is created</p>
<p>Console output of build.single.js:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Tracing dependencies <span style="color: #000066; font-weight: bold;">for</span><span style="color: #339933;">:</span> ..<span style="color: #339933;">/</span>app<br />
Uglifying file<span style="color: #339933;">:</span> C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>app<span style="color: #339933;">-</span>built.<span style="color: #660066;">js</span><br />
<br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>app<span style="color: #339933;">-</span>built.<span style="color: #660066;">js</span><br />
<span style="color: #339933;">----------------</span><br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>jquery.<span style="color: #660066;">js</span><br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>category.<span style="color: #660066;">js</span><br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">js</span><br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>app<span style="color: #339933;">/</span>category<span style="color: #339933;">/</span>specialItem.<span style="color: #660066;">js</span><br />
C<span style="color: #339933;">:/</span>vlada<span style="color: #339933;">/</span>practice<span style="color: #339933;">/</span>require<span style="color: #339933;">/</span>webapp<span style="color: #339933;">/</span>js<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>..<span style="color: #339933;">/</span>app.<span style="color: #660066;">js</span></div></div>
<p>All of our modules are now glued together into single <strong>[webapp/build/app-built.js]</strong> file.</p>
<h1><a name="source" id="source">Source code listing</a></h1>
<p>build.js</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;js/lib&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; appDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;..&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; dir<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dist&quot;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; modules<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app/category/specialItem&quot;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>build.single.js</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;../js/lib&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;../app&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; out<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;app-built.js&quot;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In the RequireJS tutorial part 3 I will talk about RequireJS integration with Backbone, Handlebars and jQuery with Require.js. Stay tuned <img src='http://www.svlada.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You can follow me on Twitter <a target="_blank" href="https://twitter.com/#!/svlada">click here </a></p>
<p>References<br />
<a target="_blank" href="http://requirejs.org/docs/optimization.html">http://requirejs.org/docs/optimization.html</a><br />
<a target="_blank" href="https://github.com/jrburke/r.js/blob/master/build/example.build.js">example.build.js</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2012/07/02/require-js-optimization-part2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Require.js Dependency management &#8211; part1</title>
		<link>http://www.svlada.com/blog/2012/06/29/require-js-dependency-management-part1/</link>
		<comments>http://www.svlada.com/blog/2012/06/29/require-js-dependency-management-part1/#comments</comments>
		<pubDate>Fri, 29 Jun 2012 12:21:14 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=281</guid>
		<description><![CDATA[Follow me on Twitter click here Jump to complete source code source code. Visit Keleraba (Backbone, Require, Handlebars) boilerplate project on github. Caution Keleraba is in early phase of development and serves as a learning resource. This is introductory RequireJS tutorial in RequireJs series(part1, part2). Reading material about AMD and RequireJS is in references section [...]]]></description>
			<content:encoded><![CDATA[<p>Follow me on Twitter <a target="_blank" href="https://twitter.com/#!/svlada">click here </a><br />
Jump to complete source code <a href="#source">source code</a>.<br />
Visit Keleraba (Backbone, Require, Handlebars) boilerplate project on <a target="_blank" href="https://github.com/svlada/Keleraba">github</a>. <span style="font-size:10px;"><em>Caution Keleraba is in early phase of development and serves as a learning resource.</em></span></p>
<p>This is introductory RequireJS tutorial in RequireJs series(<a target="_blank" href="http://www.svlada.com/blog/2012/06/29/require-js-dependency-management-part1/">part1</a>, <a target="_blank"  href="http://www.svlada.com/blog/2012/07/02/require-js-optimization-part2/">part2</a>). Reading material about AMD and RequireJS is in references section of article.</p>
<h3>Table of contents:</h2>
<p><a href="#t0">Introduction</a><br />
<a href="#t1">Basic structure</a><br />
<a href="#t2">Prototypal inheritance and Require.js</a></p>
<h1><a name="t0" id="t0">Introduction</a></h2>
<p>Before or after reading this article I strongly recommend to read documentation on <a target="_blank" href="http://requirejs.org/docs/start.html">RequireJS website</a>. James Burke did great job here.<br />
RequireJS is a Javascript file and module loader which allows for asynchronous JavaScript loading and dependency management(can be used to load more than just Javascript files).</p>
<p>Require.js is a dependency management and asynchronous script loading tool(AMD library).<br />
What that means? AMD stands for Asynchronous Module Definition.</p>
<p>RequireJS is asynchronous which means that you can do non-blocking and parallel fetch of your javascript files.</p>
<p>RequireJS is built around Module pattern. Modern web applications tend to have fairly complex front-ends. Module pattern should improve maintability of our bloated javascript code. Remmember those giant javascript files and svn merge conflict hell on your last project? Javascript code should consist of smaller components enforcing separation of concerns and avoiding globals since modules are wrapped by closures. </p>
<p>RequireJS is well defined and standardized. While we wait ES-Harmony to knock on our doors, libraries like RequireJS are giving us hint how we should structure our applications. Require.js works in current browsers.</p>
<h1><a name="t1" id="t1">Basic structure</a></h2>
<h4>STEP 1 &#8211; How to structure RequireJS project</h4>
<p>This is our sample RequireJS project structure.</p>
<p>|-[wepapp]<br />
|&#8212; [build]<br />
|&#8212;&#8211; r.js<br />
|&#8212;&#8211; build.js<br />
|&#8212;&#8211; build.single.js<br />
|&#8212; [js]<br />
|&#8212;&#8212; [app]<br />
|&#8212;&#8212; [lib]<br />
|&#8212;&#8212;&#8212; jquery.js<br />
|&#8212;&#8211; app.js<br />
|&#8212;&#8211; app-built.js*<br />
|-index.html<br />
|-readme.md</p>
<p>Build scripts and RequireJS optimization tool r.js are inside <strong>[build]</strong> directory.</p>
<p>Application code reside in <strong>[webapp/js/app]</strong> directory. This is place where you should place all of your /Model/View/Router/Template code.</p>
<p>Libraries like jQuery, Backbone, Handlebars and others are inside <strong>[webapp/js/lib]</strong> directory.</p>
<p>Application config and main entry point is <strong>[webapp/js/app.js]</strong></p>
<h4>STEP 2 &#8211; How to include RequireJS</h4>
<p>Your <strong>[webapp/index.html]</strong> should look like this</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;&lt;/</span>head<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">&gt;</span>Some sample content<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>script data<span style="color: #339933;">-</span>main<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;js/app.js&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;js/lib/require.js&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span><br />
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></div></div>
<p>Attribute <strong>[data-main="js/app.js"]</strong> is entry point of our application. This means that RequireJS will first load <strong>[js/app.js]</strong> after initialization.</p>
<h4>STEP 3 &#8211; How to configure RequireJS</h4>
<p>Open your <strong>[webapp/js/app.js]</strong> file.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">requirejs.<span style="color: #660066;">config</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; baseUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">'js/lib'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; paths<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; app<span style="color: #339933;">:</span> <span style="color: #3366CC;">'../app'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
requirejs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'jquery'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#test'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>[baseUrl: 'js/lib']</strong> &#8211; RequireJS loads code realative to directory specified in baseUrl. By default baseUrl is set to the same directory as data-main. If you dont specify data-main attribute and baseUrl property is not present in RequireJS config, than default path is directory that contains html page which include RequireJS library.<br />
<a href="http://requirejs.org/docs/api.html#config-baseUrl">base url</a></p>
<p>Attribute <strong>[paths]</strong> &#8211; <a href="http://requirejs.org/docs/api.html#config-paths">paths config</a></p>
<h4>STEP 4 &#8211; How to use requre() and define() in RequireJS</h4>
<p>require() and define() are most important concepts in RequireJS.</p>
<p><strong>define()</strong> &#8211; Used for module definition.<br />
Consists of module wrapper, dependency list and definition function.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">define(['dependency1', ['dependency2', ['dependency3'], function(dependency1, dependency2, dependency3) {<br />
&nbsp; &nbsp; var Module = function(value) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.property = value;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; return (Module);<br />
});</div></div>
<p><strong>require()</strong> &#8211; Used for dependency loading<br />
Consists of public api, dependency list, callback function</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">requirejs(['dependency1', ['dependency2', ['dependency3'], function(dependency1, dependency2, dependency3) {<br />
&nbsp; &nbsp; // You can use your imported module here<br />
&nbsp; &nbsp; var instance = dependency1;<br />
&nbsp; &nbsp; var constructorFunction = new dependency2();<br />
});</div></div>
<h1><a name="t2" id="t2">Prototypal inheritance and Require.js</a></h2>
<p>Skip to <a href="#source">complete source code</a>.</p>
<p>With RequireJS you can organize your modules in separate files with ease.</p>
<p>Now we are going to add 3 object definitions to our project: Category, Item and Specialized Item.</p>
<p>|-[wepapp]<br />
|&#8212; [build]<br />
|&#8212;&#8211; r.js<br />
|&#8212;&#8211; build.js<br />
|&#8212;&#8211; build.single.js<br />
|&#8212; [js]<br />
|&#8212;&#8212; [app]<br />
|&#8212;&#8212;&#8212; [category]<br />
|&#8212;&#8212;&#8212;&#8212; category.js<br />
|&#8212;&#8212;&#8212;&#8212; item.js<br />
|&#8212;&#8212;&#8212;&#8212; specializedItem.js<br />
|&#8212;&#8212; [lib]<br />
|&#8212;&#8212;&#8212; jquery.js<br />
|&#8212;&#8211; app.js<br />
|&#8212;&#8211; app-built.js*<br />
|-index.html<br />
|-readme.md</p>
<p>With RequireJS you can provide function or object in return statement. We are going to use this feature to retrieve constructor functions for our object.</p>
<p>With RequireJS you can import and use external dependencies inside your module. We are going to use this feature to achieve prototypal inheritance in RequireJS.</p>
<p><strong>[webapp/js/app/category/category.js]</strong></p>
<p>Category is collection of our Items.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(function() {<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; var Category = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.name = 'Default name';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.category = 'Default category';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.items = [];&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Category.prototype.showItems = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0, len = this.items.length; i &lt; len; i++) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(this.items[i]);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Category.prototype.addItem = function(item) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.items.push(item);<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; return (Category);<br />
<br />
});</div></div>
<p><strong>[webapp/js/app/category/item.js]</strong></p>
<p>Item is base object.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(function() {<br />
<br />
&nbsp; &nbsp; var Item = function(itemName) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.name = itemName;<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Item.prototype.getItemName = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; return this.name;<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; return (Item);<br />
<br />
});</div></div>
<p><strong>[webapp/js/app/category/specialItem.js]</strong></p>
<p>SpecialItem is object that extends Item. In order to extend Item object, we must import Item as a dependency<strong>['./item'].</strong><br />
Now Item object is available for use in our new SpecialItem module.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(['./item'], function(Item) {<br />
<br />
&nbsp; &nbsp; var SpecialItem = function(itemName) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.color = 'Default color';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.weigth = 'Default weigth';<br />
&nbsp; &nbsp; &nbsp; &nbsp; Item.call(this, itemName);<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; SpecialItem.prototype = new Item;<br />
&nbsp; &nbsp; SpecialItem.prototype.constructor = SpecialItem;<br />
<br />
&nbsp; &nbsp; return (SpecialItem);<br />
<br />
});</div></div>
<h1><a name="source" id="source">Source code listing</a></h1>
<p>[webapp/js/app.js]</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">requirejs.config({<br />
&nbsp; &nbsp; baseUrl: 'js/lib',<br />
&nbsp; &nbsp; paths: {<br />
&nbsp; &nbsp; &nbsp; &nbsp; app: '../app',<br />
&nbsp; &nbsp; &nbsp; &nbsp; jquery: 'jquery'<br />
&nbsp; &nbsp; }<br />
});<br />
<br />
requirejs(['jquery', 'app/category/category', 'app/category/item', 'app/category/specialItem'],<br />
function($, Category, Item, SpecialItem) {<br />
&nbsp; &nbsp; var c1 = new Category();<br />
&nbsp; &nbsp; c1.items.push(new Item('RequireJS in Action'));<br />
&nbsp; &nbsp; c1.items.push(new Item('Javascript Good parts'));<br />
&nbsp; &nbsp; c1.items.push(new Item('Some book'));<br />
&nbsp; &nbsp; c1.items.push(new SpecialItem('Special promotion book'));<br />
&nbsp; &nbsp; c1.showItems();<br />
&nbsp; &nbsp; console.log(c1);<br />
});</div></div>
<p>[webapp/js/app/category/category.js]</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(function() {<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; var Category = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.name = 'Default name';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.category = 'Default category';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.items = [];&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Category.prototype.showItems = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0, len = this.items.length; i &lt; len; i++) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(this.items[i]);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Category.prototype.addItem = function(item) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.items.push(item);<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; return (Category);<br />
<br />
});</div></div>
<p>[webapp/js/app/category/item.js]</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(function() {<br />
<br />
&nbsp; &nbsp; var Item = function(itemName) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.name = itemName;<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; Item.prototype.getItemName = function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; return this.name;<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; return (Item);<br />
<br />
});</div></div>
<p>[webapp/js/app/category/specialItem.js]</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;use strict&quot;;<br />
<br />
define(['./item'], function(Item) {<br />
<br />
&nbsp; &nbsp; var SpecialItem = function(itemName) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.color = 'Default color';<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.weigth = 'Default weigth';<br />
&nbsp; &nbsp; &nbsp; &nbsp; Item.call(this, itemName);<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; SpecialItem.prototype = new Item;<br />
&nbsp; &nbsp; SpecialItem.prototype.constructor = SpecialItem;<br />
<br />
&nbsp; &nbsp; return (SpecialItem);<br />
<br />
});</div></div>
<p>In next part of Require.js series I will talk about r.js RequireJS optimization and build tool.</p>
<p>Follow me on Twitter <a href="https://twitter.com/#!/svlada">click here </a></p>
<p><strong>References:</strong><br />
<a href="http://addyosmani.com/writing-modular-js/">http://addyosmani.com/writing-modular-js/</a><br />
<a href="http://msdn.microsoft.com/en-us/magazine/hh227261.aspx">http://msdn.microsoft.com/en-us/magazine/hh227261.aspx</a><br />
<a href="http://tomdale.net/2012/01/amd-is-not-the-answer/">http://tomdale.net/2012/01/amd-is-not-the-answer/</a><br />
<a href="http://tagneto.blogspot.com/2012/01/reply-to-tom-on-amd.html">http://tagneto.blogspot.com/2012/01/reply-to-tom-on-amd.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2012/06/29/require-js-dependency-management-part1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to get url parameters javascript</title>
		<link>http://www.svlada.com/blog/2012/06/15/how-to-get-url-parameters-javascript/</link>
		<comments>http://www.svlada.com/blog/2012/06/15/how-to-get-url-parameters-javascript/#comments</comments>
		<pubDate>Fri, 15 Jun 2012 08:42:55 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=231</guid>
		<description><![CDATA[This is simple function for extraction of query string parameters. I think that code is self explanatory. &#60;!-- &#160; &#160; Author: Stankovic Vladimir &#160; &#160; Contact: svlada@gmail.com --&#62; &#60;script type=&#34;text/javascript&#34;&#62; function p&#40;p&#41; &#123; console.log&#40;p&#41;; &#125; var testQueries = &#91; &#160; &#160; &#34;https://www.google.com/search?sugexp=chrome,mod=1&#38;sourceid=chrome&#38;ie=UTF-8&#38;q=how+to+get+url+params&#34;, &#160; &#160; &#34;https://www.google.com/search?sugexp=chrome,mod=1&#38;sourceid=chrome&#38;ie=UTF-8&#38;q=extract+query+string+javascript&#34; &#93;; function split&#40;query&#41; &#123; &#160; &#160; var queryString = &#123;&#125;; [...]]]></description>
			<content:encoded><![CDATA[<p>This is simple function for extraction of query string parameters. I think that code is self explanatory.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;!-- <br />
&nbsp; &nbsp; Author: Stankovic Vladimir<br />
&nbsp; &nbsp; Contact: svlada@gmail.com<br />
--&gt;<br />
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> p<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> testQueries <span style="color: #339933;">=</span> <br />
<span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;https://www.google.com/search?sugexp=chrome,mod=1&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=how+to+get+url+params&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;https://www.google.com/search?sugexp=chrome,mod=1&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=extract+query+string+javascript&quot;</span><br />
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> split<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> queryString <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> extractQueryString<span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> extractQueryStringRegular<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>parts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parts <span style="color: #339933;">=</span> extractQueryString<span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> extractQueryStringHashed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>parts<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> queryStringPairs <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryStringPairs.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">&amp;&amp;</span> queryStringPairs<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> queryStringPairs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> qm <span style="color: #339933;">=</span> queryStringPairs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>qm <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryString<span style="color: #009900;">&#91;</span>queryStringPairs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> queryItem <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>queryStringPairs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>qm<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> queryStringPairs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span>qm<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> queryName <span style="color: #339933;">=</span> decodeURIComponent<span style="color: #009900;">&#40;</span>queryItem<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> queryValue <span style="color: #339933;">=</span> decodeURIComponent<span style="color: #009900;">&#40;</span>queryItem<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryName.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryString<span style="color: #009900;">&#91;</span>queryName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> queryValue<span style="color: #339933;">;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> queryString<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> extractQueryString<span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> splitterFn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">==</span> query <span style="color: #339933;">||</span> undefined <span style="color: #339933;">==</span> query<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> splitterFn<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">length</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> extractQueryStringHashed<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> extractQueryStringRegular<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> testQueries.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> split<span style="color: #009900;">&#40;</span>testQueries<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; p<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>Result </p>
<p><a href="http://www.svlada.com/blog/wp-content/uploads/2012/06/javscripturlparams.png"><img src="http://www.svlada.com/blog/wp-content/uploads/2012/06/javscripturlparams.png" alt="" title="javscripturlparams" width="355" height="208" class="alignnone size-full wp-image-237" /></a></p>
<p><strong>Follow me on Twitter</strong> <a href="https://twitter.com/#!/svlada">click here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2012/06/15/how-to-get-url-parameters-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Proxy Ajax requests, Curl and Symfony 2</title>
		<link>http://www.svlada.com/blog/2012/06/11/proxy-ajax-requests-curl-and-symfony-2/</link>
		<comments>http://www.svlada.com/blog/2012/06/11/proxy-ajax-requests-curl-and-symfony-2/#comments</comments>
		<pubDate>Mon, 11 Jun 2012 19:23:15 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=217</guid>
		<description><![CDATA[In this article I will explain, how to make cross-domain requests through the proxy using Curl and Symfony 2. Jump to #source-code and skip following errata on how to proxy Ajax requests. Usual scenario looks like this 1. Client send ajax request to server 2. Your server forwards request to external/remote server 3. Waiting on [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I will explain, how to make cross-domain requests through the proxy using Curl and Symfony 2.</p>
<p>Jump to <a href="#source-code">#source-code</a> and skip following errata on how to proxy Ajax requests.</p>
<p>Usual scenario looks like this<br />
1. Client send ajax request to server<br />
2. Your server forwards request to external/remote server<br />
3. Waiting on response from remote server<br />
4. Parse and process response from remote server<br />
5. Send response back to client</p>
<p>First we shall check if client request is XmlHttpRequest using Symfony 2 built in method</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isXmlHttpRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></div>
<h3>STEP 1: Client code</h3>
<p>Client code is quite simple. </p>
<p>You need to craft Ajax request in following way.</p>
<p>1. Specify rest url on your server for handling cross-domain ajax requests.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;{{ path('_ajaxProxy') }}&quot;</span></div></div>
<p>2. Wrap request data</p>
<p>We need some data in order to create curl request on server-side.<br />
For this example you need to send object with following properties to your server.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; restUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;external-api-url&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Your target url on remote server</span><br />
&nbsp; &nbsp; method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Type of request you want to issue to remote server</span><br />
&nbsp; &nbsp; params<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; action<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;getFriendsList&quot;</span> <span style="color: #006600; font-style: italic;">// Parameters you are sending to remote server</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>STEP2: Forward request to remote server</h3>
<p>This step is bit tricky. </p>
<p>Client request is arrived to your server-side code. First you need to parse and validate request data.</p>
<p>To repeat once more &#8230; you need following data on server:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$restUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'restUrl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Your target url on remote server</span><br />
<span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Type of request you want to issue to remote server</span><br />
<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Parameters you are sending to remote server</span><br />
<span style="color: #000088;">$contentType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contentType'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Content-type</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>You need to create curl request and set parameters you have recieved from client.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Initialize curl handle</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set request url</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$restUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// TRUE to include the header in the output.</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// A custom request method to use instead of &quot;GET&quot; or &quot;HEAD&quot; when doing a HTTP request. &nbsp;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CUSTOMREQUEST<span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The full data to post in a HTTP &quot;POST&quot; operation.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <a href="http://www.php.net/http_build_query"><span style="color: #990000;">http_build_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>I strongly suggest to read php manual in order to get familiar with the curl configuration: http://php.net/manual/en/function.curl-setopt.php</p>
<p>You have enough data to send request to remote server. After executing curl handle, response from remote server is stored in $response variable.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>You have basic setup. Now we are going to add cookie support to our ajax proxy.</p>
<p>Why do we need cookies? </p>
<p>Recently I needed to integrate symfony 2 application with wordpress portal. Authentication is done on WordPress side, and symfony application is using wordpress cookies to authorize users for accessing protected featues.</p>
<p>In production both applications share same domain, but in test enviorment there is a lot of mess (different domains, ports etc).</p>
<p>Where is problem? </p>
<p>Application written in Symfony 2 need to consume some protected backend functionalities on WordPress side. There is a lot personalized data that is fetched via ajax calls, and this is reason why we need cookies.</p>
<h3>How to get cookies from Symfony2 request and set mutplie cookies to Curl?</h3>
<p>We need to extract multiple cookies from Symfony 2 request object and set them to curl handle.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get all cookies from Symfony request object</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$requestCookies</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cookies</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Prepare and set multiple cookies to curl handle</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$cookieArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$requestCookies</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cookieName</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$cookieValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookieArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$cookieName}</span>=<span style="color: #006699; font-weight: bold;">{$cookieValue}</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Be sure to set whitespace after '; ' when creating cookie string</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$cookie_string</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'; '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookieArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h3>How to get cookies Curl response and set multiple cookies to Symfony response?</h3>
<p>Remmember when we configured curl with CURLOPT_HEADER=true? Now we are going to parse cookies from curl http response.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get header and response data from curl response</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/list"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// We are using regex to parse cookies from curl response</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/preg_match_all"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Set-Cookie: (.*)\b/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Store cookies</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$cookies</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cookies</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Raw cookies are parsed and stored in cookie array. Then each cookie need to be converted to Symfony Cookie object and injected to Symfony response headers.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookies</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rawCookie</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookie</span> <span style="color: #339933;">=</span> \Symfony\Component\BrowserKit\Cookie<span style="color: #339933;">::</span><span style="color: #004000;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rawCookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'+'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$customCookie</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cookie<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpiresTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">null</span>?<span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpiresTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><a href="http://www.php.net/setcookie"><span style="color: #990000;">setCookie</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$customCookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>FINAL NOTICE: </p>
<p>Close and store session data before initializing curl handle or you can run into deadlock.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/session_write_close"><span style="color: #990000;">session_write_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>What could happen if you forgot to close session?</p>
<p>Here is one scenario:</p>
<p>PHP script S1 create/sends curl POST request to script S2 on same Apache server/PHP Enviorment. If page S1 and S2 share same session, script S2 will not start executing until end of script S1 lifetime. But script S1 is waiting on response from script S2. This is point where deadlock happen. The default session handler locks the session file for the duration of the page request.</p>
<p><a id="source-code" name="source-code">Complete source code listing for ajax proxy</a><br />
<strong>Client sample code</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> sendAjaxRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'json'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;{{ path('_ajaxProxy') }}&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restUrl <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/external-api-url/&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;getFriendList&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> textStatus<span style="color: #339933;">,</span> jqXHR<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Server proxy code</strong></p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">namespace</span> Proxy\Bundle\ProxyBundle\Controller<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Template<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Symfony\Bundle\FrameworkBundle\Controller\Controller<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Method<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Route<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Symfony\Component\HttpFoundation\Response<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Symfony\Component\HttpFoundation\Request<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Symfony\Component\HttpFoundation\Cookie<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> AjaxProxyController <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* @Route(&quot;/r/ajax/proxy&quot;, name=&quot;_ajaxProxy&quot;)<br />
&nbsp; &nbsp; &nbsp;* @Template()<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> proxyAction<span style="color: #009900;">&#40;</span>Request <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Forbid every request but jquery's XHR</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isXmlHttpRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// isn't it an Ajax request?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Response<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$restUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'restUrl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$contentType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contentType'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$contentType</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$contentType</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$restUrl</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$method</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'POST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'DELETE'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Response<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$contentType</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/session_write_close"><span style="color: #990000;">session_write_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$restUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CUSTOMREQUEST<span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <a href="http://www.php.net/http_build_query"><span style="color: #990000;">http_build_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$requestCookies</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cookies</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookieArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$requestCookies</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cookieName</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$cookieValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookieArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$cookieName}</span>=<span style="color: #006699; font-weight: bold;">{$cookieValue}</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookie_string</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'; '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookieArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/list"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/preg_match_all"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Set-Cookie: (.*)\b/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookies</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cookies</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Response<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$contentType</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Response<span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$contentType</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookies</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rawCookie</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cookie</span> <span style="color: #339933;">=</span> \Symfony\Component\BrowserKit\Cookie<span style="color: #339933;">::</span><span style="color: #004000;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rawCookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'+'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$customCookie</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cookie<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpiresTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">null</span>?<span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpiresTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><a href="http://www.php.net/setcookie"><span style="color: #990000;">setCookie</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$customCookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Read more about this topic:</strong> </p>
<p>http://developer.yahoo.com/javascript/howto-proxy.html</p>
<p><strong>Follow me on Twitter</strong> <a href="https://twitter.com/#!/svlada">click here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2012/06/11/proxy-ajax-requests-curl-and-symfony-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Segmentacija bazirana na teksturama</title>
		<link>http://www.svlada.com/blog/2011/11/12/segmentacija-bazirana-na-teksturama/</link>
		<comments>http://www.svlada.com/blog/2011/11/12/segmentacija-bazirana-na-teksturama/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 18:17:08 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Image Processing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Vision]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=195</guid>
		<description><![CDATA[Slike iz prirodne okoline su najčešće jako kompleksne. Površine različitih objekata mogu biti prekrivene teksturama koje nemaju jasno izražene ivice. Uzmimo za primer koru drveta, keramičke pločice, ljudsku kožu, okean. Njihova struktura je ono što jasno karakteriše izgled površina koje zauzimaju. Poznat je veliki broj metoda za opisivanje i ekstrakciju tekstura, međutim ne postoji jedinstvena [...]]]></description>
			<content:encoded><![CDATA[<p>Slike iz prirodne okoline su najčešće jako kompleksne. Površine različitih objekata mogu biti prekrivene teksturama koje nemaju jasno izražene ivice. Uzmimo za primer koru drveta, keramičke pločice, ljudsku kožu, okean. Njihova struktura je ono što jasno karakteriše izgled površina koje zauzimaju. Poznat je veliki broj metoda za opisivanje i ekstrakciju tekstura, međutim ne postoji jedinstvena definicija teksture kao ni jedinstven matematički model koji bi proizveo teksturu. Kao još uvek potpuno neistražena analiza tekstura predstavlja zanimljivu oblast za naučnike i istraživače koji se bave kompjuterskom vizijom.<br />
Uglavnom na slikama postoji više od jedne teksture. Glavni cilj je na neki način opisati teksturu na osnovu određenih matematičkih mera, izvršiti prepoznavanje a zatim je klasifikovati prema skupu unapred određenih vrednosti/mera u odgovarajuću klasu. </p>
<h2>Ekstrakcija osobina tekstura pomoću GLC matrica</h2>
<p>GLC matrica (<em>Grey-Level Co-occurrence Matrix</em>) je tabelerna predstava informacija koje se odnose na to koliko često se različite kombinacije osvetljenosti pojavljuju u slici. Primenom metoda GLC matrica moguće je izračunati različite osobine tekstura u određenom regionu slike, nezavisno od slike. Pre nego što krenemo u detaljnu diskusiju o načinu funkcionisanja GLC matrica treba napomenuti da postoje različite metode kojima je moguće izvući različite osobine tekstura. Postoje metodi prvog, drugog, trećeg i viših redova. </p>
<ul>
<li>
<strong>Metode prvog reda tekstura</strong> su statističke mere koje se mogu izračunati direktno iz slike, neuzimajući u obzir međusobne veze između piksela. Pogodne su za slike  koje imaju slučajnu teksturu. Neke od vrednosti koje se mogu izračunati su <em>varijansa, srednja vrednost, devijacija</em>&#8230; Kompleksnije metode prvog reda, kao što su <em>momenti, apsolutni momenti, centralni momenti ili entropija</em>, moguće je izračunati preko histograma slike.
</li>
<li><strong>Metode drugog reda tekstura</strong> imaju svest od vezi u grupama od između dva najčešće susednih piksela. </li>
<li><strong>Metode trećeg i viših redova tekstura</strong> (razmatrajući veze između tri i više piksela) su teorijski moguće, ali se ne sreću često u praksi zbog izuzetno velike vremenske zahtevnosti i težine interpretacije. </li>
</ul>
<p>Nakon kraćeg osvrta na različite metode ekstrakcije tekstura iz slika nastavljamo se opisom funkcionisanja GLC matrica kao pripadnika metoda drugog reda. Svaki element koonkurense matrice predstavlja verovatnoću pojave para tačaka u određenom regionu slike. U daljem razmatranju piksel koji trenutno posmatramo definišemo kao referentni piksel a njegovog suseda kao susedni piksel. Radi lakšeg razumevanja GLC matrica jednostavnim primerom ilustrovaćemo funkcionisanje ovog metoda. Na slici ispod nalazi se primer jednog regiona u okviru slike.</p>
<p><a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc1.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc1.jpg" alt="Primer regiona slike" title="Primer regiona slike" width="194" height="108" class="alignnone size-full wp-image-196" /></a></p>
<p>Iteracijom kroz region slike sa leva na desno, svaki piksel u jednom trenutku postaje referentni piksel, počevši od piksela u gornjem levem uglu regiona. Pikseli koji se nalaze uz desnu ivicu nemaju svog suseda pa se ne uzimaju u razmatranje. U okviru ovog posta i prateće implementacije kao rastojanje između dva piksela koristi se korak od jednog piksela.</p>
<p><a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc2.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc2.jpg" alt="Pikaz sadržaja GLC matrice" title="Pikaz sadržaja GLC matrice" width="371" height="158" class="alignnone size-full wp-image-197" /></a><br />
<em>*U zaglavlju kolona priložene tabele nalaze se moguće vrednosti inteziteta susednog piksela. U zaglavlju redova se nalaze vrednosti referentnog piksela. U preseku reda i kolone nalazi se broj ponavljanja tranzicija između susednog i referentnog piksela</em></p>
<p>Tabela iznad je priložena radi lakšeg razumevanja sadržaja same GLC matrice. Ćelija sa koordinatama (0, 0) se popunjava sa brojem koji označava koliko puta u određenom regionu slike, referentni piksel sa intezitetom 0 ima desnog suseda sa intezitetom 0. Postoji četiri GLC matrice koje pokrivaju susede koji se nalaze pod različitim uglovima u odnosu na referentni.</p>
<p>Izračunavanje osobina tekstura zahteva simetričnu matricu. Sledeći korak je dovođenje matrice u simetričnu formu. Simetrična matrica znači da se iste vrednosti ponavalju na suprotnim stranama u odnosu na dijagonalu, primera radi sadržaj ćelije (1, 2) bi morao biti jednak sadržaju ćelije (2, 1). </p>
<p>Ukoliko bi računali vrednosti samo u jednom smeru, rezultat ne bi bio simetrična matrica. Primer vrednosti dobijenih računanjem ponavaljanja samo u smeru ka istoku</p>
<p><a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc3.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc3.jpg" alt="Prikaz sadržaja nakon računanja vrednosti u smeru ka istoku" title="Prikaz sadržaja nakon računanja vrednosti u smeru ka istoku" width="316" height="165" class="alignnone size-full wp-image-200" /></a></p>
<p>Ukoliko se brojanje ponavljanja vrši na ovaj način, samo u jednom pravcu, dobijena matrica neće biti simetrična. Vrednosti sa suprotnih strana nisu jednake. Simetrija može biti ostvarena ukoliko se svaki par piksela posmatra u oba pravca, kada je reč o horizonalnoj GLC matrici, levo i desno. Svaka pojava para X1 i X2 inkrementirala bi brojač u dva polja za 1, na lokacijama X1X2 i X2X1. Tako dobijena matrica može se smatrati simetričnom i naziva se horizontalna matrica.</p>
<p>Nakon izračunavanja četiri GLC matrice, po jedna za svaku orijentaciju 0̊, 45̊, 90̊ i 135̊ potrebno je izvršiti normalizaciju vrednosti u tabelama. Mere osobina tekstura zahtevaju da broj u ćeliji bude verovatnoća ponavaljanja a ne samo broj. Kako bi izračunali verovatnoću potrebno je vrednost svake ćelije podeliti sa brojem ukupnih ponavaljanja vrednosti tabele. </p>
<p>Opšti pojam verovatnoće može se definisati kao “broj ponavljanja nekog događaja, podeljen ukupnim brojem mogućih ishoda”. </p>
<p>Normalizaciju GLC tabele na osnovu prethodne definicije možemo predstaviti pomoću sledeće formule (gde i predstavlja red, a j kolonu)</p>
<p><a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc4.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc4.jpg" alt="" title="Formula" width="135" height="67" class="alignnone size-full wp-image-202" /></a></p>
<p>Ukratko gore opisani koraci za dobijanje simetrične normalizovane GLC matrice se mogu opisati sledećim koracima</p>
<ul>
<li>Kreirati GLC matrice po jedna za svaku orijentaciju 0̊, 45̊, 90̊ i 135̊</li>
<li>Dimenzije matrice jednake su broju nijansi sive u posmatranoj slici</li>
<li>U svaku ćeliju upisati broj ponavaljanja</li>
<li>Dobijenoj matrici sabrati sa svojom transponovanom verzijom kako bi se dobila simetrična matrica.</li>
<li>Normalizacija vrednosti u GLC matrice kako bi se generisale verovatnoće za svaki ishod</li>
</ul>
<p>GLC matrice imaju isti broj redova i kolona kao kvantizacioni nivo slike. To znači da je za jednu 8bitnu grayscale sliku potrebna kvadratna matrica dimenzija 256&#215;256, sa 65 536 ćelija. Ukoliko bi ulazna slika imala 16bitni podatak o nijansi sive, dimenzije GLC matrice bi iznosile 65 536&#215;65 536 što bi ukupno dalo 429 496 720 ćelija. Primećuje se da veći nivo detalja slike uključuje znatno veći broj iteracija. </p>
<p>Određen broj aplikacija žrtvuje nivo detalja slike zarad manjeg procesorskog opterećenja, pa se 8 bitne slike često pretvaraju u 4 bitne (16&#215;16 matrica sa 256 ćelija). Postoji još jedan razlog za degradaciju kvaliteta i kompresiju podatka o intezitetu slike. Ukoliko se koriste sve ćelije matrice dimenzija 256&#215;256, veliki broj ćelija imao bi vrednost nula. GLC matrice posmatraju verovatnoće između dva piksela, pa bi veliki broj nula u tabeli doprineo lošoj aproksimaciji. Ukoliko je broj sivih nijansi u slici smanjen, smanjuje se i broj nultih vrednosti u tabeli, pa se i statistička validnost znatno poboljšava.</p>
<p>Nakon kreiranja i incijalizacije GLC matrica potrebno je izvući korisne informacije, statističke podatke o teksturama. Rezultat računanja pojedinačne osobine teksture je broj koji opisuje region slike. Osobine se mogu podeliti u tri grupe. Prva grupa sadrži metode bazirane na kontrastu, a to su : Kontrast, Homogenost i Raznolikost. Druga grupa predstavlja metode koje opisiju uređenost tekstura : energija i entropija. Metode ove grupe pokazuju koliko je neka tekstura regularna u okviru klizećeg prozora. Metode korelacije mere linearnu zavisnost sivih nivoa susednih piksela</p>
<p>Priložene su jednačine za ekstrakciju osobina tekstura : </p>
<p><strong>Entropija </strong><br />
Entropija daje meru kompleksnosti slike. Kompleksne teksture uglavnom imaju veću entropiju.<br />
<a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc5.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc5.jpg" alt="" title="Entropija" width="200" height="62" class="alignnone size-full wp-image-204" /></a><br />
<strong>Kontrast  </strong><br />
Kontrast je mera kontrasta slike, preciznije količine lokalnih varijacija u intezitetu posmatrane slike.<br />
<a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc6.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc6.jpg" alt="" title="Kontrast" width="175" height="71" class="alignnone size-full wp-image-205" /></a><br />
Homogenost<br />
Homogenost je osobina teksture suprotna kontrastu<br />
<a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc7.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc7.jpg" alt="" title="Homogenost" width="163" height="68" class="alignnone size-full wp-image-206" /></a><br />
Energija<br />
<a href="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc8.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/11/glc8.jpg" alt="" title="Energija" width="135" height="64" class="alignnone size-full wp-image-207" /></a></p>
<p>Na kraju prilažem i implementaciju metode za ekstrakciju osobina tekstura.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Author: Stankovic Vlada svlada@gmail.com</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FeatureExtractorGLCM <br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> dw<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> dh<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> nW<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> nH<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> height<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> width<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> img<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> featureMatrix<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> glcm0, glcm45, glcm90, glcm135<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> FeatureExtractorGLCM<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> p_img, <span style="color: #000066; font-weight: bold;">int</span> p_dw, <span style="color: #000066; font-weight: bold;">int</span> p_dh<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; img <span style="color: #339933;">=</span> p_img<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; height <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; width <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; dw <span style="color: #339933;">=</span> p_dw<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dh <span style="color: #339933;">=</span> p_dh<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">prepareImage</span><span style="color: #009900;">&#40;</span>p_img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// nW*nH Total number of feature vectors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Every feature vector consists of six features</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// rowMin, rowMax, colMin, colMax, Homogenity, Contrast</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// featureMatrix = new double[nW*nH][20];</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// glcm matrixes for all orientations of image</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; glcm0 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; glcm45 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; glcm90 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; glcm135 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//16,8</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> FeatureExtractorGLCM<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> p_img<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>p_img, <span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinkedlist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LinkedList</span></a> glcmWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Extend image size if there is need, if block size is unstandard<br />
&nbsp; &nbsp; &nbsp;* @param p_img<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> prepareImage<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> p_img<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newWidth <span style="color: #339933;">=</span> width<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newHeight <span style="color: #339933;">=</span> height<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newWidth <span style="color: #339933;">%</span> dw<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newWidth <span style="color: #339933;">=</span> width <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>dw<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">%</span>dw<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newHeight <span style="color: #339933;">%</span> dh<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newHeight <span style="color: #339933;">=</span> height <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>dh<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">%</span>dh<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> filteredImage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a><span style="color: #009900;">&#40;</span>newWidth, newHeight, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a>.<span style="color: #006633;">TYPE_INT_RGB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> row <span style="color: #339933;">&lt;</span> height<span style="color: #339933;">;</span> row<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> column <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> column <span style="color: #339933;">&lt;</span> width<span style="color: #339933;">;</span> column<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pixVal <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRGBPixel</span><span style="color: #009900;">&#40;</span>img, column, row<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImgUtil.<span style="color: #006633;">setRGBPixelByValue</span><span style="color: #009900;">&#40;</span>filteredImage, column, row, pixVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; height <span style="color: #339933;">=</span> newHeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; width <span style="color: #339933;">=</span> newWidth<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; nW <span style="color: #339933;">=</span> width<span style="color: #339933;">/</span>dw<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; nH <span style="color: #339933;">=</span> height<span style="color: #339933;">/</span>dh<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; featureMatrix <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span>nW<span style="color: #339933;">*</span>nH<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; img <span style="color: #339933;">=</span> filteredImage<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> filteredImage<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> calculateGLCM<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> p_row, <span style="color: #000066; font-weight: bold;">int</span> p_col<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> i2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pixCnt0 &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt45 &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt90 &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt135 &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> p_row<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> p_row<span style="color: #339933;">+</span>dh<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> p_col<span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> p_col<span style="color: #339933;">+</span>dw<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Generation of GLC, angle 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// komsijski pixel</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i2 <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> offsetX<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j2 <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> offsetY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i2 <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> i2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&lt;</span> width <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> referencePix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> neiPix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j2, i2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm0<span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm0<span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt0<span style="color: #339933;">+=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Generation of GLC, angle 45 </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Neighbourh pixel</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i2 <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> offsetX<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j2 <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> offsetY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i2 <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> i2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&lt;</span> width <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> referencePix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> neiPix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j2, i2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm45<span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm45<span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt45<span style="color: #339933;">+=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp;Generation of GLC, angle 90</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Neighbourh pixel</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i2 <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> offsetX<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j2 <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> offsetY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i2 <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> i2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&lt;</span> width <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> referencePix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> neiPix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j2, i2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm90<span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm90<span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt90<span style="color: #339933;">+=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Generation of GLC, angle 135</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Neighbourh pixel</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i2 <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> offsetX<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j2 <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> offsetY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i2 <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> i2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> j2 <span style="color: #339933;">&lt;</span> width <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> referencePix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> neiPix <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, j2, i2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm135<span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm135<span style="color: #009900;">&#91;</span>neiPix<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>referencePix<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixCnt135<span style="color: #339933;">+=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end_for j</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end_for i</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// GLC Normalization</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm0<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/=</span> pixCnt0<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm45<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/=</span> pixCnt45<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm90<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/=</span> pixCnt90<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glcm135<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/=</span> pixCnt135<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> populateFeatureMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> mean <span style="color: #339933;">=</span> calculateMean<span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> dev <span style="color: #339933;">=</span> calculateStandardDeviation<span style="color: #009900;">&#40;</span>img, mean<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dev &quot;</span> <span style="color: #339933;">+</span> dev<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> cnt <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> maxDev <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> height<span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span>dh<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> width<span style="color: #339933;">;</span> j<span style="color: #339933;">+=</span>dw<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">calculateGLCM</span><span style="color: #009900;">&#40;</span>i, j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> featureCalcMean<span style="color: #009900;">&#40;</span>i, j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> featureCalcDeviation<span style="color: #009900;">&#40;</span>i, j, mean<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> maxDev<span style="color: #009900;">&#41;</span> maxDev <span style="color: #339933;">=</span> featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> feat <span style="color: #339933;">=</span> featureCalcContrast<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat <span style="color: #339933;">=</span> featureCalcHomogeneity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat <span style="color: #339933;">=</span> featureCalcEnergy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> feat<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Homogenity</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][6] &gt; 0.5 &amp;&amp; featureMatrix[cnt][6] &lt; 1 &amp;&amp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][7] &gt; 0.5 &amp;&amp; featureMatrix[cnt][7] &lt; 1 &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][8] &gt; 0.5 &amp;&amp; featureMatrix[cnt][8] &lt; 1 &amp;&amp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][9] &gt; 0.5 &amp;&amp; featureMatrix[cnt][9] &lt; 1 &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][1] &gt; maxDev/3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][13] &gt; 0.001 &amp;&amp; featureMatrix[cnt][13] &lt; 0.1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix[cnt][11] &gt; 0.001 &amp;&amp; featureMatrix[cnt][11] &lt; 0.1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> featureMatrix<span style="color: #009900;">&#91;</span>cnt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> i<span style="color: #339933;">+</span>dh<span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> y <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImgUtil.<span style="color: #006633;">setRGBPixelByRGB</span><span style="color: #009900;">&#40;</span>img, 0xff, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, j, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> i<span style="color: #339933;">+</span>dh<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> i<span style="color: #339933;">+</span>dh<span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">&lt;</span> height <span style="color: #339933;">&amp;&amp;</span> y <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImgUtil.<span style="color: #006633;">setRGBPixelByRGB</span><span style="color: #009900;">&#40;</span>img, 0xff, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, j, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> j<span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> j<span style="color: #339933;">+</span>dw<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> width <span style="color: #339933;">&amp;&amp;</span> x <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImgUtil.<span style="color: #006633;">setRGBPixelByRGB</span><span style="color: #009900;">&#40;</span>img, 0xff, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, x, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> j<span style="color: #339933;">+</span>dw<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> j<span style="color: #339933;">+</span>dw<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> width <span style="color: #339933;">&amp;&amp;</span> x <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImgUtil.<span style="color: #006633;">setRGBPixelByRGB</span><span style="color: #009900;">&#40;</span>img, 0xff, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, x, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FSUtil.<span style="color: #006633;">dumpToHtml</span><span style="color: #009900;">&#40;</span>featureMatrix, <span style="color: #0000ff;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>feature.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//FSUtil.dumpMatrixToTxt(featureMatrix, &quot;Feature Matrix&quot;);</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; Returns a measure of the intensity contrast between a pixel and its neighbor over the whole image.<br />
&nbsp; &nbsp; &nbsp; &nbsp; Range = [0 (size(GLCM,1)-1)^2] <br />
&nbsp; &nbsp; &nbsp; &nbsp; Contrast is 0 for a constant image.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> featureCalcContrast<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal0 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> glcm0.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> glcm0<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal0 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #339933;">*</span> glcm0<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> glcm45<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> glcm90<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span> glcm135<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>retVal0, retVal45, retVal90, retVal135<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Returns a value that measures the closeness of the distribution of elements in the GLCM to the GLCM diagonal.<br />
&nbsp; &nbsp; &nbsp; &nbsp; Range = [0 1]<br />
&nbsp; &nbsp; &nbsp; &nbsp; Homogeneity is 1 for a diagonal GLCM.<br />
&nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> featureCalcHomogeneity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal0 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> glcm0.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> glcm0<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal0 <span style="color: #339933;">+=</span> glcm0<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">+=</span> glcm45<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">+=</span> glcm90<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">+=</span> glcm135<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">-</span> j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>retVal0, retVal45, retVal90, retVal135<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Returns the sum of squared elements in the GLCM.<br />
&nbsp; &nbsp; &nbsp; &nbsp; Range = [0 1]<br />
&nbsp; &nbsp; &nbsp; &nbsp; Energy is 1 for a constant image.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> featureCalcEnergy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal0 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> glcm0.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> glcm0<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal0 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>glcm0<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal45 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>glcm45<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal90 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>glcm90<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal135 <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>glcm135<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>retVal0, retVal45, retVal90, retVal135<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> featureCalcMean<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> p_row, <span style="color: #000066; font-weight: bold;">int</span> p_col<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> p_row<span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> p_row<span style="color: #339933;">+</span>dh<span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> p_col<span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> p_col<span style="color: #339933;">+</span>dw<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal<span style="color: #339933;">+=</span>ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, x, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">=</span> retVal<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">*</span>width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> retVal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> featureCalcDeviation<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> p_row, <span style="color: #000066; font-weight: bold;">int</span> p_col, <span style="color: #000066; font-weight: bold;">double</span> p_mean<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> p_row<span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> p_row<span style="color: #339933;">+</span>dh<span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> p_col<span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> p_col<span style="color: #339933;">+</span>dw<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pixel <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>img, x, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span>p_mean <span style="color: #339933;">-</span> pixel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">=</span> retVal<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">*</span>width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> retVal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Mean</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">double</span> calculateMean<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> p_img<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> height <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> width <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> row <span style="color: #339933;">&lt;</span> height<span style="color: #339933;">;</span> row<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> column <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> column <span style="color: #339933;">&lt;</span> width<span style="color: #339933;">;</span> column<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal<span style="color: #339933;">+=</span>ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>p_img, column, row<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">=</span> retVal<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">*</span>width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> retVal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Standard deviation</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">double</span> calculateStandardDeviation<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedimage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedImage</span></a> p_img, <span style="color: #000066; font-weight: bold;">double</span> p_mean<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> retVal <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> height <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> width <span style="color: #339933;">=</span> p_img.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> row <span style="color: #339933;">&lt;</span> height<span style="color: #339933;">;</span> row<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> column <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> column <span style="color: #339933;">&lt;</span> width<span style="color: #339933;">;</span> column<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pixel <span style="color: #339933;">=</span> ImgUtil.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>p_img, column, row<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span>p_mean <span style="color: #339933;">-</span> pixel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; retVal <span style="color: #339933;">=</span> retVal<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">*</span>width<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#41;</span> retVal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printMatrix<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> p_Matrix<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> p_Matrix.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> p_Matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; | &quot;</span> <span style="color: #339933;">+</span> p_Matrix<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Follow me on Twitter</strong> <a href="https://twitter.com/#!/svlada">click here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2011/11/12/segmentacija-bazirana-na-teksturama/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven &#8211; unable to update index for central</title>
		<link>http://www.svlada.com/blog/2011/09/22/unable-to-update-index-for-centralhttprepo1-maven-orgmaven2/</link>
		<comments>http://www.svlada.com/blog/2011/09/22/unable-to-update-index-for-centralhttprepo1-maven-orgmaven2/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 18:52:58 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=123</guid>
		<description><![CDATA[Recently I wanted to refresh my maven repos, but the following problem have occured Unable to update index for central&#124;http://repo1.maven.org/maven2 If you experience this problem, try to delete contents in the following directory workspace_location\.metadata\.plugins\org.maven.ide.eclipse\nexus Before directory deletion ensure that your Eclipse instance is not running. After you deleted directory go to Window->Preferences->Maven and enable checkbox [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I wanted to refresh my maven repos, but the following problem have occured</p>
<blockquote><p>Unable to update index for central|http://repo1.maven.org/maven2</p></blockquote>
<p>If you experience this problem, try to delete contents in the following directory</p>
<blockquote><p>workspace_location\.metadata\.plugins\org.maven.ide.eclipse\nexus</p></blockquote>
<p>Before directory deletion ensure that your Eclipse instance is not running.</p>
<p>After you deleted directory go to <em>Window->Preferences->Maven</em> and enable checkbox named <em>&#8220;Download repository index updates on startup&#8221;</em>.<br />
Press OK and restart Eclipse. </p>
<p>Next time you start Eclipse Nexus indexer will update repository indexes.</p>
<p><strong>Follow me on Twitter</strong> <a href="https://twitter.com/#!/svlada">click here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2011/09/22/unable-to-update-index-for-centralhttprepo1-maven-orgmaven2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WSH Javascript remove duplicates from array</title>
		<link>http://www.svlada.com/blog/2011/09/15/wsh-javascript-remove-duplicates-from-array/</link>
		<comments>http://www.svlada.com/blog/2011/09/15/wsh-javascript-remove-duplicates-from-array/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 16:49:03 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Comp Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Sorting]]></category>
		<category><![CDATA[wsh]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=109</guid>
		<description><![CDATA[How to remove duplicates from array in Javascript? First, let&#8217;s see what the problem is. We have an unsorted array with integer numbers. 20, 10, 9, 4, 9, 30, 20, 4, 9 We expect our algorithm to output sorted array without any duplicates. Step 1 &#8211; Sort array The first step is to sort array [...]]]></description>
			<content:encoded><![CDATA[<p>How to remove duplicates from array in Javascript?<br />
First, let&#8217;s see what the problem is. We have an unsorted array with integer numbers.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span></div></div>
<p>We expect our algorithm to output sorted array without any duplicates.</p>
<p><strong>Step 1 &#8211; Sort array</strong></p>
<p>The first step is to sort array of numbers. Sorting an array alphabetically can be achieved by calling following function</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">array.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>But what if you want to sort numbers? When executed without any parameters, javascript built-in sort() function produces the following result:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span></div></div>
<p>Array.sort() accepts optional parameter in the form of function pointer. The only thing you need to do is to provide the method that defines sort order(compares two numbers).</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">array.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> a <span style="color: #339933;">-</span> b <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Less than 0: Sort &#8220;a&#8221; to be a lower index than &#8220;b&#8221;<br />
Zero: &#8220;a&#8221; and &#8220;b&#8221; should be considered equal, and no sorting performed.<br />
Greater than 0: Sort &#8220;b&#8221; to be a lower index than &#8220;a&#8221;.</p>
<p>You can always implement some custom sort function. In this example insertion sort implementation is provided as alternative to built-in javascript sort function.</p>
<p><strong>Step 2 &#8211; Remove duplicates</strong></p>
<p>Loop through array starting with the second element. If the current and the previous elements in the array are the same, delete current element. At the end you will have sorted array without duplicates.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> array.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> i<span style="color: #339933;">++;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Complete source code listing</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//@author Stankovic Vlada [svlada@gmail.com]</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> testArrays <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; sorted<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; unsorted<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
removeDuplicates<span style="color: #009900;">&#40;</span>testArrays.<span style="color: #660066;">sorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
printArray<span style="color: #009900;">&#40;</span>testArrays.<span style="color: #660066;">sorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
removeDuplicates<span style="color: #009900;">&#40;</span>testArrays.<span style="color: #660066;">unsorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
printArray<span style="color: #009900;">&#40;</span>testArrays.<span style="color: #660066;">unsorted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> removeDuplicates<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; array.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> a <span style="color: #339933;">-</span> b <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// insertionSort(array);</span><br />
&nbsp; &nbsp; printArray<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> array.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> i<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> array<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> printArray<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> insertionSort<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> list.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> list<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> j <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> key <span style="color: #339933;">&lt;</span> list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <span style="color: #339933;">=</span> i <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; list<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> key<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Follow me on Twitter</strong> <a href="https://twitter.com/#!/svlada">click here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2011/09/15/wsh-javascript-remove-duplicates-from-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to evaluate Xpath when DOCTYPE present in XML declaration</title>
		<link>http://www.svlada.com/blog/2011/09/14/how-to-evaluate-xpath-when-doctype-present-in-xml-declaration/</link>
		<comments>http://www.svlada.com/blog/2011/09/14/how-to-evaluate-xpath-when-doctype-present-in-xml-declaration/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 17:45:27 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[wsh]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=78</guid>
		<description><![CDATA[At the end of the article, the complete source code, compiled in one place, is available. I had problem when loading and evaluating Xml document with declaration. Parsing breaks when you try to parse Xml document with a declaration on top. The first idea was to remove the declaration from Xml until we finish manipulating [...]]]></description>
			<content:encoded><![CDATA[<p>At the end of the article, the complete source code, compiled in one place, is available.</p>
<p>I had problem when loading and evaluating Xml document with <!DOCTYPE ...> declaration. Parsing breaks when you try to parse Xml document with <!DOCTYPE ...> a declaration on top.</p>
<p>The first idea was to remove <!DOCTYPE ...> the declaration from Xml until we finish manipulating DOM Nodes.<br />
You can just preprocess your xml as following way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> rexCleanDoctype <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;!DOCTYPE.*?&gt;/gm</span><span style="color: #339933;">;</span><br />
content <span style="color: #339933;">=</span> content.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>rexCleanDoctype<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
XmlDocument.<span style="color: #660066;">loadXML</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Now you can evaluate XPath expression and everything works like a charm.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Node <span style="color: #339933;">=</span> XmlDocument.<span style="color: #660066;">selectSingleNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//docroot/dir&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This is not a very clean and proper solution. So let&#8217;s get down to business and see what is happening inside. The first thing to remember is: &#8220;Always check for parsing errors&#8221;! They can give you a clue of what is happening. The following function will report whether there have been parsing errors.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> printParseError<span style="color: #009900;">&#40;</span>xmlDoc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlDoc.<span style="color: #660066;">parseError</span>.<span style="color: #660066;">errorCode</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> myErr <span style="color: #339933;">=</span> xmlDoc.<span style="color: #660066;">parseError</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; WScript.<span style="color: #660066;">Echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Xml parse error: &quot;</span> <span style="color: #339933;">+</span> myErr.<span style="color: #660066;">reason</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now when we try to load Xml file with <!DOCTYPE ...> declaration, we get the following error message:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Xml parse error<span style="color: #339933;">:</span> DTD <span style="color: #000066; font-weight: bold;">is</span> prohibited</div></div>
<p>What to do now?</p>
<p>After few more hours of reading documentation I have found out were the problem lays.</p>
<p>The problem occurs when ProhibitDTD property is set to true. Microsoft changed default version of <em>ProhibitDTD</em> property to true in MSXML 6.0.<br />
Earlier versions of MSXML(3.0, 4.0 and 5.0) had this property set to false by default. This can lead to various security implications, such as denial of service attack. There is great article on MSDN Magazine site on this subject <em><a href="http://msdn.microsoft.com/en-us/magazine/ee335713.aspx">&#8220;XML Denial of Service Attacks and Defenses&#8221;</a></em>. </p>
<p>If you want to read more about security issues regarding <em>ProhibitDTD</em> property, fine reading material is found on the two following urls: <em><a href="http://msdn.microsoft.com/en-us/library/ms762632(v=VS.85).aspx">ProhibitDTD Property</a></em>, <em><a href="http://msdn.microsoft.com/en-us/library/ms754611(VS.85).aspx">MSXML Security Overview</a></em>.</p>
<p>It is essential for you to set <em>ProhibitDTD</em> property to false.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">XmlDocument.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ProhibitDTD'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>There are two more important properties &#8211; <em>resolveExternals</em> and <em>validateOnParse</em>.</p>
<p>If you want to validate against DTD then set XmlDocument properties like this</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">XmlDocument.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ProhibitDTD'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
XmlDocument.<span style="color: #660066;">resolveExternals</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
XmlDocument.<span style="color: #660066;">validateOnParse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></div>
<p>Otherwise, if you just want to parse well formed Xml and skip validation ignoring <!DOCTYPE ...> declaration</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">XmlDocument.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ProhibitDTD'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
XmlDocument.<span style="color: #660066;">resolveExternals</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
XmlDocument.<span style="color: #660066;">validateOnParse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></div></div>
<p>There is one more case. If <em>resolveExternals</em> is accidently set to false and <em>validateOnParse</em> to true following error occurs:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">The element docroot <span style="color: #000066; font-weight: bold;">is</span> used but not declared <span style="color: #000066; font-weight: bold;">in</span> the DTD<span style="color: #339933;">/</span>Schema</div></div>
<p><strong>Complete source code listing</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//@author Stankovic Vlada [svlada@gmail.com]</span><br />
<span style="color: #003366; font-weight: bold;">var</span> fs <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> inputFilePath <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>js<span style="color: #000099; font-weight: bold;">\\</span>test.xml&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> stream <span style="color: #339933;">=</span> fs.<span style="color: #660066;">openTextFile</span><span style="color: #009900;">&#40;</span>inputFilePath<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> stream.<span style="color: #660066;">readAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
stream.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// var rexCleanDoctype = /&lt;!DOCTYPE.*?&gt;/gm;</span><br />
<span style="color: #006600; font-style: italic;">// content = content.replace(rexCleanDoctype, &quot;&quot;);</span><br />
<span style="color: #003366; font-weight: bold;">var</span> XmlDocument<span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; XmlDocument <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;msxml2.DOMDocument.6.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; XmlDocument.<span style="color: #660066;">async</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Allow DTD</span><br />
&nbsp; &nbsp; XmlDocument.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ProhibitDTD'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; XmlDocument.<span style="color: #660066;">resolveExternals</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; XmlDocument.<span style="color: #660066;">validateOnParse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error while creating DOM Object: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// Load an XML file into the DOM instance</span><br />
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// To load from file path call function XmlDocument.load(inputFilePath);</span><br />
&nbsp; &nbsp; XmlDocument.<span style="color: #660066;">loadXML</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; printParseError<span style="color: #009900;">&#40;</span>XmlDocument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error while loading XML from String: &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> e.<span style="color: #660066;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> Node<span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Node <span style="color: #339933;">=</span> XmlDocument.<span style="color: #660066;">selectSingleNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//docroot/dir&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; printParseError<span style="color: #009900;">&#40;</span>XmlDocument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error resolving Node value from XPath: &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> e.<span style="color: #660066;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Node <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span>Node.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> printParseError<span style="color: #009900;">&#40;</span>xmlDoc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlDoc.<span style="color: #660066;">parseError</span>.<span style="color: #660066;">errorCode</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> myErr <span style="color: #339933;">=</span> xmlDoc.<span style="color: #660066;">parseError</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; WScript.<span style="color: #660066;">Echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Xml parse error: &quot;</span> <span style="color: #339933;">+</span> myErr.<span style="color: #660066;">reason</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>test.xml</strong></p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE docroot SYSTEM &quot;test.dtd&quot; &gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;docroot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dir<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/springapp/index.jsp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dir<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>link<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/docroot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p><strong>test.dtd</strong></p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;!ELEMENT docroot <span style="color: #66cc66;">&#40;</span>dir,url<span style="color: #66cc66;">&#41;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;">&lt;!ELEMENT dir <span style="color: #66cc66;">&#40;</span>#PCDATA<span style="color: #66cc66;">&#41;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;">&lt;!ELEMENT url <span style="color: #66cc66;">&#40;</span>#PCDATA<span style="color: #66cc66;">&#41;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></div>
<p><strong>Useful references:</strong></p>
<p><em>XmlDocument.validateOnParse</em><br />
<a href="http://msdn.microsoft.com/en-us/library/ms762791(VS.85).aspx">http://msdn.microsoft.com/en-us/library/ms762791(VS.85).aspx</a></p>
<p><em>XmlDocument.resolveExternals </em><br />
<a href="http://msdn.microsoft.com/en-us/library/ms761375(v=vs.85).aspx">http://msdn.microsoft.com/en-us/library/ms761375(v=vs.85).aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2011/09/14/how-to-evaluate-xpath-when-doctype-present-in-xml-declaration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Script Host(WSH) Javascript Create directory from path</title>
		<link>http://www.svlada.com/blog/2011/08/01/windows-script-host-wsh-jscript-create-directory-from-path/</link>
		<comments>http://www.svlada.com/blog/2011/08/01/windows-script-host-wsh-jscript-create-directory-from-path/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 19:57:33 +0000</pubDate>
		<dc:creator>Vladimir Stanković</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[wsh]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.svlada.com/blog/?p=44</guid>
		<description><![CDATA[The Microsoft Windows Script Host (WSH) is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features. It was originally called Windows Scripting Host, but was renamed for the second release. WSH (VBS) is a very useful tool that allows you [...]]]></description>
			<content:encoded><![CDATA[<p>The Microsoft Windows Script Host (WSH) is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features. It was originally called Windows Scripting Host, but was renamed for the second release. WSH (VBS) is a very useful tool that allows you to run JScript, VBScript, and XML scripts natively within the OS. </p>
<p>You can read more about Windows Script Host(WSH) on Microsoft official site <a href="http://msdn.microsoft.com/en-us/library/shzd7dy4%28v=vs.85%29.aspx">http://msdn.microsoft.com/en-us/library/shzd7dy4%28v=vs.85%29.aspx</a></p>
<p>Back to the subject. How to create directories and subdirectories from String path using JScript and Windows Script Host? Process is quite simple and still there is no support for this simple functionality out of box in WSH.</p>
<p>First we must tokenize input and extract each directory name as separate String. We can achieve this using Regular Expressions. I have following pattern:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/(.*?)\\/gm</span><span style="color: #339933;">;</span></div></div>
<p>The results after evaluating regex for tokenizing String can be seen on image below<br />
<div id="attachment_62" class="wp-caption alignleft" style="width: 291px"><a href="http://www.svlada.com/blog/wp-content/uploads/2011/08/wsh-create-dir1.jpg"><img src="http://www.svlada.com/blog/wp-content/uploads/2011/08/wsh-create-dir1.jpg" alt="" title="wsh-create-dir" width="281" height="391" class="size-full wp-image-62" /></a><p class="wp-caption-text">Regex used to tokenize String input</p></div></p>
<p>The final step is to create folders, starting with the root folder to the last one.</p>
<p>Additionaly you can add check wheter Directory exists before trying to create one. Check if Directory exists in Windows Script Host can be achieved calling method object.FolderExists(folder). </p>
<p>More specific information about this method can be found on msdn <a href="http://msdn.microsoft.com/en-us/library/5xc78d8d%28v=VS.85%29.aspx">http://msdn.microsoft.com/en-us/library/5xc78d8d%28v=VS.85%29.aspx</a></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// author: Stankovic Vlada (svlada@gmail.com)</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> debug <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/(.*?)\\/gm</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>test<span style="color: #000099; font-weight: bold;">\\</span>test1<span style="color: #000099; font-weight: bold;">\\</span>test2<span style="color: #000099; font-weight: bold;">\\</span>test3<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> fileSystemOut <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
createDirectoryFromPath<span style="color: #009900;">&#40;</span>fileSystemOut<span style="color: #339933;">,</span> path<span style="color: #339933;">,</span> pattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Java Script function that creates folder tree from input String</span><br />
<span style="color: #006600; font-style: italic;">// @param fileSystemOut </span><br />
<span style="color: #006600; font-style: italic;">// File System Object (FSO) Provides access to a computer's file system.</span><br />
<span style="color: #006600; font-style: italic;">// @param path - Full path that represents directory tree </span><br />
<span style="color: #006600; font-style: italic;">// (for example var path = &quot;c:\\test\\test1\\test2\\test3\\&quot;)</span><br />
<span style="color: #006600; font-style: italic;">// @param pattern - Regex pattern that tokenize path</span><br />
<span style="color: #006600; font-style: italic;">// &nbsp;String to individual directories</span><br />
<span style="color: #003366; font-weight: bold;">function</span> createDirectoryFromPath<span style="color: #009900;">&#40;</span>fileSystemOut<span style="color: #339933;">,</span> path<span style="color: #339933;">,</span> pattern<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> tmpFileLoc <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> pattern.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tmpFileLoc <span style="color: #339933;">=</span> tmpFileLoc.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>debug <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating dir - &gt;&quot;</span> <span style="color: #339933;">+</span> tmpFileLoc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileSystemOut.<span style="color: #660066;">CreateFolder</span><span style="color: #009900;">&#40;</span>tmpFileLoc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>debug <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WScript.<span style="color: #660066;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;***ERROR while creating Folder: &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">+</span> e.<span style="color: #660066;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>File System Object (FSO) &#8211; <a href="http://msdn.microsoft.com/en-us/library/z9ty6h50%28v=vs.85%29.aspx">@link</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.svlada.com/blog/2011/08/01/windows-script-host-wsh-jscript-create-directory-from-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching using disk: basic
Object Caching 3695/3779 objects using disk: basic

Served from: www.svlada.com @ 2013-06-18 23:19:08 -->