/**
   * Get an old revision of the attachment which this is an archive of.
   *
   * @param attachment This attachment will be used to get the document to associate the attachment
   *     revision with.
   * @param rev a String representation of the version to load.
   * @param context the context for the request which needed this revision.
   * @return an XWikiAttachment for the given revision.
   * @throws XWikiException if any Exception is thrown while getting the revision.
   */
  public XWikiAttachment getRevision(
      final XWikiAttachment attachment, final String rev, final XWikiContext context)
      throws XWikiException {
    try {
      final Archive rcsArchive = getRCSArchive();

      if (rcsArchive == null) {
        return null;
      }

      final Version version = rcsArchive.getRevisionVersion(rev);
      if (version == null) {
        return null;
      }
      final Object[] lines = rcsArchive.getRevision(version);
      final StringBuffer content = new StringBuffer();
      for (int i = 0; i < lines.length; i++) {
        String line = lines[i].toString();
        content.append(line);
        if (i != lines.length - 1) {
          content.append("\n");
        }
      }

      final String scontent = content.toString();
      final XWikiAttachment revattach = new XWikiAttachment();
      revattach.fromXML(scontent);
      revattach.setDoc(attachment.getDoc());
      revattach.setVersion(rev);
      return revattach;
    } catch (Exception e) {
      final Object[] args = {attachment.getFilename()};
      throw new XWikiException(
          XWikiException.MODULE_XWIKI_STORE,
          XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT,
          GENERIC_EXCEPTION_MESSAGE,
          e,
          args);
    }
  }
Пример #2
0
  /**
   * Unit test for <a href="http://jira.xwiki.org/browse/XWIKI-9075">XWIKI-9075</a> to prove that
   * calling {@code fromXML} doesn't set the metadata dirty flag.
   *
   * <p>Note: I think there's a bug in that fromXML should return a new instance of XWikiAttachment
   * and not modify the current one as this would mean changing its identity...
   */
  @Test
  public void fromXML() throws Exception {
    XWikiAttachment attachment = new XWikiAttachment();
    attachment.fromXML(
        "<attachment>\n"
            + "<filename>XWikiLogo.png</filename>\n"
            + "<filesize>1390</filesize>\n"
            + "<mimetype>image/png2</mimetype>\n"
            + "<author>xwiki:XWiki.Admin</author>\n"
            + "<date>1252454400000</date>\n"
            + "<version>1.1</version>\n"
            + "<comment/>\n"
            + "<content>MDEyMzQ1Njc4OQ==</content>\n"
            + "</attachment>");

    assertEquals("XWikiLogo.png", attachment.getFilename());
    assertEquals(new Date(1252454400000L), attachment.getDate());
    assertEquals("1.1", attachment.getVersion());
    assertEquals("0123456789", IOUtils.toString(attachment.getContentInputStream(null)));
    assertEquals("image/png2", attachment.getMimeType());
    assertEquals("image/png2", attachment.getMimeType(null));

    assertFalse(attachment.isMetaDataDirty());
  }