Sunday, July 30, 2017

Scripts and .screenrc to make GNU Screen splits easier

Update Thursday, 3rd of August 2017, 11:04:30 AM: much better to use bind over stuff to run a script within the .screenrc file.

I enjoy GNU screen via Cygwin on Windows a lot, but some of the commands get a bit fiddly. Creating splits is like that: you create a split, then move to it, then assign a window to it.

My first set of shortcuts were to create scripts to do those jobs.

To make a vertical split:

To make a horizontal script:

Lastly, key bindings that go in my .screenrc file to run those scripts.

# ------------------------------
# ------------------------------
# Uses split scripts.
# Control+a, V for vertical; control+a, H for horizontal.
# bind V stuff 'screenSplitVertical'\012''
# bind H stuff 'screenSplitHorizontal'\012''
bind V exec $HOME/bin/
bind H exec $HOME/bin/

Explanation for version that uses exec:

  1. bind V says the rest of this line will be executed upon "control+a, shift+v"
  2. exec means run the rest of the line as a command.
  3. $HOME/bin/ is the full path to the command (script) I want to run.

Explanation for version that uses stuff:

  1. bind V says the rest of this line will be executed upon "control+a, shift+v"
  2. stuff will "write stuff to the command line" or "stuff text onto the command line".
  3. 'screenSplitVertical'\012'' will write out the command and then '\012' which outputs a newline (ENTER key) to actually cause the script to run.

In general, exec is more suitable than stuff in this situation because stuff will leave the command in my command history.

More screen magic? Check out joaopizani's .screenrc config for some great ideas about resizing splits and moving between them.

Sunday, November 08, 2015

Maven project with Spring, Log4j2 and properties

This a reference I use to quickly come up with a standalone app (no server) that uses Spring Boot, Log4j2 and a properties file.

The pom.xml file.

<project xmlns="" xmlns:xsi=""




The configuration class that launches the app.

package org.testproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

 * Configuration and runner for the test app.
@ComponentScan(basePackages = { "org.testproject.*" })
public final class TestApp {

    * Launch app.
    * @param args
    *           not used
   public static void main(final String[] args) {, args);


The runner - the class that does the actual work.

package org.testproject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

 * Read in a message file from Touch Copy and transform the contents to be diary
 * ready.
 * @author RobertMarkBram
@PropertySources(value = {@PropertySource("classpath:/")})
public final class Runner implements CommandLineRunner {

   /** Logger for this class. */
   static final Logger LOG = LogManager.getLogger(Runner.class

   /** Use to access properties. */
   private Environment env;

   /* (non-Javadoc)
    * @see org.springframework.boot.CommandLineRunner#run(java.lang.String[])
   public void run(final String... args) throws Exception {
      String testproperty = env.getProperty("testProperty");;


The application property file.

testProperty=Hello World

The log4j2 configuration - note that number 2 is there.. this not old log4j anymore.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
      <Console name="CONSOLE" target="SYSTEM_OUT">
         <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
      <logger name="org.testproject" level="DEBUG" />
      <Root level="ERROR">
         <AppenderRef ref="CONSOLE"/>

Tuesday, September 15, 2015

Date time stamps in bash with ordinal indicator

Update (Wednesday, 4th of November 2015, 12:15:35 AM): added link to more complicated script.

Date time stamps are easy in bash:

> echo $(date +"%Y%m%d_%H%M%S")

But I want more friendly date time stamps, like Tuesday, 15th of September 2015, 09:54:42 PM.

I found an algorithm that Greg Mattes wrote in this StackOverflow answer for Java. Here it is in bash:

n=$(date +"%d")

if [ $n -ge 11 -a $n -le 13 ] ; then
  echo "th"
 case $(( $n%10 )) in
   echo st
   echo nd
   echo rd
   echo th

So now I can do:

> echo $(date +"%A, %d`` of %B %Y, %I:%M:%S %p")
Tuesday, 15th of September 2015, 09:57:53 PM

Here is a more complicated version of this script that will accept a date, a date format and output formatted date where you can use %O as a placeholder for the ordinal in the format:

Thursday, June 18, 2015

JQuery UI tooltip with a close link for mobile

Here is the JQuery code I use to to set up JQuery UI tool tips with a close link that only appears on mobile platforms. This is useful because on a desktop, the tool tip will close when you mouseout from the target element, but on a touch platform it sticks around.

// Set up tool tips for images and anchors.
$( document ).tooltip({
   items: "a[title], img[alt], .toolTip[title], :not(.noToolTip)",
  track: true,
  position: { my: "left+15 center", at: "right center" },
   content: function() {
      var element = $( this );
      var closer = closerLink = '';
      if (isMobile()) {
         closer = ' <br><div onClick="$(this).parent().parent().remove();" style="color: blue; text-decoration: underline; text-align: right;">Close</div>';
         closerLink = ' <br>Tap link again to open it.<br><div onClick="$(this).parent().parent().remove();" style="color: blue; text-decoration: underline; text-align: right;">Close</div>';
      // noToolTip means NO TOOL TIP.
      if ( ".noToolTip" ) ) {
         return null;
      // Anchor - use title.
      if ( "a[title]" ) ) {
         return element.attr( "title" ) + closerLink;
      // Image - use alt.
      if ( "img[alt]" ) ) {
         return element.attr( "alt" ) + closer;
      // Any element with toolTip class - use title.
      if ( ".toolTip[title]" ) ) {
         return element.attr( "title" ) + closer;

function isMobile() {
   return (/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent);

I am targeting three types of things here:

  • Anchor tags (a) with a title attribute.
  • Image tags (img) with a title attribute.
  • Any element with class toolTip.
  • And specifically exclude any element with class noToolTip.

You can see this in action on my newly refreshed site: Chihuahua Rescue Victoria.

Wednesday, June 17, 2015

Run main class in test dir from maven with Java agent and Log4J configuration

It took a bit of help from Michal (on this StackOverflow post: Running main method from test class via maven) before I got this right. But here it is:

                  <classpath />

And I run it with the command line: mvn -e exec:exec -Prun-importer

The advantages of this approach:

  • The whole purpose of this profile is to run "special code" that should never be deployed but needs to make use of code in src and test src.
  • It leaves room in case this ever needs to be duplicated, so there is no "competition" regarding what gets run with mvn -e exec:exec.
  • I can specify java agent, log4j and lots of other config using variables that already exist in the pom.
  • I can override any of these arguments on the command line with -Darg1="bar"

Sunday, April 26, 2015

Generating getters setters toString hashCode and equals

Generating code is a good way to remove so much boilerplate in Java that you would otherwise write by hand. Getters and setters is a big one. Eclipse can generate those, but Eclipse generation clashes with Checkstule rules that I use. The toString(), equals() and hashCode() methods are important to get right too. Previously I set up Eclipse templates but I still had to fill in the details by hand. Most of the time it's better to start with a default implementation for each that uses every non-static field in the class.

This post demonstrates some code I have written to automate the generation of these things.

Checkstyle vs Getters and Setters

I use Checkstyle wherever I can. It encourages me to write code with a consistent style and, like FindBugs, helps me to avoid common errors. Here are some Checkstyle rules that I use:

  1. Ensure that I have a jadvadoc comment on fields and methods. This includes checking that I have a @return tag for non-void methods and a @param tag for each parameter. Javadoc comments let me impart important context that cannot always be achieved by self documentating code.
  2. Final parameters on methods. Avoid accidentally re-assigning a parameter during method execution by making sure that all parameters are delcared as final. If I need a primitive (or wrapper or other immutable object like String) whose value will change, it is clearer to define a new local variable and assign it an initial value from the corresponding final parameter if needed.
  3. Avoid hidden fields. Don't have a parameter with the same name as an instance variable, because it is too easy to accidentally modify the wrong one. Using this on the instance variable will avoid these errors, but it is easy to forget.

In my opinion, these are very good rules to live by when coding and as I write new methods, I find myself automatically implementing these rules: they have become muscle memory for me.

However these rules have a cost when dealing with getters and setters. Let's say I create a new class, add ten fields, and in Eclipse select Source > Generate getters and setters > select all > OK and bam, I have ten corresponding accessor and mutator methods for my ten fields. Now I would have to edit twenty methods to correct the comments and edit ten methods to make parameters final and give them a different name. Here is an example of the getters and setters generated by Eclipse.

private String name;

private int id;

 * @return the name
public final String getName() {
   return name;

 * @param name the name to set
public final void setName(String name) { = name;

 * @return the id
public final int getId() {
   return id;

 * @param id the id to set
public final void setId(int id) { = id;

Eclipse generation cannot, as yet, make parameters final or give them different names, and the comments are very basic and only use the parameter name to make them different. It is less likely that following the Checkstyle rules will create as much value on getters and setters as on other methods. Getters and setters are very simple methods, usually one-liners. Because they are so simple that they can be auto-generated, we don't generally modify them after creation - so there is less need to give them the protection of checkstyle rules like final parameters and avoiding hidden fields. Thus, the cost of editing so many methods is rarely worth the value it gives.

Having said all that, there a couple of reasons why I still want to follow these rules, even on getters and setters if I can cut the cost significantly.

  • Comments are still valuable for fields/getters/setters that cannot be truly un-ambiguously named, which happens often enough no matter how much thought you can put into it. For the simple ones, simple comments are fine.
  • If my Eclipse Problems or Markers view is filled up with dozens of Checkstyle warnings from getters and setters, it is harder for me to spot the warnings I really should take care of. So I would prefer to keep my Problem/Markers view clean and automatically implement the Checkstyle rules if possible.

Generate Boilerplate

So here is how I have attempted to fix this issue and make it as painless as possible to generate getters and setters and implement the Checkstyle rules I want. At the same time, I will generate decent default toString(), equals() and hashCode() methods to copy and paste too.

As a pre-requisite, I have checked out my utils project from GitHub into Eclipse: so that I can use three classes in particular:

  • which has some utility functions that use reflection to inspect a class and do things I sometimes find useful such as
    • output a list of fields or getters and setters that are present in a class.
    • output sample equals(), hashCode() and toString() impelementations using the commons lang builders.
    • outputs sample getters and setters from fields defined in a class.
  • which is an annotation that stores a field's comment that OutputFieldList will read and output as javadoc comments for getter and setter fields. It's based on a solution Abhi wrote about in this StackOverflow answer to the question: How to get a JavaDoc of a method at run time?
  • which is just a place I copy the field declarations into so that I can use the FieldComments annotation in the same project as the other things in RobUtils.

I begin by writing out the fields in the actual class they belong to. I write out comments for each.

public class Foo {
   /** User friendly label for display. */
   private String name;

   /** Unique ID within datastore. */
   private int id;


I then copy these comments into, which is just a scratch pad that has the advantage of being in the same project as the @FieldComment annotation. Within, I do a couple of find and replace operations to change the javadoc comments to @FieldComment annotations.

  1. Replace /**  with @FieldComment(comment=".
  2. Replace */ with ").

So that now have fields with the @FieldComment annotation:

public class FieldCommentSampleClass {
   @FieldComment(comment="User friendly label for display. ")
   private String name;

   @FieldComment(comment="Unique ID within datastore. ")
   private int id;

I then run the main method in with these two calls. This will generate getters and setters, plus decent toString(), hashCode() and equals() methods for me.

public static void main(final String[] args) throws Exception {
   generateGetAndSetMethods(FieldCommentSampleClass.class, //
      OutputFields.NO_FIELDS, //
      OutputGetters.OUTPUT_GETTERS, //

I now have the following output that I copy and paste into my original class.

/** @return user friendly label for display.  */
public String getName() {
   return name;

/** @return unique ID within datastore.  */
public int getId() {
   return id;

/** @param theName user friendly label for display.  */
public void setName(final String theName) { = theName;

/** @param theId unique ID within datastore.  */
public void setId(final int theId) { = theId;

public boolean equals(final Object obj) {
   if (obj == this) {
      return true; // test for reference equality
   if (obj == null) {
      return false; // test for null
   if (obj instanceof FieldCommentSampleClass) {
      final FieldCommentSampleClass other = (FieldCommentSampleClass) obj;
      // @formatter:off
      return new org.apache.commons.lang.builder.EqualsBuilder()
      // @formatter:on
   } else {
      return false;

public int hashCode() {
   // @formatter:off
   return new org.apache.commons.lang.builder.HashCodeBuilder()
   // @formatter:on

public String toString() {
   // @formatter:off
   return new org.apache.commons.lang.builder.ToStringBuilder(this)
      .append("name", name)
      .append("id", id)
   // @formatter:on

Final notes

A few notes to keep in mind.

  • OutputFieldList needs JDK 8 because it uses the Stream API with lambdas.
  • The toString(), equals() and hashCode() methods generated use the fully qualified class names for the apache commons lang builders. I them use Source > Add Import (or control+shift+m) to replace the fully qualified class names with just the class names.
  • If the code I am generating boiler plate for relies on custom code (e.g. field types are not standard java types), I may have to modify my RobUtils project so that it can reference those types, which is easy: Project > Properties > Java Build Path > Projects tab > Add and select the projects I need to reference.
  • I am pragmatic about following these Checkstyle rules.
    • When editing code written by someone else that has Checkstyle warnings, should I fix them? Perhaps. If there are just a few and I have time, sure. If there are many then only fix the ones in the actual lines of code I need to edit.
    • I don't force my own practices on other developers in a team. If the team has a policy already in place around the use of tools like Checkstyle or Findbugs, that takes precedence. Otherwise I practice what makes sense to me. If other developers are interested, explain how I use the tooling to help me write good code, but don't proselytise.
    • As I mentioned above, there is a cost to taking on processes like Checkstyle rules. If the cost is too high (i.e. there is little benefit to be gained), or the rules doesn't make sense to you, don't do it. Checkstyle warnings can be suppressed for a class, method or code block and rules can be removed entirely from a project specific configuration if the rule is too burdensome.

Sunday, April 19, 2015

PHP iterating over Blogger posts from Atom Feed XML

Thanks to some decent PHP APIs, it is really easy to read XML from a URL and process the results.

Use case: I have written an expression of interest form that people can use to raise an application to adopt a rescued Chihuahua. Part of the form lists the dogs available - and this list comes from a Blogger feed. The blog is used as a list of all the Chihuahuas that have been rescued (one dog per post) and the ones that are currently available all have a specific label: Available now. The expression of interest form reads the Atom feed for this label and displays a list of the available now dogs on the form.

Retrieve XML from URL and access child elements

The function below reads an XML document from a URL (an Atom feed) and will return an array of the child elements that represent specific posts with a given label from the blog.

function retrieveAvailableNowPosts() {
   // Set URL to XML we want to read - Available now.
   // Load specified XML file or report failure
   $xml = simplexml_load_file($file);
   if (!$xml) {
      return false;
   // Load blog entries
   $posts =  $xml -> entry;
   if (sizeOf($posts) > 0) {
      return $posts;
   } else {
      return null;

Notes about this function.

  • $xml = simplexml_load_file($file);
      • Loading the contents of a URL and then parsing the XML it contains is done with simplexml_load_file. The return from this function is either an SimpleXMLElement object or a boolean false if there was an error reading XML from the file (URL in this case).
      • The parameter to simplexml_load_file can be a file or URL.
      • Blogger supports feeds either from RSS 2.0 or Atom 1.0, and you can switch between them simply with a different URL.
      • The URL I am using ( is for an Atom feed. The label is the part after the last forward slash after it has been URL encoded i.e. Available now is the label, which becomes Available%20now after URL encoding. This was easy in my case because I only had to swap the space with %20. If you have more complicated labels (or perhaps need to URL encode dynamic labels), you can use the PHP function urlencode to do this.
  • $posts = $xml -> entry;
    • $xml is the variable containing the XML read from simplexml_load_file.
    • The Atom feed XML returned from the URL has feed as the root element and it contains a variable number of entry elements, each of which is a blog post that was made against the target label. The skeleton is shown below.
      <feed ...>
    • We access the array of the entry elements on the feed using the "arrow" operator (T_OBJECT_OPERATOR for objects).
    • $posts should therefore be an array which might be empty.
  • if (sizeOf($posts) > 0)
    • If the array has 1 or more elements, return it.
    • Otherwise, return null.

Iterate through XML elements

The function below accepts the XML elements read from the earlier function and iterates through them to output HTML.

function createDogList($posts) {
   $list = '<ul class="availableNowList">';
   // Check if posts is undefined, null, false or empty.
   if (!$posts || sizeOf($posts) == 0) {
      $list .= '<li>Unfortunately there are no dogs available at this time.</li>';
   } else {
      // Go over each entry.
      foreach($posts as $post) {
         // Publish time
         $dateTime = date("l jS F, Y", strtotime(strtok($post->published, 'T')));
         // Link.
         $link = $post->link[4]["href"];
         // Title.
         $title = $post->title;
         // List the entry.
         $list .=
               <a href="' . $link . '" target="_blank">'
                     . $title . '</a> <em><small>(published ' . $dateTime . ')</small></em>.
   $list .= '</ul>';
   return $list;

Notes about this function.

  • This function builds up an un-ordered list (ul) of blog posts, with each list item having
    • A link to the blog post - the link text being the blog entry title.
    • The date on which the post was published. Example list:
      • RUDI (published Sunday 29th March, 2015).
      • MIMI (published Sunday 29th March, 2015).
      • BAXTER (published Sunday 29th March, 2015).
  • If there are no posts against this label, output only one list item with an explanation that there are no matches at this time.
  • The parameter to this function is a list of blog posts against a certain label, retrieved by the previous function: retrieveAvailableNowPosts().
  • if (!$posts || sizeOf($posts) == 0) { .. }
    • This is more than just a null-check: !$posts will be true if the variable $posts is null, not set (undefined, has no value) or false.
    • Here is a quick overview, showing that an IF is a good test for all three things.
         echo '<pre>';
         $foo1;        if($foo1) { ?>foo1 is set and not null/false.<?php } else { ?>foo1 is not set/null/false.<br><?php }
         $foo2=null;   if($foo2) { ?>foo2 is set and not null/false.<?php } else { ?>foo2 is not set/null/false.<br><?php }
         $foo3=false;  if($foo3) { ?>foo3 is set and not null/false.<?php } else { ?>foo3 is not set/null/false.<br><?php }
         echo 'foo1 - ';
         echo 'foo2 - ';
         echo 'foo3 - ';
         echo '</pre>';
      The output of the above is:
      foo1 is not set/null/false.
      foo2 is not set/null/false.
      foo3 is not set/null/false.
      foo1 - bool(false)
      foo2 - bool(false)
      foo3 - bool(true)
    • This allows us to respond to two sad cases from retrieveAvailableNowPosts() at once.
      • retrieveAvailableNowPosts() returns false if it couldn't read the Atom feed XML from the blog.
      • retrieveAvailableNowPosts() returns null if the list of posts is empty.
      • The call to sizeof (sizeOf($posts) == 0) is actually not needed, because retrieveAvailableNowPosts() returns null if the list of posts is empty, but I left it here in case I ever call this function from a different place and neglect to included the same rule.
  • $dateTime = date("l jS F, Y", strtotime(strtok($post->published, 'T')));
    • $post->published
      • The value of this looks like 2015-03-29T03:10:00.003-07:00.
      • I only want the year, month and date only: I want to discard all the time information and just output the date. I will use a string tokenizer to do this in the next step.
    • strtok($post->published, 'T')
      • I use a string tokenizer with the letter "T" as the delimiter. Note that the first call strtok will return the first token, and since I only need the first token, I don't store reference to the tokenizer. Here is how you would use the tokenizer in another situation to go over all tokens:
        $string = "String to split";
        delimiter = " \n\t";  // Split string on spaces, newlines and tabs.
        $token = strtok($string, $delimiter);
        while ($token !== false) {
            echo "Next token: $token <br />";
            $token = strtok($delimiter);
      • The result of strtok($post->published, 'T') will be something like 2015-03-29 (note that it does not include the delimiter itself).
    • date("l jS F, Y", strtotime(strtok($post->published, 'T')))
      • I use the date function to parse the date (from text like 2015-03-29) and output it in a different format (like Sunday 29th March, 2015).
      • See the PHP page for date function to find the full list of date format options, but here is what my format uses.
        • l - A full textual representation of the day of the week: Sunday through Saturday.
        • j - Day of the month without leading zeros: 1 to 31.
        • S - English ordinal suffix for the day of the month, 2 characters: st, nd, rd or th.
        • F - A full textual representation of a month, such as January or March: January through December.
        • Y - A full numeric representation of a year, 4 digits: 1999 or 2003.
  • $link = $post->link[4][href]
    • In a given entry element, get the href attribute of the fifth link element (using a zero based index).
    • The fifth link element holds a direct URL to the post, such as: <link rel="alternate" type="text/html" href="" title="MIMI"/>.
  • Google's description of what is in each post element shows you what things you can access this way for each post:
    • posts: A list of all posts for this page. Each post contains the following:
      • dateHeader: The date of this post, only present if this is the first post in the list that was posted on this day.
      • id: The numeric post ID.
      • title: The post's title.
      • body: The content of the post.
      • author: The display name of the post author.
      • url: The permalink of this post.
      • timestamp: The post's timestamp. Unlike dateHeader, this exists for every post.
      • labels: The list of the post's labels. Each label contains the following:
        • name: The label text.
        • url: The URL of the page that lists all posts in this blog with this label.
        • isLast: True or false. Whether this label is the last one in the list (useful for placing commas).

Error handling

The primary error condition is from the call to simplexml_load_file, which returns false if there was a failure reading XML from the URL. The secondary error condition occurs if we read the XML okay, but found it contained none of the elements we are interested in. On the page that uses these functions, both error conditions are treated as normal outputs from the retrieveAvailableNowPosts function and dealt with nicely, as you can see below. We output error messages if either error occurs, and display the "normal" content of the page otherwise.

$posts = retrieveAvailableNowPosts();
// If a boolean false is returned there was an error.
if ($posts === false) {
   <p style="text-align: center; color: red;">Unable to load list of Available Now dogs from Chihuahua Rescue Victoria!</p>
// And null means there is nothing present.
} else if ($posts === null) {
   <p style="text-align: center;">Unfortunately there are no dogs available at this time. Please try again later.</p>
// Otherwise, all good. Carry on.
} else {
 ... normal page content goes here.
} // end else

Just die!

We could have handled the error from simplexml_load_file in this way:

$xml = simplexml_load_file($file) or die("<p>An error message.</p>");

This offers a very poor user experience, especially on a web page because it will cause PHP to immediately exit and no further code on the page will be processed. This will most likely result in an ugly page with broken HTML.

Resources that helped me.