java.lang.Object
com.aspose.words.DocumentVisitor
public abstract class DocumentVisitor
- extends java.lang.Object
Base class for custom document visitors.
With DocumentVisitor you can define and execute custom operations
that require enumeration over the document tree.
For example, Aspose.Words uses DocumentVisitor internally for saving Document
in various formats and for other operations like finding fields or bookmarks over
a fragment of a document.
To use DocumentVisitor:
- Create a class derived from DocumentVisitor.
- Override and provide implementations for some or all of the VisitXXX methods
to perform some custom operations.
- Call Node.accept(com.aspose.words.DocumentVisitor) on the Node that
you want to start the enumeration from.
DocumentVisitor provides default implementations for all of the VisitXXX methods
to make it easier to create new document visitors as only the methods required for the particular
visitor need to be overridden. It is not necessary to override all of the visitor methods.
For more information see the Visitor design pattern.
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;
}
visitDocumentStart | |
public int visitDocumentStart(Document doc)
throws java.lang.Exception |
-
Called when enumeration of the document has started.
- Parameters:
doc
- The Document that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitDocumentEnd | |
public int visitDocumentEnd(Document doc)
throws java.lang.Exception |
-
Called when enumeration of the document has finished.
- Parameters:
doc
- The Document that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSectionStart | |
public int visitSectionStart(Section section)
throws java.lang.Exception |
-
Called when enumeration of a section has started.
- Parameters:
section
- The Section that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSectionEnd | |
public int visitSectionEnd(Section section)
throws java.lang.Exception |
-
Called when enumeration of a section has ended.
- Parameters:
section
- The Section that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBodyStart | |
public int visitBodyStart(Body body)
throws java.lang.Exception |
-
Called when enumeration of the main text story in a section has started.
- Parameters:
body
- The Body node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitBodyEnd | |
public int visitBodyEnd(Body body)
throws java.lang.Exception |
-
Called when enumeration of the main text story in a section has ended.
- Parameters:
body
- The Body node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitHeaderFooterStart | |
public int visitHeaderFooterStart(HeaderFooter headerFooter)
throws java.lang.Exception |
-
Called when enumeration of a header or footer in a section has started.
- Parameters:
headerFooter
- The HeaderFooter node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitHeaderFooterEnd | |
public int visitHeaderFooterEnd(HeaderFooter headerFooter)
throws java.lang.Exception |
-
Called when enumeration of a header or footer in a section has ended.
- Parameters:
headerFooter
- The HeaderFooter node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitParagraphStart | |
public int visitParagraphStart(Paragraph paragraph)
throws java.lang.Exception |
-
Called when enumeration of a paragraph has started.
- Parameters:
paragraph
- The Paragraph node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitParagraphEnd | |
public int visitParagraphEnd(Paragraph paragraph)
throws java.lang.Exception |
-
Called when enumeration of a paragraph has ended.
- Parameters:
paragraph
- The Paragraph node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitTableStart | |
public int visitTableStart(Table table)
throws java.lang.Exception |
-
Called when enumeration of a table has started.
- Parameters:
table
- The Table node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitTableEnd | |
public int visitTableEnd(Table table)
throws java.lang.Exception |
-
Called when enumeration of a table has ended.
- Parameters:
table
- The Table node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitRowStart | |
public int visitRowStart(Row row)
throws java.lang.Exception |
-
Called when enumeration of a table row has started.
- Parameters:
row
- The Row node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitRowEnd | |
public int visitRowEnd(Row row)
throws java.lang.Exception |
-
Called when enumeration of a table row has ended.
- Parameters:
row
- The Row node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCellStart | |
public int visitCellStart(Cell cell)
throws java.lang.Exception |
-
Called when enumeration of a table cell has started.
- Parameters:
cell
- The Cell node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCellEnd | |
public int visitCellEnd(Cell cell)
throws java.lang.Exception |
-
Called when enumeration of a table cell has ended.
- Parameters:
cell
- The Cell node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitRun | |
public int visitRun(Run run)
throws java.lang.Exception |
-
Called when a run of text in the is encountered.
- Parameters:
run
- The Run node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitFieldStart | |
public int visitFieldStart(FieldStart fieldStart)
throws java.lang.Exception |
-
Called when a field starts in the document.
A field in a Word Word document consists of a field code and field value.
For example, a field that displays a page number can be represented as follows:
[FieldStart]PAGE[FieldSeparator]98[FieldEnd]
The field separator separates field code from field value in the document. Note that some
fields have only field code and do not have field separator and field value.
Fields can be nested.
- Parameters:
fieldStart
- The FieldStart node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitFieldSeparator | |
public int visitFieldSeparator(FieldSeparator fieldSeparator)
throws java.lang.Exception |
-
Called when a field separator is encountered in the document.
The field separator separates field code from field value in the document. Note that some
fields have only field code and do not have field separator and field value.
For more info see visitFieldStart(com.aspose.words.FieldStart)
- Parameters:
fieldSeparator
- The FieldSeparator node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitFieldEnd | |
public int visitFieldEnd(FieldEnd fieldEnd)
throws java.lang.Exception |
-
Called when a field ends in the document.
For more info see visitFieldStart(com.aspose.words.FieldStart)
- Parameters:
fieldEnd
- The FieldEnd node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
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;
}
visitFormField | |
public int visitFormField(FormField formField)
throws java.lang.Exception |
-
Called when a form field is encountered in the document.
- Parameters:
formField
- The FormField node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBookmarkStart | |
public int visitBookmarkStart(BookmarkStart bookmarkStart)
throws java.lang.Exception |
-
Called when a start of a bookmark is encountered in the document.
- Parameters:
bookmarkStart
- The BookmarkStart node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitBookmarkEnd | |
public int visitBookmarkEnd(BookmarkEnd bookmarkEnd)
throws java.lang.Exception |
-
Called when an end of a bookmark is encountered in the document.
- Parameters:
bookmarkEnd
- The BookmarkEnd node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitFootnoteStart | |
public int visitFootnoteStart(Footnote footnote)
throws java.lang.Exception |
-
Called when enumeration of a footnote or endnote text has started.
- Parameters:
footnote
- The Footnote node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitFootnoteEnd | |
public int visitFootnoteEnd(Footnote footnote)
throws java.lang.Exception |
-
Called when enumeration of a footnote or endnote text has ended.
- Parameters:
footnote
- The Footnote node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentStart | |
public int visitCommentStart(Comment comment)
throws java.lang.Exception |
-
Called when enumeration of a comment text has started.
- Parameters:
comment
- The Comment node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitCommentEnd | |
public int visitCommentEnd(Comment comment)
throws java.lang.Exception |
-
Called when enumeration of a comment text has ended.
- Parameters:
comment
- The Comment node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitShapeStart | |
public int visitShapeStart(Shape shape)
throws java.lang.Exception |
-
Called when enumeration of a shape has started.
- Parameters:
shape
- The Shape node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitShapeEnd | |
public int visitShapeEnd(Shape shape)
throws java.lang.Exception |
-
Called when enumeration of a shape has ended.
- Parameters:
shape
- The Shape node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitGroupShapeStart | |
public int visitGroupShapeStart(GroupShape groupShape)
throws java.lang.Exception |
-
Called when enumeration of a group shape has started.
- Parameters:
groupShape
- The GroupShape node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitGroupShapeEnd | |
public int visitGroupShapeEnd(GroupShape groupShape)
throws java.lang.Exception |
-
Called when enumeration of a group shape has ended.
- Parameters:
groupShape
- The GroupShape node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSpecialChar | |
public int visitSpecialChar(SpecialChar specialChar)
throws java.lang.Exception |
-
Called when a SpecialChar node is encountered in the document.
- Parameters:
specialChar
- The SpecialChar node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSmartTagStart | |
public int visitSmartTagStart(SmartTag smartTag)
throws java.lang.Exception |
-
Called when enumeration of a smart tag has started.
- Parameters:
smartTag
- The SmartTag node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
visitSmartTagEnd | |
public int visitSmartTagEnd(SmartTag smartTag)
throws java.lang.Exception |
-
Called when enumeration of a smart tag has ended.
- Parameters:
smartTag
- The SmartTag node that is being visited.
- Returns:
- A VisitorAction value that specifies how to continue the enumeration.
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.