use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.
the class XWikiDocumentOutputFilterStream method createFilter.
@Override
protected Object createFilter() {
List<XWikiDocumentFilter> filters = new ArrayList<>(this.children.size() + 1);
for (EntityOutputFilterStream<?> child : this.children) {
filters.add((XWikiDocumentFilter) child.getFilter());
}
filters.add(this);
this.filter = new XWikiDocumentFilterCollection(filters) {
@Override
public void beginWikiClass(FilterEventParameters parameters) throws FilterException {
if (!objectFilter.isEnabled()) {
classFilter.enable();
}
super.beginWikiClass(parameters);
}
@Override
public void endWikiClass(FilterEventParameters parameters) throws FilterException {
super.endWikiClass(parameters);
classFilter.disable();
}
@Override
public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException {
objectFilter.enable();
super.beginWikiObject(name, parameters);
}
@Override
public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException {
super.endWikiObject(name, parameters);
objectFilter.disable();
}
@Override
public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters) throws FilterException {
attachmentFilter.enable();
super.onWikiAttachment(name, content, size, parameters);
attachmentFilter.disable();
}
};
if (this.contentListener != null) {
// Inject listener for the document content events
return this.filterManager.createCompositeFilter(this.contentListener, this.filter);
} else {
return this.filter;
}
}
use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.
the class XWikiDocumentOutputFilterStream method begin.
private void begin(FilterEventParameters parameters) throws FilterException {
DocumentReference documentReference = this.documentEntityResolver.resolve(this.currentEntityReference, getDefaultDocumentReference());
if (this.entity == null) {
this.entity = new XWikiDocument(documentReference, this.currentLocale);
} else {
this.entity.setDocumentReference(documentReference);
this.entity.setLocale(this.currentLocale);
}
// Find default author
DocumentReference defaultAuthorReference;
if (this.properties.isAuthorSet()) {
defaultAuthorReference = this.properties.getAuthor();
} else {
XWikiContext xcontext = xcontextProvider.get();
defaultAuthorReference = xcontext != null ? xcontext.getUserReference() : null;
}
this.entity.setCreationDate(getDate(WikiDocumentFilter.PARAMETER_CREATION_DATE, this.currentLocaleParameters, null));
this.entity.setCreatorReference(getUserReference(WikiDocumentFilter.PARAMETER_CREATION_AUTHOR, this.currentLocaleParameters, defaultAuthorReference));
this.entity.setDefaultLocale(this.currentDefaultLocale);
this.entity.setSyntax(getSyntax(WikiDocumentFilter.PARAMETER_SYNTAX, parameters, null));
this.entity.setParentReference(getEntityReference(WikiDocumentFilter.PARAMETER_PARENT, parameters, null));
this.entity.setCustomClass(getString(WikiDocumentFilter.PARAMETER_CUSTOMCLASS, parameters, null));
this.entity.setTitle(getString(WikiDocumentFilter.PARAMETER_TITLE, parameters, null));
this.entity.setDefaultTemplate(getString(WikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE, parameters, null));
this.entity.setValidationScript(getString(WikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT, parameters, null));
this.entity.setHidden(getBoolean(WikiDocumentFilter.PARAMETER_HIDDEN, parameters, false));
this.entity.setMinorEdit(getBoolean(WikiDocumentFilter.PARAMETER_REVISION_MINOR, parameters, false));
this.entity.setAuthorReference(getUserReference(WikiDocumentFilter.PARAMETER_REVISION_AUTHOR, parameters, defaultAuthorReference));
this.entity.setContentAuthorReference(getUserReference(WikiDocumentFilter.PARAMETER_CONTENT_AUTHOR, parameters, defaultAuthorReference));
String revisions = getString(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS, this.currentLocaleParameters, null);
if (revisions != null) {
try {
this.entity.setDocumentArchive(revisions);
} catch (XWikiException e) {
throw new FilterException("Failed to set document archive", e);
}
}
if (this.currentVersion != null && this.properties.isVersionPreserved()) {
if (VALID_VERSION.matcher(this.currentVersion).matches()) {
this.entity.setVersion(this.currentVersion);
} else if (NumberUtils.isDigits(this.currentVersion)) {
this.entity.setVersion(this.currentVersion + ".1");
} else {
// TODO: log something, probably a warning
}
}
this.entity.setDate(getDate(WikiDocumentFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
this.entity.setComment(getString(WikiDocumentFilter.PARAMETER_REVISION_COMMENT, parameters, ""));
this.entity.setContentUpdateDate(getDate(WikiDocumentFilter.PARAMETER_CONTENT_DATE, parameters, new Date()));
if (this.contentListener != null) {
// Remember the current rendering context target syntax
this.previousTargetSyntax = this.renderingContext.getTargetSyntax();
}
if (parameters.containsKey(WikiDocumentFilter.PARAMETER_CONTENT)) {
this.entity.setContent(getString(WikiDocumentFilter.PARAMETER_CONTENT, parameters, null));
if (this.contentListener != null) {
// Cancel any existing content listener
this.currentWikiPrinter = null;
this.contentListener.setWrappedListener(null);
}
} else if (this.contentListener != null) {
if (this.properties != null && this.properties.getDefaultSyntax() != null) {
this.entity.setSyntax(this.properties.getDefaultSyntax());
} else {
// Make sure to set the default syntax if none were provided
this.entity.setSyntax(this.entity.getSyntax());
}
ComponentManager componentManager = this.componentManagerProvider.get();
String syntaxString = this.entity.getSyntax().toIdString();
if (componentManager.hasComponent(PrintRendererFactory.class, syntaxString)) {
PrintRendererFactory rendererFactory;
try {
rendererFactory = componentManager.getInstance(PrintRendererFactory.class, syntaxString);
} catch (ComponentLookupException e) {
throw new FilterException(String.format("Failed to find PrintRendererFactory for syntax [%s]", this.entity.getSyntax()), e);
}
this.currentWikiPrinter = new DefaultWikiPrinter();
((MutableRenderingContext) this.renderingContext).setTargetSyntax(rendererFactory.getSyntax());
this.contentListener.setWrappedListener(rendererFactory.createRenderer(this.currentWikiPrinter));
}
}
// Initialize the class
getBaseClassOutputFilterStream().setEntity(this.entity.getXClass());
}
use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.
the class XARInputFilterStream method readXAR.
private void readXAR(Object filter, XARInputFilter proxyFilter) throws FilterException {
WikiReader wikiReader = this.wikiReaderProvider.get();
wikiReader.setProperties(this.properties);
try {
wikiReader.read(filter, proxyFilter);
} catch (Exception e) {
throw new FilterException("Failed to read XAR package", e);
}
}
use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.
the class XARInputFilterStream method readDocument.
protected void readDocument(Object filter, XARInputFilter proxyFilter) throws FilterException {
DocumentLocaleReader documentReader = documentLocaleReaderProvider.get();
documentReader.setProperties(this.properties);
try {
documentReader.read(filter, proxyFilter);
} catch (Exception e) {
throw new FilterException("Failed to read XAR XML document", e);
}
// Close remaining opened spaces
if (documentReader.getSentSpaceReference() != null) {
for (EntityReference space = documentReader.getSentSpaceReference(); space != null; space = space.getParent()) {
proxyFilter.endWikiSpace(space.getName(), FilterEventParameters.EMPTY);
}
}
}
use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.
the class XAROutputFilterStream method beginWikiDocumentRevision.
@Override
public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException {
checkXMLWriter();
this.currentDocumentVersion = revision;
try {
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_PARENT)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_PARENT, toString((EntityReference) parameters.get(XWikiWikiDocumentFilter.PARAMETER_PARENT)));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_AUTHOR, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CUSTOMCLASS, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_AUTHOR, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_DATE, toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_DATE, toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)));
}
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION, this.currentDocumentVersion);
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_TITLE)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_TITLE, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_TITLE));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTTEMPLATE, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_VALIDATIONSCRIPT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_COMMENT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_MINOR, toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_SYNTAX, toString((Syntax) parameters.get(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_HIDDEN, toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_HTML, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML));
}
} catch (Exception e) {
throw new FilterException(String.format("Failed to write begin document [%s] with version [%s]", this.currentDocumentReference, this.currentDocumentVersion), e);
}
}
Aggregations