Archive for July, 2006

Quick Snippets: Filter JTextField Data

Posted on July 26, 2006, under Development.

Java makes it real easy to limit the data type of a JTextField with documents. Java allows you to set different documents that will validate user inputed data on the fly. For example in an options dialog, it is common to see a field that will only allow numerical inputs. This technique can be accomplished with a simple class and one line of code to implement that class.

[java]public class NumericDocument extends PlainDocument
{
// Variables
// — Private
private int m_nMaxLength;
private static final int MAX_LENGTH = 2;

public NumericDocument()
{
m_nMaxLength = 999999999;
}

public NumericDocument(int nMaxLength)
{
m_nMaxLength = nMaxLength;
}

// Insert string
public void insertString(int nOffset, String strString, AttributeSet aAttributes) throws BadLocationException
{
// Replace
strString = strString.replaceAll(”[^0-9]“, “”);

// Contruct
super.insertString(nOffset, strString, aAttributes);
}
}[/java]

And to implement with your JTextField

[java]
JTextField m_ctlTxtOption;

m_ctlTxtOption.setDocument(new NumericDocument());
[/java]

This technique can be extended and you can customize the data validation to your liking. Lets say you wanted a JTextField without any spaces allowed in it, you could use this line of code to validate the document data:

[java]
strString = strString.replaceAll(” “, “”);
[/java]

1 Comment



Template Engines

Posted on July 25, 2006, under Development.

My recent article about PHP frameworks details the many different avenues one can travel when in need of a PHP framework. Just like frameworks, template engines in PHP also give you a lot of different options. Options can be good, however their are advantages to the different forms of template engines.

Compiling

Some template engines like the popular Smarty template engine, compile templates. This means that template files have there own syntax, and then Smarty will compile this syntax into PHP code which will be used to display your template. For the longest time I used the Smarty template engine, until I learned that this might not be the best solution. PHP already acts as a template engine in it self, and even though Smarty carries lots of advantages, it arguably doing more work than it needs by compiling templates into PHP code. In addition, there is a bit of a learning curve in order to write templates in the Smarty scripting language. Other template engines, like Savant, build a nice template engine around PHP. This means that there is no extra scripting language to learn, and no extra resources used to compile your templates into PHP code. I have recently switched most of my sites over to the Savant engine and seen a slight performance increase because Savant is not compiling templates on the fly like Smarty.

Caching

Some template engines, again like Smarty, have caching options that will cache you templates so that they do not need to recompile your template every time a page is requested. This is avoided in template engines that do not compile templates. Caching is a good feature, but sometimes hinders the way a site operates. Lets say if you have a side bar that displays information dynamically, with caching changes will not be live instead they will reflect the cache settings on how often the data is updated. There are several ways around this, but it is something to consider when choosing a template engine and deciding what works for your situation and needs.

Plug ins

Most all template engines have a plug in architecture, and depending on how active of a community supports the project; there will be a directory of pre-made templates. One thing to look for in a template engine is not only what plug ins you will need, but how are the plug ins loaded. You do not want a template engine that will load all plug ins installed. Rather a better approach is allowing you to have control on when a plug in is loaded. Think about it: if a plug in is only used in one section of your site, you do not want that plug in loaded into memory for every other page on your application when it is not needed. Unused plug ins should not take execution time to load and memory resources when they are not going to be used.

Install Globally

When you decide on a template framework to go with, install it globally on your server and include it in you php.ini file. This will allow you to use it for numerous websites on your server and only have to upgrade the template framework in one place when updates are available. This is a good tip to follow for any other classes that you use across applications, it will give you one place that will include all the necessities for your applications.

No Comments



My Transition to Subversion

Posted on July 20, 2006, under Development.

So for my home office, I have always chosen Perforce for my source code control needs. But most recently I got a new laptop for work, and finally get a chance to experience how bad Perforce is in allowing you to work disconnected from the server. Over the years I have experienced my little frustrations with Perforce and had bee meaning to check out alternatives, but this was the last straw. It took me longer to get setup with Perforce on my new laptop, than it did to format and customize Windows to my liking.

So we move on; I have used CVS at past jobs, but never really liked it. I can’t put my finger on exactly what it was, but it just wasn’t intuitive to use. I figure an application has to be easy enough for anyone to get started, and then over time you can play with advance features and become an expert at it.

Subversion has been on my radar for a while now, of course never having the time to sit down and play with it. That’s kind of how my to do list has worked as of late, things only get done when they are an absolute necessity, all others wait their turn. So a quick Google for a tutorial on how to setup Subversion on a Windows server, lead me to this neat little project. From there setup was a breeze on the server side, took less than a minute. I then went on to choose my client, yes I choose a graphical user interface over being a command line guru. Seriously, mad props to those who can do this kind of stuff via command line, it’s on my to do list to become better at it. For now, I like the pretty.

SmartSVN was in my bookmarks because I used their SmartCVS client at my last job so I gave it a go. So about half way through importing my workspace, it complains that it ran out of memory. Ahh the one set back to Java, it didn’t have enough memory allocated to the JavaHeap. So I changed their config file, and tried again. Things progressed, it took a while to get half way through my workspace, and it crashed again. So once again bumping the config file up to almost 1GB of memory! However, this time when I started the application, it had to scan my current workspace which took about twelve minutes. Enough on this, I reinstalled the server just to get a clean start, and installed TortoiseSVN which is where I should have started at the beginning.

So after twenty four hours I can say I like it. My only little complaint is the “.svn” folders everywhere, which is similar to the “.cvs” folders from CVS. These tend to get in the way when you are packaging up install files and need to recursively load a directory. Or with websites and when you go to upload a webpage. However, a little ignore rule takes care of those problems, and all the benefits out-way that issue. I also must say, it’s fast. I know I am on a local network, but it seems to be hauling it’s little tail along.

So Subversion is my new source code control solution, I will come back and share my experiences again in a couple months. I hope things continue to go well.

6 Comments



Take Your Object Oriented Quiz

Posted on July 19, 2006, under Development.

Object orientation is a large part of software development. When I was in college, I can remember tons of lessons and test covering the fundamentals of OO development. Felipe Gaucho has come up with a quiz that he says is great for teachers interested in offering their pupils a revision material and also to interchange ideas about how to enhance the OO teaching methodologies.


One of the most tricky parts to learn Object Orientation is the concept of classification. The task of separating entities of the real word in groups and then enumerating the characteristics of each group seems quite simple for experienced programmers but could be messy for novice students. The discussion about the fundamentals of classification is very important to establish a baseline about what are classes. A clean explanation about this subject is a challenge for the teachers and the fluency about the OO terminology is the first duty for absolute beginners in Java or any other object oriented programming language. This test is not closed to Java - it was created to be independent of programming languages.

The audience: this test could be useful for teachers interested in offer their pupils a revision material and also to interchange ideas about how to enhance the OO teaching methodologies. The students of introductorial classes about Java and OO could check their knowledge and discuss variations about the answers. The feedback from the students brainstorm and also from the teachers usage of this material is the most valuable result I can expect from this text. The questions below are the result of a continuous effort in order to facilitate the learning of the basic concepts of classification. If you remember an amazing question - the one that helped you to figure out the idea of classes, objects and its relationships, post your comments on the bottom of this page.

Take the test, and see how you do.

1 Comment

60 More AJAX Tutorials

Posted on July 18, 2006, under Development.

If you haven’t had enough AJAX yet, Max Kiesler has put together a great article containing 60 AJAX tutorials. So now the next web 2.0 service you start can look and feel like the big boys.

1 Comment

How to Use CURL With PHP

Posted on July 17, 2006, under Development.

Introduction

CURL is a library created by Daniel Stenberg, which allows you to connect and communicate with many different types of servers through various protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

Requirements

You will first need to install the lubcurl package if not already installed on your webserver. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that’s 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.

Getting Started

To initiate a CURL session we use the curl_init() function. There are many different options that we are allowed to set with the curl_setopt() function. Once CURL has been setup the way we want, we need to execute the request with the curl_exec() function. Here is an example of a simple script to return the contents of Google’s homepage.

[php] // Initialize the CURL library
$cURL = curl_init();

// Set the URL to execute
curl_setopt($cURL, CURLOPT_URL, "http://www.google.com");

// Set options
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

// Execute, saving results in a variable
$strPage = curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);
?>[/php]

CURL and Form Data

We can also use CURL to post data to a form. The default method of sending form data with CURL is in GET, but in this example we will use POST to submit a search term to an imaginary form.

[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";

// Initialize the CURL library
$cURL = curl_init($cURL);

// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);

// Execute, saving results in a variable
$strPage = curl_exec();

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?>[/php]

CURL and Proxy Support

CURL has support for proxies, including SSL. Below we refer to the code snippet in the first example, but modify it to use a proxy.

[php] // Initialize the CURL library
$cURL = curl_init();

// Set the URL to execute
curl_setopt($cURL, CURLOPT_URL, "http://www.google.com");

// Set options
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($cCURL, CURLOPT_PROXY, "myproxy.com:1080");
curl_setopt($cCURL, CURLOPT_PROXYUSERPWD, "mysuername:mypassword");

// Execute, saving results in a variable
$strPage = curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);
?>[/php]

CURL with Authentication

CURL comes with support for most forms of HTTP authentication, including HTTP basic, digest, GSS and NTLM. We will use out code snippet from the second example, but modify it to use basic authentication.

[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";

// Initialize the CURL library
$cURL = curl_init();

// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);
curl_setopt($cCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, "[myusername]:[mypassword]");

// Execute, saving results in a variable
$strPage = curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?>[/php]

Conclusion

CURL has many other great uses and features, and I suggest visiting the website to learn more about CURL and ways to implement it into your PHP applications.

6 Comments

Selling byteMyCode

Posted on July 17, 2006, under Development.

So most recently I have made the decision to sell byteMyCode. It was a fun project that took me away from things I had to do, and allowed me to do something I wanted to do. The truth is I no longer have the time it takes to keep the visitors coming in, and keep the site up and running. I am making sure it goes to a good home, and will stay involved in the project as much as I can.

The site is being sold at the SitePoint’s market place for those who are interested in potentially placing a bid.

No Comments

yGuard 2.0 Released

Posted on July 13, 2006, under Development.

We are proud to announce the availability of yGuard 2.0.

http://www.yworks.com/products/yguard

yGuard is our free bytecode obfuscator that integrates perfectly with the Ant build tool. It can be used to obfuscate jar files and text-resource files accordingly in order to both make decompilation a lot more difficult and decrease download file sizes of your applications.

Version 2.0 makes configuring the obfuscation process easier and uses sophisticated bytecode analysis that allows it to remove unneeded fields, methods, and classes.

Please see the change log for more details:
http://www.yworks.com/en/products_yguard_changes.htm

So for as many advantages that Java brings to the table, the one major set back is that it is easy to reverse engineer. Obfuscation have made it almost impossible to completely reserect the full source code from Java class files. When I was looking at obfuscators’ a couple months back, yGuard was at the top of the list. I did not end up going with yGuard because it lack string encryption, which I am disappointed to see still has not made it in the product. Still, yGuard is the best free Java obfuscator on the market these days.

No Comments

Make Anything Web 2.0

Posted on July 12, 2006, under Development.

Want to spruce up your site with all of your favorite Web 2.0 AJAX effects? Head over to dhtmlgoodies.com, because they have the answer for you. This site has examples and source code downloads for some of the coolest effects on the web today. Dynamic form validation, sliders, color selection, menus, trees, drag and drop, tooltips, tables, and more! This is a must bookmark for any developer, because some day you will want to implement these scripts into your page. Even if it is just for the heck of it.

1 Comment

PHP Frameworks

Posted on July 12, 2006, under Development.

Every time I start a new web project, I have to ask myself the one question that developers have to answer all the time. “Do I reinvent the wheel?” Frameworks have become a major player in online development, they carry lots of benefits in helping you efficiently writing applications and doing so quickly. Frameworks can also help you with scaling issues. If your application or service grows in users, frameworks can sometimes help you with that load. And no one can forget AJAX, some frameworks even have built in support to allow you to easily include spiffy AJAX effects into your application.

I for one, have yet to use a framework in a real world application and this has been out of choice. My main reason for never implementing a framework is because it most of my cases it would be an overkill. None of my applications would see 1/4 the benefits of using a framework. In addition, like most developers, I suffer from the “wanting to build it myself” gene. Even if it is already written by someone else, and even if it would make my life a lot easier; I still want to create it myself. Why? To see if I can do it. In addition, a lot of the frameworks out there are open source and development on them is moving along really fast right now. It is great to see innovation come that fast, it is definitely something that the commercial software industry can’t compete with. However, this might deter some developers because they now have to stay on top of the development and update their framework with bug fixes and new features. Additionally the more popular that framework becomes, the more exploits thee might be.

On the flip side, frameworks help your application more than you know. If you have a series of functions a design patterns from the beginning, and those are solid and well written, it will help you continue that path with the rest of the code you write. Later on I talk about code I wrote, and how frameworks made me look back and see there were a lot of things I could have done better with that code. Frameworks give you a good foundation, that the rest of your site will follow.
With all of that said, frameworks are still a cool thing and I have been playing with one for a couple months. CakePHP is a free open source PHP framework. When choosing a framework to study and test out, a lot of the ones I tried suffered from some of the problems stated above. CakePHP on the other hand did not. It takes the cake for PHP frameworks. (sorry) Even if you are not interested in PHP frameworks; you have to check out their website, it is a very well designed and laid out site with kind of a retro spin on it.

So CakePHP was a breeze to setup, it took five minutes maybe ten because I was a newbie and following instructions to a T. Next I thought, OK now I have to learn how to use this; when actually it wasn’t as hard as I thought it would be. I decided to make a test application, I figured this would be a real life scenario for myself and would allow me to cover all of the basics: forms, databases, and recurring functions. CakePHP was amazing in all of those aspects and more. I am still learning new functionality with CakePHP.
In the past I have always had a series of PHP files that I use in different web applications. Ones I wrote years ago that work, and work to my liking. CakePHP has definitely taken me outside of my comfort zone, but has done so in a good way allowing me to see better ways of accomplishing things. I will be continuing to follow development on CakePHP and other frameworks. For now I am starting to integrate CakePHP into one of my bigger web applications to see what it will do in a real world scenario for me.

7 Comments

« Older Entries