Ejemplo n.º 1
0
  /**
   * Add rendered document to ZIP stream.
   *
   * @param pageName the name (used with {@link com.xpn.xwiki.XWiki#getDocument(String,
   *     XWikiContext)}) of the page to render.
   * @param zos the ZIP output stream.
   * @param context the XWiki context.
   * @param vcontext the Velocity context.
   * @throws XWikiException error when rendering document.
   * @throws IOException error when rendering document.
   */
  private void renderDocument(
      String pageName, ZipOutputStream zos, XWikiContext context, VelocityContext vcontext)
      throws XWikiException, IOException {
    @SuppressWarnings("unchecked")
    EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.class);
    DocumentReference docReference =
        new DocumentReference(resolver.resolve(pageName, EntityType.DOCUMENT));
    XWikiDocument doc = context.getWiki().getDocument(docReference, context);

    String zipname = doc.getDocumentReference().getWikiReference().getName();
    for (EntityReference space : doc.getDocumentReference().getSpaceReferences()) {
      zipname += POINT + space.getName();
    }
    zipname += POINT + doc.getDocumentReference().getName();
    String language = doc.getLanguage();
    if (language != null && language.length() != 0) {
      zipname += POINT + language;
    }

    zipname += ".html";

    ZipEntry zipentry = new ZipEntry(zipname);
    zos.putNextEntry(zipentry);

    String originalDatabase = context.getDatabase();
    try {
      context.setDatabase(doc.getDocumentReference().getWikiReference().getName());
      context.setDoc(doc);
      vcontext.put(VCONTEXT_DOC, doc.newDocument(context));
      vcontext.put(VCONTEXT_CDOC, vcontext.get(VCONTEXT_DOC));

      XWikiDocument tdoc = doc.getTranslatedDocument(context);
      context.put(CONTEXT_TDOC, tdoc);
      vcontext.put(VCONTEXT_TDOC, tdoc.newDocument(context));

      String content = context.getWiki().evaluateTemplate("view.vm", context);

      zos.write(content.getBytes(context.getWiki().getEncoding()));
      zos.closeEntry();
    } finally {
      context.setDatabase(originalDatabase);
    }
  }
 private void setDocument(
     ScriptContext scriptContext, String key, XWikiDocument document, XWikiContext xcontext) {
   // Change the Document instance only if it's not already wrapping the same XWikiDocument
   // (otherwise we might
   // loose modifications made in a previous script and not yet saved)
   Document previousDoc = (Document) scriptContext.getAttribute(key);
   if (previousDoc == null || !previousDoc.same(document)) {
     Document apiDocument = document.newDocument(xcontext);
     scriptContext.setAttribute(key, apiDocument, ScriptContext.ENGINE_SCOPE);
   }
 }
Ejemplo n.º 3
0
 protected void handleRevision(XWikiContext context) throws XWikiException {
   String rev = context.getRequest().getParameter("rev");
   if (rev != null) {
     context.put("rev", rev);
     XWikiDocument doc = (XWikiDocument) context.get("doc");
     XWikiDocument tdoc = (XWikiDocument) context.get("tdoc");
     XWikiDocument rdoc =
         (!doc.getLanguage().equals(tdoc.getLanguage()))
             ? doc
             : context.getWiki().getDocument(doc, rev, context);
     XWikiDocument rtdoc =
         (doc.getLanguage().equals(tdoc.getLanguage()))
             ? rdoc
             : context.getWiki().getDocument(tdoc, rev, context);
     context.put("tdoc", rtdoc);
     context.put("cdoc", rdoc);
     context.put("doc", rdoc);
     VelocityContext vcontext = (VelocityContext) context.get("vcontext");
     vcontext.put("doc", rdoc.newDocument(context));
     vcontext.put("cdoc", vcontext.get("doc"));
     vcontext.put("tdoc", rtdoc.newDocument(context));
   }
 }
Ejemplo n.º 4
0
 public Group(XWikiDocument doc, XWikiContext context) throws XWikiException {
   this(doc.newDocument(context), context);
 }