Validation (9) XML (9) Geeky (3) Java (3) Android (2) Business IT (1) Chromecast (1) Devfest (1) Web (1)

Monday, September 19, 2011

Synchronize your IDE with Google Docs for you Google Phone interview

I had a phone interview with Google recently. When I prepared for the interview I tried to write code in Google Docs and was very much missing all the features of my IDE.

Maybe someone else may find my simple code snippet below useful. It does auto-synchronize you local file with the Google Document shared for you interview. So whenever you save the file in the IDE it gets uploaded to Docs so that you interview can see what you wrote. At the same time you have full code-completion/generation and the code does also syntax highlighting through JHighlight.

Here is the result:

In order to make it working you need to put your self to the sharing list (which you can do), otherwise there would be a permission error. If anyone would like to improve the code, back-synchronization of what the interviewer writes would be great.

package com.urbandroid.gdata;


import com.uwyn.jhighlight.renderer.JavaXhtmlRenderer;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UpdateDocument {

    public static void main(String[] args) throws Exception {
        UpdateDocument updater = new UpdateDocument();
        File file = new File(args.length == 1 ? args[0] : "/home/petr/project/google/src/");

        JavaXhtmlRenderer renderer = new JavaXhtmlRenderer();
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");

        long modified = -1;
        while (true) {
            if (modified != file.lastModified()) {
                modified = file.lastModified();
                System.err.println("File modified...");
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                renderer.highlight("GPhoneInterview by Petr Nalevka, last update: " + dateFormat.format(new Date()), new FileInputStream(file), out, "UTF-8", false);
                updater.update(new ByteArrayInputStream(out.toByteArray()), "Phone Interview");

    public void update(InputStream in, String titleSubstring) throws Exception {
        DocsService client = new DocsService("PetrNalevka-DocSync-v1");
        client.setUserCredentials("", PassProvider.PASSWORD);
        URL feedUri = new URL("");
        DocumentListFeed feed = client.getFeed(feedUri, DocumentListFeed.class);
        DocumentListEntry found = null;
        for (DocumentListEntry entry : feed.getEntries()) {
            System.err.println("Entry: "+entry.getTitle().getPlainText());
            if (entry.getTitle().getPlainText().contains(titleSubstring)) {
                found = entry;
        if (found == null) {
            System.err.println("Entry not found, exiting.");


        MediaContent mediaContent = (MediaContent) found.getContent();
        MediaSource mediaSource = client.getMedia(mediaContent);


        String page =;
        mediaContent.setMediaSource(new MediaByteArraySource(page.getBytes(), DocumentListEntry.MediaType.HTM.getMimeType()));

        // shared documents do not provide edit link?
        URL url;
        if (found.getEditLink() != null) {
            url = new URL(found.getEditLink().getHref());
        } else {
            url = new URL(""+found.getDocId());
        System.err.println("Updating URL: " + url);
        try {
            client.updateMedia(url, found);
        } catch (Exception e) {
            // ignore, hack because GData returns error on shared documents, but update works