com.aspose.words
Class FindReplaceDirection

java.lang.Object
    extended by com.aspose.words.FindReplaceDirection

public class FindReplaceDirection 
extends java.lang.Object

Utility class containing constants. Specifies direction for replace operations.

Example:

Shows how to determine which direction a find-and-replace operation traverses the document in.
public void direction(int findReplaceDirection) throws Exception
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert three runs which we can search for using a regex pattern.
    // Place one of those runs inside a text box.
    builder.writeln("Match 1.");
    builder.writeln("Match 2.");
    builder.writeln("Match 3.");
    builder.writeln("Match 4.");

    // We can use a "FindReplaceOptions" object to modify the find-and-replace process.
    FindReplaceOptions options = new FindReplaceOptions();

    // Assign a custom callback to the "ReplacingCallback" property.
    TextReplacementRecorder callback = new TextReplacementRecorder();
    options.setReplacingCallback(callback);

    // Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
    // operation to start from the end of the range, and traverse back to the beginning.
    // Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
    // operation to start from the beginning of the range, and traverse to the end.
    options.setDirection(findReplaceDirection);

    doc.getRange().replace(Pattern.compile("Match \\d*"), "Replacement", options);

    Assert.assertEquals("Replacement.\r" +
                    "Replacement.\r" +
                    "Replacement.\r" +
                    "Replacement.", doc.getText().trim());

    switch (findReplaceDirection)
    {
        case FindReplaceDirection.FORWARD:
            Assert.assertEquals(new String[] { "Match 1", "Match 2", "Match 3", "Match 4" }, callback.getMatches().toArray());
            break;
        case FindReplaceDirection.BACKWARD:
            Assert.assertEquals(new String[] { "Match 4", "Match 3", "Match 2", "Match 1" }, callback.getMatches().toArray());
            break;
    }
}

@DataProvider(name = "directionDataProvider")
public static Object[][] directionDataProvider() {
    return new Object[][]
    {
        {FindReplaceDirection.BACKWARD},
        {FindReplaceDirection.FORWARD},
    };
}

/// <summary>
/// Records all matches that occur during a find-and-replace operation in the order that they take place.
/// </summary>
private static class TextReplacementRecorder implements IReplacingCallback
{
    public int replacing(ReplacingArgs e)
    {
        mMatches.add(e.getMatch().group(0));
        return ReplaceAction.REPLACE;
    }

    public ArrayList<String> getMatches() { return mMatches; }

    private ArrayList<String> mMatches = new ArrayList<String>();
}

Field Summary
static final intFORWARD = 0
           Matched items are replaced from first to last.
static final intBACKWARD = 1
           Matched items are replaced from last back to first.
 

Field Detail

FORWARD = 0

public static final int FORWARD
Matched items are replaced from first to last.

BACKWARD = 1

public static final int BACKWARD
Matched items are replaced from last back to first.

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.