BatchedEventHandler.java

00001 package edu.stanford.hci.r3.pen.batch;
00002 
00003 import java.io.File;
00004 import java.util.ArrayList;
00005 import java.util.Date;
00006 import java.util.List;
00007 import java.util.regex.Matcher;
00008 import java.util.regex.Pattern;
00009 
00010 import edu.stanford.hci.r3.pattern.coordinates.PageAddress;
00011 import edu.stanford.hci.r3.pen.PenSample;
00012 import edu.stanford.hci.r3.pen.ink.Ink;
00013 import edu.stanford.hci.r3.pen.ink.InkStroke;
00014 import edu.stanford.hci.r3.units.PatternDots;
00015 import edu.stanford.hci.r3.util.DebugUtils;
00016 import edu.stanford.hci.r3.util.files.FileUtils;
00017 
00040 public abstract class BatchedEventHandler {
00041 
00045         private static final String BEGIN_PAGE_TAG = "<page address=\"(.*?)\".*?>";
00046 
00047         private static final String BEGIN_SAMPLE_TAG_END_SAMPLE_TAG = "<p x=\"(.*?)\" y=\"(.*?)\" f=\"(.*?)\" t=\"(.*?)\".*?/>";
00048 
00049         private static final String BEGIN_STROKE_TAG = "<stroke begin=\"(.*?)\".*?>";
00050 
00054         private static final String END_PAGE_TAG = "</page>";
00055 
00056         private static final String END_STROKE_TAG = "</stroke>";
00057 
00058         private static final Pattern PATTERN_BEGIN_PAGE = Pattern.compile(BEGIN_PAGE_TAG);
00059 
00060         private static final Pattern PATTERN_BEGIN_STROKE = Pattern.compile(BEGIN_STROKE_TAG);
00061 
00062         private static final Pattern PATTERN_END_PAGE = Pattern.compile(END_PAGE_TAG);
00063 
00064         private static final Pattern PATTERN_END_STROKE = Pattern.compile(END_STROKE_TAG);
00065 
00066         private static final Pattern PATTERN_SAMPLE = Pattern.compile(BEGIN_SAMPLE_TAG_END_SAMPLE_TAG);
00067 
00068         private String name;
00069 
00070         private PatternDots referenceUnit = new PatternDots();
00071 
00072 
00076         public BatchedEventHandler(String theName) {
00077                 name = theName;
00078         }
00079 
00083         public void batchedDataArrived(File xmlDataFile) {
00084                 // parse it like we used to do... in BNet
00085                 DebugUtils.println("BatchEventHandler got the file: " + xmlDataFile);
00086 
00087                 // read in the whole request file into a String
00088                 // is this an issue if the xml file is large, say 20MB?
00089                 final StringBuilder requestBuffer = FileUtils.readFileIntoStringBuffer(xmlDataFile);
00090 
00091                 final Matcher matcherPageBegin = PATTERN_BEGIN_PAGE.matcher(requestBuffer);
00092                 final Matcher matcherPageEnd = PATTERN_END_PAGE.matcher(requestBuffer);
00093 
00094                 while (matcherPageBegin.find() && matcherPageEnd.find()) {
00095                         // DebugUtils.println("Processing Page: ");
00096 
00097                         // location of the opening tag <page ...>
00098                         final int beginTagEndIndex = matcherPageBegin.end();
00099                         final int beginTagStartIndex = matcherPageBegin.start();
00100 
00101                         // location of the closing tag </page>
00102                         final int endTagStartIndex = matcherPageEnd.start();
00103                         final int endTagEndIndex = matcherPageEnd.end();
00104 
00105                         // DebugUtils.println(BEGIN_PAGE_TAG + " found at " + beginTagStartIndex + " to "
00106                         // + beginTagEndIndex);
00107                         // DebugUtils.println(END_PAGE_TAG + " found at " + endTagStartIndex + " to "
00108                         // + endTagEndIndex);
00109 
00110                         // extract page address
00111                         final String pageAddress = matcherPageBegin.group(1);
00112                         DebugUtils.println("Page Address: " + pageAddress);
00113 
00114                         // for every page, we create ONE ink object
00115                         final Ink inkOnThisPage = new Ink();
00116                         inkOnThisPage.setName("Ink for " + pageAddress);
00117 
00118                         // save where we got this ink, so we will know later on...
00119                         final PageAddress address = new PageAddress(pageAddress);
00120                         inkOnThisPage.setSourcePageAddress(address);
00121 
00122                         // extract front and end matter
00123                         // final String beginText = requestBuffer.substring(beginTagStartIndex, beginTagEndIndex);
00124                         // final String endText = requestBuffer.substring(endTagStartIndex, endTagEndIndex);
00125 
00126                         // extract text in between the begin and end tags
00127                         final String insideText = requestBuffer.substring(beginTagEndIndex, endTagStartIndex);
00128                         // System.out.println("Internal Text Length: " + insideText.length());
00129                         // System.out.println(insideText);
00130 
00131                         final Matcher matcherStrokeBegin = PATTERN_BEGIN_STROKE.matcher(insideText);
00132                         final Matcher matcherStrokeEnd = PATTERN_END_STROKE.matcher(insideText);
00133                         // look through the strokes for this page
00134                         while (matcherStrokeBegin.find() && matcherStrokeEnd.find()) {
00135                                 final String strokeTimeStamp = matcherStrokeBegin.group(1);
00136                                 final long ts = Long.parseLong(strokeTimeStamp);
00137                                 // date/time of the beginning of the stroke!
00138                                 DebugUtils.println("Stroke Time: " + new Date(ts));
00139 
00140                                 // samples between the <stroke...></stroke>
00141                                 final String strokeSampleText = insideText.substring(matcherStrokeBegin.end(),
00142                                                 matcherStrokeEnd.start());
00143 
00144                                 final List<PenSample> samples = new ArrayList<PenSample>();
00145 
00146                                 final Matcher matcherSample = PATTERN_SAMPLE.matcher(strokeSampleText);
00147                                 while (matcherSample.find()) {
00148                                         final String x = matcherSample.group(1);
00149                                         final String y = matcherSample.group(2);
00150                                         final String f = matcherSample.group(3);
00151                                         final String t = matcherSample.group(4);
00152 
00153                                         // make samples and stuff.... add it to the ink
00154                                         // DebugUtils.println(x + " " + y + " f=" + f + " ts=" + t);
00155 
00156                                         final PenSample sample = new PenSample(Double.parseDouble(x), Double.parseDouble(y),
00157                                                         Integer.parseInt(f), Long.parseLong(t));
00158                                         samples.add(sample);
00159                                 }
00160 
00161                                 // we create one stroke object and add it to the current ink object
00162                                 // we also add all the recent samples into this stroke
00163                                 final InkStroke stroke = new InkStroke(samples, referenceUnit);
00164                                 inkOnThisPage.addStroke(stroke);
00165                         }
00166 
00167                         inkArrived(inkOnThisPage);
00168                 }
00169                 // System.out.println();
00170         }
00171 
00177         public abstract void inkArrived(Ink inkOnThisPage);
00178 
00182         public String toString() {
00183                 return name;
00184         }
00185 }

Generated on Sat Apr 14 18:21:33 2007 for R3 Paper Toolkit by  doxygen 1.4.7