Re: [xwiki-devs] [xwiki-notifications] r34808 - in platform/core/trunk/xwiki-rendering: xwiki-rendering-api/src/main/java/org/xwiki/rendering/block/match xwiki-rendering-macros/xwiki-rendering-macro-include/src/main/java/org/xwiki/rendering/internal/macro/include
On Sat, Feb 19, 2011 at 10:38, vmassol <[email protected]> wrote:
Author: vmassol Date: 2011-02-19 10:38:37 +0100 (Sat, 19 Feb 2011) New Revision: 34808
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/block/match/CompositeBlockMatcher.java Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-include/src/main/java/org/xwiki/rendering/internal/macro/include/IncludeMacro.java Log: XWIKI-1776: Support Include of Section Content
* Added javadoc * Introduced CompositeBlockMatcher
Added: platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/block/match/CompositeBlockMatcher.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/block/match/CompositeBlockMatcher.java (rev 0) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/block/match/CompositeBlockMatcher.java 2011-02-19 09:38:37 UTC (rev 34808) @@ -0,0 +1,72 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.rendering.block.match; + +import java.util.ArrayList; +import java.util.List; + +import org.xwiki.rendering.block.Block; + +/** + * Implementation of {@link BlockMatcher} which matches blocks using passed matchers in series. + * + * @version $Id$ + * @since 3.0M3 + */ +public class CompositeBlockMatcher implements BlockMatcher +{ + /** + * The matchers to match in series. + */ + private List<BlockMatcher> matchers = new ArrayList<BlockMatcher>(); + + /** + * @param matchers list of matchers to add + */ + public CompositeBlockMatcher(List<BlockMatcher> matchers) + { + this.matchers.addAll(matchers); + } + + /** + * @param matchers vararg list of matchers to add + */ + public CompositeBlockMatcher(BlockMatcher... matchers) + { + for (BlockMatcher matcher : matchers) { + this.matchers.add(matcher); + } + } + + /** + * {@inheritDoc} + * + * @see org.xwiki.rendering.block.match.BlockMatcher#match(org.xwiki.rendering.block.Block) + */ + public boolean match(Block block) + { + for (BlockMatcher matcher : this.matchers) { + if (!matcher.match(block)) { + return false; + } + } + return true; + } +}
Modified: platform/core/trunk/xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-include/src/main/java/org/xwiki/rendering/internal/macro/include/IncludeMacro.java =================================================================== --- platform/core/trunk/xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-include/src/main/java/org/xwiki/rendering/internal/macro/include/IncludeMacro.java 2011-02-18 22:26:41 UTC (rev 34807) +++ platform/core/trunk/xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-include/src/main/java/org/xwiki/rendering/internal/macro/include/IncludeMacro.java 2011-02-19 09:38:37 UTC (rev 34808) @@ -41,6 +41,8 @@ import org.xwiki.rendering.block.MetaDataBlock; import org.xwiki.rendering.block.XDOM; import org.xwiki.rendering.block.match.BlockMatcher; +import org.xwiki.rendering.block.match.ClassBlockMatcher; +import org.xwiki.rendering.block.match.CompositeBlockMatcher; import org.xwiki.rendering.internal.macro.MacroContentParser; import org.xwiki.rendering.listener.MetaData; import org.xwiki.rendering.macro.AbstractMacro; @@ -207,24 +209,33 @@ return result; }
+ /** + * Get the content to include (either full target document or a specific section's content). + * + * @param document the reference to the document from which to get the content + * @param section the id of the section from which to get the content in that document or null to take the whole + * content + * @param includedReference the resolved absolute reference of the included document + * @return the content as an XDOM tree + * @throws MacroExecutionException if no section of the passed if exists in the included document + */ private XDOM getContent(DocumentModelBridge document, final String section, DocumentReference includedReference) throws MacroExecutionException { XDOM includedContent = document.getXDOM();
if (section != null) { - HeaderBlock headerBlock = (HeaderBlock) includedContent.getFirstBlock(new BlockMatcher() { - public boolean match(Block block) - { - if (block instanceof HeaderBlock) { + HeaderBlock headerBlock = (HeaderBlock) includedContent.getFirstBlock( + new CompositeBlockMatcher(new ClassBlockMatcher(HeaderBlock.class), new BlockMatcher() { + public boolean match(Block block) + { HeaderBlock headerBlock = (HeaderBlock) block; if (headerBlock.getId().equals(section)) { return true; } + return false;
What about return headerBlock.getId().equals(section); ?
} - return false; - } - }, Block.Axes.DESCENDANT); + }),Block.Axes.DESCENDANT); if (headerBlock == null) { throw new MacroExecutionException("Cannot find section [" + section + "] in document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]"); @@ -291,19 +302,26 @@ { DocumentReference result;
- String sourceMetadata = null; - MetaDataBlock metadataBlock = block.getPreviousBlockByType(MetaDataBlock.class, true); - while (sourceMetadata == null && metadataBlock != null) { - sourceMetadata = (String) metadataBlock.getMetaData().getMetaData(MetaData.SOURCE); - metadataBlock = metadataBlock.getPreviousBlockByType(MetaDataBlock.class, true); - } + MetaDataBlock metaDataBlock = (MetaDataBlock) block.getFirstBlock( + new CompositeBlockMatcher(new ClassBlockMatcher(MetaDataBlock.class), new BlockMatcher() { + public boolean match(Block block) + { + MetaDataBlock metaDataBlock = (MetaDataBlock) block; + String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE); + if (sourceMetaData != null) { + return true; + } + return false;
Maybe we should add something like MetaDataBlock#contains(String key) method.
+ } + }), Block.Axes.ANCESTOR);
// If no Source MetaData was found resolve against the current document as a failsafe solution. - if (sourceMetadata == null) { + if (metaDataBlock == null) { result = this.currentDocumentReferenceResolver.resolve(documentName); } else { + String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE); result = this.currentDocumentReferenceResolver.resolve(documentName, - this.currentDocumentReferenceResolver.resolve(sourceMetadata)); + this.currentDocumentReferenceResolver.resolve(sourceMetaData)); }
return result;
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications
-- Thomas Mortagne
participants (1)
-
Thomas Mortagne