Search in sources :

Example 16 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class AbstractInstanceFilterStreamTest method importFromXML.

protected void importFromXML(String resource, InstanceOutputProperties instanceProperties) throws FilterException {
    if (instanceProperties == null) {
        instanceProperties = new InstanceOutputProperties();
        instanceProperties.setVerbose(false);
    }
    OutputFilterStream outputFilterStream = this.outputFilterStreamFactory.createOutputFilterStream(instanceProperties);
    URL url = getClass().getResource("/filter/" + resource + ".xml");
    FilterXMLInputProperties properties = new FilterXMLInputProperties();
    properties.setSource(new DefaultURLInputSource(url));
    InputFilterStream inputFilterStream = this.xmlInputFilterStreamFactory.createInputFilterStream(properties);
    inputFilterStream.read(outputFilterStream.getFilter());
    try {
        inputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close input wiki stream", e);
    }
    try {
        outputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close output wiki stream", e);
    }
}
Also used : InstanceOutputProperties(org.xwiki.filter.instance.output.InstanceOutputProperties) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) DefaultURLInputSource(org.xwiki.filter.input.DefaultURLInputSource) FilterException(org.xwiki.filter.FilterException) FilterXMLInputProperties(org.xwiki.filter.filterxml.input.FilterXMLInputProperties) IOException(java.io.IOException) InputFilterStream(org.xwiki.filter.input.InputFilterStream) URL(java.net.URL)

Example 17 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class PropertyClassOutputFilterStream method onWikiClassPropertyField.

@Override
public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters) throws FilterException {
    if (this.entity != null) {
        PropertyClass propertyClass;
        try {
            propertyClass = (PropertyClass) this.currentClassPropertyMeta.get(name);
        } catch (XWikiException e) {
            throw new FilterException(String.format("Failed to get definition of field [%s] for property type [%s]", name, this.entity.getClassType()), e);
        }
        // Make sure the property is known
        if (propertyClass == null) {
            this.logger.warn("Unknown property meta class field [{}] for property type [{}]", name, this.entity.getClassType());
            return;
        }
        BaseProperty<?> field = propertyClass.fromString(value);
        this.entity.safeput(name, field);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

Example 18 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class PropertyClassOutputFilterStream method beginWikiClassProperty.

// Events
@Override
public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException {
    if (this.enabled) {
        ComponentManager componentManager = this.componentManagerProvider.get();
        this.currentClassPropertyMeta = null;
        PropertyClassProvider provider;
        // First try to use the specified class type as hint.
        try {
            if (componentManager.hasComponent(PropertyClassProvider.class, type)) {
                provider = componentManager.getInstance(PropertyClassProvider.class, type);
            } else {
                // In previous versions the class type was the full Java class name of the property class
                // implementation. Extract the hint by removing the Java package prefix and the Class suffix.
                String classType = StringUtils.removeEnd(StringUtils.substringAfterLast(type, "."), "Class");
                if (componentManager.hasComponent(PropertyClassProvider.class, classType)) {
                    provider = componentManager.getInstance(PropertyClassProvider.class, classType);
                } else {
                    this.logger.warn("Unknown property type [{}]", type);
                    return;
                }
            }
        } catch (ComponentLookupException e) {
            throw new FilterException(String.format("Failed to get instance of the property class provider for type [%s]", type), e);
        }
        this.currentClassPropertyMeta = provider.getDefinition();
        if (this.entity == null) {
            // We should use PropertyClassInterface (instead of PropertyClass, its default implementation) but it
            // doesn't have the set methods and adding them would breaks the backwards compatibility. We make the
            // assumption that all property classes extend PropertyClass.
            this.entity = (PropertyClass) provider.getInstance();
        }
        this.entity.setName(name);
        this.entity.setObject(this.currentXClass);
    }
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) FilterException(org.xwiki.filter.FilterException) PropertyClassProvider(com.xpn.xwiki.internal.objects.classes.PropertyClassProvider)

Example 19 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class UserInstanceOutputFilterStream method addMember.

private void addMember(String member, XWikiDocument groupDocument, BaseClass groupClass, XWikiContext xcontext) throws FilterException {
    BaseObject memberObject;
    try {
        memberObject = groupDocument.newXObject(groupClass.getReference(), xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to add a group member object", e);
    }
    memberObject.setStringValue("member", member);
}
Also used : FilterException(org.xwiki.filter.FilterException) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 20 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class XWikiAttachmentOutputFilterStream method onWikiAttachment.

@Override
public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters) throws FilterException {
    if (this.entity == null) {
        this.entity = new XWikiAttachment();
    }
    this.entity.setFilename(name);
    if (content != null) {
        try {
            this.entity.setContent(content);
        } catch (IOException e) {
            throw new FilterException("Failed to set attachment content", e);
        }
    }
    // Author
    this.entity.setAuthorReference(getUserReference(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, parameters, null));
    if (this.properties == null || this.properties.isVersionPreserved()) {
        setVersion(parameters);
        this.entity.setComment(getString(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, parameters, ""));
        this.entity.setDate(getDate(WikiAttachmentFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
        this.entity.setMimeType(getString(WikiAttachmentFilter.PARAMETER_MIMETYPE, parameters, null));
        String revisions = getString(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS, parameters, null);
        if (revisions != null) {
            try {
                this.entity.setArchive(revisions);
            } catch (XWikiException e) {
                throw new FilterException("Failed to set attachment archive", e);
            }
        }
        this.entity.setMetaDataDirty(false);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

FilterException (org.xwiki.filter.FilterException)35 IOException (java.io.IOException)17 XWikiException (com.xpn.xwiki.XWikiException)10 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Date (java.util.Date)6 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ArrayList (java.util.ArrayList)4 InputFilterStream (org.xwiki.filter.input.InputFilterStream)3 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)3 WikiReference (org.xwiki.model.reference.WikiReference)3 QueryException (org.xwiki.query.QueryException)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2