package com.github.robotdf;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.Range;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

@SuppressWarnings("serial")
public class RobotDFServlet extends AbstractRobotServlet {

	private final static String VERSION = "1";
	
	Pattern pattern = Pattern.compile("owl:sameAs\\[.+\\]");

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
        
	    if (bundle.wasSelfAdded()) {
	      Blip blip = wavelet.appendBlip();
	      TextView textView = blip.getDocument();
	      textView.appendMarkup("RobotDF v" + VERSION + ".<br />");
	    }
	            
	    for (Event e: bundle.getEvents()) {
	      if (e.getType() == EventType.BLIP_SUBMITTED ||
	    	  e.getType() == EventType.BLIP_VERSION_CHANGED) {
	    	  Blip blip = e.getBlip();
	    	  if (!blip.getCreator().equals("RobotDF")) {
	    		  TextView textView = blip.getDocument();
	    		  // apply all known commands
	    		  owlSameAs(textView);
	    	  }
	      }
	    }
	}

	private void owlSameAs(TextView textView) {
		while (true) {
			Matcher matcher = pattern.matcher(textView.getText());
			if (matcher.find()) {
				String match = matcher.group();
				int start = matcher.start();
				int end = matcher.end();

				String replacement = match.substring("owl:sameAs[".length());
				replacement = replacement.substring(0, replacement.length()-1);
				if (replacement != null && replacement.length() > 0) {
					textView.replace(
						new Range(start, end),
						replacement
					);
//					textView.setAnnotation(new Range(start, start+replacement.length()), "chem/molForm", replacement);
				}
			} else {
				// OK, nothing more found, so return
				return;
			}
		}
	}

}

@@ -22,16 +22,135 @@
 botdf;%0A%0A
+import java.io.IOException;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0A
 import j
@@ -173,16 +173,16 @@
 atcher;%0A
-
 import j
@@ -513,16 +513,103 @@
 Wavelet;
+%0Aimport com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;
 %0A%0A@Suppr
@@ -730,17 +730,17 @@
 SION = %22
-1
+2
 %22;%0A%09%0A%09Pa
@@ -1936,24 +1936,412 @@
 th() %3E 0) %7B%0A
+%09%09%09%09%09try %7B%0A%09%09%09%09%09%09URL realURL = new URL(replacement);%0A%09%09%09%09        URLConnection connection = realURL.openConnection();%0A%09%09%09%09        connection.setRequestProperty(%0A%09%09%09%09            %22Accept%22,%0A%09%09%09%09            %22application/xml, application/rdf+xml%22%0A%09%09%09%09        );%0A%09%09%09%09        Model model = ModelFactory.createOntologyModel();%0A%09%09%09%09        model.read(connection.getInputStream(), %22%22, %22RDF/XML%22);%0A%09
 %09%09%09%09%09textVie
@@ -2351,16 +2351,17 @@
 eplace(%0A
+%09
 %09%09%09%09%09%09ne
@@ -2383,24 +2383,25 @@
 end),%0A%09%09%09%09%09%09
+%09
 replacement%0A
@@ -2399,17 +2399,60 @@
 lacement
-%0A
+ + %22%5BTriples found: %22 + model.size() + %22%5D%22%0A%09
 %09%09%09%09%09);%0A
@@ -2453,16 +2453,17 @@
 %09%09%09);%0A//
+%09
 %09%09%09%09%09tex
@@ -2554,24 +2554,340 @@
 placement);%0A
+%09%09%09%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09%09%09%09textView.replace(%0A%09%09%09%09%09%09    new Range(start, end),%0A%09%09%09%09%09%09    textView.getText() + %22%5BBad URL%5D%22%0A%09%09%09%09%09%09);%0A%09%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09%09textView.replace(%0A%09%09%09%09%09%09%09new Range(start, end),%0A%09%09%09%09%09%09%09textView.getText() + %22%5BIO Error: %22 + e.getMessage() + %22%5D%22%0A%09%09%09%09%09%09);%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%7D e

Use Jena to do something... still struggling with org.slf4j.LoggerFactory not found in classpath :(