1

Let's take a simple scenario of HTML5 tag. A <html> tag can only permit one <head> and one <body> tag. Suppose we have a HTMLUIComponent, HTMLHeadUIComponent, HTMLBodyUIComponent represent each HTML tags specified above respectively, how would I allow that the HTMLHeadUIComponent and HTMLBodyUIComponent be rendered only inside a HTMLUIComponent and not in other component?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228

1 Answers1

1

Easiest way would be to just do something like this during encodeBegin():

if (!(component.getParent() instanceof HTMLUIComponent)) {
    throw new IllegalArgumentException("HTMLHeadUIComponent must have a parent of type HTMLUIComponent");
}

If you need to do this during view build time (instead of view render time), then you can't go around creating and adding a tag handler for the component and do the check over there in apply() method.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC! Can you please elaborate on your last paragraph, if you please? – Buhake Sindi Jun 30 '13 at 20:36
  • Here's a Hello World tag handler: http://stackoverflow.com/a/15007720. Register it as `` inside `` entry of `your.taglib.xml`. Or if your concrete question actually concerns "view build time" vs "view render time", then head to this answer: http://stackoverflow.com/a/3343681/. Reason is, throwing an exception during view render time may not necessarily end op in an error page as there's risk that the response is already committed at that point (but it will end up in log). I would however not worry about this, the developer would immediately see it during development. – BalusC Jun 30 '13 at 20:48
  • Thanks so much! That made a lot of sense... :-) – Buhake Sindi Jun 30 '13 at 21:12
  • My apologies for asking this: How do you enforce the parent `UIComponent` have specific child `UIComponent`? Is it during `encodeChildren()` on the `Renderer`? – Buhake Sindi Jun 30 '13 at 22:42
  • That would indeed make the most sense. It does not necessarily need to happen in the renderer, it can also be checked in the `UIComponent` itself (which was what my initial answer assumed, before you edited it). It just depends on where you'd like to put the responsibility. Components can be used with different renderers. One could develop a renderer where this isn't checked. If this requirement of a specific hierarchy is at component level, then it'd make more sense to check that in the component iself rather than the renderer. See also the `` component of OmniFaces as an example. – BalusC Jul 01 '13 at 01:24