formatting

Using sed to add a newline on Mac OS X alvin June 12, 2015 - 10:36am

As a quick note today, I have been converting parts of the Scala Cookbook from a plain text format to a Markdown format, and as part of that I needed to add some newline characters to add spacing to the document. This wouldn’t be bad if it was a few pages, but it’s hundreds of pages, so I decided to use the Unix sed command to do the work.

Scala: Displaying XML in a human-readable format (pretty printing) alvin February 17, 2014 - 6:06pm

Problem: You have some XML in a hard-to-read format in a Scala application, and want to print it in a format that’s easier to read, at least for humans.

Solution

Use the scala.xml.PrettyPrinter class. To see how it works, imagine starting with a long, continuous string of XML:

Drupal 6 - The CKEditor is removing/deleting CODE tags

I had a problem using the CKEditor with Drupal 6 where the CKEditor would not display <code> tags properly in the editor, and would then delete trailing spaces after the <code> tag. After some digging around, I finally found that I needed to comment out the following line in the ckeditor.config.js of my CKEditor module installation:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Android JSON - Print a JSON string in a human-readable format (for debugging) alvin March 27, 2012 - 8:31am

Android JSON tip: How to use the JSONObject toString(n) method to debug a JSON string in an Android app, by printing the string in a human-readable format.

While working on an Android project recently, I noticed there is a toString method on a JSONObject object that looks like this:

The Scala String format approach (and Java String.format) alvin March 24, 2012 - 11:28am

Scala String formatting FAQ: How do I write code that is equivalent to the Java String.format class? That is, how do I format strings like that in Scala?

NOTE: As of Scala 2.10 this approach is a little out of date. You can still use this approach, but there's a better way to handle this situation in Scala 2.10 and newer Scala version.

In Java I always write code like this to format a String:

Android EditText inputType - XML formatting examples, options alvin March 15, 2012 - 8:35am

When using Android with "soft" touchscreen keyboards, you can change how the keyboard looks, and how the EditText widget works by specifying an inputType when you create your EditText widget.

A Log4J format example

Log4J formatting FAQ: Can you share a Log4J output/logging format example?

Sure. I'll share a Java Log4J format example that I'm pretty happy with.

I've used the following Log4J logging format quite a bit lately, as I've been working on a headless Java app that can be deployed on thousands of computers, and I was looking for a good Log4J format that was easily readable by humans, and also easy to parse by computers. Here's what my Log4J output format look like these days:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Ant - How to use a date or timestamp in an Ant build script

Summary: An Ant date and timestamp (tstamp) task example.

I was just digging through some Ant build scripts I've created, and I noticed a segment of a build script that first creates a timestamp, and then uses that timestamp in the process of creating a manifest file. (This build script is used for building a Java Swing application.)

Here's the code from my Ant script that does this timestamp magic:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Java printf - output format examples with System.out.format alvin September 13, 2008 - 10:16am

In my previous tutorial on using the Java String format method ("Java sprintf") I showed how to use the format method of the Java String class to format strings and text output. That method works fine in many situations where you use the sprintf function in other languages, such as when you need to either concatenate strings, or print formatted output using something like Log4J.

Fri, May 30, 2003 (printf for Java) alvin May 30, 2003 - 1:56pm

Ahh, a printf()-like utility for Java ... life is good again. :)

Follow-up in 2009

These links are much more up-to-date, now that we have real printf capabilities in Java: