Search in sources :

Example 1 with Div

use of org.apache.ecs.html.Div in project xwiki-platform by xwiki.

the class WatchListEvent method getObjectsHTMLDiff.

/**
 * @param objectDiffs List of object diff
 * @param isXWikiClass is the diff to compute the diff for a xwiki class, the other possibility being a plain xwiki
 *            object
 * @param documentFullName full name of the document the diff is computed for
 * @param diff the diff plugin API
 * @return The HTML diff
 */
private String getObjectsHTMLDiff(List<List<ObjectDiff>> objectDiffs, boolean isXWikiClass, String documentFullName, DiffPluginApi diff) {
    StringBuffer result = new StringBuffer();
    String propSeparator = ": ";
    String prefix = (isXWikiClass) ? "class" : "object";
    try {
        for (List<ObjectDiff> oList : objectDiffs) {
            if (oList.size() > 0) {
                Div mainDiv = createDiffDiv(prefix + "Diff");
                Span objectName = createDiffSpan(prefix + "ClassName");
                if (isXWikiClass) {
                    objectName.addElement(getFullName());
                } else {
                    objectName.addElement(oList.get(0).getClassName());
                }
                mainDiv.addElement(prefix + HTML_IMG_PLACEHOLDER_SUFFIX);
                mainDiv.addElement(objectName);
                for (ObjectDiff oDiff : oList) {
                    String propDiff = getPropertyHTMLDiff(oDiff, diff);
                    if (!StringUtils.isBlank(oDiff.getPropName()) && !StringUtils.isBlank(propDiff)) {
                        Div propDiv = createDiffDiv("propDiffContainer");
                        Span propNameSpan = createDiffSpan("propName");
                        propNameSpan.addElement(oDiff.getPropName() + propSeparator);
                        String shortPropType = StringUtils.removeEnd(oDiff.getPropType(), "Class").toLowerCase();
                        if (StringUtils.isBlank(shortPropType)) {
                            // When the diff shows a property that has been deleted, its type is not available.
                            shortPropType = HTML_IMG_METADATA_PREFIX;
                        }
                        propDiv.addElement(shortPropType + HTML_IMG_PLACEHOLDER_SUFFIX);
                        propDiv.addElement(propNameSpan);
                        Div propDiffDiv = createDiffDiv("propDiff");
                        propDiffDiv.addElement(propDiff);
                        propDiv.addElement(propDiffDiv);
                        mainDiv.addElement(propDiv);
                    }
                }
                result.append(mainDiv);
            }
        }
    } catch (XWikiException e) {
        // Catch the exception to be sure we won't send emails containing stacktraces to users.
        e.printStackTrace();
    }
    return result.toString();
}
Also used : Div(org.apache.ecs.html.Div) ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) Span(org.apache.ecs.html.Span) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with Div

use of org.apache.ecs.html.Div in project xwiki-platform by xwiki.

the class WatchListEvent method getHTMLDiff.

/**
 * @return The diff, formatted in HTML, to display to the user when a document has been updated, or null if an error
 *         occurred while computing the diff
 */
public String getHTMLDiff() {
    if (htmlDiff == null) {
        try {
            DiffPluginApi diff = (DiffPluginApi) context.getWiki().getPluginApi("diff", context);
            StringBuffer result = new StringBuffer();
            XWikiDocument d2 = context.getWiki().getDocument(getPrefixedFullName(), context);
            if (getType().equals(WatchListEventType.CREATE)) {
                d2 = context.getWiki().getDocument(d2, INITIAL_DOCUMENT_VERSION, context);
            }
            XWikiDocument d1 = context.getWiki().getDocument(d2, getPreviousVersion(), context);
            List<AttachmentDiff> attachDiffs = d2.getAttachmentDiff(d1, d2, context);
            List<List<ObjectDiff>> objectDiffs = d2.getObjectDiff(d1, d2, context);
            List<List<ObjectDiff>> classDiffs = d2.getClassDiff(d1, d2, context);
            List<MetaDataDiff> metaDiffs = d2.getMetaDataDiff(d1, d2, context);
            if (!d1.getContent().equals(d2.getContent())) {
                Div contentDiv = createDiffDiv("contentDiff");
                String contentDiff = diff.getDifferencesAsHTML(d1.getContent(), d2.getContent(), false);
                contentDiv.addElement(contentDiff);
                result.append(contentDiv);
            }
            for (AttachmentDiff aDiff : attachDiffs) {
                Div attachmentDiv = createDiffDiv("attachmentDiff");
                attachmentDiv.addElement(HTML_IMG_ATTACHMENT_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
                attachmentDiv.addElement(aDiff.toString());
                result.append(attachmentDiv);
            }
            result.append(getObjectsHTMLDiff(objectDiffs, false, getFullName(), diff));
            result.append(getObjectsHTMLDiff(classDiffs, true, getFullName(), diff));
            for (MetaDataDiff mDiff : metaDiffs) {
                Div metaDiv = createDiffDiv("metaDiff");
                metaDiv.addElement(HTML_IMG_METADATA_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
                metaDiv.addElement(mDiff.toString());
                result.append(metaDiv);
            }
            htmlDiff = result.toString();
        } catch (XWikiException e) {
            // Catch the exception to be sure we won't send emails containing stacktraces to users.
            e.printStackTrace();
        }
    }
    return htmlDiff;
}
Also used : MetaDataDiff(com.xpn.xwiki.doc.MetaDataDiff) Div(org.apache.ecs.html.Div) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DiffPluginApi(com.xpn.xwiki.plugin.diff.DiffPluginApi) ArrayList(java.util.ArrayList) List(java.util.List) XWikiException(com.xpn.xwiki.XWikiException) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Example 3 with Div

use of org.apache.ecs.html.Div in project xwiki-platform by xwiki.

the class DefaultWatchListEventHTMLDiffExtractor method createDiffDiv.

/**
 * @param classAttr The class of the div to create
 * @return a HTML div element
 */
private static Div createDiffDiv(String classAttr) {
    Div div = new Div();
    div.setClass(classAttr);
    div.setStyle(HTML_STYLE_PLACEHOLDER_PREFIX + classAttr);
    return div;
}
Also used : Div(org.apache.ecs.html.Div)

Example 4 with Div

use of org.apache.ecs.html.Div in project xwiki-platform by xwiki.

the class WatchListEvent method createDiffDiv.

/**
 * @param classAttr The class of the div to create
 * @return a HTML div element
 */
private Div createDiffDiv(String classAttr) {
    Div div = new Div();
    div.setClass(classAttr);
    div.setStyle(HTML_STYLE_PLACEHOLDER_PREFIX + classAttr);
    return div;
}
Also used : Div(org.apache.ecs.html.Div)

Example 5 with Div

use of org.apache.ecs.html.Div in project xwiki-platform by xwiki.

the class DefaultWatchListEventHTMLDiffExtractor method getObjectsHTMLDiff.

/**
 * @param objectDiffs the list of object diff
 * @param isXWikiClass true if the diff to compute is for an XWiki class, false if it's a plain XWiki object
 * @param documentFullName full name of the document the diff is computed for
 * @param diff the diff plugin API
 * @return the HTML string displaying the specified list of diffs
 */
private String getObjectsHTMLDiff(List<List<ObjectDiff>> objectDiffs, boolean isXWikiClass, String documentFullName, DiffPluginApi diff) {
    StringBuffer result = new StringBuffer();
    String propSeparator = ": ";
    String prefix = (isXWikiClass) ? "class" : "object";
    try {
        for (List<ObjectDiff> oList : objectDiffs) {
            if (oList.isEmpty()) {
                continue;
            }
            // The main container
            Div mainDiv = createDiffDiv(prefix + "Diff");
            // Class name
            Span objectName = createDiffSpan(prefix + "ClassName");
            if (isXWikiClass) {
                objectName.addElement(documentFullName);
            } else {
                objectName.addElement(oList.get(0).getClassName());
            }
            mainDiv.addElement(prefix + HTML_IMG_PLACEHOLDER_SUFFIX);
            mainDiv.addElement(objectName);
            // Diffs for each property
            for (ObjectDiff oDiff : oList) {
                String propDiff = getPropertyHTMLDiff(oDiff, diff);
                if (StringUtils.isBlank(oDiff.getPropName()) || StringUtils.isBlank(propDiff)) {
                    // Skip invalid properties or the ones that have no changed.
                    continue;
                }
                Div propDiv = createDiffDiv("propDiffContainer");
                // Property name
                Span propNameSpan = createDiffSpan("propName");
                propNameSpan.addElement(oDiff.getPropName() + propSeparator);
                String shortPropType = StringUtils.removeEnd(oDiff.getPropType(), "Class").toLowerCase();
                if (StringUtils.isBlank(shortPropType)) {
                    // When the diff shows a property that has been deleted, its type is not available.
                    shortPropType = HTML_IMG_METADATA_PREFIX;
                }
                propDiv.addElement(shortPropType + HTML_IMG_PLACEHOLDER_SUFFIX);
                propDiv.addElement(propNameSpan);
                // Property diff
                Div propDiffDiv = createDiffDiv("propDiff");
                propDiffDiv.addElement(propDiff);
                propDiv.addElement(propDiffDiv);
                // Add it to the main div
                mainDiv.addElement(propDiv);
            }
            result.append(mainDiv);
        }
    } catch (XWikiException e) {
        // Catch the exception to be sure we won't send emails containing stacktraces to users.
        logger.error("Failed to compute HTML objects or class diff", e);
    }
    return result.toString();
}
Also used : Div(org.apache.ecs.html.Div) ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) Span(org.apache.ecs.html.Span) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Div (org.apache.ecs.html.Div)6 XWikiException (com.xpn.xwiki.XWikiException)4 AttachmentDiff (com.xpn.xwiki.doc.AttachmentDiff)2 MetaDataDiff (com.xpn.xwiki.doc.MetaDataDiff)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ObjectDiff (com.xpn.xwiki.objects.ObjectDiff)2 DiffPluginApi (com.xpn.xwiki.plugin.diff.DiffPluginApi)2 List (java.util.List)2 Span (org.apache.ecs.html.Span)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 ArrayList (java.util.ArrayList)1 WatchListException (org.xwiki.watchlist.internal.api.WatchListException)1