Package com.deltawalker.script
Interface TextComparison
- All Superinterfaces:
Comparison,FileComparison,HistoryEntry
A
Comparison of text files. Its results are DeltaRanges -
blocks of text, given by an offset and a length in each document - that can
be inspected, merged from one Role into another, and saved.
const Comparison = Java.type('com.deltawalker.script.Comparison');
const Role = Java.type('com.deltawalker.script.Role');
const IN = App.getProperty('user.home') + '/test/samples';
const comp = App.getComparisons().create(Comparison.Type.TEXT,
[IN + '/q0.txt', IN + '/q1.txt'], null);
await comp.compare();
const deltas = comp.getAllDeltas();
console.log('Blocks: ' + deltas.length);
for (let i = 0; i < deltas.length; i++) {
const d = deltas[i];
if (d.isChanged()) {
console.log('Changed: '
+ comp.getText(Role.ONE, d.offset(Role.ONE), d.length(Role.ONE)));
}
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.deltawalker.script.Comparison
Comparison.Method, Comparison.State, Comparison.Type -
Method Summary
Modifier and TypeMethodDescriptionvoiddeleteText(DeltaRange[] deltas, Role[] roles, ProgressListener pm) Deletes the text of the specifiedDeltaRanges from the documents with the specifiedRoles.Returns the top-level blocks of text found by the lastComparison.compare().Returns part of the text of the document with the specifiedRole, for instance the text aDeltaRangerefers to:intReturns the number of characters in the document with the specifiedRole.merge(MergeOp mergeOp, DeltaRange[] ranges, Role src, Role dst, ProgressListener pl) Applies the specifiedMergeOpto the specifiedDeltaRanges, from the source role to the destination role, and returns an operation that settles when the merge completes.report(String path, ReportKind kind, PatchFormat format) Writes an HTML report of the results of this comparison to a file, and returns an operation that settles when the report has been written.reportEx(UriInfo uriInfo, ReportKind kind, PatchFormat format) Likereport(String, ReportKind, PatchFormat), but takes aUriInfofor the report file, so that, for example, a remote location, a user name and password, a character encoding or a line ending can be specified.voidReplaces the specified range of the document with the specifiedRolewith the specified text.Methods inherited from interface com.deltawalker.script.Comparison
addListener, close, compare, hasDifferences, isClosed, print, removeListenerMethods inherited from interface com.deltawalker.script.FileComparison
save, saveAll, saveExMethods inherited from interface com.deltawalker.script.HistoryEntry
getCreationTime, getDisplayCreationTime, getName, getPaths, getRoles, getSettings, getType, getUriInfos, setName
-
Method Details
-
deleteText
void deleteText(DeltaRange[] deltas, Role[] roles, ProgressListener pm) throws IllegalArgumentException, BadLocationException Deletes the text of the specifiedDeltaRanges from the documents with the specifiedRoles. The change is made in the comparison only; callFileComparison.save(Role, String, ProgressListener)to save it.- Parameters:
deltas- the ranges whose text is to be deleted; cannot benullor emptyroles- the roles of the documents to delete text from; cannot benullor empty, and must all be roles of this comparisonpm- an optionalProgressListenerto track the progress of this operation and to cancel it; can benull- Throws:
NullPointerException- ifdeltasorrolesisnullIllegalArgumentException- ifdeltasorrolesis empty, or if a role is not a role of this comparison, for instanceRole.TWOin a two-way comparisonBadLocationException- if a range refers to an offset or length that does not exist in a document
-
getAllDeltas
DeltaRange[] getAllDeltas()Returns the top-level blocks of text found by the lastComparison.compare(). Use theis...()methods ofDeltato tell what kind of difference each block represents, andDeltaRange.getChildren()for the inline differences within a block.await comp.compare(); const deltas = comp.getAllDeltas(); console.log('Blocks found: ' + deltas.length);- Returns:
- an array of this comparison's
DeltaRanges
-
getText
String getText(Role role, int offset, int length) throws BadLocationException, IllegalArgumentException Returns part of the text of the document with the specifiedRole, for instance the text aDeltaRangerefers to:const text = comp.getText(Role.ONE, delta.offset(Role.ONE), delta.length(Role.ONE));To get the entire content of a document use an offset of
0and the length returned bylength(Role). Be careful with very large documents: the text is copied, which can run out of memory.- Parameters:
role- the role of the document; must be a role of this comparisonoffset- the 0-based offset of the first character to returnlength- the number of characters to return- Returns:
- the text of the document with the specified
roleat the specifiedoffsetwith the specifiedlength - Throws:
BadLocationException- ifoffsetandlengthdo not describe a range within the documentIllegalArgumentException- declared for compatibility
-
length
Returns the number of characters in the document with the specifiedRole.- Parameters:
role- the role of the document; must be a role of this comparison, for instance notRole.TWOin a two-way comparison- Returns:
- the length of the document, in characters
- Throws:
IllegalArgumentException- declared for compatibility
-
merge
ScriptOperation merge(MergeOp mergeOp, DeltaRange[] ranges, Role src, Role dst, ProgressListener pl) throws BadLocationException, IllegalArgumentException Applies the specifiedMergeOpto the specifiedDeltaRanges, from the source role to the destination role, and returns an operation that settles when the merge completes. The merge changes the documents in the comparison only; callFileComparison.save(Role, String, ProgressListener)to save the result.const MergeOp = Java.type('com.deltawalker.script.MergeOp'); // Three-way: bring all non-conflicting changes into Role.REF await comp.merge(MergeOp.MERGE, comp.getAllDeltas(), null, Role.REF, null); comp.save(Role.REF, '/path/to/merged.txt', null); // Replace the Role.REF text of one block with its Role.ONE text await comp.merge(MergeOp.REPLACE, [delta], Role.ONE, Role.REF, null);- Parameters:
mergeOp- the operation to perform; cannot benullranges- the ranges to merge; cannot benull. When empty, nothing is done and the returned operation is already resolved.src- the source role. PassnullforMergeOp.MERGE, which takes its sources from bothRole.ONEandRole.TWO.dst- the destination rolepl- an optionalProgressListenerto track the progress of this operation and to cancel it; can benull- Returns:
- an operation that resolves when the merge completes
- Throws:
BadLocationException- if a range is invalid in the source or the destination documentNullPointerException- ifmergeOporrangesisnullIllegalArgumentException- ifRole.TWOis used in a two-way comparison, or ifMergeOp.MERGEis used in a two-way comparison
-
report
ScriptOperation report(String path, ReportKind kind, PatchFormat format) throws IllegalArgumentException Writes an HTML report of the results of this comparison to a file, and returns an operation that settles when the report has been written.const ReportKind = Java.type('com.deltawalker.script.ReportKind'); await comp.report('/path/to/report.html', ReportKind.HTML, null);Text comparisons always produce an HTML report;
kindandformatare only checked, not used to choose another format.- Parameters:
path- the path of the report file; cannot benullor emptykind- the kind of report; cannot benull. UseReportKind.HTML.format- the format of a patch report; cannot benullwhenkindisReportKind.PATCH, otherwise can benull- Returns:
- an operation that resolves when the report has been written
- Throws:
NullPointerException- ifpathorkindisnull, or ifformatisnullfor a patch reportIllegalArgumentException- ifpathis empty- See Also:
-
reportEx
ScriptOperation reportEx(UriInfo uriInfo, ReportKind kind, PatchFormat format) throws IllegalArgumentException Likereport(String, ReportKind, PatchFormat), but takes aUriInfofor the report file, so that, for example, a remote location, a user name and password, a character encoding or a line ending can be specified. When theUriInfospecifies no character encoding, UTF-8 is used.- Parameters:
uriInfo- the location of the report file; cannot benulland must have a non-empty URIkind- the kind of report; cannot benull. UseReportKind.HTML.format- the format of a patch report; cannot benullwhenkindisReportKind.PATCH, otherwise can benull- Returns:
- an operation that resolves when the report has been written
- Throws:
NullPointerException- ifuriInfoorkindisnull, or ifformatisnullfor a patch reportIllegalArgumentException- if the URI ofuriInfois empty
-
setText
void setText(Role role, int offset, int length, String text) throws BadLocationException, IllegalArgumentException Replaces the specified range of the document with the specifiedRolewith the specified text. The change is made in the comparison only; callFileComparison.save(Role, String, ProgressListener)to save it.// Insert a line at the beginning of the Role.ONE document comp.setText(Role.ONE, 0, 0, 'First line\n');- Parameters:
role- the role of the document to change; cannot benulloffset- the 0-based offset of the range to replacelength- the length of the range to replace;0to inserttext- the replacement text; cannot benull- Throws:
BadLocationException- ifoffsetandlengthdo not describe a range within the documentNullPointerException- ifroleortextisnullIllegalArgumentException- ifRole.TWOis specified in a two-way comparison
-