List of usage examples for org.apache.commons.jrcs.diff Delta getRevised
public Chunk getRevised()
From source file:com.ecyrd.jspwiki.filters.SpamFilter.java
/** * Creates a simple text string describing the added content. * * @param context//from w w w . java2 s . c om * @param newText * @return Empty string, if there is no change. */ private static Change getChange(WikiContext context, String newText) { WikiPage page = context.getPage(); StringBuffer change = new StringBuffer(); WikiEngine engine = context.getEngine(); // Get current page version Change ch = new Change(); try { String oldText = engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION); String[] first = Diff.stringToArray(oldText); String[] second = Diff.stringToArray(newText); Revision rev = Diff.diff(first, second, new MyersDiff()); if (rev == null || rev.size() == 0) { return ch; } for (int i = 0; i < rev.size(); i++) { Delta d = rev.getDelta(i); if (d instanceof AddDelta) { d.getRevised().toString(change, "", "\r\n"); ch.m_adds++; } else if (d instanceof ChangeDelta) { d.getRevised().toString(change, "", "\r\n"); ch.m_adds++; } else if (d instanceof DeleteDelta) { ch.m_removals++; } } } catch (DifferentiationFailedException e) { log.error("Diff failed", e); } // // Don't forget to include the change note, too // String changeNote = (String) page.getAttribute(WikiPage.CHANGENOTE); if (changeNote != null) { change.append("\r\n"); change.append(changeNote); } // // And author as well // if (page.getAuthor() != null) { change.append("\r\n" + page.getAuthor()); } ch.m_change = change.toString(); return ch; }
From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java
private int computeInsertedByteCount(Delta replaceDelta) throws UnsupportedEncodingException { // This should be the byte count of the revised chunk. @SuppressWarnings("unchecked") List<CompareLineInfo> revisedChunk = replaceDelta.getRevised().chunk(); int seekPosition = revisedChunk.get(0).getLineSeekPosition(); CompareLineInfo lastLine = revisedChunk.get(revisedChunk.size() - 1); int lastLineSeekStart = lastLine.getLineSeekPosition(); byte[] lastLineAsByteArray = lastLine.getLineString().getBytes(UTF8); int lastLineEnd = lastLineSeekStart + lastLineAsByteArray.length; return lastLineEnd - seekPosition; }
From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java
private byte[] computeSecondFileByteBufferForInsert(Delta delta, int insertedByteCount) throws UnsupportedEncodingException { byte[] insertedBytes = new byte[insertedByteCount]; @SuppressWarnings("unchecked") List<CompareLineInfo> insertedChunk = delta.getRevised().chunk(); int insertionIndex = 0; for (CompareLineInfo lineInfo : insertedChunk) { byte[] chunkBytes = lineInfo.getLineString().getBytes(UTF8); for (int i = 0; i < chunkBytes.length; i++) { insertedBytes[insertionIndex++] = chunkBytes[i]; }/* w w w . jav a 2s . co m*/ } assert (insertionIndex == insertedByteCount); if (insertionIndex != insertedByteCount) { System.out.println("Oops"); throw new RuntimeException("Error in compare with apache."); } return insertedBytes; }
From source file:com.qumasoft.guitools.compare.CompareFilesForGUI.java
private void deduceRowTypes(Revision apacheRev, CompareLineInfo[] fileA, CompareLineInfo[] fileB) throws QVCSOperationException { // First get all the deltas... Delta[] deltas = new Delta[apacheRev.size()]; for (int i = 0; i < apacheRev.size(); i++) { deltas[i] = apacheRev.getDelta(i); }/*from w w w. j a v a 2 s . co m*/ fileARowTypeArray = new byte[fileA.length]; fileBRowTypeArray = new byte[fileB.length]; fileADeltaForRowArray = new Delta[fileA.length]; fileBDeltaForRowArray = new Delta[fileB.length]; // Set all the rows to default to NORMAL. for (int i = 0; i < fileA.length; i++) { fileARowTypeArray[i] = ContentRow.ROWTYPE_NORMAL; } for (int i = 0; i < fileB.length; i++) { fileBRowTypeArray[i] = ContentRow.ROWTYPE_NORMAL; } for (Delta delta : deltas) { byte rowType; if (delta instanceof AddDelta) { rowType = ContentRow.ROWTYPE_INSERT; // We need to set the row type and delta here for fileA because the // for loop doesn't traverse any rows on fileA. if (delta.getOriginal().anchor() > 0) { fileARowTypeArray[delta.getOriginal().anchor() - 1] = ContentRow.ROWTYPE_NORMAL; fileADeltaForRowArray[delta.getOriginal().anchor() - 1] = delta; } } else if (delta instanceof ChangeDelta) { rowType = ContentRow.ROWTYPE_REPLACE; } else if (delta instanceof DeleteDelta) { rowType = ContentRow.ROWTYPE_DELETE; // We need to set the row type and delta here for fileB because the // for loop doesn't traverse any rows on fileB. if (delta.getRevised().anchor() > 0) { fileBRowTypeArray[delta.getRevised().anchor() - 1] = ContentRow.ROWTYPE_NORMAL; fileBDeltaForRowArray[delta.getRevised().anchor() - 1] = delta; } } else { throw new QVCSOperationException("Unknown delta type."); } @SuppressWarnings("unchecked") int firstA = delta.getOriginal().first(); int lastA = delta.getOriginal().last(); for (int j = firstA; j <= lastA; j++) { fileARowTypeArray[j] = rowType; fileADeltaForRowArray[j] = delta; } int firstB = delta.getRevised().first(); int lastB = delta.getRevised().last(); for (int j = firstB; j <= lastB; j++) { fileBRowTypeArray[j] = rowType; fileBDeltaForRowArray[j] = delta; } } }
From source file:org.jmeld.diff.MyersDiff.java
private JMRevision buildRevision(Revision revision, Object[] orig, Object[] rev) { JMRevision result;// w w w .j ava2 s . c o m Delta delta; Chunk original; Chunk revised; if (orig == null) { throw new IllegalArgumentException("original sequence is null"); } if (rev == null) { throw new IllegalArgumentException("revised sequence is null"); } result = new JMRevision(orig, rev); for (int i = 0; i < revision.size(); i++) { delta = revision.getDelta(i); original = delta.getOriginal(); revised = delta.getRevised(); result.add(new JMDelta(new JMChunk(original.anchor(), original.size()), new JMChunk(revised.anchor(), revised.size()))); } return result; }