Site Sponsors:
World's Simplest Source Code Formatter? 
Formatting code need not be tough. Indeed, when it came time to write a simple brace-based source code indentation-class for ClassIO, here is what we came up with:


package com.soft9000;

import java.util.Formatter;
import javax.xml.bind.DatatypeConverter;

/**
* A simple "brace based" code-formatter. Source code comments are supported.
*
* @author profnagy
*/
public class SourceCodeFormatter {

public static String FormatCode(String sText) {
SourceCodeFormatter ref = new SourceCodeFormatter();
return ref.format(sText, " ");
}

static class StringTokenizer {

private final String sBlockOn;
private final String codeStringReplacement;
private final String codeString;

public StringTokenizer(String sBlockOn, char chCode) {
this.sBlockOn = sBlockOn;
codeString = "" + chCode;
Formatter ref = new java.util.Formatter();
codeStringReplacement = ".:" + DatatypeConverter.printHexBinary(codeString.getBytes()) + ":.";
}

public String encode(String sSource) {
sSource = sSource.replace(codeString, codeStringReplacement);
return sSource.replace(sBlockOn, codeString);
}

public String decode(String sSource) {
sSource.replace(codeString, sBlockOn);
sSource = sSource.replace(codeStringReplacement, codeString);
return sSource;
}

public char getCode() {
return codeString.charAt(0);
}

public String getStirng() {
return sBlockOn;
}
}
StringTokenizer blockCommentOn;
StringTokenizer blockCommentOff;
StringTokenizer lineCommentOn;

public SourceCodeFormatter() {
blockCommentOn = new StringTokenizer("/*", '$');
blockCommentOff = new StringTokenizer("*/", '&');
lineCommentOn = new StringTokenizer("//", '#');
}

public String format(String sText, String sLevelPrefix) {
if (sText == null || sLevelPrefix == null) {
return "";
}
sText = this.lineCommentOn.encode(sText);
sText = this.blockCommentOff.encode(sText);
sText = this.blockCommentOn.encode(sText);

int pwComment = 0;

char chStart = '{';
char chStop = '}';
StringBuilder sb = new StringBuilder();
int iLevel = 0;
char[] chars = sText.toCharArray();
boolean bIgnoreWS = false;
for (char ch : chars) {
switch (pwComment) {
case 0: {
if (ch == blockCommentOn.getCode()) {
pwComment = 2; // flag block comment
sb.append(blockCommentOn.getStirng());
bIgnoreWS = false;
continue;
}
if (ch == lineCommentOn.getCode()) {
pwComment = 1; // flag linear comment
sb.append(lineCommentOn.getStirng());
bIgnoreWS = false;
continue;
}
}
case 1: {
if (ch == '\n') {
pwComment = 0; // passthru of single-line comment ends
bIgnoreWS = true;
sb.append(ch);
continue;
}
}
break;
case 2: {
if (ch == blockCommentOff.getCode()) {
pwComment = 0; // passthru of block comment ends
bIgnoreWS = true;
sb.append(blockCommentOff.getStirng());
continue;
}
}
break;
}
if (pwComment == 0) {
if (ch == '\n') {
bIgnoreWS = true;
sb.append(ch);
continue;
}
if (ch == chStart) {
iLevel++;
}
if (ch == chStop) {
iLevel--;
}
if (bIgnoreWS) {
boolean isWS = Character.isWhitespace(ch);
if (isWS) {
continue;
}
for (int ss = 0; ss < iLevel; ss++) {
sb.append(sLevelPrefix);
}
}
}
sb.append(ch);
bIgnoreWS = false;
}
sText = this.lineCommentOn.decode(sb.toString());
sText = this.blockCommentOff.decode(sText);
sText = this.blockCommentOn.decode(sText);
return sText;
}
}


Notice that we did not have to create an entire project, include a jar, or add hundreds of lines to perform such a simple task. -A nice little class to use when we need to keep things simple, feel free to use SourceCodeFormatter to indent-format your C/C++, Java, and C# project(s), as well.

[ add comment ] ( 2217 views )   |  permalink  |  related link
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

[ add comment ] ( 1722 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");
}
}
}

}



[ add comment ] ( 2542 views )   |  permalink

<<First <Back | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | Next> Last>>