Search in sources :

Example 1 with EventParameter

use of org.xwiki.filter.xar.internal.XARFilterUtils.EventParameter in project xwiki-platform by xwiki.

the class AttachmentReader method read.

@Override
public WikiAttachment read(XMLStreamReader xmlReader, XARInputProperties properties) throws XMLStreamException, FilterException {
    WikiAttachment wikiAttachment = new WikiAttachment();
    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();
        EventParameter parameter = XARAttachmentModel.ATTACHMENT_PARAMETERS.get(elementName);
        if (parameter != null) {
            Object wsValue = convert(parameter.type, xmlReader.getElementText());
            if (wsValue != null) {
                wikiAttachment.parameters.put(parameter.name, wsValue);
            }
        } else {
            if (XARAttachmentModel.ELEMENT_NAME.equals(elementName)) {
                wikiAttachment.name = xmlReader.getElementText();
            } else if (XARAttachmentModel.ELEMENT_CONTENT_SIZE.equals(elementName)) {
                wikiAttachment.size = Long.valueOf(xmlReader.getElementText());
            } else if (XARAttachmentModel.ELEMENT_CONTENT.equals(elementName)) {
                // We copy the attachment content to use it later. We can't directly send it as a stream because XAR
                // specification does not force any order for the attachment properties and we need to be sure we
                // have everything when sending the event.
                // Allocate a temporary file in case the attachment content is big
                File temporaryFile;
                try {
                    temporaryFile = File.createTempFile("xar/attachments/attachment", ".bin");
                } catch (IOException e) {
                    throw new FilterException(e);
                }
                // Create a deferred file based content (if the content is bigger than 10000 bytes it will end up in
                // a file)
                wikiAttachment.content = new DeferredFileOutputStream(100000, temporaryFile);
                // Copy the content to byte array or file depending on its size
                for (xmlReader.next(); xmlReader.isCharacters(); xmlReader.next()) {
                    try {
                        wikiAttachment.content.write(xmlReader.getText().getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        throw new FilterException(e);
                    }
                }
            }
        }
    }
    return wikiAttachment;
}
Also used : EventParameter(org.xwiki.filter.xar.internal.XARFilterUtils.EventParameter) FilterException(org.xwiki.filter.FilterException) IOException(java.io.IOException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File)

Example 2 with EventParameter

use of org.xwiki.filter.xar.internal.XARFilterUtils.EventParameter in project xwiki-platform by xwiki.

the class DocumentLocaleReader method readDocument.

private void readDocument(XMLStreamReader xmlReader, Object filter, XARInputFilter proxyFilter) throws XMLStreamException, FilterException {
    xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);
    this.currentSourceType = SourceType.DOCUMENT;
    // Initialize with a few defaults (thing that don't exist in old XAR format)
    this.currentDocumentRevisionParameters.put(XWikiWikiDocumentFilter.PARAMETER_SYNTAX, Syntax.XWIKI_1_0);
    this.currentDocumentRevisionParameters.put(XWikiWikiDocumentFilter.PARAMETER_HIDDEN, false);
    // Reference
    String referenceString = xmlReader.getAttributeValue(null, XARDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
    if (StringUtils.isNotEmpty(referenceString)) {
        this.currentDocumentReference = this.relativeResolver.resolve(referenceString, EntityType.DOCUMENT);
        this.currentSpaceReference = this.currentDocumentReference.getParent();
        // Send needed wiki spaces event if possible
        switchWikiSpace(proxyFilter, false);
    }
    // Locale
    String localeString = xmlReader.getAttributeValue(null, XARDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
    if (localeString != null) {
        this.currentDocumentLocale = toLocale(localeString);
        this.localeFromLegacy = false;
    }
    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();
        if (elementName.equals(XARAttachmentModel.ELEMENT_ATTACHMENT)) {
            readAttachment(xmlReader, filter, proxyFilter);
        } else if (elementName.equals(XARObjectModel.ELEMENT_OBJECT)) {
            readObject(xmlReader, filter, proxyFilter);
        } else if (elementName.equals(XARClassModel.ELEMENT_CLASS)) {
            readClass(xmlReader, filter, proxyFilter);
        } else {
            String value = xmlReader.getElementText();
            if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
                this.currentLegacySpace = value;
                if (this.currentDocumentReference == null) {
                    // Its an old thing
                    if (this.currentLegacyDocument == null) {
                        this.currentSpaceReference = new EntityReference(value, EntityType.SPACE);
                    } else {
                        this.currentDocumentReference = new LocalDocumentReference(this.currentLegacySpace, this.currentLegacyDocument);
                        this.currentSpaceReference = this.currentDocumentReference.getParent();
                    }
                    // Send needed wiki spaces event if possible
                    switchWikiSpace(proxyFilter, false);
                }
            } else if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
                this.currentLegacyDocument = value;
                if (this.currentDocumentReference == null) {
                    // Its an old thing
                    if (this.currentLegacySpace != null) {
                        this.currentDocumentReference = new LocalDocumentReference(this.currentLegacySpace, this.currentLegacyDocument);
                        this.currentSpaceReference = this.currentDocumentReference.getParent();
                    }
                }
            } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
                if (this.localeFromLegacy) {
                    this.currentDocumentLocale = toLocale(value);
                }
            } else if (XarDocumentModel.ELEMENT_REVISION.equals(elementName)) {
                this.currentDocumentRevision = value;
            } else {
                EventParameter parameter = XARDocumentModel.DOCUMENT_PARAMETERS.get(elementName);
                if (parameter != null) {
                    Object wsValue = convert(parameter.type, value);
                    if (wsValue != null) {
                        this.currentDocumentParameters.put(parameter.name, wsValue);
                    }
                } else {
                    parameter = XARDocumentModel.DOCUMENTLOCALE_PARAMETERS.get(elementName);
                    if (parameter != null) {
                        Object wsValue = convert(parameter.type, value);
                        if (wsValue != null) {
                            this.currentDocumentLocaleParameters.put(parameter.name, wsValue);
                        }
                    } else {
                        parameter = XARDocumentModel.DOCUMENTREVISION_PARAMETERS.get(elementName);
                        if (parameter != null) {
                            Object objectValue;
                            if (parameter.type == EntityReference.class) {
                                objectValue = this.relativeResolver.resolve(value, EntityType.DOCUMENT);
                            } else {
                                objectValue = convert(parameter.type, value);
                            }
                            if (objectValue != null) {
                                this.currentDocumentRevisionParameters.put(parameter.name, objectValue);
                            }
                        } else {
                        // Unknown property
                        // TODO: log something ?
                        }
                    }
                }
            }
        }
    }
    sendBeginWikiDocumentRevision(proxyFilter, true);
    sendWikiAttachments(proxyFilter);
    sendWikiClass(proxyFilter);
    sendWikiObjects(proxyFilter);
    sendEndWikiDocument(proxyFilter);
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) EventParameter(org.xwiki.filter.xar.internal.XARFilterUtils.EventParameter) EntityReference(org.xwiki.model.reference.EntityReference) WikiObject(org.xwiki.filter.xar.internal.input.WikiObjectReader.WikiObject)

Aggregations

EventParameter (org.xwiki.filter.xar.internal.XARFilterUtils.EventParameter)2 File (java.io.File)1 IOException (java.io.IOException)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1 FilterException (org.xwiki.filter.FilterException)1 WikiObject (org.xwiki.filter.xar.internal.input.WikiObjectReader.WikiObject)1 EntityReference (org.xwiki.model.reference.EntityReference)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1