Editing XML without namespaces or attributes - just value [message #719311] |
Fri, 26 August 2011 12:48  |
Eclipse User |
|
|
|
Hi guys...
Wow it's been a while since I've been back to playing with Sapphire. And you guys have done a ton of work since then - and the progress just keeps chugging along!
So I have a question...
I have a dumb (i.e. non-namespace aware, no XSD) configuration file I'm trying to develop an editor for in Sapphire. So far so good - haven't run across anything big or too wacky.
But I have an element called "depends" (no adult undergarment jokes please) that has an optional attribute and the element text, that's it. And there's usually two of these but I'm modeling it so folks can have zero or more. Here are a couple of examples:
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
The optional attribute I can do fine (it needs some tweaking, but it works for now). But how do I (using annotations) mark it up so that I can modify the contents of the tag, not an attribute or whatnot. I'm sure I'm just overlooking something simple here.
Can somebody point me to an example?
Thanks!
--Fitz
|
|
|
|
Re: Editing XML without namespaces or attributes - just value [message #719519 is a reply to message #719341] |
Sat, 27 August 2011 11:41   |
Eclipse User |
|
|
|
Hey Fitz
You aren't doing anything wrong. By default Sapphire treats all properties child elements (Element Node) and add them as childNodes (Element Node) hence you are having this issue. We need to tell Sapphire to add it as "Text Node" rather than "ElementNode". To do that we need to provide a @CustomXmlValueBinding
Here is one such implementation I used in one of my implementation,
import org.eclipse.sapphire.modeling.IModelElement;
import org.eclipse.sapphire.modeling.ModelProperty;
import org.eclipse.sapphire.modeling.xml.XmlElement;
import org.eclipse.sapphire.modeling.xml.XmlNamespaceResolver;
import org.eclipse.sapphire.modeling.xml.XmlNode;
import org.eclipse.sapphire.modeling.xml.XmlPath;
import org.eclipse.sapphire.modeling.xml.XmlValueBindingImpl;
/**
* @author kamesh.sampath
*/
public final class TextNodeValueBinding
extends XmlValueBindingImpl
{
private XmlPath path;
/*
* (non-Javadoc)
* @see org.eclipse.sapphire.modeling.BindingImpl#init(org.eclipse.sapphire.modeling.IModelElement,
* org.eclipse.sapphire.modeling.ModelProperty, java.lang.String[])
*/
@Override
public void init( final IModelElement element, final ModelProperty property, final String[] params ) {
super.init( element, property, params );
final XmlNamespaceResolver xmlNamespaceResolver = resource().getXmlNamespaceResolver();
this.path = new XmlPath( params[0], xmlNamespaceResolver );
// System.out.println( "TextNodeValueBinding.init()" + this.path );
}
/*
* (non-Javadoc)
* @see org.eclipse.sapphire.modeling.ValueBindingImpl#read()
*/
@Override
public String read() {
String value = null;
final XmlElement element = xml( false );
if ( element != null ) {
value = xml( true ).getText();
// System.out.println( "Reading VALUE ___________________ " + value );
if ( value != null ) {
value = value.trim();
}
}
return value;
}
/*
* (non-Javadoc)
* @see org.eclipse.sapphire.modeling.ValueBindingImpl#write(java.lang.String)
*/
@Override
public void write( final String value ) {
String val = value;
// System.out.println( "VALUE ___________________ " + val );
if ( val != null ) {
val = value.trim();
}
// System.out.println( "TextNodeValueBinding.write() - Parent " + xml( true ).getParent() );
xml( true ).setText( val );
}
/*
* (non-Javadoc)
* @see org.eclipse.sapphire.modeling.xml.XmlValueBindingImpl#getXmlNode()
*/
@Override
public XmlNode getXmlNode() {
final XmlElement element = xml( false );
if ( element != null ) {
return element.getChildNode( this.path, false );
}
return null;
}
}
I would rather attribute it to the wrong XML Schema or DTD design , anyways thats apart, hope this helps you.
~Kamesh
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04385 seconds