/** * Search the child's markup in the header section of the markup * * @param container * @param child * @return Null, if not found */ public IMarkupFragment findMarkupInAssociatedFileHeader( final MarkupContainer container, final Component child) { // Get the associated markup IMarkupFragment markup = container.getAssociatedMarkup(); IMarkupFragment childMarkup = null; // MarkupStream is good at searching markup MarkupStream stream = new MarkupStream(markup); while (stream.skipUntil(ComponentTag.class) && (childMarkup == null)) { ComponentTag tag = stream.getTag(); if (TagUtils.isWicketHeadTag(tag)) { if (tag.getMarkupClass() == null) { // find() can still fail an return null => continue the search childMarkup = stream.getMarkupFragment().find(child.getId()); } } else if (TagUtils.isHeadTag(tag)) { // find() can still fail an return null => continue the search childMarkup = stream.getMarkupFragment().find(child.getId()); } // Must be a direct child. We are not interested in grand children if (tag.isOpen() && !tag.hasNoCloseTag()) { stream.skipToMatchingCloseTag(tag); } stream.next(); } return childMarkup; }
/** * Handle tag <head> * * @param tag */ private void handleHeadTag(ComponentTag tag) { // we found <head> if (tag.isOpen()) { if (foundHead) { throw new MarkupException( new MarkupStream(markup), "Tag <head> is not allowed at this position (do you have multiple <head> tags in your markup?)."); } foundHead = true; if (tag.getId() == null) { tag.setId(HEADER_ID); tag.setAutoComponentTag(true); tag.setModified(true); } } else if (tag.isClose()) { if (foundHeaderItemsTag) { // revert the settings from above ComponentTag headOpenTag = tag.getOpenTag(); // change the id because it is special. See HtmlHeaderResolver headOpenTag.setId(HEADER_ID + "-Ignored"); headOpenTag.setAutoComponentTag(false); headOpenTag.setModified(false); headOpenTag.setFlag(ComponentTag.RENDER_RAW, true); } foundClosingHead = true; } }
/** * Resolves the child component which is the controller of this Enclosure * * @param markupStream the markup stream of this Enclosure * @param enclosureParent the non-auto parent component of this Enclosure * @return The component associated with the {@linkplain #childId} */ private Component getChildComponent( final MarkupStream markupStream, MarkupContainer enclosureParent) { String fullChildId = getChildId(); Component controller = enclosureParent.get(fullChildId); if (controller == null) { int orgIndex = markupStream.getCurrentIndex(); try { while (markupStream.hasMore()) { markupStream.next(); if (markupStream.skipUntil(ComponentTag.class)) { ComponentTag tag = markupStream.getTag(); if ((tag != null) && (tag.isOpen() || tag.isOpenClose())) { String tagId = tag.getId(); if (fullChildId.equals(tagId)) { ComponentTag fullComponentTag = new ComponentTag(tag); fullComponentTag.setId(childId.toString()); controller = ComponentResolvers.resolve( enclosureParent, markupStream, fullComponentTag, new ResolverFilter() { @Override public boolean ignoreResolver(final IComponentResolver resolver) { return resolver instanceof EnclosureHandler; } }); break; } else if (fullChildId.startsWith(tagId + PATH_SEPARATOR)) { fullChildId = Strings.afterFirst(fullChildId, PATH_SEPARATOR); } } } } } finally { markupStream.setCurrentIndex(orgIndex); } } checkChildComponent(controller); return controller; }
/** * Process next header markup fragment. * * @param associatedMarkupStream * @return index or -1 when done */ private int nextHeaderMarkup(final MarkupStream associatedMarkupStream) { // No associated markup => no header section if (associatedMarkupStream == null) { return -1; } // Scan the markup for <wicket:head>. MarkupElement elem = associatedMarkupStream.get(); while (elem != null) { if (elem instanceof WicketTag) { WicketTag tag = (WicketTag) elem; if (tag.isOpen() && tag.isHeadTag()) { if (noMoreWicketHeadTagsAllowed == true) { throw new MarkupException( "<wicket:head> tags are only allowed before <body>, </head>, <wicket:panel> etc. tag"); } return associatedMarkupStream.getCurrentIndex(); } // wicket:head must be before border, panel or extend // @TODO why is that? Why can't it be anywhere? (except inside wicket:fragment) else if (tag.isOpen() && (tag.isPanelTag() || tag.isBorderTag() || tag.isExtendTag())) { noMoreWicketHeadTagsAllowed = true; } } else if (elem instanceof ComponentTag) { ComponentTag tag = (ComponentTag) elem; // wicket:head must be before </head> // @TODO why?? if (tag.isClose() && TagUtils.isHeadTag(tag)) { noMoreWicketHeadTagsAllowed = true; } // wicket:head must be before <body> // @TODO why?? else if (tag.isOpen() && TagUtils.isBodyTag(tag)) { noMoreWicketHeadTagsAllowed = true; } } elem = associatedMarkupStream.next(); } // No (more) wicket:head found return -1; }
/** * Search for <wicket:panel ...> on the same level. * * @param markup * @return null, if not found */ private final IMarkupFragment findStartTag(final IMarkupFragment markup) { MarkupStream stream = new MarkupStream(markup); while (stream.skipUntil(ComponentTag.class)) { ComponentTag tag = stream.getTag(); if (tag.isOpen() || tag.isOpenClose()) { if (tag instanceof WicketTag) { WicketTag wtag = (WicketTag) tag; if (tagName.equalsIgnoreCase(wtag.getName())) { return stream.getMarkupFragment(); } } stream.skipToMatchingCloseTag(tag); } stream.next(); } return null; }
@Override public IMarkupFragment getMarkup() { if (getParent() == null) { throw new WicketRuntimeException( "Bug: The Wicket internal instance of HtmlHeaderContainer is not connected to a parent"); } // Get the page markup IMarkupFragment markup = getPage().getMarkup(); if (markup == null) { throw new MarkupException("Unable to get page markup: " + getPage().toString()); } // Find the markup fragment MarkupStream stream = new MarkupStream(markup); IMarkupFragment headerMarkup = null; while (stream.skipUntil(ComponentTag.class) && (headerMarkup == null)) { ComponentTag tag = stream.getTag(); if (tag.isOpen() || tag.isOpenClose()) { if (tag instanceof WicketTag) { WicketTag wtag = (WicketTag) tag; if (wtag.isHeadTag()) { if (tag.getMarkupClass() == null) { headerMarkup = stream.getMarkupFragment(); } } } else if (tag.getName().equalsIgnoreCase("head")) { headerMarkup = stream.getMarkupFragment(); } } stream.next(); } setMarkup(headerMarkup); return headerMarkup; }
@Override protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException { // We only need ComponentTags if (tag instanceof WicketTag) { return tag; } // Has wicket:enclosure attribute? String enclosureAttr = getAttribute(tag, null); if (enclosureAttr != null) { if (tag.isOpen()) { // Make sure 'wicket:id' and 'id' are consistent String htmlId = tag.getAttribute("id"); if ((tag.getId() != null) && !Strings.isEmpty(htmlId) && !htmlId.equals(tag.getId())) { throw new ParseException( "Make sure that 'id' and 'wicket:id' are the same if both are provided. Tag:" + tag.toString(), tag.getPos()); } // if it doesn't have a wicket-id already, then assign one now. if (Strings.isEmpty(tag.getId())) { if (Strings.isEmpty(htmlId)) { String id = getWicketNamespace() + "_" + INLINE_ENCLOSURE_ID_PREFIX + getRequestUniqueId(); tag.setId(id); } else { tag.setId(htmlId); } tag.setAutoComponentTag(true); tag.setAutoComponentFactory( new ComponentTag.IAutoComponentFactory() { @Override public Component newComponent(MarkupContainer container, ComponentTag tag) { String attributeName = getInlineEnclosureAttributeName(null); String childId = tag.getAttribute(attributeName); return new InlineEnclosure(tag.getId(), childId); } }); tag.setModified(true); } // Put the enclosure on the stack. The most current one will be on top if (enclosures == null) { enclosures = new ArrayDeque<>(); } enclosures.push(tag); } else { throw new ParseException( "Open-close tags don't make sense for InlineEnclosure. Tag:" + tag.toString(), tag.getPos()); } } // Are we within an enclosure? else if ((enclosures != null) && (enclosures.size() > 0)) { // In case the enclosure tag did not provide a child component id, then assign the // first ComponentTag's id found as the controlling child to the enclosure. if (tag.isOpen() && (tag.getId() != null) && !(tag instanceof WicketTag) && !tag.isAutoComponentTag()) { Iterator<ComponentTag> componentTagIterator = enclosures.descendingIterator(); while (componentTagIterator.hasNext()) { ComponentTag lastEnclosure = componentTagIterator.next(); String attr = getAttribute(lastEnclosure, null); if (Strings.isEmpty(attr) == true) { lastEnclosure.getAttributes().put(getInlineEnclosureAttributeName(null), tag.getId()); lastEnclosure.setModified(true); } } } else if (tag.isClose() && tag.closes(enclosures.peek())) { ComponentTag lastEnclosure = enclosures.pop(); String attr = getAttribute(lastEnclosure, null); if (Strings.isEmpty(attr) == true) { throw new ParseException( "Did not find any child for InlineEnclosure. Tag:" + lastEnclosure.toString(), tag.getPos()); } } } return tag; }