Site Sponsors:
Reading, Writing, & Converting between XML, TDF, & CSV 
Just a quick note to let everyone know that a new release of ClassIO is out:

https://sourceforge.net/p/classio/home/Home/

ClassIO is a command-line tool (executable JAR) that allows us to quickly create POJOs. Java Source Code that can create, read, update, and save arrays of objects via file.

Because support for every format is automatically provided, ClassIO objects can save / convert their data to / from Extended Markup Language (DOM/XML), Comma-Separated Value (CSV), and / or Tab-Delimited Format (TDF.)

Enjoy,

-Rn

[ view entry ] ( 1937 views )   |  permalink  |  related link
Parsing CSV 

Too Many Newbies?


Call me old fashioned - but to-date there is simply nothing more efficient than a 1970's transaction!

XM-ell


Oh sure, XML is great for many things. -But for pushing data between mainframes it is about as fat and chatty as a 400 pound valley-girl. Might the need to ever-purchase more hardware, licenses, and bandwidth to make speeds tolerable be why so many vendors love it?

And let's not start taking about security --Unless we are slowing things down even further with SSL, we might just as well be giving away our data with a bell tied around it.

Gee, Son?


To be sure JSON is far more efficient. Parsers like com.google.gson work sweetly along side of XMLs DOM and SAX. But hey, since Google has to pay-for both the bandwidth and servers themselves, even search-city has finally seen the light. By making clients do a little parable working themselves, GWT (XDR) is even better than JSON. -Yet for all of that big-ended Motorola-binary goodness, XDR is still not as efficient, or arguably as obfuscated (!), as those old-school transactions! (e.g. no bit-field encoding?)

More Old-School Coolness


So as we wait for the technical 'experts' at places like the Gartner Group, IBM (!), and Microsoft to get a clue once again, we thought we would take a moment to celebrate the stark simplicity of yet another old-school data convention: The Comma-Separated-Value (CSV) Format.

Java


A lot of my .NET students rib me about how much Java I write. -Yet when it comes time for a "read-my-lips" demonstration, even my VB guys understand what we are blogging about.

So without further adeau - and in as much as we have seen a ludicrous amount of machination over doing what others consider to be very simple chore - submitted for your consideration is a quick way to parse, as well as to compose, arrays of CSV strings:


/**
* Convert an array of String to a classic CSV-style string set.
*
* @param array An array of String.
* @return A CSV representation of same. Readable by ReadCSV(below)
*/
public static String WriteCSV(String[] array) {
if (array == null) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append("\"");
for (int ss = 0; ss < array.length; ss++) {
if (ss != 0) {
sb.append("\",\"");
}
sb.append(array[ss]);
}
sb.append("\"");
return sb.toString();
}

/**
* Read a classic CSV representation of an array of strings. Both pure-string
* and pure-integer formats are parsed. (note that the convention was CSV, not
* CS_V)
*
* @param sLine An array of strings - possibly created via WriteCSV(above)
* @return An array of String. Array is empty on error.
*/
public static String[] ReadCSV(String sLine) {
if (sLine == null || sLine.length() < 2) {
return new String[0];
}
sLine = sLine.trim();
sLine = sLine.replace(", ", "\n"); // literal - english
String[] array = sLine.split(","); // field
for (int ss = 0; ss < array.length; ss++) {
array[ss] = array[ss].replace("\n", ", ");
int iPos = array[ss].indexOf('"');
if (iPos == 0) {
array[ss] = array[ss].substring(1);
}
iPos = array[ss].lastIndexOf('"');
if (iPos == array[ss].length() - 1) {
if (iPos == 0) {
array[ss] = "";
} else {
array[ss] = array[ss].substring(0, iPos);
}
}
}
return array;
}

When used as the basis for converting strings to native integers & other .Parse()able types, that ReadCSV(sLine) alone is well worth a happy cut-and-paste into your library collection.


--Keep The Change, :-)

-R.A. Nagy

p.s. Here it the test case:

public void testCsvReadWrite() {
{
String[] test = {
"One", "Two", "Three"
};
String str = Text.WriteCSV(test);
String[] result = Text.ReadCSV(str);
if (result.length != test.length) {
fail("Error: XML Reade / Write Failure 100");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 101");
}
}
}
{
String[] test = {
"", "", ""
};
String str = Text.WriteCSV(test);
String[] result = Text.ReadCSV(str);
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 200");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 201");
}
}

}
{
String[] test = {
""
};
String str = Text.WriteCSV(test);
String[] result = Text.ReadCSV(str);
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 300");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 301");
}
}

}
{
String[] result = Text.ReadCSV("");
if (result.length != 0) {
fail("Error: XML Read / Write Failure 400");
}
}
{
String[] result = Text.ReadCSV("\"\"");
if (result.length != 1) {
fail("Error: XML Read / Write Failure 500");
}
}
{
String[] result = Text.ReadCSV("\"");
if (result.length != 0) {
fail("Error: XML Read / Write Failure 600");
}
}
{
String[] test = {
"On\"e", "T\"wo\"", "T\"h\"r\"ee"
};
String str = Text.WriteCSV(test);
String[] result = Text.ReadCSV(str);
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 700");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 701");
}
}
}
{
String[] test = {
"\"One", "\"Two\"", "\"Th\"ree\""
};
String str = Text.WriteCSV(test);
String[] result = Text.ReadCSV(str);
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 800");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 801");
}
}
}
{
String[] test = {
"1", "2", "3"
};
String[] result = Text.ReadCSV("1,2,3");
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 900");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 901");
}
}
}
{
String[] test = {
"One", "2", "3"
};
String[] result = Text.ReadCSV("\"One\",2,3");
if (result.length != test.length) {
fail("Error: XML Read / Write Failure 1000");
}
for (int ss = 0; ss < result.length; ss++) {
if (result[ss].equals(test[ss]) == false) {
fail("Error: XML Read / Write Failure 1001");
}
}
}

}



[ view entry ] ( 2737 views )   |  permalink
Android, Eclipse & Ubuntu 

We Love NetBeans


My friends and I learned how to develop software for Android on NetBeans. -While far from the usual path taken to Android 'Nerdvanna, since we have started training on Android, we have been increasingly reverting back to bad habits. We have been using Eclipse.





Why dread teaching with Eclipse you might ask? -Because we have seen far too many students blow-away a weeks' weight of work while using it. --While admittedly just about all student ''disasters'' center around students tweaking their files and / or settings outside of the IDE (NEVER mess with anything in the workspace folders directly -bugs can sleep until a re-boot -even backups can fail to fix the problem!) --I'm just sayin' ... once burned, is twice shy... Ten time so roasted in front of a classroom could make even Johnny Carson a 'tad neurotic.

Yet in as much as only we old timers expect to treat files as stand-alone, given that the IDE is presently the opiate of the masses, what cannot be cured, must be endured. -After all, according to "Jafar's Golden Rule" - the customer is always right. ;-)





Not Galileo?


Certainly the first place to stop for a great overview of the Android SDK installation process - in general - would be at the Android Site.

Once we find ourselves merrily 'skipping down the Eclipse road to the emerald 'droid habitation, students are often keen to use a customized distro like Galileo.





While jump-start distros are great if you can start there from-scratch, most clients like to integrate the SDK into their existing tool-chain. Hence the need to document the integration process, ourselves. It makes a great how-to.

-Of course, if you need to install the ADK for another reason ('distro blows up on Windows, etc...) then we will assume a 'from scratch' here, too.

The Step-By-Step


So, beginning from step zero, we must jump to:

Step #1 - Get It


You will need:

(1) A Java Run-Time Environment (JRE).

Eclipse is a Java Application. It needs a JRE. So if not already installed, then you can get the JRE from Java.com. Stick with version 1.6 or greater.

Assuming that the JRE installed okay, you might - or might not - want to use Sun's SDK. Eclipse has its own.

Note to OS X Users: If you do not have Java 1.6 available, consider installing Ubuntu under Oracle's Virtual Box -You can get that here. --In fact, if you use more than one machine - be it a laptop at home, or a desktop at work - then everyone should install Android on Ubuntu using Virtual Box Software. Why? Because if you don't mind spending $99 for a pocket drive, then using a virtual machine is a great way to backup, as well as transplant gigabytes of work-in-progress from one place, to another.

(2) You will need a copy of Eclipse - we got mine from The Eclipse Site.

(3) You will also need The Android SDK - The archive is at the Android Download Page.

Step #2 - Installing Eclipse


Setting up Eclipse has always been easy: Once Java has been installed, all we need do is to open the Eclipse archive. After open, we can just drag, drop, and click on the eclipse file, in the eclipse folder.



Note: If Eclipse complains about not being able to find your Java, then note that some versions of Ubuntu have an extremely quirky problem with setting up your path... -Our pristine, updated, and properly-patched install-base on a 32 bit Lucid Lynx VM was one such version. -After too much howling frustration, after updating the path in $HOME/.profile we discovered that all we had to do was to re-boot for $PATH to 'take' to a new shell session... -Who knew?

Step #3 - The ADK


The Android SDK (ADK) is also a queer thing: While you just drag and drop it to install much like Eclipse, once we have done so, we need to run the SDK Management Tool before we can use the download to do much of anything:



See the README in the top ADK folder for instructions on how to see:



-Note that you will probably not want to install as many packages as depicted above -We do a lot of experiments with other platforms, so we installed 1.5, 2.2, 3.2 and 4.0. --When in doubt, 1.5 is the way to go if you have old hardware laying around. -That said however, 2.1 is probably what most folks will want to work with.

Step #4 - The ADK Plug-in Under Eclipse


Google has the poop here. It worked well.

Note that during the install that you will have to browse to select the location where you dropped the ADK (Step #3) proper. Because we also used the tools on NetBeans, we kept that ADK Folder next to - rather than inside of - the eclipse folder.

I was a bit surprised this time to see the plug-in re-download all of the platforms we had selected in Step #3, as well as to suggest the use of the 2.1 Platform. After being prompted, we installed Version 2.1, as well.

Ten dialogs & promptings later, here is what we saw:



Here is the log:


[2011-11-11 15:37:19 - SDK Manager] Fetching https://dl-ssl.google.com/android/repository/addons_list-1.xml
[2011-11-11 15:37:20 - SDK Manager] Validate XML
[2011-11-11 15:37:20 - SDK Manager] Parse XML
[2011-11-11 15:37:20 - SDK Manager] Fetched Add-ons List successfully
[2011-11-11 15:37:20 - SDK Manager] Fetching URL: https://dl-ssl.google.com/android/repository/repository-5.xml
[2011-11-11 15:37:21 - SDK Manager] Validate XML: https://dl-ssl.google.com/android/repository/repository-5.xml
[2011-11-11 15:37:21 - SDK Manager] Parse XML: https://dl-ssl.google.com/android/repository/repository-5.xml
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 1.1, API 2, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 1.5, API 3, revision 4
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 1.6, API 4, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.0, API 5, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.0.1, API 6, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.1, API 7, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.2, API 8, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.3.1, API 9, revision 2 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 2.3.3, API 10, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 3.0, API 11, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 3.1, API 12, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 3.2, API 13, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found SDK Platform Android 4.0, API 14, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found ARM EABI v7a System Image, Android API 14, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 7, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 8, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 9, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 10, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 11, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 12, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 13, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Samples for SDK API 14, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Android SDK Platform-tools, revision 9
[2011-11-11 15:37:21 - SDK Manager] Found Android SDK Tools, revision 15
[2011-11-11 15:37:21 - SDK Manager] Found Documentation for Android SDK, API 14, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Android Support package, revision 4
[2011-11-11 15:37:21 - SDK Manager] Fetching URL: https://dl-ssl.google.com/android/repository/addon.xml
[2011-11-11 15:37:21 - SDK Manager] Validate XML: https://dl-ssl.google.com/android/repository/addon.xml
[2011-11-11 15:37:21 - SDK Manager] Parse XML: https://dl-ssl.google.com/android/repository/addon.xml
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 3, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 4, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 5, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 6, revision 1 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 7, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 8, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 9, revision 2 (Obsolete)
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 10, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 11, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 12, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 13, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google APIs by Google Inc., Android API 14, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google TV by Google Inc., Android API 12, revision 2
[2011-11-11 15:37:21 - SDK Manager] Found Google Market Licensing package, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google USB Driver package, revision 4
[2011-11-11 15:37:21 - SDK Manager] Found Google Market Billing package, revision 1
[2011-11-11 15:37:21 - SDK Manager] Found Google Admob Ads Sdk package, revision 3
[2011-11-11 15:37:21 - SDK Manager] Found Google Webdriver package, revision 1
[2011-11-11 15:37:21 - SDK Manager] Fetching URL: http://www.echobykyocera.com/download/e ... sitory.xml
[2011-11-11 15:37:22 - SDK Manager] Validate XML: http://www.echobykyocera.com/download/e ... sitory.xml
[2011-11-11 15:37:22 - SDK Manager] Parse XML: http://www.echobykyocera.com/download/e ... sitory.xml
[2011-11-11 15:37:22 - SDK Manager] Found Dual Screen APIs by KYOCERA Corporation, Android API 8, revision 1
[2011-11-11 15:37:22 - SDK Manager] Found Dual Screen APIs by KYOCERA Corporation, Android API 10, revision 1
[2011-11-11 15:37:22 - SDK Manager] Fetching URL: http://developer.lgmobile.com/sdk/andro ... sitory.xml
[2011-11-11 15:37:23 - SDK Manager] Validate XML: http://developer.lgmobile.com/sdk/andro ... sitory.xml
[2011-11-11 15:37:23 - SDK Manager] Parse XML: http://developer.lgmobile.com/sdk/andro ... sitory.xml
[2011-11-11 15:37:23 - SDK Manager] Found Real3D by LGE, Android API 8, revision 1
[2011-11-11 15:37:23 - SDK Manager] Fetching URL: http://innovator.samsungmobile.com/andr ... sitory.xml
[2011-11-11 15:37:24 - SDK Manager] Validate XML: http://innovator.samsungmobile.com/andr ... sitory.xml
[2011-11-11 15:37:24 - SDK Manager] Parse XML: http://innovator.samsungmobile.com/andr ... sitory.xml
[2011-11-11 15:37:24 - SDK Manager] Found GALAXY Tab by Samsung Electronics., Android API 8, revision 1
[2011-11-11 15:37:24 - SDK Manager] Fetching URL: http://developer.sonyericsson.com/edk/a ... sitory.xml
[2011-11-11 15:37:25 - SDK Manager] Validate XML: http://developer.sonyericsson.com/edk/a ... sitory.xml
[2011-11-11 15:37:25 - SDK Manager] Parse XML: http://developer.sonyericsson.com/edk/a ... sitory.xml
[2011-11-11 15:37:25 - SDK Manager] Found EDK 1.1 by Sony Ericsson Mobile Communications AB, Android API 10, revision 1
[2011-11-11 15:37:25 - SDK Manager] Done loading packages.
[2011-11-11 15:38:26 - SDK Manager] Preparing to install archives
[2011-11-11 15:38:26 - SDK Manager] Downloading Android SDK Tools, revision 15
[2011-11-11 15:39:07 - SDK Manager] Installing Android SDK Tools, revision 15
[2011-11-11 15:39:09 - SDK Manager] Installed Android SDK Tools, revision 15
[2011-11-11 15:39:09 - SDK Manager] Downloading Android SDK Platform-tools, revision 9
[2011-11-11 15:39:25 - SDK Manager] Installing Android SDK Platform-tools, revision 9
[2011-11-11 15:39:25 - SDK Manager] 'adb kill-server' succeeded.
[2011-11-11 15:39:26 - SDK Manager] Installed Android SDK Platform-tools, revision 9
[2011-11-11 15:39:26 - SDK Manager] Downloading SDK Platform Android 4.0, API 14, revision 1
[2011-11-11 15:40:37 - SDK Manager] Installing SDK Platform Android 4.0, API 14, revision 1
[2011-11-11 15:40:42 - SDK Manager] Installed SDK Platform Android 4.0, API 14, revision 1
[2011-11-11 15:40:42 - SDK Manager] Downloading SDK Platform Android 2.1, API 7, revision 3
[2011-11-11 15:43:04 - SDK Manager] Installing SDK Platform Android 2.1, API 7, revision 3
[2011-11-11 15:43:08 - SDK Manager] Installed SDK Platform Android 2.1, API 7, revision 3
[2011-11-11 15:43:33 - SDK Manager] 'adb kill-server' succeeded.
[2011-11-11 15:43:36 - SDK Manager] ADB: * daemon not running. starting it now on port 5037 *
[2011-11-11 15:43:36 - SDK Manager] 'adb start-server' succeeded.
[2011-11-11 15:43:36 - SDK Manager] Done. 4 packages installed.
[2011-11-11 15:43:36 - SDK Manager] ADB: * daemon started successfully *


Conclusion


Having used Helios (Eclipse 3.6), we must say that the Indigo install (Eclipse 3.7) went a lot smoother. Unlike the last time however, we installed it-all on a Virtual Box; With absolutely no trial-and-error, this same 32-bit Ubuntu Operating System image ran just fine on OS X, Windows XP, 64-bit Windows 7, and 64-bit Ubuntu.





Because we now have a VM ready to help jump-start every student project from-scratch, perhaps the best reason for this blog entry is to remind myself of what others might expect. -Since we have saved a few copies of the virtual image, we will probably not have to repeat the above process for a good deal longer than human-memory should be required to recollect such a mismatched hodge of geeky-wonderment.

Enjoy!

-R.A. Nagy



[ view entry ] ( 3319 views )   |  permalink

<<First <Back | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | Next> Last>>