MDBitz - Matthew Denton
Java
Overview
A common task in various programming languages is to split a string based on delimiter and create a resulting array. In java we sometimes prefer to utilize collections instead of a basic array object, luckily java has the Arrays object that can be used to perform various functions on an inputted array.
The Step by Step
In java the String object has a built in split function that takes a regular expression as an input and returns an array of string values split by the regex. lets illustrate this by creating a new String.
String str = new String("001,002,003,004;005:006");
Next lets split the string based on 3 delimiters ; , and |
String[] sArray = str.split( "[,;:]" );
This will return a String array containing the values 001, 002, 003, 004, 005, and 006 as the regex [,;:] means to match any character contained between the []. Once we have our String Array we can then utilize the Arrays Class’s asList static function to convert the Array to a list to instantiate an ArrayList
List sList = new ArrayList( Arrays.asList(sArray) );
If we wanted to condense the above to one line we could use the following:
List sList = new ArrayList( Arrays.asList( str.split( "[,:;]" ) );
Conclusion
That is the process in a nut shell, don’t forget that with user supplied information you may also want to scrub your data for any invalid values or for empty values especially when your delimiter is a new line. To do this you can easily use an Iterator or manipulate the array generated by the split function before generating the Array List.
MDBitz - Matthew Denton
Java
What is iBATIS?
iBATIS is a framework that utilize DAO and SQL Maps to create a light weight persistence layer interfaces that allows you as the developer to easily interact with your Database using objects and sql that you write instead of learning complex ORMs such as Hibernate. Simply put iBATIS acts as the mapper layer between your Database and Objects. Also for those that find themselves working in both Java and .NET iBATIS has implementations in both languages. Basic information on both SQL Maps and the DAO Framework is below, to get started either visit the iBATIS website or their official Developer Documentation.
SQL Maps
SQL Maps are XML descriptor files that outline how to connect with your database, and how to map your objects to SQL Queries and SQL Queries Results to your objects. iBATIS provides basic a tutorial on using SQLMaps on their project site.
DAO
Data Access Objects (DAOs) are objects that represent an interface for interacting with data without exposing the underlying database. This provides a separation of application or business logic from the persistance layer or database allowing code to be reused across multiple environments.
.NET, ORM, Persistence Layer, SQL
MDBitz - Matthew Denton
PHP
Email Validation is a tough subject to nail down, as their are tons of available code snippets that claim to validate an email address. To make it easier below you will find the requirements for a valid email address as outlined by RFC 2822 followed by a script that can be used to validate email addresses provided by linuxjournal.com.
Email Address Requirements
- An e-mail address consists of local part and domain separated by an at sign (@) character (RFC 2822 3.4.1).
- The local part may consist of alphabetic and numeric characters, and the following characters: !, #, $, %, &, ‘, *, +, -, /, =, ?, ^, _, `, {, |, } and ~, possibly with dot separators (.), inside, but not at the start, end or next to another dot separator (RFC 2822 3.2.4).
- The local part may consist of a quoted string—that is, anything within quotes (“), including spaces (RFC 2822 3.2.5).
- Quoted pairs (such as \@) are valid components of a local part, though an obsolete form from RFC 822 (RFC 2822 4.4).
- The maximum length of a local part is 64 characters (RFC 2821 4.5.3.1).
- A domain consists of labels separated by dot separators (RFC1035 2.3.1).
- Domain labels start with an alphabetic character followed by zero or more alphabetic characters, numeric characters or the hyphen (-), ending with an alphabetic or numeric character (RFC 1035 2.3.1).
- The maximum length of a label is 63 characters (RFC 1035 2.3.1).
- The maximum length of a domain is 255 characters (RFC 2821 4.5.3.1).
- The domain must be fully qualified and resolvable to a type A or type MX DNS address record (RFC 2821 3.6).
Validation Script
function validEmail($email) {
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex) {
$isValid = false;
} else {
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64) {
// local part length exceeded
$isValid = false;
} else if ($domainLen < 1 || $domainLen > 255) {
// domain part length exceeded
$isValid = false;
} else if ($local[0] == '.' || $local[$localLen-1] == '.') {
// local part starts or ends with '.'
$isValid = false;
} else if (preg_match('/\\.\\./', $local)) {
// local part has two consecutive dots
$isValid = false;
} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
// character not valid in domain part
$isValid = false;
} else if (preg_match('/\\.\\./', $domain)) {
// domain part has two consecutive dots
$isValid = false;
} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
eMail, Validation