JcrExportCommand 过滤器以在 magnolia cms 中排除 "mgnl:page"
JcrExportCommand filter to exclude "mgnl:page" in magnolia cms
当我在具有自定义操作的节点上执行 JcrExportCommand 时,我想在 magnolia 中过滤掉 "mgnl:page" 个节点。
我在下面的代码中编写的过滤器不起作用。它仍然给我导出文件中的 mgnl:page 个子节点。
//set filter to only export mgnl:area subnodes
DefaultFilter filter = new JcrExportCommand.DefaultFilter();
NodeFilteringPredicate nodePredicate = new NodeFilteringPredicate();
nodePredicate.setNodeTypes(Lists.newArrayList("mgnl:area"));
filter.setNodePredicate(nodePredicate);
如何设置正确的过滤器以导出除 "mgnl:page" 子节点以外的所有内容?我相信将 NodeFilteringPredicate 设置为 "mgnl:area" 我只会得到那种类型的节点。
您必须在 JcrExportCommand
上设置过滤器才能生效:
DefaultFilter filter = new DefaultFilter();
filter.getNodePredicate().getNodeTypes().add("mgnl:page");
jcrExport.setFilter(Collections.singletonMap("website", filter));
* 这不是我的问题的答案,而是评论的答案,因为评论中的代码格式不正确 *
正如@michid 所建议的那样,我创建了一个自定义 Predicator 并使用了 JcrExportCommand.DefaultFilter#setNodePredicate() 来应用它。
我希望根据谓词获得带有过滤节点的导出 YAML,但我仍然获得所有节点(包括 mgnl:page 类型的子节点)。
我的自定义谓词 class 是:
public class MyPredicate extends NodeFilteringPredicate {
public boolean evaluate(Node node) throws AccessDeniedException, ItemNotFoundException, RepositoryException {
//only nodes that are not of type mgnl:page
if((node.getParent().getPrimaryNodeType().getName().contains("mgnl:page"))&&(node.getPrimaryNodeType().getName().contains("mgnl:page"))) {
return false;
}else{
return true;
}
}
}
我的自定义操作 class 是:
public class MyAction extends AbstractMultiItemAction<UzhVersioning> {
private AbstractPredicate<Node> MyPredicate;
public MyAction(xxxVersioning definition, JcrItemAdapter item, UiContext uiContext) {
super(definition, item, uiContext);
// TODO Auto-generated constructor stub
}
@Override
public void execute() {
//export nodes from a JCR workspace
JcrExportCommand exporter = new JcrExportCommand();
//sets export format to yaml
exporter.setFormat("yaml");
exporter.setRepository("website");
//set filter to only export top mgnl:page and its mgnl:area nodes
DefaultFilter filter = new JcrExportCommand.DefaultFilter();
AbstractPredicate<Node> predicate = new MyPredicate();
filter.setNodePredicate(predicate);
exporter.setFilters(Collections.singletonMap("website", filter));
//setup the root directory for exports
File rootDir = new File("/Users/asusti/Downloads/yamlExport");
// clean up first
rootDir.delete();
rootDir.mkdirs();
//get root node
Node node = (Node) getItems().get(0).getJcrItem();
try {
//set export path
exporter.setPath(node.getPath());
File file = new File(rootDir, node.getName()+".yaml");
FileOutputStream out = new FileOutputStream(file);
exporter.setOutputStream(out);
exporter.execute(MgnlContext.getInstance());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
当我在具有自定义操作的节点上执行 JcrExportCommand 时,我想在 magnolia 中过滤掉 "mgnl:page" 个节点。
我在下面的代码中编写的过滤器不起作用。它仍然给我导出文件中的 mgnl:page 个子节点。
//set filter to only export mgnl:area subnodes
DefaultFilter filter = new JcrExportCommand.DefaultFilter();
NodeFilteringPredicate nodePredicate = new NodeFilteringPredicate();
nodePredicate.setNodeTypes(Lists.newArrayList("mgnl:area"));
filter.setNodePredicate(nodePredicate);
如何设置正确的过滤器以导出除 "mgnl:page" 子节点以外的所有内容?我相信将 NodeFilteringPredicate 设置为 "mgnl:area" 我只会得到那种类型的节点。
您必须在 JcrExportCommand
上设置过滤器才能生效:
DefaultFilter filter = new DefaultFilter();
filter.getNodePredicate().getNodeTypes().add("mgnl:page");
jcrExport.setFilter(Collections.singletonMap("website", filter));
* 这不是我的问题的答案,而是评论的答案,因为评论中的代码格式不正确 *
正如@michid 所建议的那样,我创建了一个自定义 Predicator 并使用了 JcrExportCommand.DefaultFilter#setNodePredicate() 来应用它。
我希望根据谓词获得带有过滤节点的导出 YAML,但我仍然获得所有节点(包括 mgnl:page 类型的子节点)。
我的自定义谓词 class 是:
public class MyPredicate extends NodeFilteringPredicate {
public boolean evaluate(Node node) throws AccessDeniedException, ItemNotFoundException, RepositoryException {
//only nodes that are not of type mgnl:page
if((node.getParent().getPrimaryNodeType().getName().contains("mgnl:page"))&&(node.getPrimaryNodeType().getName().contains("mgnl:page"))) {
return false;
}else{
return true;
}
}
}
我的自定义操作 class 是:
public class MyAction extends AbstractMultiItemAction<UzhVersioning> {
private AbstractPredicate<Node> MyPredicate;
public MyAction(xxxVersioning definition, JcrItemAdapter item, UiContext uiContext) {
super(definition, item, uiContext);
// TODO Auto-generated constructor stub
}
@Override
public void execute() {
//export nodes from a JCR workspace
JcrExportCommand exporter = new JcrExportCommand();
//sets export format to yaml
exporter.setFormat("yaml");
exporter.setRepository("website");
//set filter to only export top mgnl:page and its mgnl:area nodes
DefaultFilter filter = new JcrExportCommand.DefaultFilter();
AbstractPredicate<Node> predicate = new MyPredicate();
filter.setNodePredicate(predicate);
exporter.setFilters(Collections.singletonMap("website", filter));
//setup the root directory for exports
File rootDir = new File("/Users/asusti/Downloads/yamlExport");
// clean up first
rootDir.delete();
rootDir.mkdirs();
//get root node
Node node = (Node) getItems().get(0).getJcrItem();
try {
//set export path
exporter.setPath(node.getPath());
File file = new File(rootDir, node.getName()+".yaml");
FileOutputStream out = new FileOutputStream(file);
exporter.setOutputStream(out);
exporter.execute(MgnlContext.getInstance());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}