Archive for September, 2009

Search and Replace in ODT using AODL

Monday, September 28th, 2009

Recently I needed to create a way to search and replace a word in a OpenOffice OpenDocument ODT. It would seem the project itself already has a library AODL (take care, at least two other project pages but with older code) that exposes the format model but I didn’t find any utility methods like search and replace functionality. I’ve created two versions of the method, one a old-style, imperative approach, and the other, a LINQ version. As I’m still new to LINQ it would seem that the classical approach produces a more efficient code. Anyone care to chip in and make a faster algorithm? In this particular version I’m just going for simple string equality, I didn’t want to go with other StringComparison methods (just look at the Equals overload method).


public static void SearchAndReplaceString(TextDocument document, string searchText, string replaceText)
{
var content = document.Content;

foreach (var item in content)
{
if (item is Paragraph)
{
foreach (var textContent in ((Paragraph)item).TextContent)
{
if (textContent.Text == searchText)
{
textContent.Text = replaceText;
}
}
}
}
}


public static void SearchAndReplaceStringLINQ(TextDocument document, string searchText, string replaceText)
{
var content = document.Content;

IEnumerable<Paragraph> paragraphs =
from item in content
where item is Paragraph
select (Paragraph)item;


foreach (var paragraph in paragraphs)
{
var paragraphText = paragraph.TextContent.Where<IText>(t => t.Text == searchText);
foreach (var textItem in paragraphText)
{
textItem.Text = replaceText;
}
}
}

I should point out this method matches only single words.

Fix 1. Replace text if under table structure, doesn’t work if the Cell is in the header.


public static void SearchAndReplaceString(TextDocument document, string searchText, string replaceText)
{
var content = document.Content;
ReplaceInContent(searchText, replaceText, content);
}

private static void ReplaceInContent(string searchText, string replaceText, ContentCollection content)
{
foreach (var item in content)
{
if (item is Paragraph)
{
foreach (var textContent in ((Paragraph)item).TextContent)
{
if (textContent.Text == searchText)
{
textContent.Text = replaceText;
}
}
}
else if (item is Table)
{
foreach (var row in ((Table)item).Rows)
{
foreach (var cell in row.Cells)
{
var cellContent = cell.Content;
ReplaceInContent(searchText, replaceText, cellContent);
}
}
}
}
}

Javascript Camp 09

Monday, September 28th, 2009

Di recente c’è stato questo bellissimo evento sponsorizzato da ideato s.r.l. un barcamp tematizzato su javascript. Devo essere sincero non pensavo di parteciparvi, sono in piena tesi e non ho molto tempo, ma è stata una fortuna esserci. I partecipanti erano preparati, e ho potuto prendere molti spunti interessanti. Il miglior talk è stato sicuramente quello sul KATA programming, ossia cercare di migliorare al massimo del codice, cercando di renderlo sempre più elegante (link utili : qui, qui). Per portare avanti i suoi esempi Gabriele Lana ha utilizzato ovviamente la riga di comando, dato che javascript usarlo da browser è un po ostico; i tool utlizzati sono stati Rhino e Jasmine Test Framework (anche se quest’ultimo l’ho ha modificato, in quanto richiederebbe un browser) , il tutto condito con una chiara spiegazione. L’esempio proposto è stato “game of life” (linklink2 ) e dovrebbe essere disponibile sulla sua pagina su github. Tra i tool che ho visto ho deciso di menzionare WireIt un simpato framework per collegare blocchetti grafici con dei fili ed esportare la struttura con JSON. Che dire la giornata è stat molto interessante. Grazie a tutti. [link al sito principale]