Substitution(Block src, XDOM dst) { this.src = src; this.dst = dst.getChildren(); }
public String extractTitle() { String title = ""; try { if (is10Syntax()) { title = extractTitle10(); } else { List<HeaderBlock> blocks = getXDOM().getChildrenByType(HeaderBlock.class, true); if (blocks.size() > 0) { HeaderBlock header = blocks.get(0); if (header.getLevel().compareTo(HeaderLevel.LEVEL2) <= 0) { XDOM headerXDOM = new XDOM(Collections.<Block> singletonList(header)); // transform TransformationContext context = new TransformationContext(headerXDOM, getSyntax()); Utils.getComponent(TransformationManager.class).performTransformations(headerXDOM, context); // render Block headerBlock = headerXDOM.getChildren().get(0); if (headerBlock instanceof HeaderBlock) { title = renderXDOM(new XDOM(headerBlock.getChildren()), Syntax.XHTML_1_0); } } } } } catch (Exception e) { // Don't stop when there's a problem rendering the title. } return title; }
document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT); if (!blocks.isEmpty()) { HeaderBlock heading = blocks.get(0); XDOM headingXDOM = new XDOM(Collections.<Block> singletonList(heading)); try { TransformationContext txContext = transformationManager.performTransformations(headingXDOM, txContext); Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null; if (headingBlock instanceof HeaderBlock) { return new XDOM(headingBlock.getChildren());
/** * @param blocks the Blocks to assert * @param factory the Renderer Factory to use to serialize the passed Block * @return The serialized block. * @since 4.2M1 */ private static String render(List<Block> blocks, PrintRendererFactory factory) { // Assert the result by parsing it through the EventsRenderer to generate easily // assertable events. XDOM dom = new XDOM(blocks); WikiPrinter printer = new DefaultWikiPrinter(); PrintRenderer eventRenderer = factory.createRenderer(printer); dom.traverse(eventRenderer); return printer.toString(); }
/** * Clone and filter wiki macro content depending of the context. * * @param context the macro execution context * @return the cleaned wiki macro content */ private XDOM prepareWikiMacroContent() { XDOM xdom = this.wikimacro.getContent().clone(); // Macro code segment is always parsed into a separate xdom document. Now if this code segment starts with // another macro block, it will always be interpreted as a block macro regardless of the current wiki macro's // context (because as far as the nested macro is concerned, it starts on a new line). This will introduce // unnecessary paragraph elements when the wiki macro is used inline, so we need to force such opening macro // blocks to behave as inline macros if the wiki macro is used inline. if (this.inline) { List<Block> children = xdom.getChildren(); if (!children.isEmpty() && children.get(0) instanceof MacroBlock) { MacroBlock old = (MacroBlock) children.get(0); MacroBlock replacement = new MacroBlock(old.getId(), old.getParameters(), old.getContent(), true); xdom.replaceChild(replacement, old); } } return xdom; }
/** * @param xdom the {@link XDOM} to convert * @return an inline version of the passed {@link XDOM} */ private XDOM convertToInline(XDOM xdom) { List<Block> blocks = new ArrayList<Block>(xdom.getChildren()); // TODO: use inline parser instead if (!blocks.isEmpty()) { this.parserUtils.removeTopLevelParagraph(blocks); // Make sure included macro is inline when script macro itself is inline Block block = blocks.get(0); if (block instanceof MacroBlock) { MacroBlock macro = (MacroBlock) block; if (!macro.isInline()) { blocks.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true)); } } xdom.setChildren(blocks); } return xdom; }
private void runTestInternal() throws Throwable { WikiPrinter printer = new DefaultWikiPrinter(); if (!this.streaming) { Parser parser = getComponentManager().lookup(Parser.class, this.parserId); XDOM xdom = parser.parse(new StringReader(this.input)); if (this.runTransformations) { SyntaxFactory syntaxFactory = getComponentManager().lookup(SyntaxFactory.class); TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class); TransformationContext txContext = new TransformationContext(xdom, syntaxFactory.createSyntaxFromIdString(this.parserId)); transformationManager.performTransformations(xdom, txContext); } BlockRenderer renderer = getComponentManager().lookup(BlockRenderer.class, this.targetSyntaxId); // remove source syntax from XDOM metadata Map<String, Object> metadataMap = new HashMap<String, Object>(xdom.getMetaData().getMetaData()); metadataMap.remove(MetaData.SYNTAX); xdom = new XDOM(xdom.getChildren(), new MetaData(metadataMap)); renderer.render(xdom, printer); } else { StreamParser parser = getComponentManager().lookup(StreamParser.class, this.parserId); PrintRendererFactory printRendererFactory = getComponentManager().lookup(PrintRendererFactory.class, this.targetSyntaxId); // remove source syntax from begin/endDocument metadata WrappingListener listener = new SyntaxWrappingListener(); listener.setWrappedListener(printRendererFactory.createRenderer(printer)); parser.parse(new StringReader(this.input), listener); } assertEquals(this.expected, printer.toString()); }
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); String source = this.defaultEntityReferenceSerializer.serialize(includedReference); metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
if (newDoc.getChildren().size() > 0) { Block firstChild = newDoc.getChildren().get(0); if (firstChild instanceof HeaderBlock) { XDOM xdom = new XDOM(clonedHeaderBlock.getChildren());
if ("verbatim".equals(syntaxId)) xdom = new XDOM(new ArrayList<Block>()); xdom.addChild(new VerbatimBlock(s, false)); for (HeaderBlock header : xdom.getChildrenByType(HeaderBlock.class, true)) return new IncludeBlock(syntaxId, xdom.getChildren());
public XDOM getXDOM() { return new XDOM(generateListFromStack(), this.idGenerator); }
List<MacroBlock> macros = xdom.getBlocks(MACROBLOCKMATCHER, Axes.DESCENDANT); for (MacroBlock macro : macros) { if ("html".equals(macro.getId())) { htmlMacroBlock.getContent(), xdom.getChildren(), htmlMacroBlock.isInline());
List<MacroBlock> macros = xdom.getChildrenByType(MacroBlock.class, true); for (MacroBlock macro : macros) { if (macro.getId().equals("html")) { .getContent(), xdom.getChildren(), htmlMacroBlock.isInline());
/** * Get the content to display (either the entire document content or the content of a specific section). * * @param document the source document * @param parameters the display parameters * @return the content as an XDOM tree */ private XDOM getContent(DocumentModelBridge document, final DocumentDisplayerParameters parameters) { XDOM content = parameters.isContentTranslated() ? getTranslatedContent(document) : document.getXDOM(); if (parameters.getSectionId() != null) { HeaderBlock headerBlock = content.getFirstBlock(new CompositeBlockMatcher(new ClassBlockMatcher(HeaderBlock.class), new BlockMatcher() { @Override public boolean match(Block block) { return ((HeaderBlock) block).getId().equals(parameters.getSectionId()); } }), Block.Axes.DESCENDANT); if (headerBlock == null) { throw new RuntimeException("Cannot find section [" + parameters.getSectionId() + "] in document [" + this.defaultEntityReferenceSerializer.serialize(document.getDocumentReference()) + "]"); } else { content = new XDOM(headerBlock.getSection().getChildren(), content.getMetaData()); } } return content; }
/** * Convert the passed content from the passed syntax to the passed new syntax. * * @param content the content to convert * @param source the reference to where the content comes from (eg document reference) * @param targetSyntax the new syntax after the conversion * @param txContext the context when Transformation are executed or null if transformation shouldn't be executed * @return the converted content in the new syntax * @throws XWikiException if an exception occurred during the conversion process * @since 2.4M2 */ private static String performSyntaxConversion(String content, String source, Syntax targetSyntax, TransformationContext txContext) throws XWikiException { try { XDOM dom = parseContent(txContext.getSyntax().toIdString(), content); // Set the source metadata for the parsed XDOM so that Renderers can resolve relative links/images based // on it. dom.getMetaData().addMetaData(MetaData.SOURCE, source); return performSyntaxConversion(dom, targetSyntax, txContext); } catch (Exception e) { throw new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to convert document to syntax [" + targetSyntax + "]", e); } }
@Override protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) throws MacroExecutionException { XDOM xdom = computeXDOM(context); // Find the correct table block. List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT); TableBlock result = null; this.logger.debug("Table id is [{}], there are [{}] tables in the document [{}]", new Object[]{this.tableId, tableBlocks.size(), this.documentReference}); if (null != tableId) { for (TableBlock tableBlock : tableBlocks) { String id = tableBlock.getParameter("id"); if (null != id && id.equals(this.tableId)) { result = tableBlock; break; } } } else { result = (tableBlocks.size() > 0) ? tableBlocks.get(0) : null; } if (null == result) { throw new MacroExecutionException("Unable to find a matching data table."); } return result; }
/** * @return the XDOM corresponding to the document's string content. */ public XDOM getXDOM() { if (this.xdom == null) { try { this.xdom = parseContent(getContent()); } catch (XWikiException e) { LOG.error("Failed to parse document content to XDOM", e); } } return this.xdom.clone(); }
if (xdom.getMetaData() != null) { Map<String, Object> metadataMap = new HashMap<String, Object>(xdom.getMetaData().getMetaData()); metadataMap.remove(MetaData.SYNTAX); xdom = new XDOM(xdom.getChildren(), new MetaData(metadataMap));
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); String source = this.defaultEntityReferenceSerializer.serialize(includedReference); metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
if (splittingCriterion.shouldSplit(block, depth)) { XDOM xdom = new XDOM(block.getChildren()); String newDocumentName = namingCriterion.getDocumentName(xdom); WikiDocument newDoc = new WikiDocument(newDocumentName, xdom, parentDoc); split(newDoc, newDoc.getXdom().getChildren(), depth + 1, result, splittingCriterion, namingCriterion);