How to identify XML node that has changed in Eclipse structured document?
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开发者_StackOverflow中文版 like to get an event with the relevant DOM node if I edited the value of anAttr or the text node child of element
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 );
}
}
精彩评论