com.aspose.words

Class NodeChangingArgs

  • java.lang.Object
    • com.aspose.words.NodeChangingArgs
public class NodeChangingArgs 
extends java.lang.Object

Provides data for methods of the INodeChangingCallback interface.

Example:

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());
}

/// <summary>
/// Logs the date and time of each node insertion and removal,
/// and also set a custom font name/size for the text contents of Run nodes.
/// </summary>
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();
}
See Also:
DocumentBase, INodeChangingCallback, NodeChangingAction

Property Getters/Setters Summary
intgetAction()
Gets a value indicating what type of node change event is occurring. The value of the property is NodeChangingAction integer constant.
NodegetNewParent()
Gets the node's parent that will be set after the operation completes.
NodegetNode()
Gets the Node that is being added or removed.
NodegetOldParent()
Gets the node's parent before the operation began.
 

    • Property Getters/Setters Detail

      • getAction

        public int getAction()
        
        Gets a value indicating what type of node change event is occurring. The value of the property is NodeChangingAction integer constant.

        Example:

        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();
        }
        
        /// <summary>
        /// Prints every node insertion/removal as it takes place in the document.
        /// </summary>
        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()));
            }
        }
      • getNewParent

        public Node getNewParent()
        
        Gets the node's parent that will be set after the operation completes.

        Example:

        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();
        }
        
        /// <summary>
        /// Prints every node insertion/removal as it takes place in the document.
        /// </summary>
        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()));
            }
        }
      • getNode

        public Node getNode()
        
        Gets the Node that is being added or removed.

        Example:

        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());
        }
        
        /// <summary>
        /// Logs the date and time of each node insertion and removal,
        /// and also set a custom font name/size for the text contents of Run nodes.
        /// </summary>
        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();
        }
      • getOldParent

        public Node getOldParent()
        
        Gets the node's parent before the operation began.

        Example:

        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();
        }
        
        /// <summary>
        /// Prints every node insertion/removal as it takes place in the document.
        /// </summary>
        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()));
            }
        }