NodeChangingArgs
Inheritance: java.lang.Object
public class NodeChangingArgs
Provides data for methods of the INodeChangingCallback interface.
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Examples:
Shows how customize node changing with a callback.
public void fontChangeViaCallback() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set the node changing callback to custom implementation,
// then add/remove nodes to get it to generate a log.
HandleNodeChangingFontChanger callback = new HandleNodeChangingFontChanger();
doc.setNodeChangingCallback(callback);
builder.writeln("Hello world!");
builder.writeln("Hello again!");
builder.insertField(" HYPERLINK \"https://www.google.com/\" ");
builder.insertShape(ShapeType.RECTANGLE, 300.0, 300.0);
doc.getRange().getFields().get(0).remove();
System.out.println(callback.getLog());
}
///
/// Logs the date and time of each node insertion and removal.
/// Sets a custom font name/size for the text contents of Run nodes.
///
public static class HandleNodeChangingFontChanger implements INodeChangingCallback {
public void nodeInserted(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
mLog.append(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode()));
if (args.getNode().getNodeType() == NodeType.RUN) {
Font font = ((Run) args.getNode()).getFont();
mLog.append(MessageFormat.format("\tFont:\tChanged from \"{0}\" {1}pt", font.getName(), font.getSize()));
font.setSize(24.0);
font.setName("Arial");
mLog.append(MessageFormat.format(" to \"{0}\" {1}pt", font.getName(), font.getSize()));
mLog.append(MessageFormat.format("\tContents:\n\t\t\"{0}\"", args.getNode().getText()));
}
}
public void nodeInserting(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\n{0}\tNode insertion:", new Date()));
}
public void nodeRemoved(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
mLog.append(MessageFormat.format("\tHash code:\t{0}", args.getNode().hashCode()));
}
public void nodeRemoving(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\n{0}\tNode removal:", new Date()));
}
public String getLog() {
return mLog.toString();
}
private final StringBuilder mLog = new StringBuilder();
}
Methods
Method | Description |
---|---|
getAction() | Gets a value indicating what type of node change event is occurring. |
getNewParent() | Gets the node’s parent that will be set after the operation completes. |
getNode() | Gets the getNode() that is being added or removed. |
getOldParent() | Gets the node’s parent before the operation began. |
getAction()
public int getAction()
Gets a value indicating what type of node change event is occurring.
Examples:
Shows how to use a NodeChangingCallback to monitor changes to the document tree in real-time as we edit it.
public void nodeChangingCallback() throws Exception {
Document doc = new Document();
doc.setNodeChangingCallback(new NodeChangingPrinter());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
builder.startTable();
builder.insertCell();
builder.write("Cell 1");
builder.insertCell();
builder.write("Cell 2");
builder.endTable();
builder.insertImage(getImageDir() + "Logo.jpg");
builder.getCurrentParagraph().getParentNode().removeAllChildren();
}
///
/// Prints every node insertion/removal as it takes place in the document.
///
private static class NodeChangingPrinter implements INodeChangingCallback {
public void nodeInserting(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertEquals(args.getOldParent(), null);
}
public void nodeInserted(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertNotNull(args.getNewParent());
System.out.println("Inserted node:");
System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
if (!"".equals(args.getNode().getText().trim())) {
System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim()));
}
System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode()));
System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode()));
}
public void nodeRemoving(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
}
public void nodeRemoved(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
Assert.assertNull(args.getNewParent());
System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode()));
}
}
Returns: int - A value indicating what type of node change event is occurring. The returned value is one of NodeChangingAction constants.
getNewParent()
public Node getNewParent()
Gets the node’s parent that will be set after the operation completes.
Examples:
Shows how to use a NodeChangingCallback to monitor changes to the document tree in real-time as we edit it.
public void nodeChangingCallback() throws Exception {
Document doc = new Document();
doc.setNodeChangingCallback(new NodeChangingPrinter());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
builder.startTable();
builder.insertCell();
builder.write("Cell 1");
builder.insertCell();
builder.write("Cell 2");
builder.endTable();
builder.insertImage(getImageDir() + "Logo.jpg");
builder.getCurrentParagraph().getParentNode().removeAllChildren();
}
///
/// Prints every node insertion/removal as it takes place in the document.
///
private static class NodeChangingPrinter implements INodeChangingCallback {
public void nodeInserting(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertEquals(args.getOldParent(), null);
}
public void nodeInserted(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertNotNull(args.getNewParent());
System.out.println("Inserted node:");
System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
if (!"".equals(args.getNode().getText().trim())) {
System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim()));
}
System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode()));
System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode()));
}
public void nodeRemoving(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
}
public void nodeRemoved(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
Assert.assertNull(args.getNewParent());
System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode()));
}
}
Returns: Node - The node’s parent that will be set after the operation completes.
getNode()
public Node getNode()
Gets the getNode() that is being added or removed.
Examples:
Shows how customize node changing with a callback.
public void fontChangeViaCallback() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set the node changing callback to custom implementation,
// then add/remove nodes to get it to generate a log.
HandleNodeChangingFontChanger callback = new HandleNodeChangingFontChanger();
doc.setNodeChangingCallback(callback);
builder.writeln("Hello world!");
builder.writeln("Hello again!");
builder.insertField(" HYPERLINK \"https://www.google.com/\" ");
builder.insertShape(ShapeType.RECTANGLE, 300.0, 300.0);
doc.getRange().getFields().get(0).remove();
System.out.println(callback.getLog());
}
///
/// Logs the date and time of each node insertion and removal.
/// Sets a custom font name/size for the text contents of Run nodes.
///
public static class HandleNodeChangingFontChanger implements INodeChangingCallback {
public void nodeInserted(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
mLog.append(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode()));
if (args.getNode().getNodeType() == NodeType.RUN) {
Font font = ((Run) args.getNode()).getFont();
mLog.append(MessageFormat.format("\tFont:\tChanged from \"{0}\" {1}pt", font.getName(), font.getSize()));
font.setSize(24.0);
font.setName("Arial");
mLog.append(MessageFormat.format(" to \"{0}\" {1}pt", font.getName(), font.getSize()));
mLog.append(MessageFormat.format("\tContents:\n\t\t\"{0}\"", args.getNode().getText()));
}
}
public void nodeInserting(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\n{0}\tNode insertion:", new Date()));
}
public void nodeRemoved(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
mLog.append(MessageFormat.format("\tHash code:\t{0}", args.getNode().hashCode()));
}
public void nodeRemoving(NodeChangingArgs args) {
mLog.append(MessageFormat.format("\n{0}\tNode removal:", new Date()));
}
public String getLog() {
return mLog.toString();
}
private final StringBuilder mLog = new StringBuilder();
}
Returns: Node - The getNode() that is being added or removed.
getOldParent()
public Node getOldParent()
Gets the node’s parent before the operation began.
Examples:
Shows how to use a NodeChangingCallback to monitor changes to the document tree in real-time as we edit it.
public void nodeChangingCallback() throws Exception {
Document doc = new Document();
doc.setNodeChangingCallback(new NodeChangingPrinter());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
builder.startTable();
builder.insertCell();
builder.write("Cell 1");
builder.insertCell();
builder.write("Cell 2");
builder.endTable();
builder.insertImage(getImageDir() + "Logo.jpg");
builder.getCurrentParagraph().getParentNode().removeAllChildren();
}
///
/// Prints every node insertion/removal as it takes place in the document.
///
private static class NodeChangingPrinter implements INodeChangingCallback {
public void nodeInserting(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertEquals(args.getOldParent(), null);
}
public void nodeInserted(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.INSERT);
Assert.assertNotNull(args.getNewParent());
System.out.println("Inserted node:");
System.out.println(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType()));
if (!"".equals(args.getNode().getText().trim())) {
System.out.println(MessageFormat.format("\tText:\t\"{0}\"", args.getNode().getText().trim()));
}
System.out.println(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode()));
System.out.println(MessageFormat.format("\tParent:\t{0} ({1})", args.getNewParent().getNodeType(), args.getNewParent().hashCode()));
}
public void nodeRemoving(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
}
public void nodeRemoved(NodeChangingArgs args) {
Assert.assertEquals(args.getAction(), NodeChangingAction.REMOVE);
Assert.assertNull(args.getNewParent());
System.out.println(MessageFormat.format("Removed node: {0} ({1})", args.getNode().getNodeType(), args.getNode().hashCode()));
}
}
Returns: Node - The node’s parent before the operation began.