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();
}
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());
}
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);
}
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);
}
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)));
}
Aggregations