Search in sources :

Example 66 with Document

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

the class ContentFilterTest method testFilterChatMessage.

@Test
public void testFilterChatMessage() throws DocumentException, IOException, XmlPullParserException {
    String chatXML = "<message to=\"doe@127.0.0.1/Adium\" type=\"chat\" id=\"iChat_E8B5ED64\" from=\"bob@127.0.0.1/frodo\">" + "<body>fox</body>" + "<html xmlns=\"http://jabber.org/protocol/xhtml-im\">" + "<body xmlns=\"http://www.w3.org/1999/xhtml\" style=\"background-color:#E8A630;color:#000000\">fox</body>" + "</html>" + "<x xmlns=\"jabber:x:event\">" + "<composing/>" + "</x>" + "</message>";
    XPPReader packetReader = new XPPReader();
    Document doc = packetReader.read(new StringReader(chatXML));
    Message m = new Message(doc.getRootElement());
    // filter on the word "fox" and "dog"
    filter.setPatterns("fox,dog,message");
    filter.setMask("**");
    String expectedXML = chatXML.replaceAll("fox", filter.getMask());
    // do filter
    boolean matched = filter.filter(m);
    assertTrue(matched);
    assertEquals(expectedXML, expectedXML, m.toXML());
}
Also used : Message(org.xmpp.packet.Message) StringReader(java.io.StringReader) XPPReader(org.dom4j.io.XPPReader) Document(org.dom4j.Document) Test(org.junit.Test)

Example 67 with Document

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

the class AdminConsole method addModel.

/**
     * Adds XML stream to the tabs/sidebar model.
     *
     * @param name the name.
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
public static void addModel(String name, InputStream in) throws Exception {
    SAXReader saxReader = new SAXReader();
    Document doc = saxReader.read(in);
    addModel(name, (Element) doc.selectSingleNode("/adminconsole"));
}
Also used : SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document)

Example 68 with Document

use of org.dom4j.Document 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)

Example 69 with Document

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

the class AdminConsole method load.

private static void load() {
    // Load the core model as the admin-sidebar.xml file from the classpath.
    InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
    if (in == null) {
        Log.error("Failed to load admin-sidebar.xml file from Openfire classes - admin " + "console will not work correctly.");
        return;
    }
    try {
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(in);
        coreModel = (Element) doc.selectSingleNode("/adminconsole");
    } catch (Exception e) {
        Log.error("Failure when parsing main admin-sidebar.xml file", e);
    }
    try {
        in.close();
    } catch (Exception ignored) {
    // Ignore.
    }
    // Load other admin-sidebar.xml files from the classpath
    ClassLoader[] classLoaders = getClassLoaders();
    for (ClassLoader classLoader : classLoaders) {
        URL url = null;
        try {
            if (classLoader != null) {
                Enumeration e = classLoader.getResources("/META-INF/admin-sidebar.xml");
                while (e.hasMoreElements()) {
                    url = (URL) e.nextElement();
                    try {
                        in = url.openStream();
                        addModel("admin", in);
                    } finally {
                        try {
                            if (in != null) {
                                in.close();
                            }
                        } catch (Exception ignored) {
                        // Ignore.
                        }
                    }
                }
            }
        } catch (Exception e) {
            String msg = "Failed to load admin-sidebar.xml";
            if (url != null) {
                msg += " from resource: " + url.toString();
            }
            Log.warn(msg, e);
        }
    }
    rebuildModel();
}
Also used : Enumeration(java.util.Enumeration) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document) URL(java.net.URL)

Example 70 with Document

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

the class UpdateManager method loadAvailablePluginsInfo.

private void loadAvailablePluginsInfo() {
    Document xmlResponse;
    File file = new File(JiveGlobals.getHomeDirectory() + File.separator + "conf", "available-plugins.xml");
    if (!file.exists()) {
        return;
    }
    // Check read privs.
    if (!file.canRead()) {
        Log.warn("Cannot retrieve available plugins. File must be readable: " + file.getName());
        return;
    }
    try (FileReader reader = new FileReader(file)) {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        xmlResponse = xmlReader.read(reader);
    } catch (Exception e) {
        Log.error("Error reading available-plugins.xml", e);
        return;
    }
    // Parse info and recreate available plugins
    Iterator it = xmlResponse.getRootElement().elementIterator("plugin");
    while (it.hasNext()) {
        Element plugin = (Element) it.next();
        String pluginName = plugin.attributeValue("name");
        String latestVersion = plugin.attributeValue("latest");
        String icon = plugin.attributeValue("icon");
        String readme = plugin.attributeValue("readme");
        String changelog = plugin.attributeValue("changelog");
        String url = plugin.attributeValue("url");
        String licenseType = plugin.attributeValue("licenseType");
        String description = plugin.attributeValue("description");
        String author = plugin.attributeValue("author");
        String minServerVersion = plugin.attributeValue("minServerVersion");
        String fileSize = plugin.attributeValue("fileSize");
        AvailablePlugin available = new AvailablePlugin(pluginName, description, latestVersion, author, icon, changelog, readme, licenseType, minServerVersion, url, fileSize);
        // Add plugin to the list of available plugins at js.org
        availablePlugins.put(pluginName, available);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Iterator(java.util.Iterator) FileReader(java.io.FileReader) Document(org.dom4j.Document) File(java.io.File) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException)

Aggregations

Document (org.dom4j.Document)288 Element (org.dom4j.Element)192 SAXReader (org.dom4j.io.SAXReader)143 Test (org.junit.Test)102 StringReader (java.io.StringReader)88 File (java.io.File)57 ArrayList (java.util.ArrayList)47 List (java.util.List)40 IOException (java.io.IOException)27 DocumentException (org.dom4j.DocumentException)25 XMLWriter (org.dom4j.io.XMLWriter)22 HashMap (java.util.HashMap)19 Map (java.util.Map)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Attribute (org.dom4j.Attribute)15 Node (org.dom4j.Node)14 FileOutputStream (java.io.FileOutputStream)11 DavException (com.zimbra.cs.dav.DavException)10 OutputFormat (org.dom4j.io.OutputFormat)9