Search in sources :

Example 11 with SAXReader

use of org.dom4j.io.SAXReader in project Openfire by igniterealtime.

the class PrivacyListProvider method loadDefaultPrivacyList.

/**
     * Loads the default privacy list of a given user from the database. Returns <tt>null</tt>
     * if no list was found.
     *
     * @param username the username of the user to get his default privacy list.
     * @return the default privacy list or <tt>null</tt> if no list was found.
     */
public PrivacyList loadDefaultPrivacyList(String username) {
    // If there are no privacy lists stored, this method is a no-op.
    if (!databaseContainsPrivacyLists.get()) {
        return null;
    }
    String listName = null;
    String listValue = null;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_DEFAULT_PRIVACY_LIST);
        pstmt.setString(1, username);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            listName = rs.getString(1);
            listValue = rs.getString(2);
        } else {
            return null;
        }
    } catch (Exception e) {
        Log.error("Error loading default privacy list of username: " + username, e);
        return null;
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    PrivacyList privacyList = null;
    SAXReader xmlReader = null;
    try {
        // Get a sax reader from the pool
        xmlReader = xmlReaders.take();
        Element listElement = xmlReader.read(new StringReader(listValue)).getRootElement();
        privacyList = new PrivacyList(username, listName, true, listElement);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    } finally {
        // Return the sax reader to the pool
        if (xmlReader != null) {
            xmlReaders.add(xmlReader);
        }
    }
    return privacyList;
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement)

Example 12 with SAXReader

use of org.dom4j.io.SAXReader in project Openfire by igniterealtime.

the class PublishedItem method getPayload.

/**
     * Returns the payload included when publishing the item. A published item may or may not
     * have a payload. Transient nodes that are configured to not broadcast payloads may allow
     * published items to have no payload.
     *
     * @return the payload included when publishing the item or <tt>null</tt> if none was found.
     */
public Element getPayload() {
    if (payload == null && payloadXML != null) {
        synchronized (this) {
            if (payload == null) {
                // payload initialized as XML string from DB
                SAXReader xmlReader = null;
                try {
                    xmlReader = xmlReaders.take();
                    payload = xmlReader.read(new StringReader(payloadXML)).getRootElement();
                } catch (Exception ex) {
                    log.error("Failed to parse payload XML", ex);
                } finally {
                    if (xmlReader != null) {
                        xmlReaders.add(xmlReader);
                    }
                }
            }
        }
    }
    return payload;
}
Also used : SAXReader(org.dom4j.io.SAXReader) StringReader(java.io.StringReader)

Example 13 with SAXReader

use of org.dom4j.io.SAXReader 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 14 with SAXReader

use of org.dom4j.io.SAXReader 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 15 with SAXReader

use of org.dom4j.io.SAXReader in project Openfire by igniterealtime.

the class OfflineMessageStore method start.

@Override
public void start() throws IllegalStateException {
    super.start();
    // Initialize the pool of sax readers
    for (int i = 0; i < POOL_SIZE; i++) {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        xmlReaders.add(xmlReader);
    }
    // Add this module as a user event listener so we can delete
    // all offline messages when a user is deleted
    UserEventDispatcher.addListener(this);
}
Also used : SAXReader(org.dom4j.io.SAXReader)

Aggregations

SAXReader (org.dom4j.io.SAXReader)172 Document (org.dom4j.Document)143 Element (org.dom4j.Element)128 StringReader (java.io.StringReader)98 Test (org.junit.Test)78 ArrayList (java.util.ArrayList)35 List (java.util.List)35 File (java.io.File)28 DocumentException (org.dom4j.DocumentException)21 IOException (java.io.IOException)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 Map (java.util.Map)10 Attribute (org.dom4j.Attribute)10 InputStream (java.io.InputStream)9 XMLWriter (org.dom4j.io.XMLWriter)9 OutputFormat (org.dom4j.io.OutputFormat)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6