Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sapphire » Editing XML without namespaces or attributes - just value
Editing XML without namespaces or attributes - just value [message #719311] Fri, 26 August 2011 16:48 Go to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
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 #719341 is a reply to message #719311] Fri, 26 August 2011 18:39 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
With the config I have now I'm ending up with this:


<depends>
<depends>depends1</depends>
</depends>
<depends>
<depends>depends2</depends>
</depends>

And I want this:

<depends>depends1</depends>
<depends>depends2</depends>

And my annotated interface looks like this:

public interface IDepends extends IModelElement {

ModelElementType TYPE = new ModelElementType( IDepends.class );

// *** optional-attribute-name ***

@Label( standard = "name (optional)", full = "optional-attribute-name" )
@XmlBinding( path = "@optional-attribute-name" )

ValueProperty PROP_OPT_NAME = new ValueProperty( TYPE, "OptName" );

Value<String> getOptName();
void setOptName( String name );

// *** Name ***

@XmlBinding( path = "depends" )
@Label( standard = "Value" )

ValueProperty PROP_VALUE = new ValueProperty( TYPE, "Value" );

Value<String> getValue();
void setValue( String value );
}

Obviously I'm doing something wrong Smile

--Fitz
Re: Editing XML without namespaces or attributes - just value [message #719519 is a reply to message #719341] Sat, 27 August 2011 15:41 Go to previous messageGo to next message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
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 Sad, anyways thats apart, hope this helps you.

~Kamesh

Re: Editing XML without namespaces or attributes - just value [message #719590 is a reply to message #719519] Sun, 28 August 2011 01:53 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
Hey Brian,

Good to see that you are back playing with Sapphire!

The @CustomXmlValueBinding facility is always an option if you cannot solve the problem declaratively, but in this case there is a simpler solution. Simply replace @XmlBinding( path = "depends" ) on the Value property with @XmlBinding( path = "" ).

- Konstantin
Re: Editing XML without namespaces or attributes - just value [message #720479 is a reply to message #719590] Tue, 30 August 2011 17:05 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Yeah, I've been drawn and quartered lately with competing priorities. Happy to be back doing more coding with Sapphire. Smile

And I *love* simple solutions and that worked perfectly. Thanks Konstantin!

(And Kamesh, I'm stashing your custom solution for later because I'm sure it'll come in handy as well.)

Thanks guys!
--Fitz
Previous Topic:Using Sapphire to Work with Java Types
Next Topic:Next question - custom editing and hidden attributes
Goto Forum:
  


Current Time: Mon Sep 22 14:22:31 GMT 2014

Powered by FUDForum. Page generated in 0.03626 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software