<c:iterate> over elements [message #60980] |
Tue, 12 May 2009 05:41  |
Eclipse User |
|
|
|
Originally posted by: nickmari77.yahoo.com
Hi Paul,
I have this (part of a) model generated with emf:
<EnumVarTypeData some attributes...">
<allInfos Name="InfoA" other attributes.../>
<allInfos Name="InfoB" other attributes.../>
<allLanguage Type="TypeA" other attributes..."/>
<allLanguage Type="TypeB" other attributes.../>
<allEnumValues>ValA</allEnumValues>
<allEnumValues>ValB</allEnumValues>
<allEnumValues>ValC</allEnumValues>
<allEnumValues>ValD</allEnumValues>
</EnumVarTypeData>
where allEnumValues are serialized as elements.
I can iterate through allInfos and allLanguage with
this a normal iterate tag:
<c:iterate select="$root/allInfos" var="info">
But, how do I iterate allEnumValues? And how do I access ValA, ValB, ...
since allEnumValues doesn't have attributes?
I've found that I can access the first allEnumValues with:
<c:get select="$root/@allEnumValues"/>
and it returns me ValA. But I still miss all the others.
Thank you for your support :)
Nick
|
|
|
|
|
Re: <c:iterate> over elements [message #61099 is a reply to message #61075] |
Tue, 12 May 2009 09:34   |
Eclipse User |
|
|
|
Nick Mari wrote:
>
> <customTag:parseValues source="{$root}"/>
>
> public void doAction(TagInfo arg0, JET2Context context, JET2Writer out)
> throws JET2TagException {
>
> EList<String> allEnumValues = ((EnumVarTypeDefinition) context.getSource())
> .getAllEnumValues();
> for (String string : allEnumValues) {
> out.write("bla bla" + string + "bla bla \n");
> }
> }
>
> The problem now is that writing the output directly with out.write()
> doesn't write the tabs (/t /t ...) as in the templates.
>
How about changing your parseValues tag into an iteratingTag? You'd have
to write two methods: doInitializeLoop(), which would calculate the
EList<String> and create an Iterator from it; and, doEvalLoopCondition,
which would check the iterator, advance it if necessary, and set some
variable to the current loop...
<customTag:parseValues source="$root" var="value">
<c:get select="$value"/>
</customTag:parseValues>
I'd also evaluate the source attribute as an XPath expression...
XPathContextExtender xpc = XPathContextExtender.getInstance(context);
Object result = xpc.resolveAsSingle(getAttribute("source"));
if(!result instanceof EnumVarTypeDefinition) {
throw new JET2TagException("source doesn't resolve to a
EnumVarTypeDefinition");
}
EList<String> allEnumValues =
((EnumVarTypeDefinition)result).getAllEnumValues();
.....
IMPORTANT - check my function names/signatures - I haven't run this
through a compiler, and, as we already know, my in-brain compiler is
unreliable :-)
> Thanks for the support :)
>
> Nick
>
|
|
|
Re: <c:iterate> over elements [message #61196 is a reply to message #61099] |
Wed, 13 May 2009 06:59   |
Eclipse User |
|
|
|
Originally posted by: nickmari77.yahoo.com
Paul Elder wrote:
> Nick Mari wrote:
>>
>> <customTag:parseValues source="{$root}"/>
>>
>> public void doAction(TagInfo arg0, JET2Context context, JET2Writer out)
>> throws JET2TagException {
>>
>> EList<String> allEnumValues = ((EnumVarTypeDefinition) context.getSource())
>> .getAllEnumValues();
>> for (String string : allEnumValues) {
>> out.write("bla bla" + string + "bla bla \n");
>> }
>> }
>>
>> The problem now is that writing the output directly with out.write()
>> doesn't write the tabs (/t /t ...) as in the templates.
>>
>
> How about changing your parseValues tag into an iteratingTag? You'd have
> to write two methods: doInitializeLoop(), which would calculate the
> EList<String> and create an Iterator from it; and, doEvalLoopCondition,
> which would check the iterator, advance it if necessary, and set some
> variable to the current loop...
>
> <customTag:parseValues source="$root" var="value">
> <c:get select="$value"/>
> </customTag:parseValues>
>
I thought about using an iterating tag, but I didn't understand how
to write back without using out.write()... Now I've understood and it
works better. And no more spacing problems ;) Thanks!
>
> I'd also evaluate the source attribute as an XPath expression...
>
> XPathContextExtender xpc = XPathContextExtender.getInstance(context);
> Object result = xpc.resolveAsSingle(getAttribute("source"));
> if(!result instanceof EnumVarTypeDefinition) {
> throw new JET2TagException("source doesn't resolve to a
> EnumVarTypeDefinition");
> }
> EList<String> allEnumValues =
> ((EnumVarTypeDefinition)result).getAllEnumValues();
> ....
>
>
> IMPORTANT - check my function names/signatures - I haven't run this
> through a compiler, and, as we already know, my in-brain compiler is
> unreliable :-)
Your in-brain compiler missed that resolveAsSingle() doesn't exist :P
I think you mean resolveSingle():
XPathContextExtender xpc = XPathContextExtender.getInstance(context);
Object currentXPathContextObject = xpc.currentXPathContextObject();
String attribute = getAttribute("source");
Object result =xpc.resolveSingle(currentXPathContextObject,attribute);
But it doesn't work; problem is that getAttribute("source") returns ""
and not "{$root}".
In plugin.xml the attribute is set as
<attribute
name="source"
type="xpath"
use="required">
</attribute>
The other attribute, var, retruns the correct value using
getAttribute("var")
<attribute
name="var"
type="xpath"
use="required">
</attribute>
And what is the advantage to evaluate the source attribute as xpath
expression?
Thanks!
Nick
|
|
|
Re: <c:iterate> over elements [message #61269 is a reply to message #61196] |
Fri, 15 May 2009 09:14  |
Eclipse User |
|
|
|
Nick Mari wrote:
> Paul Elder wrote:
snip
> Your in-brain compiler missed that resolveAsSingle() doesn't exist :P
> I think you mean resolveSingle():
>
> XPathContextExtender xpc = XPathContextExtender.getInstance(context);
> Object currentXPathContextObject = xpc.currentXPathContextObject();
> String attribute = getAttribute("source");
> Object result =xpc.resolveSingle(currentXPathContextObject,attribute);
>
> But it doesn't work; problem is that getAttribute("source") returns ""
> and not "{$root}".
>
JET treats an XPath expression within {} as a 'dynamic expression'. It
is evaluated before the tag handler sees the result of getAttribute().
IN your case, $root probably refers to the root element of your model.
When converting this to a string, the XPath engine traverses the entire
document, looking for 'text' elements, and concatenates them all together.
You want to set the source attributes as:
<your:tag
source="$root" />
> In plugin.xml the attribute is set as
>
> <attribute
> name="source"
> type="xpath"
> use="required">
> </attribute>
>
This is correct. The type="xpath" attribute has no impact on the runtime
environment. It is provided as a hint to editors that might choose to
provide code assist.
> The other attribute, var, retruns the correct value using
> getAttribute("var")
>
> <attribute
> name="var"
> type="xpath"
> use="required">
> </attribute>
>
A var attribute is, by convention, a name of an XPath variable. I would
set type="string" rather than type="xpath". But, this won't make any
difference to the execution of the tag.
> And what is the advantage to evaluate the source attribute as xpath
> expression?
As stated above, none - its just a hint to any editor that might want to
provide code assist.
>
> Thanks!
>
> Nick
>
|
|
|
Powered by
FUDForum. Page generated in 0.30432 seconds