Scruffy is outputting partial or blank images

October 1st, 2009

Scruffy gem that uses rmagick has a bug that caused me a lot of troubles since I am new to Ruby programming language. I am using windows environment.
Anyway, I was trying an example from Ruby in Practice by Jeremy McAnally and Assaf Arkin when I encountered a strange bug with Scruffy (now I know that it is its bug but it took me a while :).
This bug causes that any graph you try to render, renders only partially, similarly to the picture below:

The problem (with solution) is described here: http://rubyforge.org/tracker/index.php?func=detail&aid=27102&group_id=2030&atid=7929.

The cause of this problem is incorrect generation of viewbox by the base renderer. When you incorporate changes mentioned in the link above to RUBY_INSTALL\lib\ruby\gems\1.8\gems\scruffy-0.2.6\lib\scruffy\renderers\base.rb you should be able to render correct graphs.

Share/Save/Bookmark

ruby , ,

Debugging GUI events

September 22nd, 2009

Debugging gui events is usually not possible with regular line per line debugging and is done mostly by tracing.
For debugging code that relies on Java swing events like drag and drop, mouse moving, etc., having something similar to code below is very useful:

 
public boolean isMouseAboveHeaderPanel() {
System.out.println("HeaderPanel.isMouseAboveHeaderPanel() called from: "
+ Thread.currentThread().getStackTrace()[2].getClassName() + "."
+ Thread.currentThread().getStackTrace()[2].getMethodName());
 
// ...
 
}

In this way you trace not only current function but also you got information from where you entered that function. Of course to make things easier you should make some kind of a shortcut for entering this trace line. In Eclipse you can do that with code template:

System.out.println("${enclosing_type}.${enclosing_method}() called from: "
+ Thread.currentThread().getStackTrace()[2].getClassName() + "." 
+ Thread.currentThread().getStackTrace()[2].getMethodName());

This helped me in lot of situations, I hope that it will help you as well.

Share/Save/Bookmark

General, Java , ,

Groovy blob and mysql

September 20th, 2009

In certain occasions e.g. when you want to save scrapped html page or just a specific part of it (e.g. div with its contents) the recommendation I found is to use the blob type and save xml document as binary stream.

In groovy, this is achieved in a rather simple way:

class Document {
  int id
  String title
  String xmlContent
}
 
Sql sql = Sql.newInstance(
          "jdbc:mysql://localhost:3306/documents_db?useUnicode=true&characterEncoding=UTF-8&useBlobToStoreUTF8OutsideBMP=true",
          "user", "pass", "com.mysql.jdbc.Driver")
 
// We assume db table similar to:
sql.execute("""
    CREATE TABLE IF NOT EXISTS `document` (
      `id` bigint(32) NOT NULL AUTO_INCREMENT,
      `title` varchar(500) NOT NULL,
      `xmlContent` blob,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    """)
 
def saveDocuments(def docs) {
  println "Saving documents to database..."
  def documents = sql.dataSet("document")
  docs.each {Document doc ->
    documents.add(title: doc.title, xmlContent: doc.xmlContent)
  }
}

When you want to retrieve data, with Groovy this is not much complicated either:

def loadDocuments() {
  def list = []
  sql.eachRow("SELECT * FROM document") {
    Document doc = new Document(it.toRowResult())
    if (it['xmlContent'] != null) {
      doc.xmlContent = new String(it['xmlContent'])
    }
    list.add(doc)
  }
  println "Loaded ${list.size()} documents from database"
  list
}

I tried the code with mysql database but I assume that you can achieve the same with any other database.

One hint for mysql blobs and utf-8: although utf-8 charset behaves very well with other database table fields (if you add useUnicode=true and characterEncoding=UTF-8 parameters to your connection string), in order to work with utf-8  blob fields you should adjust one more parameter: useBlobToStoreUTF8OutsideBMP=true. With this parameter set, your utf8 encoded xml documents will behave well with mysql database.

Share/Save/Bookmark

Java, groovy , , ,

Groovy meliorates and dries

June 12th, 2009


Dynamic languages are very popular at this time and you probably had insight into at least one of them (Ruby, Python, Groovy).

If you are bound to Java platform the best way to “be dynamic” IMHO is to work with Groovy.

Apart from the http://groovy.codehaus.org/ where you can find documentation, getting started examples etc. there are many blogs around that blog about groovy.

My favorite (although very slow) is Andres Almiray’s blog. He mostly blogs about griffon and groovy and has nice tutorials and examples. There I found info about many open source libraries I never heard of but which are quite useful, for example: FEST, Glazed list, Swing clarity, etc.

Try Groovy cause it’s groovy :)

I felt like when I run my first program at elementary school and it wrote “Ninja” on the screen :)

Share/Save/Bookmark

Java, groovy ,

Why would you spend time on warez sites?

March 3rd, 2009

Even if not being a “regular” warez user it would be good if you stay informed about what is happening inside warez community.
One reason would be finding really popular and valuable programs by browsing warez sites. This is better than searching through regular download sites because programs found on these kind of sites are often more relevant to the field in which they belong. One reason could be this - If somebody put an effort to crack a piece of software then that software has some value for sure :).

I use: http://www.area51warez.info/ and http://softarchive.net/.

Share/Save/Bookmark

General, Links , , ,

HtmlUnit as Java Screen Scraping Library

January 23rd, 2009

If you are needing behavior ‘as though a real browser was scraping and using the page’ HtmlUnit is definitely the best option available. It was designed for testing websites but works great for screen scraping and navigating through multiple pages. It takes care of cookies and other session-related stuff and can execute (if you want it to) the Javascript in the page.

I’ve personally tried several other tools from this list (Jericho, Web Harvest) but neither of them is as good as this library. For example, writing a screen scraper with Web Harvest is an easy task, but badly formatted pages cause xml parser to break and this happened to me a lot of times. Jericho is ok but it took me much more coding to achieve the same as with HtmlUnit.

Take a look at HtmlUnit getting started and start scraping in no time.

Share/Save/Bookmark

Java , , , ,

‘Awesome’ administrator

January 20th, 2009

I am not sure if this clip is fake or not but it’s hilarious for sure. This guy is the administrator you don’t want to employ at your company (maybe you can recommend him to your competitors :).

Watch it from here:

Share/Save/Bookmark

fun , ,

Weblog for web-developers and designers

December 26th, 2008

Smashing magazine is a weblog dedicated to web-developers and designers. Its great because of its overview of interesting stuff for each month. Their published content is a must read if you want to become a good web developer or a designer that follows web design trends.

Share/Save/Bookmark

General, Links , , , , ,

Swing blog recommendation

December 9th, 2008

Pushing-pixels by Kirill Grouchnikov is a dynamic blog on GUIs with a special attention to swing. It has weekly digest on popular swing links and is good place to start when searching for news in that field.

Share/Save/Bookmark

Java, Links , , ,

Do you use launchy?

November 10th, 2008

Launchy is a handy tool that can help you when you have messy folders and want to quickly find and open application, document or play the mp3 song. With it you can forgot about desktop, quicklaunch or start menu for starting applications. Iit’s free and makes your desktop life a lot easier. Try it!

Share/Save/Bookmark

General , ,