Search in sources :

Example 61 with Element

use of org.dom4j.Element in project Openfire by igniterealtime.

the class DelegateVCardProvider method createVCard.

@Override
public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException {
    final Element element = delegate.createVCard(username, vCardElement);
    Resizer.resizeAvatar(element);
    return element;
}
Also used : Element(org.dom4j.Element)

Example 62 with Element

use of org.dom4j.Element in project Openfire by igniterealtime.

the class Resizer method resizeAvatar.

public static void resizeAvatar(final Element vCardElement) {
    if (vCardElement == null) {
        return;
    }
    // XPath didn't work?
    if (vCardElement.element("PHOTO") == null) {
        return;
    }
    if (vCardElement.element("PHOTO").element("BINVAL") == null || vCardElement.element("PHOTO").element("TYPE") == null) {
        return;
    }
    final Element element = vCardElement.element("PHOTO").element("BINVAL");
    if (element.getTextTrim() == null || element.getTextTrim().isEmpty()) {
        return;
    }
    // Get a writer (check if we can generate a new image for the type of the original).
    final String type = vCardElement.element("PHOTO").element("TYPE").getTextTrim();
    final Iterator it = ImageIO.getImageWritersByMIMEType(type);
    if (!it.hasNext()) {
        Log.debug("Cannot resize avatar. No writers available for MIME type {}.", type);
        return;
    }
    final ImageWriter iw = (ImageWriter) it.next();
    // Extract the original avatar from the VCard.
    final byte[] original = Base64.decode(element.getTextTrim());
    // Crop and shrink, if needed.
    final int targetDimension = JiveGlobals.getIntProperty("avatar.resize.targetdimension", 96);
    final byte[] resized = cropAndShrink(original, targetDimension, iw);
    // If a resized image was created, replace to original avatar in the VCard.
    if (resized != null) {
        Log.debug("Replacing original avatar in vcard with a resized variant.");
        vCardElement.element("PHOTO").element("BINVAL").setText(Base64.encodeBytes(resized));
    }
}
Also used : Element(org.dom4j.Element) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter)

Example 63 with Element

use of org.dom4j.Element in project Openfire by igniterealtime.

the class AdminConsole method overrideEntry.

private static void overrideEntry(Element entry, Element overrideEntry) {
    // Override name.
    if (overrideEntry.attributeValue("name") != null) {
        entry.addAttribute("name", overrideEntry.attributeValue("name"));
    }
    if (overrideEntry.attributeValue("url") != null) {
        entry.addAttribute("url", overrideEntry.attributeValue("url"));
    }
    if (overrideEntry.attributeValue("description") != null) {
        entry.addAttribute("description", overrideEntry.attributeValue("description"));
    }
    if (overrideEntry.attributeValue("plugin") != null) {
        entry.addAttribute("plugin", overrideEntry.attributeValue("plugin"));
    }
    // Override any sidebars contained in the entry.
    for (Iterator i = overrideEntry.elementIterator(); i.hasNext(); ) {
        Element sidebar = (Element) i.next();
        String id = sidebar.attributeValue("id");
        Element existingSidebar = getElemnetByID(id);
        // Simple case, there is no existing sidebar with the same id.
        if (existingSidebar == null) {
            entry.add(sidebar.createCopy());
        } else // More complex case -- a sidebar with the same id already exists.
        // In this case, we have to overrite only the difference between
        // the two elements.
        {
            overrideSidebar(existingSidebar, sidebar);
        }
    }
}
Also used : Element(org.dom4j.Element) Iterator(java.util.Iterator)

Example 64 with Element

use of org.dom4j.Element in project Openfire by igniterealtime.

the class AdminConsole method overrideSidebar.

private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
    // Override name.
    if (overrideSidebar.attributeValue("name") != null) {
        sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
    }
    if (overrideSidebar.attributeValue("plugin") != null) {
        sidebar.addAttribute("plugin", overrideSidebar.attributeValue("plugin"));
    }
    // Override entries.
    for (Iterator i = overrideSidebar.elementIterator(); i.hasNext(); ) {
        Element entry = (Element) i.next();
        String id = entry.attributeValue("id");
        Element existingEntry = getElemnetByID(id);
        // Simple case, there is no existing sidebar with the same id.
        if (existingEntry == null) {
            sidebar.add(entry.createCopy());
        } else // More complex case -- an entry with the same id already exists.
        // In this case, we have to overrite only the difference between
        // the two elements.
        {
            overrideEntry(existingEntry, entry);
        }
    }
}
Also used : Element(org.dom4j.Element) Iterator(java.util.Iterator)

Example 65 with Element

use of org.dom4j.Element in project Openfire by igniterealtime.

the class AdminConsole method rebuildModel.

/**
     * Rebuilds the generated model.
     */
private static synchronized void rebuildModel() {
    Document doc = DocumentFactory.getInstance().createDocument();
    generatedModel = coreModel.createCopy();
    doc.add(generatedModel);
    // Add in all overrides.
    for (Element element : overrideModels.values()) {
        // See if global settings are overriden.
        Element appName = (Element) element.selectSingleNode("//adminconsole/global/appname");
        if (appName != null) {
            Element existingAppName = (Element) generatedModel.selectSingleNode("//adminconsole/global/appname");
            existingAppName.setText(appName.getText());
            if (appName.attributeValue("plugin") != null) {
                existingAppName.addAttribute("plugin", appName.attributeValue("plugin"));
            }
        }
        Element appLogoImage = (Element) element.selectSingleNode("//adminconsole/global/logo-image");
        if (appLogoImage != null) {
            Element existingLogoImage = (Element) generatedModel.selectSingleNode("//adminconsole/global/logo-image");
            existingLogoImage.setText(appLogoImage.getText());
            if (appLogoImage.attributeValue("plugin") != null) {
                existingLogoImage.addAttribute("plugin", appLogoImage.attributeValue("plugin"));
            }
        }
        Element appLoginImage = (Element) element.selectSingleNode("//adminconsole/global/login-image");
        if (appLoginImage != null) {
            Element existingLoginImage = (Element) generatedModel.selectSingleNode("//adminconsole/global/login-image");
            existingLoginImage.setText(appLoginImage.getText());
            if (appLoginImage.attributeValue("plugin") != null) {
                existingLoginImage.addAttribute("plugin", appLoginImage.attributeValue("plugin"));
            }
        }
        Element appVersion = (Element) element.selectSingleNode("//adminconsole/global/version");
        if (appVersion != null) {
            Element existingVersion = (Element) generatedModel.selectSingleNode("//adminconsole/global/version");
            if (existingVersion != null) {
                existingVersion.setText(appVersion.getText());
                if (appVersion.attributeValue("plugin") != null) {
                    existingVersion.addAttribute("plugin", appVersion.attributeValue("plugin"));
                }
            } else {
                ((Element) generatedModel.selectSingleNode("//adminconsole/global")).add(appVersion.createCopy());
            }
        }
        // Tabs
        for (Object o : element.selectNodes("//tab")) {
            Element tab = (Element) o;
            String id = tab.attributeValue("id");
            Element existingTab = getElemnetByID(id);
            // Simple case, there is no existing tab with the same id.
            if (existingTab == null) {
                // url of the first item.
                if (tab.attributeValue("url") == null) {
                    Element firstItem = (Element) tab.selectSingleNode("//item[@url]");
                    if (firstItem != null) {
                        tab.addAttribute("url", firstItem.attributeValue("url"));
                    }
                }
                generatedModel.add(tab.createCopy());
            } else // More complex case -- a tab with the same id already exists.
            // In this case, we have to overrite only the difference between
            // the two elements.
            {
                overrideTab(existingTab, tab);
            }
        }
    }
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document)

Aggregations

Element (org.dom4j.Element)2207 Document (org.dom4j.Document)500 ArrayList (java.util.ArrayList)294 List (java.util.List)249 SAXReader (org.dom4j.io.SAXReader)196 Iterator (java.util.Iterator)163 IQ (org.xmpp.packet.IQ)142 HashMap (java.util.HashMap)135 IOException (java.io.IOException)114 File (java.io.File)101 Attribute (org.dom4j.Attribute)97 StringReader (java.io.StringReader)90 DefaultElement (org.dom4j.tree.DefaultElement)87 JID (org.xmpp.packet.JID)87 Test (org.junit.jupiter.api.Test)78 DocumentException (org.dom4j.DocumentException)74 QName (org.dom4j.QName)68 AnnotatedElement (java.lang.reflect.AnnotatedElement)64 Node (org.dom4j.Node)64 Test (org.junit.Test)64