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; }
@Override protected List<Block> getContent(ContainerMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { return this.contentParser.parse(content, context, false, false).getChildren(); }
/** * Parse provided content with the parser of the current wiki syntax. * * @param content the content to parse. * @param context the context of the macro transformation. * @return the result of the parsing. * @throws MacroExecutionException failed to parse content */ protected List<Block> parseSourceSyntax(String content, MacroTransformationContext context) throws MacroExecutionException { return this.contentParser.parse(content, context, false, false).getChildren(); } }
/** * Parses the given title as plain text and returns the generated XDOM. * * @param title the title to be parsed * @return the XDOM generated from parsing the title as plain text */ protected XDOM parseTitle(String title) { try { XDOM xdom = plainTextParser.parse(new StringReader(title)); parserUtils.removeTopLevelParagraph(xdom.getChildren()); return xdom; } catch (ParseException e) { throw new RuntimeException(e); } }
@Override public List<WikiDocument> split(WikiDocument rootDoc, SplittingCriterion splittingCriterion, NamingCriterion namingCriterion) { List<WikiDocument> result = new ArrayList<WikiDocument>(); // Add the rootDoc into the result result.add(rootDoc); // Recursively split the root document. split(rootDoc, rootDoc.getXdom().getChildren(), 1, result, splittingCriterion, namingCriterion); updateAnchors(result); return result; }
private List<Block> renderGadgetProperty(String content, Syntax sourceSyntax, EntityReference sourceReference, MacroTransformationContext context) throws MissingParserException, ParseException, ContentExecutorException { XDOM xdom = this.contentExecutor.execute(content, sourceSyntax, sourceReference, context); List<Block> xdomBlocks = xdom.getChildren(); this.parserUtils.removeTopLevelParagraph(xdomBlocks); return xdomBlocks; }
private List<Block> convertScriptExecution(Object scriptResult, StringWriter scriptContextWriter, P parameters, MacroTransformationContext context) throws MacroExecutionException { List<Block> result; if (scriptResult instanceof XDOM) { result = ((XDOM) scriptResult).getChildren(); } else if (scriptResult instanceof Block) { result = Collections.singletonList((Block) scriptResult); } else if (scriptResult instanceof List && !((List<?>) scriptResult).isEmpty() && ((List<?>) scriptResult).get(0) instanceof Block) { result = (List<Block>) scriptResult; } else if (scriptResult instanceof Class) { // Class result means class definition and we don't want to print anything in this case result = Collections.emptyList(); } else { // If the Script Context writer is empty and the Script Result isn't, then convert the String Result // to String and display it! String contentToParse = scriptContextWriter.toString(); if (StringUtils.isEmpty(contentToParse) && scriptResult != null) { // Convert the returned value into a String. contentToParse = this.converterManager.convert(String.class, scriptResult); } // Run the wiki syntax parser on the Script returned content result = parseScriptResult(contentToParse, parameters, context); } return result; }
@Override public void format(String tokenType, String value, Map<String, Object> style) { List<Block> blockList; if (StringUtils.isNotEmpty(value)) { try { blockList = this.plainTextParser.parse(new StringReader(value)).getChildren().get(0).getChildren(); } catch (ParseException e) { // This shouldn't happen since the parser cannot throw an exception since the source is a memory // String. throw new RuntimeException("Failed to parse [" + value + "] as plain text.", e); } String styleParameter = formatStyle(style); FormatBlock formatBlock = null; if (styleParameter.length() > 0) { formatBlock = new FormatBlock(blockList, Format.NONE); formatBlock.setParameter("style", styleParameter); this.blocks.add(formatBlock); } else { this.blocks.addAll(blockList); } } }
@Override protected List<Block> parseContent(CodeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { List<Block> result; try { if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) { if (StringUtils.isEmpty(content)) { result = Collections.emptyList(); } else { result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren(); } } else { result = highlight(parameters, content); } } catch (Exception e) { throw new MacroExecutionException("Failed to highlight content", e); } Map<String, String> formatParameters = new LinkedHashMap<String, String>(); formatParameters.put("class", "code"); if (context.isInline()) { result = Arrays.<Block> asList(new FormatBlock(result, Format.NONE, formatParameters)); } else { result = Arrays.<Block> asList(new GroupBlock(result, formatParameters)); } return result; }
@Override protected List<Block> parseContent(P parameters, String content, MacroTransformationContext context) throws MacroExecutionException { // Don't execute transformations explicitly. They'll be executed on the generated content later on. List<Block> children = getMacroContentParser().parse(content, context, false, context.isInline()).getChildren(); return Collections.singletonList(new MetaDataBlock(children, this.getNonGeneratedContentMetaData())); } }
return this.plainTextParser.parse(new StringReader(label)).getChildren().get(0).getChildren(); } catch (ParseException e) {
/** * {@inheritDoc} * @see MacroContentParser#parse(String, org.xwiki.rendering.syntax.Syntax, boolean) */ public List<Block> parse(String content, Syntax syntax, boolean removeTopLevelParagraph) throws MacroExecutionException { try { List<Block> blocks = getSyntaxParser(syntax).parse(new StringReader(content)).getChildren(); if (removeTopLevelParagraph) { this.parserUtils.removeTopLevelParagraph(blocks); } return blocks; } catch (Exception e) { throw new MacroExecutionException("Failed to parse content [" + content + "]", e); } }
/** * @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; }
/** * 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; }
@Override public List<Block> execute(ContentMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { try { List<Block> blocks = getSyntaxParser(parameters.getSyntax()).parse(new StringReader(content)).getChildren(); MetaDataBlock metaDataBlock = new MetaDataBlock(blocks, MetaData.SYNTAX, parameters.getSyntax().toIdString()); metaDataBlock.getMetaData().addMetaData(this.getNonGeneratedContentMetaData()); return Collections.singletonList(metaDataBlock); } catch (ParseException e) { throw new MacroExecutionException( String.format("Failed to parse macro content in syntax [%s]", parameters.getSyntax()), e); } }
@Override public List<Block> execute(OfficeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { // Check if the office server is started and if not, generate an error. if (!this.officeServer.getState().equals(OfficeServer.ServerState.CONNECTED)) { throw new MacroExecutionException("The wiki needs to be connected to an office server in order to view " + "office files. Ask your administrator to configure such a server."); } ResourceReference resourceReference = getResourceReference(context.getCurrentMacroBlock(), parameters); Map<String, Object> viewParameters = new HashMap<String, Object>(); viewParameters.put("filterStyles", parameters.isFilterStyles()); viewParameters.put("ownerDocument", getOwnerDocument(context.getCurrentMacroBlock())); try { return this.officeViewer.createView(resourceReference, viewParameters).getChildren(); } catch (Exception e) { throw new MacroExecutionException("Failed to view office attachment.", e); } }
blocks = listener.getBlocks(); } else { blocks = this.plainTextParser.parse(new StringReader(code)).getChildren().get(0).getChildren();
for (Block block : xdom.getChildren()) { block.traverse(renderer);
@Override public List<Block> execute(CacheMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { // Idea for improvement: use context.getId() (which contains the document name) as part of the cache key to // make it even more unique (when the cache macro parameter id is not specified). String cacheKey; if (parameters.getId() != null) { // Consider that the id contains wiki syntax and parse it with the same wiki parser than the current // transformation is using and render the result as plain text. WikiPrinter printer = new DefaultWikiPrinter(); this.plainTextBlockRenderer.render(this.contentParser.parse(parameters.getId(), context, true, false), printer); cacheKey = printer.toString(); } else { cacheKey = content; } Cache<List<Block>> contentCache = getContentCache(parameters.getTimeToLive(), parameters.getMaxEntries()); List<Block> result = contentCache.get(cacheKey); if (result == null) { // Run the parser for the syntax on the content // We run the current transformation on the cache macro content. We need to do this since we want to cache // the XDOM resulting from the execution of Macros because that's where lengthy processing happens. result = this.contentParser.parse(content, context, true, context.isInline()).getChildren(); contentCache.set(cacheKey, result); } return result; }