Archive for the 'Coding' Category

Recursively Remove .svn Folders

I was moving my svn projects to git repository. I needed to get rid of all the .svn folders recursively. Following is the script that I used:

Popularity: 1% [?]

Groovy : Parse All Soccer Players Info

I am new to groovy and am still getting used to the scripting way of thing coming from Java. So as a learning exercise I wrote up the following lines to parse information of all the soccer players from ESPN Soccernet. I have used the Jsoup library to get the document and parse it.

def leagues = [
        "http://soccernet.espn.go.com/clubs/_/league/eng.1/english-premier-league?cc=4716",
        "http://soccernet.espn.go.com/clubs/_/league/esp.1/spanish-la-liga?cc=4716",
        "http://soccernet.espn.go.com/clubs/_/league/ita.1/italian-serie-a?cc=4716",
        "http://soccernet.espn.go.com/clubs/_/league/ger.1/german-bundesliga?cc=4716",
        "http://soccernet.espn.go.com/clubs/_/league/fra.1/french-ligue-1?cc=4716",
]

leagues.each {leagueUrl ->
    Utils.getDocument(leagueUrl).select("table[class=tablehead]").get(0).select("td:eq(2)").select("a[href]").each {teamStatsUrl ->
        Utils.getDocument(teamStatsUrl.attr("abs:href")).select("tbody").each {playerGroup ->
            playerGroup.select("td:eq(1)").select("a[href]").each {playerLink ->
                Element playerProfile = Utils.getDocument(playerLink.attr("abs:href")).select("div.profile").get(0)
                String playerName = playerProfile.select("h1").text()

                def profilePrperties = [:]
                playerProfile.select("li").each {item ->
                    String[] itemProperties = item.text().split(":")
                    if(itemProperties.size() == 1) profilePrperties.get("teams", []).add(itemProperties[0])
                    else profilePrperties[itemProperties[0]] = itemProperties[1]
                }
                println playerName + " " + profilePrperties
            }
        }
    }
}

All that Utils.getDocument(url) here does is to call Jsup.connect(url).get() within a loop with number of retries set to 5. The script produces output as follows:

Ramires [Full Name: Ramires, Squad No: 7, Position: Midfielder, Age: 24, Birth Date: Mar 24, 1987, Birth Place: Barra do PiraĆ­, Rio de Janeiro, Brazil, Height: 5' 11'' (1.80m), Weight: 73 kg, teams:[Brazil, Chelsea]]
Frank Lampard [Squad No: 8, Position: Midfielder, Age: 33, Birth Date: Jun 21, 1978, Birth Place: Romford, Height: 6' 0" (1.83m), Weight: 174 lbs (78.7 kg), teams:[England, Chelsea]]
Fernando Torres [Full Name: Fernando Torres, Squad No: 9, Position: Forward, Age: 27, Birth Date: Mar 20, 1984, Birth Place: Fuenlabrada, Madrid, Height: 6' 1'' (1.85m), Weight: 174 lbs (78.7 kg), teams:[Spain, Chelsea]]
John Mikel Obi [Squad No: 12, Position: Midfielder, Age: 24, Birth Date: Apr 22, 1987, Birth Place: Jos, Nigeria, Height: 5' 11'' (1.80m), Weight: 179 lbs (81.3 kg), teams:[Chelsea]]
Raul Meireles [Full Name: Raul Meireles, Squad No: 16, Position: Midfielder, Age: 28, Birth Date: Mar 17, 1983, Birth Place: Porto, Portugal, Height: 1.79m, Weight: 65 kg, teams:[Chelsea, Liverpool, Portugal]]
Branislav Ivanovic [Squad No: 2, Position: Defender, Age: 27, Birth Date: Feb 22, 1984, Birth Place: Sremska Mitrovica, Yugoslavia, Height: 6' 2" (1.88m), Weight: 86 kg, teams:[Serbia, Chelsea]]
Juan Mata [Full Name: Juan Mata, Squad No: 10, Position: Forward, Age: 23, Birth Date: Apr 28, 1988, Birth Place: Burgos, Spain, Height: 1.70m, Weight: 61 kg, teams:[Spain, Valencia, Chelsea, Spain U21]]

Popularity: 2% [?]

Java ArrayList Is Actually Just An Array !

I was reading The Art of Computer Programming Vol 1, the chapter about arrays and list. Knuth describes the differences and mentions how it’s very easy to add a new element to a list in a constant time if we have the node where the insertion needs to happen. Same for deletion. That is when I realized I did not know how the ArrayList implementation handles it. I did a code walk through of that class – something Prasun had advised me long ago but I always ignored.

I always knew that ArrayList used array for implementation, which is how it can guarantee the O(1) element access time. But I did not know how the other methods worked. For instance, one need not provide any initial size of the ArrayList. How does Java handle it? I remember Prasun telling me that the initial size in such cases is 10. But then what happens in the case of an overflow? What is the new space that gets assigned to the list?

But I was more interested in the add(index, element) and the remove(index) methods. And they are not constant time! They are O(n) operations. Say we want to remove(6). Java actually copies over all the elements from 7..n to indexes 6..n-1 and then sets the nth element as null. This is so not what we learn as deletion in list where it was just a matter of changing a couple of pointers (the next and prev ones).

Popularity: 2% [?]

Java : PropertiesConfiguration Escapes Characters on Save :(

Damn! I am using the commons-configuration from apache commons library. I have a properties file which has properties like:

widgetcentral_us=http://widgets.amazon.com

widget_source_widgets_available=”carousel”

When I open this file, change a property and save the file, unfortunately, the characters get escaped and the file ends up like:

widgetcentral_us = http:\/\/widgets.amazon.com

widget_source_widgets_available = \”carousel\”:\”mp3\”

I am trying to figure if I can prevent this behavior.

Popularity: 4% [?]

Groovy : Invoking One Script From Another

It was like one of those Aha! moments when I finally found how to invoke one script from within another. It is so simple!!

Say you have a script RunScript.groovy which is as follows:

        package com.singhanuvrat.sandbox

        println "Whoa!! ${input}"

Now you want this RunScript.groovy to be called from CallerScript.groovy with different values for the variable input. Our script will be:

        package com.singhanuvrat.sandbox

        GroovyScriptEngine gse = new GroovyScriptEngine("src/com/singhanuvrat/sandbox");
        Binding binding = new Binding();
        binding.setVariable("input", "world");
        gse.run("RunScript.groovy", binding);

Infact, using the binding, we can also extract value of variables from the called script. Now we shall have fun! :)

Popularity: 3% [?]

Puzzle: Complete the C Code Block

A very simple and trivial question:

I am giving a C code block that you need to complete. Here it is:

if(______) {
  printf(" Fair ");
}
else {
  printf(" Isaac ");
}

The question is this:

Propose a condition for the if statement so that the output of the block is Fair Isaac.

Popularity: 6% [?]

Java Code: Pair Class

A pair class. Nothing special. And of course, bits and pieces taken from other people’s code.

Popularity: 4% [?]

Parsing XML With Castor XML

After lot of trying I finally managed to get Castor tools working to parse XML files. And now that I have it working for me, I am always going to use it for XML parsing. It makes things so much simpler and easier.

Castor takes in a xml file and unmarshals it into Java objects. There are three ways to associate Java Classes with XML elements.

  • The first one is introspection. Given the class to the Unmarshelar, Castor populates the instance fields from XML.
  • The second is to use bindings defined by user.
  • The third is to use the XML Code Generator tool and have it generate Java Classes.

Of course, I used the third option, and that is the one I am going to mention here.

Castor jars can be downloaded from the Castor Project.It has a few dependencies. So I decided top use Maven. And I am already using Eclipse.

I imported the castor-code-generation jar. Then created a pom.xml file. I added a plugin to it:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>castor-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <schema>config.xsd</schema>
          <packaging>pba.plgen.xml.binding</packaging>
          <properties>generation.properties</properties>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Where the properties file is:

# Specifies whether the sources generated should be source compatible with
# Java 1.4 or Java 5.0. Legal values are "1.4" and "5.0".  When "5.0" is
# selected, generated source will use Java 5 features such as generics and
# annotations.
# Defaults to "5.0".
#
org.exolab.castor.builder.javaVersion=5.0

# Set to true if you want to have an equals() and
# hashCode() method generated for each generated class;
# false by default
org.exolab.castor.builder.equalsmethod=true

# Specifies whether automatic class name conflict resolution
# should be used or not; defaults to false.
org.exolab.castor.builder.automaticConflictResolution=true

# Property specifying whether extra members/methods for extracting XML schema
# documentation should be made available; defaults to false
org.exolab.castor.builder.extraDocumentationMethods=false

Right Click -> Run As -> Maven generate-sources

And all the source code is generated.

Suppose that the root element is plgen. Then a class Plgen is generated. All the child nodes of plgen become instance variables of Plgen. To unmarshal the xml file,

        public void parse() {
		try {
			m_plgen = Plgen.unmarshal(new FileReader(m_configFilePath));
		} catch (MarshalException e) {
			e.printStackTrace();
		} catch (ValidationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

Well, I haven’t written this properly. But, its enough to make me remember how to get castor working again. My mission accomplished.

Popularity: 9% [?]

I Hate XMLs

XMLs have never been my favourite. Attributes, Elements, Values, Child nodes … aargh, they are so confusing. And yet, configuring an application using an xml file seems so simple.

I have written my own XML parsers using the DOM. The whole document gets mapped to a tree structure which you can iterate over and get any value you want. But I find it to be very restrictive, and most of my code depends on the structure of my xml file. I would like to make my xml parser independent of the xml file structure.

I came across Castor XML. It has tools to read the schema and generate Java Classes out of it. Then the xml file is parsed and objects are created. Now this is good. I think this should be much easier than iterating a tree. But !! I have spent a lot of time trying to get Castor working. Its dependencies !!! Damn.

Time to get back to xml parsing again.

Popularity: 4% [?]

Varargs

Just like you have variadic functions in C, Java has the Varagrs :) . Its usage is as simple as this :

public List<string> getModifiers(String... arg) {
  // arg is String[]
}

And the wonderful thing is that the arg is a String[].

Popularity: 2% [?]