/** * 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); } }
private Block tranform(XDOM xdom, Block block) throws RenderingException { TransformationContext transformationContext = new TransformationContext(xdom, this.configuration.getDefaultSyntax(), false); transformationContext.setTargetSyntax(this.configuration.getTargetSyntax()); transformationContext.setId(this.configuration.getTransformationId()); transform(block, transformationContext); // The result is often inserted in a bigger content so we remove the XDOM around it if (block instanceof XDOM) { return new MetaDataBlock(block.getChildren(), ((XDOM) block).getMetaData()); } return block; } }
/** * Displays the given document in the current execution context. * * @param document the document to display * @param nameSpace the name-space to be used when performing the display transformations * @param parameters the display parameters * @return the result of displaying the given document */ protected XDOM display(DocumentModelBridge document, String nameSpace, DocumentDisplayerParameters parameters) { // This is a clone of the cached content that can be safely modified. XDOM content = getContent(document, parameters); if (!parameters.isContentTransformed()) { return content; } // Before executing the XDOM transformations make sure the references used by them (e.g. the 'reference' // parameter of the Include macro) are resolved relative to the current document on the execution context. content.getMetaData().addMetaData(MetaData.BASE, defaultEntityReferenceSerializer.serialize(documentAccessBridge.getCurrentDocumentReference())); TransformationContext txContext = new TransformationContext(content, document.getSyntax(), parameters.isTransformationContextRestricted()); txContext.setId(nameSpace); try { transformationManager.performTransformations(content, txContext); } catch (Exception e) { throw new RuntimeException(e); } return content; }
/** * creates XDOM. */ private XDOM createXDOM(String content, MacroTransformationContext macroContext, boolean transform, MetaData metadata, boolean inline, Syntax syntax) throws MacroExecutionException { try { XDOM result = getSyntaxParser(syntax).parse(new StringReader(content)); if (metadata != null) { result.getMetaData().addMetaData(metadata); } if (transform && macroContext.getTransformation() != null) { TransformationContext txContext = new TransformationContext(result, syntax); txContext.setId(macroContext.getId()); performTransformation((MutableRenderingContext) this.renderingContext, macroContext.getTransformation(), txContext, result); } if (inline) { result = convertToInline(result); } return result; } catch (Exception e) { throw new MacroExecutionException("Failed to parse content [" + content + "]", e); } }
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); String source = this.defaultEntityReferenceSerializer.serialize(includedReference); metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
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()); }
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));
/** * 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; }
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); String source = this.defaultEntityReferenceSerializer.serialize(includedReference); metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), macroXDOM.getMetaData());