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.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.media.MediaByteArraySource;
import com.google.gdata.data.media.MediaSource;
import com.uwyn.jhighlight.renderer.JavaXhtmlRenderer;
import java.io.*;
import java.net.URL;
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/Interview.java");
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");
}
Thread.sleep(1500);
}
}
public void update(InputStream in, String titleSubstring) throws Exception {
DocsService client = new DocsService("PetrNalevka-DocSync-v1");
client.setUserCredentials("petr.nalevka@gmail.com", PassProvider.PASSWORD);
URL feedUri = new URL("http://docs.google.com/feeds/default/private/full/");
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;
break;
}
}
if (found == null) {
System.err.println("Entry not found, exiting.");
return;
}
found.setCanEdit(true);
MediaContent mediaContent = (MediaContent) found.getContent();
MediaSource mediaSource = client.getMedia(mediaContent);
found.setMediaSource(mediaSource);
String page = InputStreamUtil.read(in);
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("http://docs.google.com/feeds/default/private/full/document%3A"+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
}
}
}