File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How to get a SimpleTag Parent  inside  Classic nested Tag ( have a look) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How to get a SimpleTag Parent  inside  Classic nested Tag ( have a look)" Watch "How to get a SimpleTag Parent  inside  Classic nested Tag ( have a look)" New topic
Author

How to get a SimpleTag Parent inside Classic nested Tag ( have a look)

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Ranchers,

The trick is disclosed.

Here is the TagAdapter which helps us to have SimpleTag parent for Classic child.

TagAdapter from API:

public class TagAdapter
extends java.lang.Object
implements Tag
Wraps any SimpleTag and exposes it using a Tag interface. This is used to allow collaboration between classic Tag handlers and SimpleTag handlers.

Because SimpleTag does not extend Tag, and because Tag.setParent() only accepts a Tag instance, a classic tag handler (one that implements Tag) cannot have a SimpleTag as its parent.
To remedy this,
a TagAdapter is created to wrap the SimpleTag parent, and the adapter is passed to setParent() instead. (This part is done by container )

A classic Tag Handler can call getAdaptee() to retrieve the encapsulated SimpleTag instance. (we need to do this alone)


All we need to do in Classic child is :

1.TagAdapter tagAdaptor = (TagAdapter)getParent();

2.Parent parent = (Parent)tagAdaptor.getAdaptee();
//getAdaptee returns SimpleTag.

I have tested this and working. I am delighted.
Please let me know once you tested.


[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Srinivasan

I would like to add one more thing.
You can also do it with findAncestorWithClass(this, TagAdapter.class).

The only difference as you can make it out is in using TagAdapter.class instead of...., I think you very well know that.

Thanks for this compact and usefull tutorial

Regards,
Khushhal


rgrds,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Khushhal,


The only difference as you can make it out is in using TagAdapter.class instead of Tag.class, I think you very well know that.


***Note: TagAdapter implements Tag
Thanks for your additions.
[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Srinivasan

Are you sure that, that would be Tag.class at the blank place.

Let me remind you Tag is an interface.
Ya, you can have TagAdapter.class, because TagAdapter is a class that implements Tag interface and includes all the classic life cycle methods.

Please check that out.

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
khushhal yadav,

yes. that works.

It will do test like this "xxxParent.class instanceof Tag.class"
First parent passes that test returns.

but we need to type cast it like this:
Parent parent = (Parent)findAncestorWithClass(this,Tag.class);
[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Thanks Srinivasan

Means we can do that, but we have to do type cast.

But that will first give you the top-most in the hierarchy and than downcast it.
Now suppose there are the two occurances of the class in the hierarchy to which you want it to downcast. To which level it will go. I mean to say whic parent it will select.

I think in this case, it will be opposite of what narmally happens in case of normal findAncestorWithClass(), which gives us the nearest or recent Ancestor parent pertaining to the class.
But in this case we will get the farthest or most Ancestor parent of the class.

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Khushhal,

Here is the API,

public static final Tag findAncestorWithClass(Tag from,
Class klass)

Find the instance of a given class type that is closest to a given instance. This method uses the getParent method from the Tag interface. This method is used for coordination among cooperating tags.

Parameters:
from - The instance from where to start looking.
klass - The subclass of Tag or interface to be matched
Returns:
the nearest ancestor that implements the interface or is an instance of the class specified
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Srinivasan

I know it.

But please read these lines carefully. And try to make out what I am saying.

I think in this case, it will be opposite of what narmally happens in case of normal findAncestorWithClass(), which gives us the nearest or recent Ancestor parent pertaining to the class.
But in this case we will get the farthest or most Ancestor parent of the class.


Means when we use

we get the instance of a given class type that is farthest to a given instance
Not
instance of a given class type that is closest to a given instance
as we get in case of


Is my assertion correct? Please confirm that.

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
khushhal yadav,

***HAPPY INDEPENDENCE DAY FOR INDIANS ALL OVER THE GLOBE.***

I don't how you derived that special behavior out of API.

findAncestorWIthClass is well-defined.

input parameter 1:It we start searching from "this"(*if we say),

input paramerter 2: Then it will start searching from its nearest enclosed tag to check classes which implement interfaces like Tag, TagSupport. and returns the matched, Tag or SimpleTag.

hence we can mention any of these in the second parameter
interface - (Tag.class,SupportTag.class}.
implementing tag class - { new Tag(){ }, new Support(){} }

This is what they mentioned in the API and what the container providers have implemented(* Tested this scenario in WAS 6.1, after you expressed doubt).

Hence you are confused by somebody.
to error is human.
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Srinivasan

I am not confused either by somebody or by myself.
I know all about what's written in specs or books.

But What I am asking is a totally different thing?

Say you have this hierarchy.



corressponding tag-classes are
tag1 - Tag1
tag2 - Tag2
tag3 - Tag3

Now tell me

What does
Tag2 parent = (Tag2)findAncestorWithClass(this,Tag2.class);
will return (1st or 2nd) in terms of hierarchy level?
It will return 1st one, damn sure..

But what about this one
What does
Tag2 parent = (Tag2)findAncestorWithClass(this,Tag.class);
will return (1st or 2nd) in terms of hierarchy level?

Since It will first go up to the top of hierarchy and then will do downcast. So, I think it will be the 2nd one

It's just an assertion...

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
khushhal yadav,

There is no point in continuing this discussion.

your argument is baseless.

Khushhal:Tag2 parent = (Tag2)findAncestorWithClass(this,Tag2.class);
will return (1st or 2nd) in terms of hierarchy level?
It will return 1st one, damn sure..


I am sorry to say this.
[ August 15, 2007: Message edited by: Srinivasan thoyyeti ]
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Srinivasan

But I didn't have any doubt regarding this one.

your argument is baseless.

Tag2 parent = (Tag2)findAncestorWithClass(this,Tag2.class);
will return (1st or 2nd) in terms of hierarchy level?
It will return 1st one, damn sure


It's giving the expected output when executin from Tag3

-----------------------------------------------------------

My assertion was regarding the second one.

But what about this one
What does
Tag2 parent = (Tag2)findAncestorWithClass(this,Tag.class);
will return (1st or 2nd) in terms of hierarchy level?

Since It will first go up to the top of hierarchy and then will do downcast. So, I think it will be the 2nd one
It's just an assertion...



Anyways, now I have tried it by myself. And have got the expected output.

Thanks a lot

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi KLhushhal,

1.It seems some thing wrong with your code.Please check.

2.Please don't doubt the functionality of

finsAncestorWithClass( this, xxx.class);

what ever xxx.class is it will start from this and gives nearest match
(parent instanceof xxx).

This is guaranteed and no bugs in finsAncestorWithClass( this, xxx);


Please check your code or post to me.
I will check.
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Srinivasan

I am not doubting the functionality of

findAncestorWithClass( this, xxx.class);

Nor I denied your statement

I just did put forward an assertion.

I know it gives the nearest match (parent instanceof xxx)

But please see what is xxx in my snippet



It's Tag not Tag1 or Tag2. Right..

And please forget about casting to Tag2 now.
Because it will be performed only after the function findAncestorWithClass() returns.

Now tell me What do you think which one it's going to catch?



Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
I assume these things

1. Tag1,Tag2,Tag3 are classic tags.
2. findAncestorWithClass(this,Tag.class) called from Tag3.

then according to the specification
<mine:tag2> // 1st should be the answer.

If you say no. Please send the code also.
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Yes, That's what I was asking.
And now too getting.

Now everything is clear to me.

Thanks once again for your quick replies

And one more thing, from where are you studying design patterns.

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hey khushhal yadav,

Come on yaar.
oooffffooooooo!
You are also saying the same ?

let it be. design patterns marc peaboy's notes sufficient for the exam.
some key words he has mentioned to identify a pattern and related pattern.

After WCD we will see that link suggested by peaboy..
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Thanks Srinivasan

I have already gone through them.
And hope that would be enough for the exam.

And also hope you will keep coming to my rescue like this in future too.

Regards,
Khushhal
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Khushhal,

Those comments are too heavy for me.

we have to help each other.
and I believe we are doing the same.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get a SimpleTag Parent inside Classic nested Tag ( have a look)