use of org.xwiki.rendering.block.match.ClassBlockMatcher in project xwiki-platform by xwiki.
the class DocumentSplitterUtils method relocateArtifacts.
/**
* Move artifacts (i.e. embedded images) from the original office document to a specific wiki document corresponding
* to a section. Only the artifacts from that section are moved.
*
* @param sectionDoc the newly created wiki document corresponding to a section of the original office document
* @param officeDocument the office document being splitted into wiki documents
* @return the relocated artifacts
*/
public static Map<String, byte[]> relocateArtifacts(WikiDocument sectionDoc, XDOMOfficeDocument officeDocument) {
Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
List<ImageBlock> imageBlocks = sectionDoc.getXdom().getBlocks(new ClassBlockMatcher(ImageBlock.class), Axes.DESCENDANT);
for (ImageBlock imageBlock : imageBlocks) {
String imageReference = imageBlock.getReference().getReference();
artifacts.put(imageReference, officeDocument.getArtifacts().remove(imageReference));
}
return artifacts;
}
use of org.xwiki.rendering.block.match.ClassBlockMatcher in project xwiki-platform by xwiki.
the class XWikiDocument method getIncludedPagesInternal.
private List<String> getIncludedPagesInternal(XWikiContext context) {
if (is10Syntax()) {
return getIncludedPagesForXWiki10Syntax(getContent(), context);
} else {
// Find all include macros listed on the page
XDOM dom = getXDOM();
List<String> result = new ArrayList<String>();
List<MacroBlock> macroBlocks = dom.getBlocks(new ClassBlockMatcher(MacroBlock.class), Block.Axes.DESCENDANT);
for (MacroBlock macroBlock : macroBlocks) {
if (macroBlock.getId().equalsIgnoreCase("include") || macroBlock.getId().equalsIgnoreCase("display")) {
String documentName = macroBlock.getParameters().get("reference");
if (StringUtils.isEmpty(documentName)) {
documentName = macroBlock.getParameters().get("document");
if (StringUtils.isEmpty(documentName)) {
continue;
}
}
DocumentReference documentReference = getExplicitDocumentReferenceResolver().resolve(documentName, getDocumentReference());
if (this.getDocumentReference().equals(documentReference)) {
// Skip auto-includes since they are not allowed anyway.
continue;
}
documentName = LOCAL_REFERENCE_SERIALIZER.serialize(documentReference);
result.add(documentName);
} else if (macroBlock.getId().equalsIgnoreCase("velocity") && !StringUtils.isEmpty(macroBlock.getContent())) {
// Try to find matching content inside each velocity macro
result.addAll(getIncludedPagesForXWiki10Syntax(macroBlock.getContent(), context));
}
}
return result;
}
}
use of org.xwiki.rendering.block.match.ClassBlockMatcher in project xwiki-platform by xwiki.
the class DocumentTitleDisplayer method extractTitleFromContent.
@Override
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) {
// Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
// generate headings for example or some other transformations could modify headings. However we don't do this
// at the moment since it would be too costly to do so. In the future we will even probably remove the feature
// of generating the title from the content.
List<HeaderBlock> blocks = document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT);
if (!blocks.isEmpty()) {
HeaderBlock heading = blocks.get(0);
// Check the heading depth after which we should return null if no heading was found.
if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) {
XDOM headingXDOM = new XDOM(Collections.<Block>singletonList(heading));
try {
TransformationContext txContext = new TransformationContext(headingXDOM, document.getSyntax(), parameters.isTransformationContextRestricted());
txContext.setTargetSyntax(parameters.getTargetSyntax());
transformationManager.performTransformations(headingXDOM, txContext);
Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null;
if (headingBlock instanceof HeaderBlock) {
return new XDOM(headingBlock.getChildren());
}
} catch (TransformationException e) {
getLogger().warn("Failed to extract title from document content.");
}
}
}
return null;
}
use of org.xwiki.rendering.block.match.ClassBlockMatcher in project xwiki-platform by xwiki.
the class NestedScriptMacroValidatorListener method check.
@Override
protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) {
// Traverse the XDOM tree up to the root
if (context.getCurrentMacroBlock() != null) {
MacroMarkerBlock parent = context.getCurrentMacroBlock().getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
while (parent != null) {
String parentId = parent.getId();
try {
Macro<?> macro = this.macroManager.getMacro(new MacroId(parentId));
if (macro instanceof ScriptMacro) {
// Find the
event.cancel(String.format("Nested scripts are not allowed. Current Script Macro [%s] " + "(source [%s]) is executed inside Script Macro [%s] (source [%s])", context.getCurrentMacroBlock().getId(), extractSourceContentReference(context.getCurrentMacroBlock()), parentId, extractSourceContentReference(parent)));
} else if (macro instanceof NestedScriptMacroEnabled) {
// This macro has the right to produce script macro whatever the parent.
return;
} else if ("include".equals(parentId)) {
// use NestedScriptMacroEnabled, we should maybe find something more generic
return;
}
} catch (MacroLookupException exception) {
// Shouldn't happen, the parent macro was already successfully executed earlier
}
parent = parent.getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
}
}
}
use of org.xwiki.rendering.block.match.ClassBlockMatcher in project xwiki-platform by xwiki.
the class ColumnsLayoutManager method layoutContainer.
@Override
public void layoutContainer(Block container) {
List<GroupBlock> innerGroups = container.getBlocks(new ClassBlockMatcher(GroupBlock.class), Block.Axes.CHILD);
// FIXME Should we cry and throw an exception if ever we meet something else than a group right under
// the container macro, or automatically put it in a group maybe ?
int count = innerGroups.size();
if (innerGroups.size() == 0) {
// nothing inside, nothing to layout
return;
}
Map<String, Object> skinxParams = new HashMap<String, Object>();
skinxParams.put("forceSkinAction", true);
ssfx.use("uicomponents/container/columns.css", skinxParams);
// add styles to all columns inside
for (int i = 0; i < count; i++) {
GroupBlock column = innerGroups.get(i);
String classValue = "column";
if (i == 0) {
// we're at the first element in the list, put a marker. Don't need it to do standard columns layout,
// but maybe somebody needs it for customization...
classValue = classValue + " first-column";
}
if (i == count - 1) {
// we're at the last element in the list, put a marker
classValue = classValue + " last-column";
}
String oldClass = column.getParameter(CLASS_ATTRIBUTE);
column.setParameter(CLASS_ATTRIBUTE, (StringUtils.isEmpty(oldClass) ? classValue : oldClass + " " + classValue));
}
// finally, clear the floats introduced by the columns
Map<String, String> clearFloatsParams = new HashMap<String, String>();
clearFloatsParams.put(CLASS_ATTRIBUTE, "clearfloats");
String oldClass = container.getParameter(CLASS_ATTRIBUTE);
String newClass = "container-columns container-columns-" + count;
container.setParameter(CLASS_ATTRIBUTE, StringUtils.isEmpty(oldClass) ? newClass : oldClass + " " + newClass);
container.addChild(new GroupBlock(clearFloatsParams));
}
Aggregations