Rough Book

random musings of just another computer nerd

Indenting XML and HTML from vim

vim has an awesome feature, using which you can pipe a range through an external command. This is pretty useful if you’re opening up an un-indented or poorly-indented XML or HTML file. If you want to indent your entire file, simply do the following:

:%!tidy -i -xml -q

The -i option tells tidy that it needs to indent the content, -xml tells tidy that the content is well-formed XML, and -q puts tidy into “quiet mode” where extraneous information is suppressed. You can also specify ranges like so:

:40, 74!tidy -i -xml -q

This indents content between lines 40 and 74 (both lines inclusive). You can also do:

:., .+50!tidy -i -xml -q

This indents the current line and the next 50 lines. You can also do the same for HTML:

:%!tidy -i -xml -q

You can of course, supply additional parameters to tidy to customize the indenting.

:%!tidy -i -q

Popularity: 1% [?]

January 17, 2012 Posted by | Computers, Programming and Development | , , , , | Leave a Comment

The Problem With Patents (Infographic)

patents infographic

Source: http://frugaldad.com

Popularity: 1% [?]

January 11, 2012 Posted by | Patents, Politics and Law | , , | Leave a Comment

A few pictures from my vacation to Oman

Here are a few pictures from my vacation to Oman. I only wish I had longer that two weeks! I traveled with my wife, my best friend Michael, as well as my sister and her husband. We visited Nizwa, Muttrah, the Grand Mosque, as well as friends and family. The following pictures were taken with my Nikon D3000. I’ve made minor edits like straightening, or converting images to black and white. Hope you enjoy the pictures!

Popularity: 1% [?]

January 10, 2012 Posted by | Photography | , , , , , , , , , | Leave a Comment

Implementing pinch-zoom and pan/drag in an Android view on the canvas

I was trying to get pinch-zoom and panning working on an Android view today. Basically I was trying to implement the same behavior you see when you use Google Maps (for example). You can zoom in and pan around until the edge of the image, but no further. Also, if the image is fully zoomed out, you can’t pan the image. Implementing the pinch-zoom functionality was pretty easy. I found an example on StackOverflow. I then wanted to implement panning (or dragging) as well. However, I wasn’t able to easily find examples and tutorials for this functionality. I started with this example that comes from the third edition of the Hello, Android! book but I didn’t get too far. So I started playing around a little bit with the events and started writing some code from scratch (using the example from Hello, Android!) so that I could have a better idea of what was happening.

As I mentioned before, getting zoom to work was pretty easy. Implementing panning/dragging was the hard part. The major issues I encountered and subsequently fixed were the following:

  1. Panning continues indefinitely in all directions.
  2. When you zoom and then pan, stop, and then start again, the view jerks to a new position instead of panning from the existing position.
  3. Excessive panning towards the left and top can be constrained, but panning towards the right and bottom is not so easily constrained.

Once I fixed all the problems, I figured that it would be nice to document it for future reference, and I also think it would be a useful resource for others who have the same problem. Now a little disclaimer before I go any further: I’m not an Android expert and I’m really not that great with graphics; I just started it learning to program for Android this semester for one of my Masters electives. So there might be a better way of doing all this, and if there is, please let me know! Also, if you want to skip all the explanations and just see the code, you can skip to the last page.

Read more »

Popularity: 7% [?]

December 4, 2011 Posted by | Android, Java, Operating Systems, Programming and Development, Software | , , , , , , , , , , , | 4 Comments

Akṣi: Handwritten-digit-recognizing neural-network

I’ve always been interested in Neural Networks (ever since I first found out about them around 10 years ago). However, I never got a chance to learn about them or write one of my own; this is something I’ve wanted to do for some time. I got the opportunity this semester when my professor in my advanced data-structures class told us that we could pick any topic we liked, for a semester project. I thought that this would be the perfect time for me to learn about neural networks and create one of my own.

The end result was Akṣi, a neural network that recognizes hand-written digits. I’ve hosted it using Google’s App Engine. Please check it out!

Popularity: 1% [?]

November 24, 2011 Posted by | Java, Programming and Development, Science | , , , | Leave a Comment

Rare pictures of Trivandrum

Some of these pictures are from an album titled “Album of South Indian Views”. The pictures were taken by the Government photographer Zacharias D’Cruz. For some of these pictures, the photographer is unknown. Most of these pictures were taken in the late 1800′s and 1900′s. I originally got these pictures from Manu Prasad Revindran’s Facebook album.

Popularity: 2% [?]

November 11, 2011 Posted by | History, Photography | , , , , , , , , , , | Leave a Comment

Setting the content type to text/plain for a JSON response from a Spring controller

I was using a jQuery plugin called a ajaxfileupload to upload a file through AJAX. Technically what the plugin does isn’t AJAX. It creates a hidden form and an iframe, and then submits the form using the iframe as the target. The iframe will then end up with the response from the server. This response is then read-in by the plugin and handled appropriately. In my case I was using a controller action that would return JSON (using the .action extension). The action uses Spring’s MappingJacksonJSONView that returns JSON with a content type of application/json (as it should). This works perfectly in Chrome, however in both Firefox and IE, the user is presented with a dialog box that asks them to download the JSON response. This is obviously not what I wanted. The reason this is happening is because the response is being directly submitted to the iframe (and therefore, the page). That is, it’s not coming through via the XMLHttpRequest object. So IE and FF don’t know what to do with it and assume that it is something the user would want to download. The solution to this problem is to set the content-type to text/plain. This wasn’t as straightforward as I thought it would be.

Initially I was going to call the render(…) method of MappingJacksonJsonView but that didn’t work because the content-type had already been set to application/json. The solution I came up with was to duplicate some of the code (ugh) inside MappingJacksonJsonView to get the JSON as a string and to then write that to the response:


@RequestMapping
public void processFileUpload(HttpServletResponse response, Model model, ...) {

    ...

    //Set the content-type and charset of the response
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    //I need to use another OutputStream here; I cannot use the response's OutputStream because that will cause errors
    //later on when the JSP needs to render its content (recall that getOutputStream() can only be called exactly once
    //on a response). Therefore I'm writing the data to a ByteArrayOutputStream and then writing the byte array from
    //the ByteArrayOutputStream to the response manually.

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectMapper objectMapper = new ObjectMapper();
    JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(byteArrayOutputStream, JsonEncoding.UTF8);

    //Before I can convert the data into JSON, I will need to filter some attributes out of the model (namely BindingResult)
    Map<String, Object> result = new HashMap<String, Object>();

    for(Map.Entry<String, Object> entry : model.asMap().entrySet()) {
        if(!(entry.getValue() instanceof BindingResult)) {
            result.put(entry.getKey(), entry.getValue());
        }
    }

    objectMapper.writeValue(generator, result);
    response.getWriter().write(new String(byteArrayOutputStream.toByteArray(), "UTF8"));
}

This still seems a little hacky to me. A possible improvement is to annotate the action with @ResponseBody and return the JSON as a string without involving the response at all. If anyone has a better solution, I’m all ears!

Popularity: 2% [?]

November 7, 2011 Posted by | Computers, Java, Nerdy Stuff, Software, Web | , , , , , , , , | Leave a Comment

Popularity Contest WordPress plugin breaks RSS feed

I’m using a somewhat old plugin (it hasn’t been updated since ’09) called Popularity Contest to show the popularity of my posts. However, I noticed that it was breaking my RSS feed. This is due to the fact that the plugin doesn’t properly escape the ampersand character inside an image URL. Instead of &#38; it uses just &. You can fix this by changind line number 2272 to:

$str .= '<img src="'.site_url('?ak_action=api_record_view&#38;id='.$post->ID.'&#38;type=feed').'" alt="" />';

Popularity: 2% [?]

October 23, 2011 Posted by | PHP, Programming and Development, Web | , , , | Leave a Comment

A new look

I decided to change the theme on my blog. I went for a wider theme this time because on my older theme the real estate for post content was rather narrow. This made the posts excessively long. This new theme has a lot more room for posts and so it doesn’t look like I’m posting a wall of text… well, mostly… I still tend to write a lot sometimes.

Popularity: 1% [?]

October 22, 2011 Posted by | Nerdy Stuff | , , | Leave a Comment

vivin.net is back… mostly

So I’m sure you’ve noticed that this website was down for a while. It went down about three weeks ago when one of the (two) hard-drives on my server died due to bad sectors; it was eight years old. I didn’t panic (too much), because my WordPress database was on the main drive, which is still alive. However, my home directory was on the bad drive and I hadn’t backed up in a while (sigh), and so I lost some images that I had uploaded. What baffled me were my WordPress uploads. I was sure that I had installed WordPress on my main drive, but when I went to search for it, I couldn’t find any trace of the install. Due to this, I’ve lost a few images and I’m not sure if I’ll be able to replace them unfortunately. Oh well.

Also, I must bid farewell to a dear and old friend: my webserver enterprise. I set up my webserver almost 10 years ago (running FreeBSD of course), and she has been serving me faithfully for all this time. Over the years I’ve dealt with all kinds of disasters and I’ve been able to keep her running. However, after this latest disaster I’ve realized that I just don’t have the time to maintain and administer a server anymore; it’s hard to do with a full-time job and with a full-load at school (did I mention that I am doing my Masters?). Therefore, I’ve moved my site over to dreamhost. The cool thing is that they also offer shell access too, which in my opinion is absolutely indispensable. It took me a little while to migrate my WordPress installation over (I had to work out a few kinks), but now it looks like everything is running smoothly.

On that note, I’m looking forward to less server-administration and more blogging!

Popularity: 2% [?]

September 19, 2011 Posted by | Computers, FreeBSD, Hardware, Nerdy Stuff | , , , , , | Leave a Comment

All original content on these pages is fingerprinted and certified by Digiprove