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% [?]
Nitwit, Blubber, Oddment, Tweak !!
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% [?]
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% [?]
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% [?]
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% [?]
A pair class. Nothing special. And of course, bits and pieces taken from other people’s code.
Popularity: 4% [?]
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.
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% [?]
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% [?]