Most Recent Blog

King's View of the Battle

 Open GL model of a chess board.

October 21, 2012

Mountain Lion Dock Freeze.

This is quite frustrating, the Mountain Lion Dock Freezes. WAY to often. What do you do when this happens?

SOME OPTIONS:
  1.  HARD REBOOT ( not fun...)
  2. killall Dock (reqires access to a terminal)
Because this happens often, a habit has formed to make sure to keep a terminal is always open.  Then just switch via the apple in the upper left, and invoke

PROMPT $ killall Dock

 
Shortest blog post I ever wrote... Just trying to get the word out...

August 29, 2012

Unique Static Content and Eclipse Non-Consume Regex Glitch

I discovered a technique to allow infinite cache expire time for images and static content on websites. This technique is highly desirable because image requests and 304 requests consume time and make the user experience poor.  The technique is to include a version as follows:
 



-
 

In Fact, in the above example you could be even more sophisticated and include the MD5-checksum or other very unique identifier.
So I cracked open Eclipse to make the REGEX search and replace.  The expression I came up with was:
#Search RegEx
# This looks for .jpg, and then does a 
# NON-Consuming match on a word boundary where there is NOT a ?
# For example .jpg" and .jpg' would match, but .jpg?version2 will not
\.jpg(?\b)(?[^\?])
#Replacement
\.jpg?STATIC_CONTENT_VERSION

I proceeded to do the simple search and replace.

To my chagrin, I received the infamous Error:
, "Match string has changed in the file" 


Why is this?  Well I googled it.  It is a know bug with Eclipse and regexes...
Eclipse Bug.

Workaround:  well we could use Netbeans, but I choose to go more primitive and use perl.  This post will include the generic perl script developed to solve the simple issue and how it was used:

To use, simple type perl findreplace.pl regexSearch regexReplace [0|1]
The 0 is a dryrun, the 1 will really do the replace.

use File::Find;
use File::Basename;

# The inputs:
# directory to start in, findRegex, replacement, dryrun indicator
@DIRS = $ARGV[0];
$patternFind = $ARGV[1];
$patternReplace = $ARGV[2];
$dryrun = $ARGV[3];

#  process a single file (this sub is called from below)
sub processFile{

    $baseN="";
    $dirN="";
    $extN="";
    $linesProcessed = 0;
    $currentFile = $File::Find::name;
    ($baseN,$dirN,$extN) = fileparse($currentFile,'\..*');
    if ($extN eq ".java" || 
        $extN eq ".jsp"  ||
        $extN eq ".properties" ||
        $extN eq ".js" ||
        $extN eq ".css" ){
        # This represents a file that we want to do a search and replace on.
        # print "found java $currentFile :$extN:$baseN:$dirN:\n";
    }else{
        # print "\nRETURNING: $currentFile\n\n";
        return;
    }

    $currentBaseName = $_;

    # OPEN THE FILE READ ONLY.
    open FILE, $currentBaseName;
    $modify= 0;
    # DETERMINE IF THE FILE CONTAINS A MATCH
    while($line =){
        if($line =~ /$patternFind/){
            $modify++
        }
    }
    close FILE;
    if($modify==0){
        # THERE WAS NO REGEX MATCH, SO EXIT.
        return;
    }

    # NO IT's not the evil number, 
    # it means set to global read/write in unix.
    # required for TFS integration.
    chmod 0666, $currentBaseName;

    open (FILE, "+< $currentBaseName")  or die "can't read $currentFile $!";
    $out = '';
    while(){
        if($_ =~ /$patternFind/){
            $linesProcessed++;
        print "\tReplace: $_\n";
            s/$patternFind/$patternReplace/eg;
        print "\tWith: $_\n";
        }

        $out .=$_;
    }

    if($dryrun==1){

        seek(FILE,0,0) or die "can't seek to start of $currentFile $!";
        print FILE $out or die "cant't print $currentFile $!";
        truncate(FILE, tell(FILE))  or die "can't truncate $currentFile: $!";
        close (FILE)         or die "can't close $currentFile: $!";

    }

    print "File:$baseN$extN :From ->:$dirN\n";
    print "Lines Changed: $linesProcessed\n";
    print "___________________________________________________\n";
}
find (\&processFile, @DIRS);

hope you find this useful!

June 1, 2012

Eclipse/NetBeans Multi Line Search and Replace Across Files

We all know System.out.println statements are not good for performance and clutter logs.  We should use a logger instead.  Loggers allow for the control of logging levels.  How do you replace these System.outs in a large project?  Here are the steps used recently:
  1. Come up with a good REGular EXspresion (REGEX), for matching your desired code.
  2. Come up with your replacement regex and capture user.
  3. Ensure that your code compiles and inspect your changes.
In this case we want to replace the following:
package com.ecokrypt...

public class ToManySystemOuts {

    System.out.println("Single Line System.out.println");


    System.out.println("Rare multiline " +
           " System.out.println ");

}
We need to find a REGEX that will match both of these cases.  There are several ways to do any REGEX, but a solution that is easy  to understand that will work is:

   (?s)System.out.println(\(([^;]+?)\));

We have to dissect what this is saying to understand a couple of subtleties:
  • (?s) - indicates that we search past line feeds.  Without this we will not match the second case because the line feed would cause the REGEX to not match at all.
  • System.out.println - matches the exact phrase
  • (\(([^;]+?)\)) - is very complicated, and will take time before you can read this.  A technique that typically works is to read it inside out.  Trying that out:
    • [^;]+ - look for anything that is NOT a ';' (semicolon).  the + is saying that you have to match at least 1, and there is no limit to how many characters you can match.
    • ([^;]+?) - Has a real subtlety to it, Regexes have an ability to be greedy, or lazy.  A greedy regex will match the largest region it can.  A lazy regex will match the smallest area it can.  It will take time to figure out when to use which, but the basic advice given here is ALWAYS try to use lazy.  Only use greedy if you really need it.  This REGEX is obviously lazy, how can you tell?  Think about it a bit.
      • the ? indicates that you are going to look for the next sequence imediately after.  
      • the (...) indicates you are going to "capture" whatever you find presumably for later use.
      • therefore the ( ) are not matched but are just part of the regex capture syntax.
      • So the answer is: if the ? is inside the capture group e.g. (...?) the matching is lazy; if the ? is outside of the capture group, the matching is greedy e.g. (...)?
    • \(([^;]+?)\)  - says, adds the \(...\) to the previous the \ is an escape character and says match a ( and a ), don't create another capture group.
    • (\(...\)) - says, now add another capture group containing everything inside the literals ( ).
  • ; the semicolon at the end says match a literal semicolon.
That is pretty much all there is to it.  Look at the following REGEX, which might be a common mistake and see if you can figure out why it won't work.  Guess what it will match.  (HINT thing about greedy vs lazy).

   (?s)System.out.println(\(([^;]+)?\));

Now that we have our matching REGEX, we need to get our replacement REGEX:

   System.out.println$1;

This is much simpler, it says:  replace the above match, with logger.debug$1; where $1 is inclusive the literal (...).  e.g. the braces and everything inside of them.

How do we do this inside of Eclipse or NetBeans?
I find NetBeans to be much nicer in some respects, and not so nice in others.


NetBeans in File Search Replace
This is the netbeans view, it uses the space very nicely and there is NO POPUPS...  You really have to like the ergonomics of NetBeans.  The search is dynamic and nicely highlighted so you can see the search as you go.
NetBeans Replace Across All Files
USE CRTL-H for a shortcut to this Menu.

NetBeans Confirmation Dialog

Eclipse:

The Eclipse Search Dialog.  (Allows you to select scope)





January 6, 2012

Creating A Java Applet to Run in Chrome, Firefox, IE, Opera, and Safari

Just throwing together a really quick page to allow people to grab a code snipit
showing how to write html to allow an applet to be embedded in a page, such that it works in Chrome, Firefox, IE, Opera and Safari.

The following should work.
I would love comments on better ideas.