<?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>Coder corner</title>
	<atom:link href="http://codercorner.altervista.org/wordpress/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://codercorner.altervista.org/wordpress</link>
	<description>Computer Science and programming</description>
	<lastBuildDate>Fri, 06 Nov 2009 09:22:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Master Web Technology(2008/09) &#8211; SEO Contest</title>
		<link>http://codercorner.altervista.org/wordpress/?p=70</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=70#comments</comments>
		<pubDate>Thu, 05 Nov 2009 11:59:19 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Promozione]]></category>
		<category><![CDATA[Master]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=70</guid>
		<description><![CDATA[
Nonstante tutto il Seo Contest organizzato nel Master in Tecnologie del Web dell&#8217;università di L&#8217;Aquila continua. Abbiamo assistito alle lezioni presso la Oracle di Roma, la quale non ringrazierò mai abbastanza per avermi ospitato, insieme ai miei colleghi, in quei giorni convulsi e disorientati. Come ogni anno il principale attore del contest e del corso [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.di.univaq.it/mwt/"><img class="alignnone size-full wp-image-72" title="logo-master-3" src="http://codercorner.altervista.org/wordpress/wp-content/uploads/2009/11/logo-master-3.png" alt="logo-master-3" width="170" height="56" /></a></p>
<p>Nonstante tutto il Seo Contest organizzato nel <a href="http://www.di.univaq.it/mwt/">Master in Tecnologie del Web</a> dell&#8217;università di L&#8217;Aquila continua. Abbiamo assistito alle lezioni presso la <a href="http://www.oracle.com/global/it/index.html">Oracle</a> di Roma, la quale non ringrazierò mai abbastanza per avermi ospitato, insieme ai miei colleghi, in quei giorni convulsi e disorientati. Come ogni anno il principale attore del contest e del corso è stato il famoso <a href="http://blog.giorgiotave.it/index.php/universita-dellaquila-master-in-web-technologydel-2009/">Giorgio Taverniti</a>, il quale non ha bisogno di presentazioni nell&#8217;ambito SEO. Il Seo contest di quest&#8217;anno ha una tematica particolare, ossia cercare di capire come Google riesce ad intercettare i contenuti duplicati.  Il tema del progetto è cercare di prendere i contenuti duplicati da più fonti in modo automatico (o tramite mashup o tramite operazioni di information retrieval) ed istruire un&#8217;algoritmo che (con supervisione umana) capisca quanto testo miscelare da ogni fonte. Inutile dire che l&#8217;interesse di studio su questo tema è alto sia dal punto di vista tecnico che in ambito SEO. Io sono impegnato, insieme ad altri mie colleghi, nella promozione di un sito che ha come tema le<a href="http://riservelazio.altervista.org/"> riserve del lazio</a>. Se avete voglia di promuoverci, potete scaricare il <a href="http://codercorner.altervista.org/wordpress/wp-content/uploads/2009/11/SEOCONTE.pdf">depliant </a>di promozione, ve ne saremo grati.</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=70</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compilare codice python da buffer o stringa [PoC]</title>
		<link>http://codercorner.altervista.org/wordpress/?p=48</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=48#comments</comments>
		<pubDate>Sat, 12 Sep 2009 16:52:41 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=48</guid>
		<description><![CDATA[Premettendo che la compilazione di un file python non e&#8217; assolutamente obbligatoria e che l&#8217;interprete riesce a riconoscere benessimo come e quando richiamare un file precompilato quando si fa l&#8217;import di un modulo, è possbile modificare la funzione py_compile.compile()  in modo tale da permette la compilazione di un sorgente contenuto in un  buffer [...]]]></description>
			<content:encoded><![CDATA[<p>Premettendo che la compilazione di un file python non e&#8217; assolutamente obbligatoria e che l&#8217;interprete riesce a riconoscere benessimo come e quando richiamare un file precompilato quando si fa l&#8217;import di un modulo, è possbile modificare la funzione py_compile.compile()  in modo tale da permette la compilazione di un sorgente contenuto in un  buffer anzichè in un file sorgente. Esempio:</p>
<p>[Proof of concept]</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">__builtin__</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">imp</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">marshal</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">traceback</span>
&nbsp;
MAGIC = <span style="color: #dc143c;">imp</span>.<span style="color: black;">get_magic</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># Define an internal helper according to the platform</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">name</span> == <span style="color: #483d8b;">&quot;mac&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> MacOS
    <span style="color: #ff7700;font-weight:bold;">def</span> set_creator_type<span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
        MacOS.<span style="color: black;">SetCreatorAndType</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span>, <span style="color: #483d8b;">'Pyth'</span>, <span style="color: #483d8b;">'PYC '</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> set_creator_type<span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> wr_long<span style="color: black;">&#40;</span>f, x<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Internal; write a 32-bit int to a file in little-endian order.&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span> x        <span style="color: #66cc66;">&amp;</span> 0xff<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>  <span style="color: #66cc66;">&amp;</span> 0xff<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0xff<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">24</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0xff<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> compileString<span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span>, filename, ext=<span style="color: #483d8b;">&quot;.pyc&quot;</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">str</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #483d8b;">&quot;Error, code malformed -&gt; type_code(buffer)&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">code</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #dc143c;">code</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>:
        <span style="color: #dc143c;">code</span> = <span style="color: #dc143c;">code</span> + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        codeobject = <span style="color: #dc143c;">__builtin__</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span>, filename, <span style="color: #483d8b;">'exec'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> codeobject.<span style="color: black;">co_code</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Error in the code&quot;</span>
    cfile = filename + ext<span style="color: #66cc66;">;</span>
    fc = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>cfile, <span style="color: #483d8b;">'wb'</span><span style="color: black;">&#41;</span>
    fc.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\0</span>'</span><span style="color: black;">&#41;</span>
    timestamp = <span style="color: #008000;">long</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">fstat</span><span style="color: black;">&#40;</span>fc.<span style="color: black;">fileno</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">st_mtime</span><span style="color: black;">&#41;</span>
    wr_long<span style="color: black;">&#40;</span>fc,timestamp<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">marshal</span>.<span style="color: black;">dump</span><span style="color: black;">&#40;</span>codeobject, fc<span style="color: black;">&#41;</span>
    fc.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    fc.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    fc.<span style="color: black;">write</span><span style="color: black;">&#40;</span>MAGIC<span style="color: black;">&#41;</span>
    fc.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    set_creator_type<span style="color: black;">&#40;</span>cfile<span style="color: black;">&#41;</span>
&nbsp;
compileString<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;print 0;&quot;</span>, <span style="color: #483d8b;">&quot;pppr&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=48</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux Kernel Debug con Eclipse</title>
		<link>http://codercorner.altervista.org/wordpress/?p=40</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=40#comments</comments>
		<pubDate>Tue, 08 Sep 2009 08:41:52 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=40</guid>
		<description><![CDATA[Molto spesso le persone mi guardano stralunate quando gli dico che sto cercando di mettere mano al codice del kernel linux per cercare di renderlo modulare. La prima reazione è senz&#8217;altro quella di dire: &#8220;a che serve? Tanto linux va bene così com&#8217;è.&#8221;.  La mia risposta è senz&#8217;altro banale quanto ovvia,  non pretendo mica di [...]]]></description>
			<content:encoded><![CDATA[<p>Molto spesso le persone mi guardano stralunate quando gli dico che sto cercando di mettere mano al codice del kernel linux per cercare di renderlo modulare. La prima reazione è senz&#8217;altro quella di dire: &#8220;a che serve? Tanto linux va bene così com&#8217;è.&#8221;.  La mia risposta è senz&#8217;altro banale quanto ovvia,  non pretendo mica di diventare ricco con una cosa che faccio durante quelle domeniche pomeriggio molto noise, anche perchè, stando a L&#8217;Aquila, non è che ci sia proprio un granchè da fare. C&#8217;è chi gioca a scacchi su internet, chi guarda la tv o chi semplicemente dorme sul divano. A me piace provare nuove cose al pc e/o leggere un bel librozzo di teoria, di quella sana teoria che sa far storcere il naso a tutti. Le difficoltà che si incontrano quando si cerca di capire come funziona il kernel sono senza dubbio capire che, benchè si conosca il C e l&#8217;assembler, se non si ha un libro o manuale di riferimento del kernel non si va molto lontano. Linux sarà open-source, ma quando a documentazione del kernel lascia un po a desiderare, quanto meno la cartella doc dei sorgenti ha uno stile organizzativo che a me non piace e i commenti sul codice  sono semplicemente&#8230; semplicemente non ci sono! Per iniziare con la comprensione, consiglio a tutti di leggere almeno questi due libri, facili e molto esaustivi:</p>
<ul>
<li><a href="http://oreilly.com/catalog/9780596005658/?CMP=AFC-ak_book&amp;ATT=Understanding+the+Linux+Kernel%2c+Third+Edition%2c">Understanding the Linux Kernel</a></li>
<li><a href="http://oreilly.com/catalog/9780596005900/">Linux Device Driver </a></li>
</ul>
<p>Se invece si ritiene che non si hanno le capacità o le basi per la programmazione di sistema in linux, un&#8217;altro ottimo testo per imparare ad usare le syscall di linux è: <a href="http://oreilly.com/catalog/9780596009588/?CMP=AFC-ak_book&amp;ATT=Linux+System+Programming%2c">Linux System Programming</a>. Partendo da questi libri, ma che non siamo di certo obbligati a leggerli per intero per renderci conto di come vanno le cose, passiamo a sporcarci le mani. Ovviamente, se devi modificare il codice del kernel linux, usa linux come sistema per il tuo ambiente operativo. Molti nello sviluppo di applicazione embedded Linux-based usano windows equipaggiato con cygwin,  tool-chain  e croos-compilatori. Ora capisco chi deve implementare su una architettura diversa, tipo un&#8217;ARM o Mips il quale ha senz&#8217;altro bosogno di un cross-compilatore, ma complicarsi la vita per configurare e compilare il kernel su un OS diverso e&#8217; come iniziare a studiare matematica partendo dall&#8217;<a href="http://it.wikipedia.org/wiki/Ipotesi_di_Riemann">ipotesi di Riemann</a>, questo per dire che è una congettura sperare di compilare il kernel usando Windows.  Con linux ed eclipse e&#8217; estramamente semplice avere un&#8217;ambiente per la visione e modifica del codice e anche per effettuarne il debug in modo molto versatile con GDB. Riporto di seguito il link dove potete trovare un&#8217;interessate tutorial, con tanto di screen-shot per configurare la cosa: <a href="http://issaris.blogspot.com/2007/12/download-linux-kernel-sourcecode-from.html">http://issaris.blogspot.com/2007/12/download-linux-kernel-sourcecode-from.html</a>. Vi informo che dovrete procuravi, oltre ai sorgenti del <a href="http://www.kernel.org">kernel linux</a> una versione recente di <a href="http://www.eclipse.org/">Eclipse</a>, anche se credo sia meglio prendere la 3.4 o ganymede per gli esperimenti. Per far partire eclipse sotto ubuntu assicuratevi di aver installato la Java Virtual Machine rilasciata da SUN e <em>non</em> quella open-source (OpenJVM). Il motivo è che la seconda è molto meno performante ed eclipse potrebbe metterci una vita per caricare.  Un&#8217;altro ottimo tool per il debug è ovviamente un simulatore e <a href="http://www.qemu.org/">Qemu</a> sembra proprio fare al caso, in quanto ci permette di testare il kernel senza neanche aver bisogno di costruire un&#8217;immagine del disco.</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=40</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Verificare l&#8217;esistenza di una destinazione (javascript, ajax e scriptacolous)</title>
		<link>http://codercorner.altervista.org/wordpress/?p=34</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=34#comments</comments>
		<pubDate>Sat, 18 Jul 2009 09:06:55 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=34</guid>
		<description><![CDATA[Ci troviamo di fronte ad un problema comune in siti che trattano di vacanze o che hanno a che fare con posizioni geografiche (geoname) per alcune destinazioni. I requisiti ci chiedono di fornire un servizio di ricerca di una posizione geografica, come fare? Si potrebbe tentare di definire una ricerca guidata, fornendo all&#8217;inizio una form [...]]]></description>
			<content:encoded><![CDATA[<p>Ci troviamo di fronte ad un problema comune in siti che trattano di vacanze o che hanno a che fare con posizioni geografiche (geoname) per alcune destinazioni. I requisiti ci chiedono di fornire un servizio di ricerca di una posizione geografica, come fare? Si potrebbe tentare di definire una ricerca guidata, fornendo all&#8217;inizio una form per selezionare il continente e lo stato, per poi raffinarla. E&#8217; un classico dilemma di information retrival quando si cerca di rispondere alla domanda: &#8220;E&#8217; meglio usare un motore di ricerca o una web directory per ottenere le informazioni che sto cercando?&#8221;.  La faccenda è abbastanza controversa, perchè noi potremmo cercare una destinazione di cui non conosciamo il continente o lo stato.In questo articolo fornirò un modo per interrogare un <a href="http://www.geonames.org">webservice esterno</a> tramite javascript per farci restituire una o più destinazioni geografiche che corrisponderanno meglio alla nostra keyword inserita. Partiamo subito con il codice che ci servirà per fare la chiamata asincrona lato client.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009966; font-style: italic;">/**
 * funzione javascript per una richiesta asincrona al webservice geoname
 * (gestita da uno script php)
 *
 * @param destinazione
 *            string prende in input la distanizione di un utente
 * @param IdDivElement
 *            string prende l'id dell'elemento div dove visualizzare il
 *            contenuto
 * @param cntx
 * 		     string specifica il contesto della ricerca del geoname
 * 		            ossia per la locazione corrente o per le destinazione preferite etc..
 *
 *
 *
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> verifyDestination<span style="color: #009900;">&#40;</span>destinazione<span style="color: #339933;">,</span> IdDivElement<span style="color: #339933;">,</span>cnxt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Elemento dove visualizzare i risultati</span>
	IdContent <span style="color: #339933;">=</span> IdDivElement<span style="color: #339933;">;</span>
&nbsp;
	context <span style="color: #339933;">=</span> cnxt<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>destinazione <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">||</span> destinazione <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		Ext.<span style="color: #006600;">MessageBox</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Error'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Please, insert a location'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #006600; font-style: italic;">//alert(destinazione);</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// e' possibile crittografare l'url per renderlo</span>
	<span style="color: #006600; font-style: italic;">// illegibile, oltre ad utilizzare l'offuscamento del codice</span>
	<span style="color: #003366; font-weight: bold;">var</span> geonameWS <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;verify_destination.php?q=&quot;</span><span style="color: #339933;">;</span>
	geonameWS <span style="color: #339933;">=</span> geonameWS <span style="color: #339933;">+</span> destinazione<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//alert(geonameWS);</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #006600;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Mozilla based</span>
		request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&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>window.<span style="color: #006600;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		request <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.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			request <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;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> request <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Ajax not supported!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// TODO: Visualizzare un messaggio di attesa dei risultati</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> indicator <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'indicator'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	indicator.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	request.<span style="color: #006600;">onreadystatechange</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span>
	request.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> geonameWS<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	request.<span style="color: #006600;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Fino a questo punto le operazioni sono ovvie e classiche per ciascuna chiamata asincrona con javascript. E&#8217; da notare come il nostro file php che inoltra la chiamata al webservice esterno si chiama verify_destination.php, al qual passiamo  un parametro che rappresenterà la nostra destinazione ricercata. Infatti, come da titolo dell&#8217;articolo noi siamo interessati a validare una località, ossia verificare che l&#8217;utente inserisce effettivamente il nome di una località esistente, non ad esempio Topolinia. Vi sono altri aspetti sul codice, come il contesto (cntx) e l&#8217;id dell&#8217;elemento div per la visualizzazione che saranno trattati in seguito. Una volta inoltrata la richiesta il nostro script php ci restituirà i risultati in formato xml che noi andremo a parsare. Lo script verify_destination è molto semplice, poichè il webservice accetta chiamate http.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;MAXROWS&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;FEATURECLASS&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;P&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">'You are trying a DoS or DDoS attack, your IP address is tracked'</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$destination</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$_GET</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//$destination = addcslashes($destination,&quot;'&quot;);</span>
<span style="color: #666666; font-style: italic;">//se uso il web service</span>
&nbsp;
<span style="color: #000033;">$destination</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripcslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;include/ws_geoname.inc.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il file incluso nello script fa la semplice chiamata http e restituisce il document xml.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Content-Type: text/xml'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://ws.geonames.org/search?q='</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$destination</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$param</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$destination</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;maxRows=5'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;featureClass='</span><span style="color: #339933;">.</span>FEATURECLASS<span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;style=FULL'</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$url</span><span style="color: #339933;">.=</span><span style="color: #000033;">$param</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$req</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span> <span style="color: #009900;">&#40;</span> <span style="color: #000033;">$url</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$req</span><span style="color: #339933;">;</span></pre></div></div>

<p>Come si può vedere con la funzione file_get_contents($url) viene fatta una richiesta http per scaricare il documento xml dal webservice e con una semplice echo ridirezioniamo il contenuto al client. Come da codice, nel sorgente javascript mostrato abbiamo dichiarato una funziona callback che verrà richiamata quando il client riceverà la richiesta. La funziona javascript si chiama semplicemente callback.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> callback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>request.<span style="color: #006600;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>request.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// XML</span>
			xmlDoc <span style="color: #339933;">=</span> request.<span style="color: #006600;">responseXML</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// document.write(xmlDoc);</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">//TODO: disabilitare il messaggio di attesa dei risultati</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> indicator <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'indicator'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			indicator.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			display<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Service no working..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Il codice precedente è quasi standard in una chiamata asincrona con ajax, quando la richiesta è stata ricevuta il browser prende il contenuto e viene dichiarato che è in formato xml con il metodo request.responseXML. Ora che abbiamo il contenuto salvato in una variabile con scope globale all&#8217;interno dello script è possibile parsarlo è recuperare il codice che ci serve.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009966; font-style: italic;">/**
 * Visualizza i risultati nella div predefinita
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> display<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> results<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> countryName<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> adminName<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> numRes<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> lat<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> lng<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> geonameId<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> continentCode<span style="color: #339933;">;</span>
&nbsp;
	document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span>IdContent<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> totalRes <span style="color: #339933;">=</span> xmlDoc.<span style="color: #006600;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;totalResultsCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		numRes <span style="color: #339933;">=</span> totalRes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
&nbsp;
		results <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Number of results &quot;</span> <span style="color: #339933;">+</span> numRes <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
&nbsp;
		results <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;
&nbsp;
&lt;ul&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> geonames <span style="color: #339933;">=</span> xmlDoc.<span style="color: #006600;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;geoname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// alert(geonames[0].childNodes[1].nodeName);</span>
		<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> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> geonames.<span style="color: #006600;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> childs <span style="color: #339933;">=</span> geonames<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// Scorro tutti nodi figli dell'elemento geoname</span>
			<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;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> childs.<span style="color: #006600;">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>
&nbsp;
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;countryName&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					countryName <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;adminName1&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					adminName <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Se sono arrivato qui stop</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;lat&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					lat <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;lng&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					lng <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;geonameId&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					geonameId <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<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>childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;continentCode&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					continentCode <span style="color: #339933;">=</span> childs<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nodeValue</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// end for</span>
&nbsp;
			<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> addslashes<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			adminName <span style="color: #339933;">=</span> addslashes<span style="color: #009900;">&#40;</span>adminName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			countryName <span style="color: #339933;">=</span> addslashes<span style="color: #009900;">&#40;</span>countryName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
			results <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&lt;li&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>javascript:selectDestination(&quot;</span><span style="color: #339933;">;</span>
			results <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;'&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;',&quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span> <span style="color: #339933;">+</span> adminName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;','&quot;</span>
					<span style="color: #339933;">+</span> countryName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;','&quot;</span> <span style="color: #339933;">+</span> lat <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;','&quot;</span> <span style="color: #339933;">+</span> lng <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;','&quot;</span>
					<span style="color: #339933;">+</span> geonameId <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;','&quot;</span> <span style="color: #339933;">+</span> continentCode <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;')<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
			results <span style="color: #339933;">+=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> adminName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> countryName
					<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt; &lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">// end for</span>
&nbsp;
	       results <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #006600; font-style: italic;">//alert(results);</span>
&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>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;An exception occurred at:&quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Error msg:&quot;</span>
				<span style="color: #339933;">+</span> e.<span style="color: #006600;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// creo il div con le informazioni</span>
	<span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span>IdContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	subdiv <span style="color: #339933;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>numRes <span style="color: #339933;">==</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		results<span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;please insert a different location&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	subdiv.<span style="color: #006600;">innerHTML</span> <span style="color: #339933;">=</span> results<span style="color: #339933;">;</span>
	div.<span style="color: #006600;">appendChild</span><span style="color: #009900;">&#40;</span>subdiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009966; font-style: italic;">/***************************************************************************
	 * Chiamata al framework scriptaculous
	 **************************************************************************/</span>
&nbsp;
	Effect.<span style="color: #006600;">SlideDown</span><span style="color: #009900;">&#40;</span>IdContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ho cercato di formattare il codice il meglio possibile, ma i limiti del plugin di wordpress non permettono di fare di più (almeno per ora). Per sopperire a tale inconveniente allegherò il codice a fondo artcolo. Lo script precendente visualizza i risultati tramite scriptacolous dando un effetto a tendina scorrevole che appare una volta ricevuti i risultati. Nel caso di poter usare questo script in più elementi di una form, come ad esempio due input form. In ultimo abbiamo bisogno di un modo per permettere all&#8217;utente di selezionare una destinazione e modificare gli elementi html predefiniti.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009966; font-style: italic;">/**
 * @author Gianfranco Murador
 * @param name
 *            string nome del geoname
 * @param adminName
 *            string regione del geoname
 * @param countryName
 *            string stato del geoname
 * @param lat
 *            string latitudine
 * @param lng
 *            string longitudine
 * @param geonameId
 *            string identificatore
 *
 *
 * Selezione il geoname e setta alcuni campi di input nascosti per la form
 *
 **/</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> selectDestination<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> adminName<span style="color: #339933;">,</span> countryName<span style="color: #339933;">,</span> lat<span style="color: #339933;">,</span> lng<span style="color: #339933;">,</span> geonameId<span style="color: #339933;">,</span> continentCode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> inputGeoname <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inputGeoname'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	inputGeoname.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> adminName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> countryName<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;lat&quot;</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> latitudine <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lat'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	latitudine.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> lat<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> longitudine <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lng'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	longitudine.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> lng<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> geoId <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'geonameId'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	geoId.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> geonameId<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> n <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	n.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> cName <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'countryName'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	cName.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> countryName<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> cCode <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'continentCode'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	cCode.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> continentCode<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> aName <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'adminName'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	aName.<span style="color: #006600;">value</span> <span style="color: #339933;">=</span> adminName<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009966; font-style: italic;">/***************************************************************************
	 * Chiamata al framework scriptaculous
	 **************************************************************************/</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span>IdContent<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Pulisco il contenuto dell'elemento div</span>
	<span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span>IdContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	inputGeoname.<span style="color: #006600;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'readonly'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'readonly'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	div.<span style="color: #006600;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> buttonChange <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'buttonChange'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	buttonChange.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> button <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button'</span><span style="color: #339933;">+</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	button.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	inputGeoname.<span style="color: #006600;">style</span>.<span style="color: #006600;">borderColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Con questo script bloccheremo l&#8217;input text per la validazione e useremo un&#8217;altro button per sbloccarlo se l&#8217;utente ha sbagliato a selezionare la destinazione. Da notare come il codice è quasi del tutto indipendente, nel funzionamento, da scriptacolous, infatti potremmo usare un diverso controllo per la visualizzazione delle risposte. Per cambiare la destinazione uso la seguente function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> changeDestination<span style="color: #009900;">&#40;</span>cntxt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> inputGeoname <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inputGeoname'</span><span style="color: #339933;">+</span>cntxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	inputGeoname.<span style="color: #006600;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'readonly'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	inputGeoname.<span style="color: #006600;">style</span>.<span style="color: #006600;">borderColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> buttonChange <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'buttonChange'</span><span style="color: #339933;">+</span>cntxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	buttonChange.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> button <span style="color: #339933;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button'</span><span style="color: #339933;">+</span>cntxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	button.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/****
 * Questa funzione aggiunge un carattere
 * di escape per permettere l'inserimento
 * di carattere contente un apice. Lato server
 * php deve ripristinare gli apici
 *
 * ****/</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> addslashes<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>str<span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/([\\&quot;'])/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>$1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\0/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Il nostro file html per funzionare deve contenere dei tag del genere per far funzionare gli script definiti precedentemente:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #339933;">&lt;!--</span> GESTIONE LOCAZIONI FUTURE <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;inputGeonameDestinazioniFuture&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;inputGeonameDestinazioniFuture&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;buttonLink&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;buttonChangeDestinazioniFuture&quot;</span> <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;formButton&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;display: none&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript:changeDestination('DestinazioniFuture')&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Change&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;buttonDestinazioniFuture&quot;</span> <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;formButton&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript:verifyDestination(document.getElementById('inputGeonameDestinazioniFuture').value,'geonameDestinazioniFuture','DestinazioniFuture')&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Validate Location&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;uploadProgressFuture&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;display: none;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;vertical-align: middle;&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;profile_edit_locations.php_files/loading.gif&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Validating<span style="color: #339933;">,</span> please wait...<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Come ultimo accorgimento è bene ricordarsi di includere lo script nell&#8217;header o in qualche altra parte, come includere anche gli script di scriptacolous necessari per l&#8217;effetto desiderato.  Allegherò il codice non appena possibile.</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=34</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Off-Topic] Toxicity</title>
		<link>http://codercorner.altervista.org/wordpress/?p=33</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=33#comments</comments>
		<pubDate>Sun, 05 Apr 2009 09:07:24 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=33</guid>
		<description><![CDATA[Conversion, software version 7.0,
Looking at life through the eyes of a tire hub,
Eating seeds as a past time activity,
The toxicity of our city, of our city,
New, what do you own the world?
How do you own disorder, disorder,
Now, somewhere between the sacred silence,
Sacred silence and sleep,
Somewhere, between the sacred silence and sleep,
Disorder, disorder, disorder.
More wood for [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">Conversion, software version 7.0,<br />
Looking at life through the eyes of a tire hub,<br />
Eating seeds as a past time activity,<br />
The toxicity of our city, of our city,</p>
<p style="text-align: center;">New, what do you own the world?<br />
How do you own disorder, disorder,<br />
Now, somewhere between the sacred silence,<br />
Sacred silence and sleep,<br />
Somewhere, between the sacred silence and sleep,<br />
Disorder, disorder, disorder.</p>
<p style="text-align: center;">More wood for their fires, loud neighbors,<br />
Flashlight reveries caught in the headlights of a truck,<br />
Eating seeds as a past time activity,<br />
The toxicity of our city, of our city,</p>
<p style="text-align: center;">New, what do you own the world?<br />
How do you own disorder, disorder,<br />
Now, somewhere between the sacred silence,<br />
Sacred silence and sleep,<br />
Somewhere, between the sacred silence and sleep,<br />
Disorder, disorder, disorder. {x2}</p>
<p style="text-align: center;">When I became the sun,<br />
I shone life into the man&#8217;s hearts,<br />
When I became the sun,<br />
I shone life into the man&#8217;s hearts.</p>
<p style="text-align: left;">
<p style="text-align: center;">
<p style="text-align: center;">
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript TIP &#8211; addslashes</title>
		<link>http://codercorner.altervista.org/wordpress/?p=32</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=32#comments</comments>
		<pubDate>Sun, 22 Feb 2009 17:35:24 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=32</guid>
		<description><![CDATA[Dopo svariate ricerche e inutili tentativi sono riuscito a definire una funzione per l&#8217;aggiunta di slash per gli apici e doppi apici.

function addslashes&#40;str&#41; &#123;
&#160;
	return &#40;str+''&#41;.replace&#40;/([\\&#34;'])/g, &#34;\\$1&#34;&#41;.replace&#40;/\0/g, &#34;\\0&#34;&#41;;
	&#125;

Spero che sia utile a qualcuno ed eviti un bel po&#8217; di mal di testa..
]]></description>
			<content:encoded><![CDATA[<p>Dopo svariate ricerche e inutili tentativi sono riuscito a definire una funzione per l&#8217;aggiunta di slash per gli apici e doppi apici.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> addslashes<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>str<span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/([\\&quot;'])/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>$1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\0/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Spero che sia utile a qualcuno ed eviti un bel po&#8217; di mal di testa..</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=32</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux, python e problemi di ottimizzazione</title>
		<link>http://codercorner.altervista.org/wordpress/?p=27</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=27#comments</comments>
		<pubDate>Wed, 08 Oct 2008 15:13:23 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=27</guid>
		<description><![CDATA[Glpk è un tool per risolvere dei problemi di programmazione lineare rilasciato sotto licenza GNU. Esso viene rilasciato un due formati: uno come eseguibile a riga di comando e un&#8217;altro come libreria dinamica. E&#8217; stato realizzato un binding per python (ce ne sono anche per altri linguaggi) che permette di sottomettere un problema di programmazione [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gnu.org/software/glpk/">Glpk</a> è un tool per risolvere dei problemi di programmazione lineare rilasciato sotto licenza GNU. Esso viene rilasciato un due formati: uno come eseguibile a riga di comando e un&#8217;altro come libreria dinamica. E&#8217; stato realizzato un binding per python (ce ne sono anche per altri linguaggi) che permette di sottomettere un problema di programmazione lineare al risolutore ed avere la risposta direttamente dall&#8217;ambiente interpretato di python. Non starò qui a discutere sulla bontà (ossia, sull&#8217;efficienza) di tale risolutore rispetto alle sue controparti commerciali. Alcuni articoli e documentazioni sono reperibili nel sito ufficiale di <a href="http://www.gnu.org/software/glpk/">glpk</a> e spiegano anche gli algoritmi implementati per risolvere problemi di programmazione lineare. Ciò che invece mi piace osservare è la flessibilità di python come linguaggio di scripting. Di frequente python viene usato come linguaggio per uso scientifico/matematico, anche se è diventato famoso per la facilità di programmazione di strumenti per web. Non sono mai stato portato a realizzare applicazioni totalmente in python, ma lo uso molto spesso per avere una componente flessibile a run-time che realizzi un determinato compito. Ad esempio, usare python per risolvere istanze di problemi di programmazione lineare o combinatorici rappresenta una buona soluzione all&#8217;interno di un&#8217;applicazione più grande scritta in un linguaggio non interpretato come C/C++ oppure java o C# (Jython, IronPython). In questo modo, l&#8217;applicazione dovrà solo preoccuparsi di formattare l&#8217;input utente comprensibile a script python e lanciarli per la soluzione.</p>
<p>ES:</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #339933;">#include &lt;Python.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span>
main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc, <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Py_Initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  PyRun_SimpleString<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;from time import time,ctime<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
                     <span style="color: #ff0000;">&quot;print 'Today is',ctime(time())<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
  Py_Finalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Non andando oltre confermo che è possibile lanciare interi script da programmi c o c++. Implementando algoritmi di ottimizzazione in python, possiamo separare la parte del software relativa alla presentazione dei risultati con quella relativa alla risoluzione del problema. Infatti, sarà necessario solo scrivere nuovo codice python per aggiungere un nuovo algoritmo. Una soluzione simile, ma per scopi diversi, è quella usata da blender. Blender infatti usa python per scrivere plug-in, come ad esempio importer o exporter di formati di modelli 3d.</p>
<p>Passiamo ora all&#8217;oggetto del topic. Premetto che il binding di glpk per python è più semplice da installare per linux, anche se non ho tentato di farlo per win. Infatti, sotto ubuntu basta impartire il seguente comando:</p>
<blockquote><p>sudo apt-get install python-glpk</p></blockquote>
<p>per avere tutto a disposizione. Scrivere un problema di programmazione lineare è molto semplice dando in input un file .mod:</p>
<p>ES:<br />
<code><br />
var x &gt;=0 integer;<br />
var y &gt;= 0;</code></p>
<p>maximize z: x + y;</p>
<p>subject to r:<br />
x + y &lt;= 10;<br />
subject to s:<br />
x + 10*y &lt;= 1;<br />
subject to t:<br />
10*x + y &lt;= 1;</p>
<p>end;</p>
<p>Salviamo il contenuto in un file chiamato example.mod. Da python lanciamo il seguente script:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> glpk
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;starting...&quot;</span>
example = glpk.<span style="color: black;">lpx</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;example.mod&quot;</span><span style="color: black;">&#41;</span>
example.<span style="color: black;">solve</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;solution:&quot;</span>, example.<span style="color: black;">sol</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;solution is also here:&quot;</span>, example.<span style="color: black;">x</span></pre></div></div>

<p>Il risultato sarà il seguente:</p>
<blockquote><p>&gt;&gt;&gt;<br />
starting&#8230;<br />
solution: {&#8217;y': 0.099999999999999992, &#8216;x&#8217;: 0, &#8216;z&#8217;: 0.099999999999999992}<br />
solution is also here: array(&#8217;d', [0.099999999999999992, 0.0, 0.099999999999999992])</p></blockquote>
<p>il campo example.sol ci da la soluzione sotto forma di dizionario, mentre il campo example.x ci da la soluzione sottoforma vettoriale. Per chi ha bisogno di una libreria più avanzata consiglio<a href="http://130.216.209.237/engsci392/pulp/FrontPage"> PuLP</a> contenente una documentazione accurata e molte soluzioni implementative a problemi noti.</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=27</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Applicazioni Desktop con Eclipse (RCP)</title>
		<link>http://codercorner.altervista.org/wordpress/?p=25</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=25#comments</comments>
		<pubDate>Tue, 23 Sep 2008 13:08:48 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=25</guid>
		<description><![CDATA[Alla domanda : che cos&#8217;è eclipse ? Molti di voi risponderanno: &#8220;è un IDE&#8221;. Alcuni diranno: &#8220;Si, ma ha delle funzionalità tipiche anche di un RAD&#8221;. Entrambi hanno ragione, ma eclipse è anche molto di più. Esso infatti permette di costruire applicazione stand-alone facendo uso dell&#8217;architettura stessa di eclipse. Ossia, creare applicazione estensibili, flessibili e [...]]]></description>
			<content:encoded><![CDATA[<p>Alla domanda : che cos&#8217;è eclipse ? Molti di voi risponderanno: &#8220;è un IDE&#8221;. Alcuni diranno: &#8220;Si, ma ha delle funzionalità tipiche anche di un RAD&#8221;. Entrambi hanno ragione, ma eclipse è anche molto di più. Esso infatti permette di costruire applicazione stand-alone facendo uso dell&#8217;architettura stessa di eclipse. Ossia, creare applicazione estensibili, flessibili e riusabili. Tutto questo va sotto il nome di RCP ( Rich Client Platform). Perchè usare questa tecnologia anzichè JFC/Swing o AWT è facilmente intuibile: &#8220;l&#8217;architettura&#8221;. Chi di voi ha esperienze con le applicazioni desktop (ma vale anche per l&#8217;enterprise) sa come sono difficili da mantenere e come sia difficoltoso aggiungere nuove funzionalità se non si è stati attenti a dare all&#8217;applicazione una struttura modulare che faciliti il refactoring. Usando una struttura estensibile a plug-in come quella che usa eclipse nelle nostre applicazioni è senz&#8217;altro un grande vantaggio e per di più a costo zero ( o quasi). Eclipse RPC permette di ereditare lo stesso framework usato da Eclipse Workbench quindi permette al programmatore di dividere le funzionalità applicative in diversi plug-in. Per iniziare a programmare applicazioni usando RCP è possibile scaricare una versione apposita di eclipse con tutti i plugin necessari, inoltre è disponibile un tutorial abbastanza completo per la programmazione.</p>
<p><em><strong>Refs</strong></em></p>
<p><a href="http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/ganymede/R/eclipse-rcp-ganymede-win32.zip">Eclipse RCP/ Ganymede</a></p>
<p><a href="http://www.vogella.de/articles/RichClientPlatform/article.html">Eclipse RCP Tutorial</a></p>
<p><em>Good programming!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=25</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSE , Swing e JFileChooser</title>
		<link>http://codercorner.altervista.org/wordpress/?p=23</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=23#comments</comments>
		<pubDate>Tue, 16 Sep 2008 14:17:44 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=23</guid>
		<description><![CDATA[E&#8217; ormai risaputo che la classe JFileChooser è tra le più problematiche in termini di performance. Al primo avvio, il dialog prende circa 7/8 secondi su un PC dotato di un&#8217;intel da 1.5 Ghz e 512 MB di memoria e la cosa sembra migliorare di poco anche con hardware migliore. Ero molto scontento di tale [...]]]></description>
			<content:encoded><![CDATA[<p>E&#8217; ormai risaputo che la classe JFileChooser è tra le più problematiche in termini di performance. Al primo avvio, il dialog prende circa 7/8 secondi su un PC dotato di un&#8217;intel da 1.5 Ghz e 512 MB di memoria e la cosa sembra migliorare di poco anche con hardware migliore. Ero molto scontento di tale performance che persiste anche nella versione 1.6 della JVM. L&#8217;unico modo per poter migliorare la situazione è usare il controllo nativo di windows, richiamabile tramite la classe java.awt.FileDialog.  La grafica di tale controllo ovviamente è meno ricca e bella da vedere, ma la velocità di caricamento e visualizzazione è notevolmente migliorata ( circa del 80%). Infatti la classe FileDialog fa apparire il controllo in circa 1/2 secondi al primo avvio e diventa istantaneo nei successivi. Benchè le performance delle JFC/Swing siano notevolmente migliorate nella nuova versione rispetto alle precedenti, programmare applicazioni desktop in Java ha ancora bisogno di qualche trucco da volpe come questo. Per la Java 6 direi che la gestione degli eventi tramite Action condivise ha migliorato notevolmente le respositività del software ad eventi esterni.</p>
<p><em>TIP del giorno</em> : <strong>Usare java.awt.FileDialog invece di javax.swing.JFileChooser </strong>per aprire o salvare file.</p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=23</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Breve tutorial sul parsing e analisi lessicale (Java)</title>
		<link>http://codercorner.altervista.org/wordpress/?p=22</link>
		<comments>http://codercorner.altervista.org/wordpress/?p=22#comments</comments>
		<pubDate>Sat, 06 Sep 2008 13:05:19 +0000</pubDate>
		<dc:creator>Gianfranco Murador</dc:creator>
				<category><![CDATA[Senza categoria]]></category>

		<guid isPermaLink="false">http://codercorner.altervista.org/wordpress/?p=22</guid>
		<description><![CDATA[In questo articolo esporrò le tecniche usate per poter creare degli analizzatori lessicali e parser in Java, usando degli strumenti gratuiti ( JFlex e byacc/J). In questo modo è facile creare un nuovo linguaggio di programmazione o più semplicemente creare un programma che estrae le parti significative da un input testuale, quale può essere un [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo esporrò le tecniche usate per poter creare degli analizzatori lessicali e parser in Java, usando degli strumenti gratuiti ( JFlex e byacc/J). In questo modo è facile creare un nuovo linguaggio di programmazione o più semplicemente creare un programma che estrae le parti significative da un input testuale, quale può essere un file in lettura o una stringa inserita da linea di comando, e le elabora a seconda della sintassi predefinita dal programma. Si pensi ad esempio ad un programma che prenda un&#8217;espressione numerica in input con variabili e costanti numeriche e ne calcoli il risultato stando attento a come usare le parentesi tramite una gerarchia prestabilità ( prima le parentesi tonde, poi le quadre ed in ultimo le graffe). Il problema è più complesso di quanto sembri, ad esempio si immagini un file sorgente C con la stringa:</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #993333;">int</span> x;
x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>E&#8217; evidente come alla frase &#8220;int&#8221; si debba dare un significato e prima ancora occorre un modo per poter estrarre la frase &#8220;int&#8221; dal file. Questo causa molti problemi, se non trattati adeguatamente, in casi del genere:</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #993333;">int</span> intero;
intero <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Siccome un analizzatore lessicale legge uno stream di caratteri partendo dal primo byte, la domanda che dobbiamo porci è come fa nelle precedente seconda riga a sapere se si deve fermare ai primi tre caratteri perchè ha trovato una parola chiave oppure deve continuare a leggere per prendere l&#8217;identificatore &#8220;intero&#8221; ? Molti testi sono stati scritti riguardanti la teoria dei compilatori e su gli algoritmi che vengono usati per poter risolvere il problema precedente. In questo articolo non ci sofferemeremo su gli aspetti teorici come l&#8217;algortimo di Thompson, ma cercherò di mostrare come in poche righe di codice è possibile definire un&#8217;analizzatore lessicale abbastanza complesso e un parser che riconosce una grammatica predefinta da noi.</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #666666; font-style: italic;">// File .flex in ingresso a JFlex.</span>
&nbsp;
<span style="color: #339933;">%%</span>
&nbsp;
<span style="color: #339933;">%</span>byaccj
&nbsp;
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Parser</span> yyparser<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Yylex<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">io</span>.<span style="color: #003399;">Reader</span> r, <span style="color: #003399;">Parser</span> yyparser<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">yyparser</span> <span style="color: #339933;">=</span> yyparser<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
ID <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span>9_<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>
NUM <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>
FLOATNUM <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>NUM<span style="color: #009900;">&#125;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#123;</span>NUM<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%%</span> 
&nbsp;
<span style="color: #009900;">&#123;</span>ID<span style="color: #009900;">&#125;</span>    <span style="color: #009900;">&#123;</span>      <span style="color: #666666; font-style: italic;">/*Salva il valore dell'ID  in yyparser.yylval
                  e lo passa in input al parser*/</span>
                  yyparser.<span style="color: #006633;">yylval</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> ParserVal<span style="color: #009900;">&#40;</span>yytext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Parser</span>.<span style="color: #006633;">ID</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">&quot;+&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">&quot;-&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">&quot;/&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">&quot;*&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">&quot;(&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">&quot;)&quot;</span>          <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
\n           <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> yycharat<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>FLOATNUM<span style="color: #009900;">&#125;</span>   <span style="color: #009900;">&#123;</span>yyparser.<span style="color: #006633;">yylval</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParserVal<span style="color: #009900;">&#40;</span>yytext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Parser</span>.<span style="color: #006633;">FLOATNUM</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>NUM<span style="color: #009900;">&#125;</span>        <span style="color: #009900;">&#123;</span>yyparser.<span style="color: #006633;">yylval</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParserVal<span style="color: #009900;">&#40;</span>yytext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Parser</span>.<span style="color: #006633;">NUM</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#91;</span> \t\b\r<span style="color: #009900;">&#93;</span>    <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Skip }</span>
<span style="color: #339933;">%%</span></pre></div></div>

<p>Ora, salvando il codice precedente in un file con estensione .flex e aprendolo con JFlex, se tutto è andato a buon fine quest&#8217;ultimo dovrebbe fornirci un file chiamato Yylex.java pronto per essere incluso nel nostro progetto. In pratica questa classe non ci è ancora molto utile perchè stiamo usando JFlex in coppia con Byacc/J e non singolarmente. Se omettessimo la direttiva:</p>
<blockquote><p>%byaccj</p></blockquote>
<p>potremmo usare la classe precendente per scandagliare un file in input contente espressioni aritmetiche ed estrarre tutte le sue componenti, ad esempio :</p>
<blockquote><p>(1+2) &#8211; 3</p></blockquote>
<p>prenderebbe il carattere &#8220;(&#8221; poi 1 come token NUM poi &#8220;+&#8221; ecc. tralasciando eventuali spazi di tabulazione intermedi.Da notare come il codice racchiuso tra parentesi graffe viene eseguito ogni volta che viene incontrato un token o lessema valido, ossia che rispecchia un&#8217;espressione regolare. Quindi, invece di passare l&#8217;argomento al parser un programma JFlex può benissimo eseguire altre operazioni.</p>
<p>Per poter dare un significato a tale espressione è necessario fornire una sintassi e una semantica. La semantica ovviamente è banale per quest&#8217;esempio come pure la sintassi, ma entrambi possono essere costruite in modo agevole con un genereatore di parser come byaccj.</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span>token ID
<span style="color: #339933;">%</span>token NUM
<span style="color: #339933;">%</span>token FLOATNUM
&nbsp;
<span style="color: #339933;">%</span>start input <span style="color: #666666; font-style: italic;">// produzione di inizio</span>
&nbsp;
<span style="color: #339933;">%</span>left <span style="color: #0000ff;">'+'</span> <span style="color: #0000ff;">'-'</span> <span style="color: #0000ff;">'*'</span> <span style="color: #0000ff;">'/'</span>  <span style="color: #666666; font-style: italic;">// Associatività degli operatori</span>
&nbsp;
<span style="color: #339933;">%%</span> 
&nbsp;
input<span style="color: #339933;">:</span>
        <span style="color: #339933;">|</span>
         input <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">/* Salta una riga vuota per
                          far avanzare il parser */</span>
                        <span style="color: #009900;">&#125;</span>
        <span style="color: #339933;">|</span> input exp  <span style="color: #009900;">&#123;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>$<span style="color: #cc66cc;">2</span>.<span style="color: #006633;">sval</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
        <span style="color: #339933;">;</span>
&nbsp;
exp <span style="color: #339933;">:</span> exp <span style="color: #0000ff;">'+'</span> exp <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span> <span style="color: #339933;">+</span>  $<span style="color: #cc66cc;">3</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span>  <span style="color: #0000ff;">'('</span> exp <span style="color: #0000ff;">')'</span> <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span>  $<span style="color: #cc66cc;">2</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> exp <span style="color: #0000ff;">'-'</span> exp  <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span> <span style="color: #339933;">-</span>  $<span style="color: #cc66cc;">3</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> exp <span style="color: #0000ff;">'*'</span> exp   <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span>  <span style="color: #339933;">*</span>  $<span style="color: #cc66cc;">3</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> exp <span style="color: #0000ff;">'/'</span> exp   <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span> <span style="color: #339933;">/</span> $<span style="color: #cc66cc;">3</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> NUM <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> FLOATNUM <span style="color: #009900;">&#123;</span>$$.<span style="color: #006633;">sval</span> <span style="color: #339933;">=</span> $<span style="color: #cc66cc;">1</span>.<span style="color: #006633;">sval</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">|</span> ID <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/*Recupera il valore dell'identificatore e memorizzalo in $$.sval,
               non implementato*/</span><span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #339933;">%%</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> Yylex lexer<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> yylex <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> yyl_return <span style="color: #339933;">=</span> <span style="color: #cc66cc;">-1</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      yylval <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParserVal<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      yyl_return <span style="color: #339933;">=</span> lexer.<span style="color: #006633;">yylex</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;IO error :&quot;</span><span style="color: #339933;">+</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> yyl_return<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> yyerror <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Syntatic Error: &quot;</span> <span style="color: #339933;">+</span> error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Parser</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Reader</span> r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     lexer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Yylex<span style="color: #009900;">&#40;</span>r, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&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;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">Parser</span> yyparser<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;(1+2)-2&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> args.<span style="color: #006633;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      yyparser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Parser</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">StringReader</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       yyparser.<span style="color: #006633;">yyparse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Salviamo il codice precendente in un file con estensione .y , ad esempio Parser.y (attenzione il nome del file è significativo per la classe in output, vedere la doc di byacc/j).Pre creare il parser è necessario richiamare byaccj con i seguenti argomenti:</p>
<blockquote><p>yacc.exe -J Parser.y</p></blockquote>
<p>L&#8217;esempio precedente non fornisce propriamente una calcolatrice perchè è stata omessa la parte relativa alla gestione dei tipi dei token i quali sono considerati tutti di tipo String. Leggendo accuratamente la documentazione di byacc/j ed i riferimenti seguenti non è difficile superare tale limitazione.</p>
<p>JFlex fornisce una comoda interfaccia grafica per poter creare il nostro analizzatore lessicale senza bisogno di usare la shell o il prompt.</p>
<p><strong>Riferimenti</strong></p>
<p><em>Aho, Sethi, Ullman</em>, Compilers: Principles, Techniques, and Tools. Per maggiori dettagli vedere <a href="http://www.amazon.com/Compilers-Principles-Techniques-Tools-2nd/dp/0321486811">qui</a>.</p>
<p><a href="http://byaccj.sourceforge.net/">byaccj</a><a href="http://jflex.de/"></a></p>
<p><a href="http://jflex.de/">JFlex</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codercorner.altervista.org/wordpress/?feed=rss2&amp;p=22</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
