com.aspose.words
Class ReplacingArgs

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

public class ReplacingArgs 
extends java.lang.Object

Provides data for a custom replace operation.

Example:

Shows how to insert content of one document into another during a customized find and replace operation.
public void insertDocumentAtReplace() throws Exception
{
    Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");
    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");

        // Insert a document after the paragraph, containing the match text.
        Paragraph para = (Paragraph)e.getMatchNode().getParentNode();
        insertDocument(para, subDoc);

        // Remove the paragraph with the match text.
        para.remove();

        return ReplaceAction.SKIP;
    }
}

Example:

Replaces text specified with regular expression with HTML.
public void replaceWithInsertHtml() throws Exception
{
    // Open the document.
    Document doc = new Document(getMyDir() + "Range.ReplaceWithInsertHtml.doc");

    doc.getRange().replace(Pattern.compile("<CustomerName>"), new ReplaceWithHtmlEvaluator(), false);

    // Save the modified document.
    doc.save(getMyDir() + "Range.ReplaceWithInsertHtml Out.doc");

}

private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
    /**
     * NOTE: This is a simplistic method that will only work well when the match
     * starts at the beginning of a run.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Replace '<CustomerName>' text with a red bold name.
        builder.insertHtml("<b><font color='red'>James Bond</font></b>");

        e.setReplacement("");
        return ReplaceAction.REPLACE;
    }
}
See Also:
IReplacingCallback, Range, Range.replace(java.lang.String,java.lang.String,boolean,boolean)

Property Getters/Setters Summary
intgetGroupIndex()
voidsetGroupIndex(int value)
           Identifies, by index, a captured group in the Match that is to be replaced with the Replacement string.
java.util.regex.MatchergetMatch()
           The java.util.regex.Matcher resulting from a single regular expression match during a Replace.
NodegetMatchNode()
           Gets the node that contains the beginning of the match.
intgetMatchOffset()
           Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match.
java.lang.StringgetReplacement()
voidsetReplacement(java.lang.String value)
           Gets or sets the replacement string.
 

Property Getters/Setters Detail

getGroupIndex/setGroupIndex

public int getGroupIndex() / public void setGroupIndex(int value)
Identifies, by index, a captured group in the Match that is to be replaced with the Replacement string.

Default is zero.


getMatch

public java.util.regex.Matcher getMatch()
The java.util.regex.Matcher resulting from a single regular expression match during a Replace.

Matcher.start() gets the zero-based starting position of the match from the start of the find and replace range.

Example:

Shows how to replace with a custom evaluator.
public void replaceWithEvaluator() throws Exception
{
    Document doc = new Document(getMyDir() + "Range.ReplaceWithEvaluator.doc");
    doc.getRange().replace(Pattern.compile("[s|m]ad"), new MyReplaceEvaluator(), true);
    doc.save(getMyDir() + "Range.ReplaceWithEvaluator Out.doc");
}

private class MyReplaceEvaluator implements IReplacingCallback
{
    /**
     * This is called during a replace operation each time a match is found.
     * This method appends a number to the match string and returns it as a replacement string.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        e.setReplacement(e.getMatch().group() + Integer.toString(mMatchNumber));
        mMatchNumber++;
        return ReplaceAction.REPLACE;
    }

    private int mMatchNumber;
}

getMatchNode

public Node getMatchNode()
Gets the node that contains the beginning of the match.

Example:

Shows how to insert content of one document into another during a customized find and replace operation.
public void insertDocumentAtReplace() throws Exception
{
    Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");
    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");

        // Insert a document after the paragraph, containing the match text.
        Paragraph para = (Paragraph)e.getMatchNode().getParentNode();
        insertDocument(para, subDoc);

        // Remove the paragraph with the match text.
        para.remove();

        return ReplaceAction.SKIP;
    }
}

getMatchOffset

public int getMatchOffset()
Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match.

getReplacement/setReplacement

public java.lang.String getReplacement() / public void setReplacement(java.lang.String value)
Gets or sets the replacement string.

Example:

Replaces text specified with regular expression with HTML.
public void replaceWithInsertHtml() throws Exception
{
    // Open the document.
    Document doc = new Document(getMyDir() + "Range.ReplaceWithInsertHtml.doc");

    doc.getRange().replace(Pattern.compile("<CustomerName>"), new ReplaceWithHtmlEvaluator(), false);

    // Save the modified document.
    doc.save(getMyDir() + "Range.ReplaceWithInsertHtml Out.doc");

}

private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
    /**
     * NOTE: This is a simplistic method that will only work well when the match
     * starts at the beginning of a run.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Replace '<CustomerName>' text with a red bold name.
        builder.insertHtml("<b><font color='red'>James Bond</font></b>");

        e.setReplacement("");
        return ReplaceAction.REPLACE;
    }
}

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