Esempio n. 1
0
  /**
   * Send redirection based on a regexp pattern (if any) set at the main wiki level. To enable this
   * feature you must add xwiki.preferences.redirect=1 to your xwiki.cfg.
   *
   * @param response the servlet response
   * @param url url of the request
   * @param context the XWiki context
   * @return true if a redirection has been sent
   */
  protected boolean sendGlobalRedirect(XWikiResponse response, String url, XWikiContext context)
      throws Exception {
    if ("1".equals(context.getWiki().Param("xwiki.preferences.redirect"))) {
      // Note: This implementation is not performant at all and will slow down the wiki as the
      // number
      // of redirects increases. A better implementation would use a cache of redirects and would
      // use
      // the notification mechanism to update the cache when the XWiki.XWikiPreferences document is
      // modified.
      XWikiDocument globalPreferences =
          context.getWiki().getDocument("xwiki:XWiki.XWikiPreferences", context);
      Vector<BaseObject> redirects = globalPreferences.getObjects("XWiki.GlobalRedirect");

      if (redirects != null) {
        for (BaseObject redir : redirects) {
          if (redir != null) {
            String p = redir.getStringValue("pattern");
            if (p != null && url.matches(p)) {
              String dest = redir.getStringValue("destination");
              response.sendRedirect(url.replaceAll(p, dest));
              return true;
            }
          }
        }
      }
    }
    return false;
  }
  @Override
  public List<T> newXObjectDocumentList(List<XWikiDocument> documents, XWikiContext context)
      throws XWikiException {
    List<T> list;

    if (!documents.isEmpty()) {
      check(context);

      list = new ArrayList<T>(documents.size());

      for (XWikiDocument doc : documents) {
        List<BaseObject> objects = doc.getObjects(getClassFullName());

        for (BaseObject bobject : objects) {
          if (bobject != null) {
            list.add(newXObjectDocument(doc, bobject.getNumber(), context));
          }
        }
      }
    } else {
      list = Collections.emptyList();
    }

    return list;
  }
  /**
   * Creates a {@link WikiMacro} from an {@link XWikiDocument} which contains a macro definition.
   *
   * @param doc the {@link XWikiDocument} to look for a macro definition
   * @return the {@link WikiMacro} found inside the document
   * @throws WikiMacroException when an invalid macro definition or no macro definition was found
   */
  private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException {
    DocumentReference documentReference = doc.getDocumentReference();

    // Check whether this document contains a macro definition.
    BaseObject macroDefinition = doc.getObject(WIKI_MACRO_CLASS);
    if (null == macroDefinition) {
      throw new WikiMacroException(
          String.format("No macro definition found in document : [%s]", documentReference));
    }

    // Extract macro definition.
    String macroId = macroDefinition.getStringValue(MACRO_ID_PROPERTY);
    String macroName = macroDefinition.getStringValue(MACRO_NAME_PROPERTY);
    // The macro description as plain text
    String macroDescription = macroDefinition.getStringValue(MACRO_DESCRIPTION_PROPERTY);
    String macroDefaultCategory = macroDefinition.getStringValue(MACRO_DEFAULT_CATEGORY_PROPERTY);
    WikiMacroVisibility macroVisibility =
        WikiMacroVisibility.fromString(macroDefinition.getStringValue(MACRO_VISIBILITY_PROPERTY));
    boolean macroSupportsInlineMode =
        (macroDefinition.getIntValue(MACRO_INLINE_PROPERTY) == 0) ? false : true;
    String macroContentType = macroDefinition.getStringValue(MACRO_CONTENT_TYPE_PROPERTY);
    // The macro content description as plain text
    String macroContentDescription =
        macroDefinition.getStringValue(MACRO_CONTENT_DESCRIPTION_PROPERTY);
    String macroCode = macroDefinition.getStringValue(MACRO_CODE_PROPERTY);

    // Verify macro id.
    if (StringUtils.isEmpty(macroId)) {
      throw new WikiMacroException(
          String.format(
              "Incomplete macro definition in [%s], macro id is empty", documentReference));
    }

    // Verify macro name.
    if (StringUtils.isEmpty(macroName)) {
      macroName = macroId;
      this.logger.debug(
          String.format(
              "Incomplete macro definition in [%s], macro name is empty", documentReference));
    }

    // Verify macro description.
    if (StringUtils.isEmpty(macroDescription)) {
      this.logger.debug(
          String.format(
              "Incomplete macro definition in [%s], macro description is empty",
              documentReference));
    }

    // Verify default macro category.
    if (StringUtils.isEmpty(macroDefaultCategory)) {
      macroDefaultCategory = null;
      this.logger.debug(
          String.format(
              "Incomplete macro definition in [%s], default macro category is empty",
              documentReference));
    }

    // Verify macro content type.
    if (StringUtils.isEmpty(macroContentType)) {
      macroContentType = MACRO_CONTENT_OPTIONAL;
    }

    // Verify macro content description.
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY)
        && StringUtils.isEmpty(macroContentDescription)) {
      String errorMsg = "Incomplete macro definition in [%s], macro content description is empty";
      this.logger.debug(String.format(errorMsg, documentReference));
      macroContentDescription = "Macro content";
    }

    // Verify macro code.
    if (StringUtils.isEmpty(macroCode)) {
      throw new WikiMacroException(
          String.format(
              "Incomplete macro definition in [%s], macro code is empty", documentReference));
    }

    // Extract macro parameters.
    List<WikiMacroParameterDescriptor> parameterDescriptors =
        new ArrayList<WikiMacroParameterDescriptor>();
    Vector<BaseObject> macroParameters = doc.getObjects(WIKI_MACRO_PARAMETER_CLASS);
    if (null != macroParameters) {
      for (BaseObject macroParameter : macroParameters) {
        // Vectors can contain null values
        if (null == macroParameter) {
          continue;
        }

        // Extract parameter definition.
        String parameterName = macroParameter.getStringValue(PARAMETER_NAME_PROPERTY);
        String parameterDescription = macroParameter.getStringValue(PARAMETER_DESCRIPTION_PROPERTY);
        boolean parameterMandatory =
            (macroParameter.getIntValue(PARAMETER_MANDATORY_PROPERTY) == 0) ? false : true;
        String parameterDefaultValue =
            macroParameter.getStringValue(PARAMETER_DEFAULT_VALUE_PROPERTY);

        // Verify parameter name.
        if (StringUtils.isEmpty(parameterName)) {
          throw new WikiMacroException(
              String.format(
                  "Incomplete macro definition in [%s], macro parameter name is empty",
                  documentReference));
        }

        // Verify parameter description.
        if (StringUtils.isEmpty(parameterDescription)) {
          String errorMessage =
              "Incomplete macro definition in [%s], macro parameter description is empty";
          this.logger.debug(String.format(errorMessage, documentReference));
        }

        // If field empty, assume no default value was provided.
        if (StringUtils.isEmpty(parameterDefaultValue)) {
          parameterDefaultValue = null;
        }

        // Create the parameter descriptor.
        parameterDescriptors.add(
            new WikiMacroParameterDescriptor(
                parameterName, parameterDescription, parameterMandatory, parameterDefaultValue));
      }
    }

    // Create macro content descriptor.
    ContentDescriptor contentDescriptor = null;
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY)) {
      contentDescriptor =
          new DefaultContentDescriptor(
              macroContentDescription, macroContentType.equals(MACRO_CONTENT_MANDATORY));
    }

    // Create macro descriptor.
    MacroId id = new MacroId(macroId, doc.getSyntax());
    MacroDescriptor macroDescriptor =
        new WikiMacroDescriptor(
            id,
            macroName,
            macroDescription,
            macroDefaultCategory,
            macroVisibility,
            contentDescriptor,
            parameterDescriptors);

    XDOM xdom;
    try {
      xdom = parser.parse(macroCode, doc.getSyntax(), documentReference);
    } catch (MissingParserException ex) {
      throw new WikiMacroException("Could not find a parser for macro content", ex);
    } catch (ParseException ex) {
      throw new WikiMacroException("Error while parsing macro content", ex);
    }

    // Create & return the macro.
    return new DefaultWikiMacro(
        documentReference,
        doc.getAuthorReference(),
        macroSupportsInlineMode,
        macroDescriptor,
        xdom,
        doc.getSyntax(),
        this.componentManager);
  }