Search in sources :

Example 56 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class XWikiDocumentFilterUtils method exportEntity.

/**
 * @param entity the entity to read
 * @param target the target where to write the result
 * @param xarProperties the configuration of the output filter
 * @param documentProperties the configuration of the input filter
 * @throws ComponentLookupException failed to find an event generator for passed entity
 * @throws FilterException when failing to generate export the passed entity
 * @throws IOException when failing to close the stream
 */
public void exportEntity(Object entity, OutputTarget target, XAROutputProperties xarProperties, DocumentInstanceInputProperties documentProperties) throws ComponentLookupException, FilterException, IOException {
    // Input
    documentProperties.setVerbose(false);
    // Output
    xarProperties.setForceDocument(true);
    if (target != null) {
        xarProperties.setTarget(target);
    }
    xarProperties.setVerbose(false);
    BeanOutputFilterStream<XAROutputProperties> xarFilter = ((BeanOutputFilterStreamFactory<XAROutputProperties>) this.xarOutputFilterStreamFactory).createOutputFilterStream(xarProperties);
    XARFilter filter = (XARFilter) xarFilter.getFilter();
    BeanEntityEventGenerator<Object, DocumentInstanceInputProperties> generator = this.componentManager.getInstance(new DefaultParameterizedType(null, EntityEventGenerator.class, getClass(entity)));
    // Spaces and document events
    FilterEventParameters documentParameters = null;
    DocumentReference documentReference = null;
    if (entity instanceof XWikiDocument) {
        documentReference = ((XWikiDocument) entity).getDocumentReference();
        for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
            filter.beginWikiSpace(spaceReference.getName(), FilterEventParameters.EMPTY);
        }
        documentParameters = new FilterEventParameters();
        documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, ((XWikiDocument) entity).getDefaultLocale());
        filter.beginWikiDocument(documentReference.getName(), documentParameters);
    }
    // Document Locale events
    generator.write(entity, xarFilter, documentProperties);
    // Document and spaces events
    if (documentParameters != null) {
        filter.endWikiDocument(documentReference.getName(), documentParameters);
        documentReference = ((XWikiDocument) entity).getDocumentReference();
        for (EntityReference reference = documentReference.getParent(); reference instanceof SpaceReference; reference = reference.getParent()) {
            filter.beginWikiSpace(reference.getName(), FilterEventParameters.EMPTY);
        }
    }
    xarFilter.close();
}
Also used : BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) SpaceReference(org.xwiki.model.reference.SpaceReference) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) FilterEventParameters(org.xwiki.filter.FilterEventParameters) BeanEntityEventGenerator(org.xwiki.filter.instance.input.BeanEntityEventGenerator) EntityEventGenerator(org.xwiki.filter.instance.input.EntityEventGenerator) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) XARFilter(org.xwiki.filter.xar.internal.XARFilter) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 57 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DocumentInstanceInputEventGenerator method setWikiDocumentParameters.

@Override
public void setWikiDocumentParameters(String name, FilterEventParameters documentParameters) throws FilterException {
    DocumentReference reference = new DocumentReference(name, new SpaceReference(this.currentReference));
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument defaultDocument;
    try {
        defaultDocument = xcontext.getWiki().getDocument(reference, xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get document [" + reference + "]", e);
    }
    documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, defaultDocument.getDefaultLocale());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 58 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class BaseClass method setName.

/**
 * {@inheritDoc}
 * <p>
 * Note: BaseElement#setName() does not support setting reference anymore since 2.4M2. This was broken and has been
 * replaced by this overridden method. See XWIKI-5285
 * </p>
 *
 * @deprecated since 2.2M2 use {@link #setDocumentReference(org.xwiki.model.reference.DocumentReference)}
 */
@Deprecated
@Override
public void setName(String name) {
    if (this instanceof MetaClass || this instanceof PropertyMetaClass) {
        super.setName(name);
    } else {
        DocumentReference reference = getDocumentReference();
        if (reference != null) {
            EntityReference relativeReference = getRelativeEntityReferenceResolver().resolve(name, EntityType.DOCUMENT);
            reference = new DocumentReference(relativeReference.extractReference(EntityType.DOCUMENT).getName(), new SpaceReference(relativeReference.extractReference(EntityType.SPACE).getName(), reference.getParent().getParent()));
        } else {
            reference = getCurrentMixedDocumentReferenceResolver().resolve(name);
        }
        setDocumentReference(reference);
    }
    setDirty(true);
}
Also used : MetaClass(com.xpn.xwiki.objects.meta.MetaClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

Example 59 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class BaseObject method setName.

/**
 * {@inheritDoc}
 * <p>
 * Note: BaseElement.setName() does not support setting reference anymore since 2.4M2.
 * </p>
 *
 * @deprecated since 2.2M2 use {@link #setDocumentReference(org.xwiki.model.reference.DocumentReference)}
 */
@Deprecated
@Override
public void setName(String name) {
    DocumentReference reference = getDocumentReference();
    if (reference != null) {
        EntityReference relativeReference = getRelativeEntityReferenceResolver().resolve(name, EntityType.DOCUMENT);
        reference = new DocumentReference(relativeReference.extractReference(EntityType.DOCUMENT).getName(), new SpaceReference(relativeReference.extractReference(EntityType.SPACE).getName(), reference.getParent().getParent()));
    } else {
        reference = getCurrentMixedDocumentReferenceResolver().resolve(name);
    }
    setDocumentReference(reference);
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 60 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class XWikiDocumentMockitoTest method testReadFromTemplate.

/**
 * Unit test for {@link XWikiDocument#readFromTemplate(DocumentReference, XWikiContext)}.
 */
@Test
public void testReadFromTemplate() throws Exception {
    SpaceReference spaceReference = new SpaceReference("Space", new WikiReference("wiki"));
    XWikiDocument template = new XWikiDocument(new DocumentReference("Template", spaceReference));
    template.setParentReference(new EntityReference("Parent", EntityType.DOCUMENT, spaceReference));
    template.setTitle("Enter title here");
    template.setSyntax(Syntax.XWIKI_2_0);
    template.setContent("Enter content here");
    XWikiAttachment aliceAttachment = new XWikiAttachment(template, "alice.png");
    aliceAttachment.setContent(new ByteArrayInputStream("alice content".getBytes()));
    template.addAttachment(aliceAttachment);
    XWikiAttachment bobAttachment = new XWikiAttachment(template, "bob.png");
    bobAttachment.setContent(new ByteArrayInputStream("bob content".getBytes()));
    template.addAttachment(bobAttachment);
    XWikiContext xcontext = this.oldcore.getXWikiContext();
    this.oldcore.getSpyXWiki().saveDocument(template, xcontext);
    XWikiDocument target = new XWikiDocument(new DocumentReference("Page", spaceReference));
    XWikiAttachment aliceModifiedAttachment = new XWikiAttachment(target, "alice.png");
    aliceModifiedAttachment.setContent(new ByteArrayInputStream("alice modified content".getBytes()));
    target.addAttachment(aliceModifiedAttachment);
    XWikiAttachment carolAttachment = new XWikiAttachment(target, "carol.png");
    carolAttachment.setContent(new ByteArrayInputStream("carol content".getBytes()));
    target.addAttachment(carolAttachment);
    target.readFromTemplate(template.getDocumentReference(), xcontext);
    Assert.assertEquals(template.getDocumentReference(), target.getTemplateDocumentReference());
    Assert.assertEquals(template.getParentReference(), target.getParentReference());
    Assert.assertEquals(template.getTitle(), target.getTitle());
    Assert.assertEquals(template.getSyntax(), target.getSyntax());
    Assert.assertEquals(template.getContent(), target.getContent());
    assertEquals(3, target.getAttachmentList().size());
    assertTrue(IOUtils.contentEquals(target.getAttachment("alice.png").getContentInputStream(xcontext), aliceModifiedAttachment.getContentInputStream(xcontext)));
    assertTrue(IOUtils.contentEquals(target.getAttachment("bob.png").getContentInputStream(xcontext), bobAttachment.getContentInputStream(xcontext)));
    assertTrue(IOUtils.contentEquals(target.getAttachment("carol.png").getContentInputStream(xcontext), carolAttachment.getContentInputStream(xcontext)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

SpaceReference (org.xwiki.model.reference.SpaceReference)142 DocumentReference (org.xwiki.model.reference.DocumentReference)96 Test (org.junit.Test)83 WikiReference (org.xwiki.model.reference.WikiReference)58 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)21 ArrayList (java.util.ArrayList)11 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)11 XWikiException (com.xpn.xwiki.XWikiException)9 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)8 Expectations (org.jmock.Expectations)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)7 ComponentManager (org.xwiki.component.manager.ComponentManager)7 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)7