java.lang.Object
com.aspose.words.VisitorAction
public class VisitorAction
- extends java.lang.Object
Utility class containing constants.
Allows the visitor to control the enumeration of nodes.
Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.
public void toText() throws Exception
{
// Open the document we want to convert.
Document doc = new Document(getMyDir() + "Visitor.ToText.doc");
// Create an object that inherits from the DocumentVisitor class.
myDocToTxtWriter myConverter = new myDocToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
System.out.println(myConverter.GetText());
}
/// <summary>
/// Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
/// </summary>
public class myDocToTxtWriter extends DocumentVisitor
{
public myDocToTxtWriter()
{
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String GetText()
{
return mBuilder.toString();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int VisitRun(Run run)
{
AppendText(run.getText());
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
public int VisitFieldStart(FieldStart fieldStart)
{
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows
// after a field start character. We want to skip field codes and output field
// result only, therefore we use a flag to suspend the output while inside a field code.
//
// Note this is a very simplistic implementation and will not work very well
// if you have nested fields in a document.
mIsSkipText = true;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
public int VisitFieldSeparator(FieldSeparator fieldSeparator)
{
// Once reached a field separator node, we enable the output because we are
// now entering the field result nodes.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
public int VisitFieldEnd(FieldEnd fieldEnd)
{
// Make sure we enable the output when reached a field end because some fields
// do not have field separator and do not have field result.
mIsSkipText = false;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when visiting of a Paragraph node is ended in the document.
/// </summary>
public int VisitParagraphEnd(Paragraph paragraph)
{
// When outputting to plain text we output Cr+Lf characters.
AppendText(ControlChar.CR_LF);
return VisitorAction.CONTINUE;
}
public int VisitBodyStart(Body body)
{
// We can detect beginning and end of all composite nodes such as Section, Body,
// Table, Paragraph etc and provide custom handling for them.
mBuilder.append("*** Body Started ***\r\n");
return VisitorAction.CONTINUE;
}
public int VisitBodyEnd(Body body)
{
mBuilder.append("*** Body Ended ***\r\n");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a HeaderFooter node is encountered in the document.
/// </summary>
public int VisitHeaderFooterStart(HeaderFooter headerFooter)
{
// Returning this value from a visitor method causes visiting of this
// node to stop and move on to visiting the next sibling node.
// The net effect in this example is that the text of headers and footers
// is not included in the resulting output.
return VisitorAction.SKIP_THIS_NODE;
}
/// <summary>
/// Adds text to the current output. Honours the enabled/disabled output flag.
/// </summary>
private void AppendText(String text)
{
if (!mIsSkipText)
mBuilder.append(text);
}
private final StringBuilder mBuilder;
private boolean mIsSkipText;
}
Field Summary |
static final int | CONTINUE | |
The visitor requests the enumeration to continue.
|
static final int | SKIP_THIS_NODE | |
The visitor requests to skip the current node and continue enumeration.
|
static final int | STOP | |
The visitor requests the enumeration of nodes to stop.
|
CONTINUE | |
public static final int CONTINUE |
-
The visitor requests the enumeration to continue.
SKIP_THIS_NODE | |
public static final int SKIP_THIS_NODE |
-
The visitor requests to skip the current node and continue enumeration.
STOP | |
public static final int STOP |
-
The visitor requests the enumeration of nodes to stop.
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.