Search in sources :

Example 16 with SAXReader

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

the class MUCRoomHistory method addOldMessage.

/**
     * Creates a new message and adds it to the history. The new message will be created based on
     * the provided information. This information will likely come from the database when loading
     * the room history from the database.
     *
     * @param senderJID the sender's JID of the message to add to the history.
     * @param nickname the sender's nickname of the message to add to the history.
     * @param sentDate the date when the message was sent to the room.
     * @param subject the subject included in the message.
     * @param body the body of the message.
     */
public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject, String body, String stanza) {
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    if (stanza != null) {
        // payload initialized as XML string from DB
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        try {
            Element element = xmlReader.read(new StringReader(stanza)).getRootElement();
            for (Element child : (List<Element>) element.elements()) {
                Namespace ns = child.getNamespace();
                if (ns == null || ns.getURI().equals("jabber:client") || ns.getURI().equals("jabber:server")) {
                    continue;
                }
                Element added = message.addChildElement(child.getName(), child.getNamespaceURI());
                if (!child.getText().isEmpty()) {
                    added.setText(child.getText());
                }
                for (Attribute attr : (List<Attribute>) child.attributes()) {
                    added.addAttribute(attr.getQName(), attr.getValue());
                }
                for (Element el : (List<Element>) child.elements()) {
                    added.add(el.createCopy());
                }
            }
            if (element.attribute("id") != null) {
                message.setID(element.attributeValue("id"));
            }
        } catch (Exception ex) {
            Log.error("Failed to parse payload XML", ex);
        }
    }
    message.setSubject(subject);
    message.setBody(body);
    // Set the sender of the message
    if (nickname != null && nickname.trim().length() > 0) {
        JID roomJID = room.getRole().getRoleAddress();
        // Recreate the sender address based on the nickname and room's JID
        message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
    } else {
        // Set the room as the sender of the message
        message.setFrom(room.getRole().getRoleAddress());
    }
    // Add the delay information to the message
    Element delayInformation = message.addChildElement("delay", "urn:xmpp:delay");
    delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(sentDate));
    if (room.canAnyoneDiscoverJID()) {
        // Set the Full JID as the "from" attribute
        delayInformation.addAttribute("from", senderJID);
    } else {
        // Set the Room JID as the "from" attribute
        delayInformation.addAttribute("from", room.getRole().getRoleAddress().toString());
    }
    historyStrategy.addMessage(message);
}
Also used : Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) List(java.util.List) Namespace(org.dom4j.Namespace) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 17 with SAXReader

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

Example 18 with SAXReader

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

the class KrakenPlugin method getOptionsConfig.

/**
     * Returns the web options config for the given transport, if it exists.
     *
     * @param type type of the transport we want the options config for.
     * @return XML document with the options config.
     */
public Document getOptionsConfig(TransportType type) {
    // Load any custom-defined servlets.
    File optConf = new File(this.pluginDirectory, "web" + File.separator + "WEB-INF" + File.separator + "options" + File.separator + type.toString() + ".xml");
    Document optConfXML;
    try {
        FileReader reader = new FileReader(optConf);
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        optConfXML = xmlReader.read(reader);
    } catch (FileNotFoundException e) {
        // Non-existent: Return empty config
        optConfXML = DocumentHelper.createDocument();
        optConfXML.addElement("optionsconfig");
    } catch (DocumentException e) {
        // Bad config: Return empty config
        optConfXML = DocumentHelper.createDocument();
        optConfXML.addElement("optionsconfig");
    }
    return optConfXML;
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) Document(org.dom4j.Document) File(java.io.File)

Example 19 with SAXReader

use of org.dom4j.io.SAXReader in project hudson-2.x by hudson.

the class SuiteResult method parse.

/**
     * Parses the JUnit XML file into {@link SuiteResult}s.
     * This method returns a collection, as a single XML may have multiple &lt;testsuite>
     * elements wrapped into the top-level &lt;testsuites>.
     */
static List<SuiteResult> parse(File xmlReport, boolean keepLongStdio) throws DocumentException, IOException {
    List<SuiteResult> r = new ArrayList<SuiteResult>();
    // parse into DOM
    SAXReader saxReader = new SAXReader();
    // install EntityResolver for resolving DTDs, which are in files created by TestNG.
    XMLEntityResolver resolver = new XMLEntityResolver();
    saxReader.setEntityResolver(resolver);
    Document result = saxReader.read(xmlReport);
    Element root = result.getRootElement();
    getTestSuites(root, r, xmlReport, keepLongStdio);
    return r;
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document)

Example 20 with SAXReader

use of org.dom4j.io.SAXReader in project randomizedtesting by randomizedtesting.

the class JUnit4Mojo method appendRawXml.

/**
   * Append raw XML configuration. 
   */
private void appendRawXml(PlexusConfiguration config, Element elem) {
    try {
        if (config == null) {
            return;
        }
        StringWriter writer = new StringWriter();
        AntrunXmlPlexusConfigurationWriter xmlWriter = new AntrunXmlPlexusConfigurationWriter();
        xmlWriter.write(config, writer);
        Element root = new SAXReader().read(new StringReader(writer.toString())).getRootElement();
        root.detach();
        elem.add(root);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) AntrunXmlPlexusConfigurationWriter(org.apache.maven.plugin.antrun.AntrunXmlPlexusConfigurationWriter) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

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