@Override public boolean verify(Primary primary) { if (!(primary instanceof TextPrimary)) throw new IllegalArgumentException(); TextPrimary textPrimary = (TextPrimary) primary; byte[] digest = checkSum(textPrimary.getContent(), this.checkSumType); return Arrays.equals(digest, this.checkSum); }
// Constructor::TextContext // // Generates a TextContext Object. // // Generates before and after contexts based on given length and values // // Parameters // // * primary -- TextPrimary object to be tested against context // * constraint -- TextConstraint object attached to this context // * checkSumType -- Hash for checksum // * contextLength - length (in chars) of the context used for testing // public TextContext( TextPrimary primary, TextConstraint constraint, HashType checkSumType, int contextLength) { super(); this.checkSumType = checkSumType; // Testing if content matches the bit-checksum tests this.checkSum = checkSum(primary.getContent(), checkSumType); int beforeStart = constraint.getStartPos() - contextLength; beforeStart = Math.max(0, beforeStart); int beforeEnd = constraint.getStartPos(); int afterStart = constraint.getEndPos(); int afterEnd = constraint.getEndPos() + contextLength; afterEnd = Math.min(primary.getContent().length(), afterEnd); // Evaluating how much of selected text to store this.totalSelectionLength = primary.getContent().length(); int cLength = this.totalSelectionLength; if (this.totalSelectionLength > DEFAULT_CONTEXTLENGTH) { double half = (double) (this.totalSelectionLength / 2); cLength = (int) (Math.floor(half * percentStorage)); this.beginSel = primary.getContent().substring(beforeEnd, (beforeEnd + cLength)); this.endSel = primary.getContent().substring((afterStart - cLength), afterStart); this.totalSelection = this.beginSel.concat(this.endSel); } else { // Use the entire selection this.beginSel = ""; this.endSel = ""; this.totalSelection = primary.getContent(); } this.beforeContext = primary.getContent().substring(beforeStart, beforeEnd); this.afterContext = primary.getContent().substring(afterStart, afterEnd); }
@Override public Constraint match(Primary primary, TextConstraint originalConstraint) throws Context.NoMatchFoundException { if (!(primary instanceof TextPrimary)) throw new IllegalArgumentException(); TextPrimary textPrimary = (TextPrimary) primary; // TextConstraint result = shingleCloudMatch(textPrimary.getContent()); TextConstraint result = exactMatch(textPrimary.getContent(), originalConstraint); if (result == null) { // perform fuzzy matching result = gstMatch(textPrimary.getContent(), originalConstraint); } // sanity check if (result.getEndPos() < result.getStartPos()) throw new Context.NoMatchFoundException(); return result; }