如何识别Eclipse结构化文档中发生变化的XML节点?

发布于 2024-11-18 05:00:34 字数 603 浏览 2 评论 0原文

在我自己的 Eclipse 插件中,我可以使用

IStructuredDocumentListener => 将编辑事件捕获在 IStructuredDocument 中。 void regionChanged(RegionChangedEvent event)

或使用 IModelStateListener => void modelChanged(IStructuredModel model)

但是我看不到这两个方法如何找出 DOM 中的哪个节点已更改。

例如,使用以下文档...

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <element anAttr="fish">blah</element>
</doc>

如果我编辑了 anAttr 的值或 element 的文本节点子节点,我希望获得具有相关 DOM 节点的事件

In my own Eclipse plugin I can trap editing events in an IStructuredDocument with

IStructuredDocumentListener => void regionChanged(RegionChangedEvent event)

or with IModelStateListener => void modelChanged(IStructuredModel model)

But I can't see with either of these how I could find out what node in the DOM has changed.

e.g. with the following document...

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <element anAttr="fish">blah</element>
</doc>

I'd like to get an event with the relevant DOM node if I edited the value of anAttr or the text node child of element

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-11-25 05:00:34

这是我为此目的编写的代码片段:

    final ISourceEditingTextTools sourceEditingTextTools = (ISourceEditingTextTools) this.sourceEditor.getAdapter( ISourceEditingTextTools.class );
    final IDOMSourceEditingTextTools domSourceEditingTextTools = (IDOMSourceEditingTextTools) sourceEditingTextTools;
    final Document document = domSourceEditingTextTools.getDOMDocument();

    final INodeAdapter adapter = new INodeAdapter()
    {
        public boolean isAdapterForType( final Object type )
        {
            return false;
        }

        public void notifyChanged( final INodeNotifier notifier,
                                   final int eventType,
                                   final Object changedFeature,
                                   final Object oldValue,
                                   final Object newValue,
                                   final int pos )
        {
            /* System.err.println( "notifyChanged" );
            System.err.println( "  notifier = " + notifier.getClass().getName() );
            System.err.println( "  eventType = " + eventType );
            System.err.println( "  changedFeature = " + ( changedFeature == null ? "null" : changedFeature.getClass().getName() ) );
            System.err.println( "  oldValue = " + ( oldValue == null ? "null" : oldValue.getClass().getName() ) );
            System.err.println( "  newValue = " + ( newValue == null ? "null" : newValue.getClass().getName() ) );
            System.err.println( "  pos = " + pos ); */

            if( eventType == INodeNotifier.ADD && newValue instanceof IDOMNode )
            {
                addAdapter( (IDOMNode) newValue, this );
            }

            handleXmlNodeChange( (Node) notifier );
        }
    };

    addAdapter( (IDOMNode) document, adapter );

    ...

private static void addAdapter( final IDOMNode node,
                                final INodeAdapter adapter )
{
    node.addAdapter( adapter );

    final NodeList children = node.getChildNodes();

    for( int i = 0, n = children.getLength(); i < n; i++ )
    {
        addAdapter( (IDOMNode) children.item( i ), adapter );
    }
}

Here is a snippet of the code that I wrote for this purpose:

    final ISourceEditingTextTools sourceEditingTextTools = (ISourceEditingTextTools) this.sourceEditor.getAdapter( ISourceEditingTextTools.class );
    final IDOMSourceEditingTextTools domSourceEditingTextTools = (IDOMSourceEditingTextTools) sourceEditingTextTools;
    final Document document = domSourceEditingTextTools.getDOMDocument();

    final INodeAdapter adapter = new INodeAdapter()
    {
        public boolean isAdapterForType( final Object type )
        {
            return false;
        }

        public void notifyChanged( final INodeNotifier notifier,
                                   final int eventType,
                                   final Object changedFeature,
                                   final Object oldValue,
                                   final Object newValue,
                                   final int pos )
        {
            /* System.err.println( "notifyChanged" );
            System.err.println( "  notifier = " + notifier.getClass().getName() );
            System.err.println( "  eventType = " + eventType );
            System.err.println( "  changedFeature = " + ( changedFeature == null ? "null" : changedFeature.getClass().getName() ) );
            System.err.println( "  oldValue = " + ( oldValue == null ? "null" : oldValue.getClass().getName() ) );
            System.err.println( "  newValue = " + ( newValue == null ? "null" : newValue.getClass().getName() ) );
            System.err.println( "  pos = " + pos ); */

            if( eventType == INodeNotifier.ADD && newValue instanceof IDOMNode )
            {
                addAdapter( (IDOMNode) newValue, this );
            }

            handleXmlNodeChange( (Node) notifier );
        }
    };

    addAdapter( (IDOMNode) document, adapter );

    ...

private static void addAdapter( final IDOMNode node,
                                final INodeAdapter adapter )
{
    node.addAdapter( adapter );

    final NodeList children = node.getChildNodes();

    for( int i = 0, n = children.getLength(); i < n; i++ )
    {
        addAdapter( (IDOMNode) children.item( i ), adapter );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文