Jan 14 2008

Accessing Local Files With Mozilla XPCOM

A few months back I took a look at TIBCO General Interface, an AJAX/Web Development framework with an IDE/Builder than runs on your browser. The one thing that amazed me about General Interface was that it saved files to your local disk drive from the browser.

Earlier today I was experimenting with TiddlyWiki, a wiki that is self contained in one file and noticed that it can also save files to your hard drive from the browser. TiddlyWiki is a entire wiki system written in HTML, CSS, and JavaScript and self contained in a single HTML file. When you make a wiki edit, TiddlyWiki writes and saves the change to your local disk drive. To learn more about how to go about doing this I took a peak at the page source code, which was like opening a can of gummy worms.

As you can imagine, saving a file to the local disk from the browser is not a standardized process. IE uses ActiveX controls to go about doing this, will FireFox uses XPCOM objects. In this article I will explore getting at local files from Firefox using XPCOM.

XPCOM is Mozilla’s Cross-Platform Component Object Model used in FireFox. Many of FireFox’s services and component objects, such as your bookmarks, are available from through XPCOM. XPCOM authors can create components in either JavaScript or C which can then be added onto Mozilla as an extension. FireFox by default has a large set of core XPCOM services and objects. Once these components are available in your browser you can make use of them in JavaScript. To better illustrate this we will use the local file component in Firefox to read data from your local disk.

To get a reference to a XPCOM object you need a contract ID, which is like a registry entry string identifier for the object component, and an interface for that object. The XPCOM interface is just like a Java interface in that it defines methods and can inherit from other interfaces.

var fileContractId = "@mozilla.org/file/local;1";
var fileInterface = Components.interfaces.nsILocalFile;

Some components, such as the local file need special privileges, since you wouldn’t want any web page to read and write files on your local system. To enable the privileges you need to invoke the following JavaScript code in your script which asks the end user for these privileges.

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

Now that we have the component contract id, interface, and the right permissions we can create a file object.

var localFileClass = Components.classes[fileContractId];
var file = localFileClass.createInstance(fileInterface);

For the most part, a local file object is just a reference to a file. The nsILocalFile interface has methods that you would expect from a file reference such as append, create, copyTo, exists, isWritable, isReadable, isDirectory, isFile. The nsILocalFile interface also has attributes such as leafName and fileSize.

Once the file object has been created you can use any of the methods defined in the interface. The first method we should use is the initWithPath which set the path to the file.

file.initWithPath('/Users/juixe/Desktop/temp.txt');

Now that the file has been created and initialized properly, we can find out the file size and if it is a file or directory with code like the following.

document.write("The file "+file.leafName+" has "+file.fileSize+" bytes of data.");
document.write("Does file exists? "+file.exists());
document.write("Is file writable?"+file.isWritable());
document.write("Is file readable?"+file.isReadable());
document.write("Is file directory?"+file.isDirectory());
document.write("Is file file?"+file.isFile());

In this article we just scratched at the surface of what you can do with XPCOM, in the next article I’ll go over the code to read and write bytes to a file using a file input/output stream. If you can’t wait ’till next time, just view the source code of TiddlyWiki….


Dec 11 2007

TechKnow Year In Review 2007

It is that time of year where we reflect on the accomplishments of the passing year and look forward to the one to come. Here is a window into the past year in technology through this year’s popular posts on TechKnow Juixe.

Top 5 Top Lists

Software Development

Java

JavaScript

Ruby

Ruby Shoes

Ruby on Rails Plugins

Windows Tips

Mac OS X Tips

Year In Review

Seasons Greetings

Technorati Tags: , , , , , , , , , , , ,


Dec 3 2007

Favorite Programming Quotes 2007

Here is a list of great programming quotes from my readings over the last year. It is interesting to note that most of my reading material this year has been from blogs. There are just a great amount of programmer bloggers writing a lot of informative tutorials on every programming language, paradigm shift, and framework under the sun.

in my experience, one of the most significant problems in software development is assuming. If you assume a method will passed the right parameter value, the method will fail.
— Paul M. Duvall
Continuous Integration

Programming languages are like girlfriends: The new one is better because *you* are better.
— Derek Sivers
7 reasons I switched back to PHP after 2 years on Rails

The sooner we start coding fewer frameworks and more programs the sooner we’ll become better programmers.
— Warped Java Guy
Elementary Java Solutions

Starting a startup is hard, but having a 9 to 5 job is hard too, and in some ways a worse kind of hard.
— Paul Graham
The Future of Web Startups

In essence, let the market design the product.
— Paul Graham
The Future of Web Startups

A startup now can be just a pair of 22 year old guys. A company like that can move much more easily than one with 10 people, half of whom have kids.
— Paul Graham
The Future of Web Startups

Startups almost never get it right the first time. Much more commonly you launch something, and no one cares. Don’t assume when this happens that you’ve failed. That’s normal for startups. But don’t sit around doing nothing. Iterate.
— Paul Graham
How Not to Die

The key to performance is elegance, not battalions of special cases.
— Jon Bentley and Doug McIlroy

You’ll spend far more time babysitting old technologies than implementing new ones.
— Jason Hiner
IT Dirty Secrets

To Iterate is Human, to Recurse, Divine.
— James O. Coplien

No one hates software more than software developers.
— Jeff Atwood
Hanselminutes Podcast 74

I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprised me.
— Matz
The Philosophy of Ruby

Good architecture is necessary to give programs enough structure to be able to grow large without collapsing into a puddle of confusion.
— Douglas Crockford
The Elements of JavaScript Style

Programming is difficult. At its core, it is about managing complexity. Computer programs are the most complex things that humans make. Quality is illusive and elusive.
— Douglas Crockford
The Elements of JavaScript Style

Code reuse is the Holy Grail of Software Engineering.
— Douglas Crockford
The Elements of JavaScript Style

The structure of software systems tend to reflect the structure of the organization that produce them.
— Douglas Crockford
The Elements of JavaScript Style

The definition of Hell is working with dates in Java, JDBC, and Oracle. Every single one of them screw it up.
— Dick Wall
CommunityOne 2007: Lunch with the Java Posse

Suppose you went back to Ada Lovelace and asked her the difference between a script and a program. She’d probably look at you funny, then say something like: Well, a script is what you give the actors, but a program is what you give the audience.
— Larry Wall
Programming is Hard, Let’s Go Scripting…

I went to school to learn how to program software applications, which inevitably have bug defects. There was no course at my university on testing, debugging, profiling, or optimization. These things you have to learn on your own, usually in a tight deadline.
— Juixe TechKnow

To most Java developers, Ruby/Rails is like a mistress. Ruby/Rails is young, new, and exciting; but eventually we go back to old faithful, dependable, and employable Java with some new tricks and idioms and we are the better programmer for it.
— Juixe TechKnow

You might as well pay your customers 50K because they are just your QA.
— Juixe TechKnow


Oct 27 2007

Top Programming Books on Google Book Search

Here is an extensive list of top programming books available for preview on Google Books. Google Books provides scans of thousands of textbooks. The scans are not the best, most books have visible scan defects in them.

Even though the scans are not the best, there are some features that just work well. Just like Google Maps, where you can send a link to a map (with a set size, address, etc), with Google Books you can send a link to a specific page in a certain book with specific words highlighted. Google also has handy links such as the table of contents, popular passages, and where to buy the book (perhaps in a better quality PDF format).

All the books listed here have a ‘limited preview’, meaning that some pages are not available for viewing but for the most part you can browse through most the the book. Google Books does indicate the pages that are not available.

Java
The Java Language Specification
Effective Java Programming language
Java: The complete Reference
Java In A Nutshell
Head First Java

C/C++
Practical C++ Programming
C++ The Core Language
The Concurrent C Programming Language
C++ Primer Plus

.Net/C#
The C# Programming Language
The Visual Basic .NET Programming Language
Pro C# 2005 and the .NET 2.0 Platform
Learning Visual Basic .NET
VB.NET Language in a Nutshell

Python
Python in a Nutshell
Learning Python
Visual Quickstart Guide: Python
Python Pocket Reference
Python Cookbook

JavaScript/DOM
JavaScript: The Definitive Guide
Beginning JavaScript with DOM Scripting and AJAX
The Book of JavaScript
The Complete Reference JavaScript
JavaScript Bible
DOM Scripting

Ruby/Rails
Ruby in a Nutshell
The Ruby Way
Beginning Ruby
Ruby on Rails: Up and Running
Rails Solutions: Ruby on Rails Made Easy
Beginning Ruby on Rails E-Commerce

PHP
PHP in a Nutshell
Programming PHP
PHP Cookbook
Learning PHP and MySQL
Learning PHP 5

Database
Visual Quickstart Guide: MySQL
MySQL Cookbook
MySQL in a Nutshell
MySQL Tutorial
Programming SQL Server 2005
SQL Server 2005: Developer’s Guide
SQL Server 2005: A Beginner’s Guide
Beginning SQL Server 2005 Express

Technorati Tags: , , , , , , , , , , , , ,


Sep 17 2007

Twelve Elements of JavaScript Style

I’ve been reading a lot of articles and viewing many presentations on JavaScript. One of the most prolific writer and presenter on the subject is Yahoo!’s Douglas Crockford. I’ve recently rediscovered Crockford’s classic article on The Elements of JavaScript Style (part 2). Between these two articles Crockford lists 12 key elements of JavaScript style.

  • Avoid archaic constructions.
  • Always use blocks in structured statements.
  • Avoid assignment expressions.
  • Use object augmentation.
  • Use common libraries.
  • Watch out for type coercion when using ==.
  • Use the ?: operator to select one of two values.
  • Don’t use the ?: operator to select one of two actions.
  • Use the || operator to specify a default value.
  • Never use implicit global variables.
  • global variables are evil.
  • Use inner functions to avoid global variables.

Sep 9 2007

Learning JavaScript

Do you want to learn JavaScript from the experts? You don’t have to pay over $1000 in conference fees to learn from the best JavaScript developers. Here is large collection of video and slide presentations given by Douglas Crockford, senior JavaScript Architect at Yahoo!, John Resig, lead developer or jQuery, and a great many other JavaScript developers.

Videos
The JavaScript Programming Language, part 2, 3, 4

JavaScript – The Good Stuff
An Inconvenient API – The Theory of the DOM, part 2, 3
Advanced JavaScript, part 2, 3
Software Quality
Advancing JavaScript with Libraries, part 2
Best Practices in JavaScript Library Design
High-Performance JavaScript – Why everything You’ve Been Taught is Wrong
Maintainable JavaScript

Slides
Building a JavaScript Library

Metaprogramming JavaScript Presentation
CSS + JavaScript tips
How To Bluff Your Way in DOM Scripting
The Behaviour Layer – Using JavaScript for Good, not Evil
JavaScript 2 and the Future of the Web
JavaScript Boot Camp Tutorial

Technorati Tags: , , , , , , , ,