You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by ml...@mherrn.de on 2012/06/21 15:28:31 UTC

"high-level" way to create SVG Elements of specific type?

Hi,

I am searching for a way to create SVG Elements that provide higher
level methods for setting values.

For example an SVG rect element is represented by the interface
SVGRectElement. This interface provides "higher level" methods to
access its attributes, for example:
 * getX()
 * getY()
 * getWidth()
 * getHeight()

This is quite fine when trying to access existing SVG elements (by
just casting it to the relevant Interface). But what if I need to
_create_ such elements? The (uncomfortable and not very typesafe)
way is by creating an org.w3c.dom.Element:

-------------------------/--------------------------
final Element element= document.createElement("rect");
element.setAttribute("x", "10");
element.setAttribute("y", "10");
element.setAttribute("width", "100");
element.setAttribute("height", "100");
document.getDocumentElement().appendChild(element);
-------------------------/--------------------------

What I am searching for is a way to create a rect like this:

-------------------------/--------------------------
final SVGRect rectElement= ..... //create the element
rectElement.setX(10);
rectElement.setY(10);
rectElement.setWidth(100);
rectElement.setHeight(100);
document.getDocumentElement().appendChild(rectElement);
-------------------------/--------------------------

The interface SVGRect provides the methods I want to use.
SVGSVGElement provides the method createSVGRect():SVGRect
Unfortunately this SVGRect doesn't implement SVGRectElement, so adding
this to the DOM via appendChild() leads to an exception.

I also found the class org.apache.batik.dom.svg.SVGOMRectElement,
but have no idea what this is used for. Anyway, this class also
only has getters, but no setters. Therefore is of no use for me.

Also there is the class
org.apache.batik.dom.svg.SVGDOMImplementation.RectElementFactory.
However, it is protected and can therefore not be used by me.

So what should I do? Is there a way to convert an instance of an SVGRect
to an SVGRectElement? Is there another way have these
convenient-methods for setting attributes to specific SVG element types?

Regards
Marco


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: "high-level" way to create SVG Elements of specific type?

Posted by ml...@mherrn.de.
> Hi Marco,
>    I think you are wrong.  You can do something like:
>      rect.x.baseVal.value = 10
> or
>     rect.x.baseVal.newValueSpecifiedUnits(SVG_LENGTHTYPE_PX, 10)
>
>    The raises specifications in the IDL are because some instances of
> SVGAnimatedLength
> and SVGLength are readonly, so attempts to set those will result in
> exceptions.

Ah thanks! I must have overlooked this. Seems to me like a good way.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: "high-level" way to create SVG Elements of specific type?

Posted by Thomas DeWeese <th...@gmail.com>.
Hi Marco,
   I think you are wrong.  You can do something like:
     rect.x.baseVal.value = 10
or
    rect.x.baseVal.newValueSpecifiedUnits(SVG_LENGTHTYPE_PX, 10)

   The raises specifications in the IDL are because some instances of
SVGAnimatedLength
and SVGLength are readonly, so attempts to set those will result in
exceptions.

    Thomas

On Thu, Jun 28, 2012 at 7:26 AM, <ml...@mherrn.de> wrote:

> > The SVG rect element makes x, y, width & height available as SVG Animated
> > lengths.  These are a bit more complex but can be used for this.
>
> Yes, but it only provides getters for these attributes. No setters.
>
> Regards
> Marco
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: "high-level" way to create SVG Elements of specific type?

Posted by ml...@mherrn.de.
> The SVG rect element makes x, y, width & height available as SVG Animated
> lengths.  These are a bit more complex but can be used for this.

Yes, but it only provides getters for these attributes. No setters.

Regards
Marco


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: "high-level" way to create SVG Elements of specific type?

Posted by Thomas DeWeese <th...@gmail.com>.
The SVG rect element makes x, y, width & height available as SVG Animated lengths.  These are a bit more complex but can be used for this.



On Jun 22, 2012, at 8:12 AM, ml@mherrn.de wrote:

> Hi Thomas,
> 
>>> final Element element= document.createElement("rect");
>> 
>>    This creates a 'rect' element in the no name namespace.
>> I think you want to use createElementNS with the SVG Namespace.
>> If you are using a Batik SVGDocument then this will return an
>> SVGRectElement.
> 
> Yes, actually I am doing it that way and just ommited it from my example.
> I didn't know that batik actually creates an SVGRectElement out of it.
> So it is enough to just cast the created element to the relevant
> interface? e.g.:
> 
> final SVGRectElement rect= (SVGRectElement) document.createElementNS(ns,
> "rect");
> 
> But that gives me an SVGRectElement that has the getters for the specific
> attributes. Is there a way to get a class that provides the setters as in
> my second (hypothetical) example?:
> 
> -------------------------/--------------------------
> final SVGRect rectElement= ..... //create the element
> rectElement.setX(10);
> rectElement.setY(10);
> rectElement.setWidth(100);
> rectElement.setHeight(100);
> document.getDocumentElement().appendChild(rectElement);
> -------------------------/--------------------------
> 
>> Make sure you read about booting the CSS and
>> SVG DOM on the Wiki, otherwise some of the more advanced SVG
>> methods won't work (also the element needs to be part of the SVG
>> tree for some of those methods to work).
> 
> I will have a look at the wiki again. Quite possible that I have
> overlooked some important info there.
> 
> 
> Thanks and regards
> Marco
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: "high-level" way to create SVG Elements of specific type?

Posted by ml...@mherrn.de.
Hi Thomas,

>> final Element element= document.createElement("rect");
>
> 	This creates a 'rect' element in the no name namespace.
> I think you want to use createElementNS with the SVG Namespace.
> If you are using a Batik SVGDocument then this will return an
> SVGRectElement.

Yes, actually I am doing it that way and just ommited it from my example.
I didn't know that batik actually creates an SVGRectElement out of it.
So it is enough to just cast the created element to the relevant
interface? e.g.:

final SVGRectElement rect= (SVGRectElement) document.createElementNS(ns,
"rect");

But that gives me an SVGRectElement that has the getters for the specific
attributes. Is there a way to get a class that provides the setters as in
my second (hypothetical) example?:

-------------------------/--------------------------
final SVGRect rectElement= ..... //create the element
rectElement.setX(10);
rectElement.setY(10);
rectElement.setWidth(100);
rectElement.setHeight(100);
document.getDocumentElement().appendChild(rectElement);
-------------------------/--------------------------

> Make sure you read about booting the CSS and
> SVG DOM on the Wiki, otherwise some of the more advanced SVG
> methods won't work (also the element needs to be part of the SVG
> tree for some of those methods to work).

I will have a look at the wiki again. Quite possible that I have
overlooked some important info there.


Thanks and regards
Marco


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: "high-level" way to create SVG Elements of specific type?

Posted by DeWeese Thomas <th...@gmail.com>.
Hi Marco,

	Replies inline below...

On Jun 21, 2012, at 9:28 AM, ml@mherrn.de wrote:

> I am searching for a way to create SVG Elements that provide higher
> level methods for setting values.
> 
> For example an SVG rect element is represented by the interface
> SVGRectElement. This interface provides "higher level" methods to
> access its attributes, for example:
> * getX()
> * getY()
> * getWidth()
> * getHeight()
> 
> This is quite fine when trying to access existing SVG elements (by
> just casting it to the relevant Interface). But what if I need to
> _create_ such elements? The (uncomfortable and not very typesafe)
> way is by creating an org.w3c.dom.Element:
> 
> -------------------------/--------------------------
> final Element element= document.createElement("rect");
	
	This creates a 'rect' element in the no name namespace.
I think you want to use createElementNS with the SVG Namespace.
If you are using a Batik SVGDocument then this will return an
SVGRectElement.  Make sure you read about booting the CSS and
SVG DOM on the Wiki, otherwise some of the more advanced SVG
methods won't work (also the element needs to be part of the SVG 
tree for some of those methods to work).

> element.setAttribute("x", "10");
> element.setAttribute("y", "10");
> element.setAttribute("width", "100");
> element.setAttribute("height", "100");
> document.getDocumentElement().appendChild(element);
> -------------------------/--------------------------
> 
> What I am searching for is a way to create a rect like this:
> 
> -------------------------/--------------------------
> final SVGRect rectElement= ..... //create the element
> rectElement.setX(10);
> rectElement.setY(10);
> rectElement.setWidth(100);
> rectElement.setHeight(100);
> document.getDocumentElement().appendChild(rectElement);
> -------------------------/--------------------------
> 
> The interface SVGRect provides the methods I want to use.
> SVGSVGElement provides the method createSVGRect():SVGRect
> Unfortunately this SVGRect doesn't implement SVGRectElement, so adding
> this to the DOM via appendChild() leads to an exception.
> 
> I also found the class org.apache.batik.dom.svg.SVGOMRectElement,
> but have no idea what this is used for. Anyway, this class also
> only has getters, but no setters. Therefore is of no use for me.
> 
> Also there is the class
> org.apache.batik.dom.svg.SVGDOMImplementation.RectElementFactory.
> However, it is protected and can therefore not be used by me.
> 
> So what should I do? Is there a way to convert an instance of an SVGRect
> to an SVGRectElement? Is there another way have these
> convenient-methods for setting attributes to specific SVG element types?
> 
> Regards
> Marco
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org