Search in sources :

Example 1 with DocumentException

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

the class ChatTranscriptManager method getTextTranscriptFromSessionID.

/**
     * Return the plain text version of a chat transcript.
     *
     * @param sessionID the sessionID of the <code>ChatSession</code>
     * @return the plain text version of a chat transcript.
     */
public static String getTextTranscriptFromSessionID(String sessionID) {
    String transcript = null;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(GET_SESSION_TRANSCRIPT);
        pstmt.setString(1, sessionID);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            transcript = DbConnectionManager.getLargeTextField(rs, 1);
        }
    } catch (Exception ex) {
        Log.error(ex.getMessage(), ex);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    if (transcript == null || "".equals(transcript)) {
        return "";
    }
    // Define time zone used in the transcript.
    SimpleDateFormat UTC_FORMAT = new SimpleDateFormat(XMPPDateTimeFormat.XMPP_DELAY_DATETIME_FORMAT);
    UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
    final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
    Document element = null;
    try {
        element = DocumentHelper.parseText(transcript);
    } catch (DocumentException e) {
        Log.error(e.getMessage(), e);
    }
    StringBuilder buf = new StringBuilder();
    // Add the Messages and Presences contained in the retrieved transcript element
    for (Iterator<Element> it = element.getRootElement().elementIterator(); it.hasNext(); ) {
        Element packet = it.next();
        String name = packet.getName();
        String message = "";
        String from = "";
        if ("presence".equals(name)) {
            String type = packet.attributeValue("type");
            from = new JID(packet.attributeValue("from")).getResource();
            if (type == null) {
                message = from + " has joined the room";
            } else {
                message = from + " has left the room";
            }
        } else if ("message".equals(name)) {
            from = new JID(packet.attributeValue("from")).getResource();
            message = packet.elementText("body");
            message = StringUtils.escapeHTMLTags(message);
        }
        List<Element> el = packet.elements("x");
        for (Element ele : el) {
            if ("jabber:x:delay".equals(ele.getNamespaceURI())) {
                String stamp = ele.attributeValue("stamp");
                try {
                    String formattedDate;
                    synchronized (UTC_FORMAT) {
                        Date d = UTC_FORMAT.parse(stamp);
                        formattedDate = formatter.format(d);
                    }
                    if ("presence".equals(name)) {
                        buf.append("[").append(formattedDate).append("] ").append(message).append("\n");
                    } else {
                        buf.append("[").append(formattedDate).append("] ").append(from).append(": ").append(message).append("\n");
                    }
                } catch (ParseException e) {
                    Log.error(e.getMessage(), e);
                }
            }
        }
    }
    return buf.toString();
}
Also used : JID(org.xmpp.packet.JID) Element(org.dom4j.Element) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Document(org.dom4j.Document) DocumentException(org.dom4j.DocumentException) ParseException(java.text.ParseException) Date(java.util.Date) DocumentException(org.dom4j.DocumentException) ResultSet(java.sql.ResultSet) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DocumentException

use of org.dom4j.DocumentException in project tdi-studio-se by Talend.

the class AlfrescoOutputModelManager method addModel.

/**
     * Adds an Alfresco model definition file.
     * 
     * @param newModelFilePath
     * @throws AlfrescoOutputException if already added or error reading it
     */
public void addModel(String newModelFilePath) throws AlfrescoOutputException {
    if (this.availableModels.contains(newModelFilePath)) {
        //$NON-NLS-1$
        throw new AlfrescoOutputException(Messages.getString("AlfrescoOutputModelManager.alreadyAdded"));
    }
    this.availableModels.add(newModelFilePath);
    // parsing the model
    org.dom4j.Document modelDoc = null;
    try {
        modelDoc = new SAXReader().read(new File(newModelFilePath));
    } catch (DocumentException dex) {
        throw new AlfrescoOutputException(//$NON-NLS-1$ //$NON-NLS-2$
        Messages.getString("AlfrescoOutputModelManager.errorReadingModel") + " " + newModelFilePath, dex);
    }
    Element modelElt = modelDoc.getRootElement();
    //$NON-NLS-1$
    Element namespacesElt = modelElt.element("namespaces");
    if (namespacesElt != null) {
        //$NON-NLS-1$
        List<Element> namespaces = (List<Element>) namespacesElt.elements("namespace");
        HashMap<String, String> availablePrefixToNamespaceMapTmp = new HashMap<String, String>(3);
        for (Element namespace : namespaces) {
            //$NON-NLS-1$
            String namespacePrefix = namespace.attributeValue("prefix");
            if (this.availablePrefixToNamespaceMap.containsKey(namespacePrefix)) {
                throw new AlfrescoOutputException(//$NON-NLS-1$ //$NON-NLS-2$
                Messages.getString("AlfrescoOutputModelManager.prefixConflict") + " " + namespacePrefix + " " + //$NON-NLS-1$
                this.availablePrefixToNamespaceMap.get(namespacePrefix));
            }
            //$NON-NLS-1$
            String namespaceUri = namespace.attributeValue("uri");
            availablePrefixToNamespaceMapTmp.put(namespacePrefix, namespaceUri);
        }
        this.availablePrefixToNamespaceMap.putAll(availablePrefixToNamespaceMapTmp);
    }
    //$NON-NLS-1$
    Element typesElt = modelElt.element("types");
    if (typesElt != null) {
        //$NON-NLS-1$
        List<Element> types = (List<Element>) typesElt.elements("type");
        this.availableTypes.addAllAlfrescoModelElement(types);
    }
    //$NON-NLS-1$
    Element aspectsElt = modelElt.element("aspects");
    if (aspectsElt != null) {
        //$NON-NLS-1$
        List<Element> aspects = (List<Element>) aspectsElt.elements("aspect");
        this.availableAspects.addAllAlfrescoModelElement(aspects);
    }
}
Also used : HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) List(java.util.List) AlfrescoOutputException(org.talend.designer.alfrescooutput.util.AlfrescoOutputException) File(java.io.File)

Example 3 with DocumentException

use of org.dom4j.DocumentException in project cubrid-manager by CUBRID.

the class SqlMapCondition method getIncludedStatement.

public String getIncludedStatement() {
    if (this.includedStatement == null) {
        if (this.statement == null) {
            throw new NullPointerException("The statement field couldn't be a null.");
        }
        SAXReader reader = new SAXReader(false);
        Document document = null;
        try {
            InputSource source = new InputSource(new StringReader(this.statement));
            document = reader.read(source);
        } catch (DocumentException e) {
            ExceptionUtils.printRootCauseStackTrace(e);
            throw new IllegalArgumentException("fail to parse condition", e);
        }
        // include 처리용 statement 저장
        Element copiedElement = document.getRootElement().createCopy();
        copiedElement.clearContent();
        copiedElement.setText(this.modifiedStatement);
        this.includedStatement = copiedElement.asXML();
    }
    return includedStatement;
}
Also used : InputSource(org.xml.sax.InputSource) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document)

Example 4 with DocumentException

use of org.dom4j.DocumentException 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 5 with DocumentException

use of org.dom4j.DocumentException in project cubrid-manager by CUBRID.

the class FormatXMLAction method run.

/**
	 * Format XML strings.
	 */
public void run() {
    //format editor.
    try {
        ByteArrayOutputStream sw = new ByteArrayOutputStream();
        writeTo(sw, editor.getDocument().get(), "utf-8");
        String string = new String(sw.toByteArray(), "utf-8");
        editor.getDocument().set(string);
        sw.close();
    } catch (UnsupportedEncodingException e) {
        showErrorMessage();
    } catch (IOException e) {
        showErrorMessage();
    } catch (DocumentException e) {
        showErrorMessage();
    }
}
Also used : DocumentException(org.dom4j.DocumentException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

DocumentException (org.dom4j.DocumentException)121 Document (org.dom4j.Document)78 SAXReader (org.dom4j.io.SAXReader)71 Element (org.dom4j.Element)51 IOException (java.io.IOException)43 File (java.io.File)27 InputStream (java.io.InputStream)19 StringReader (java.io.StringReader)15 InputStreamReader (java.io.InputStreamReader)11 ArrayList (java.util.ArrayList)10 XMLWriter (org.dom4j.io.XMLWriter)9 InputSource (org.xml.sax.InputSource)9 FileInputStream (java.io.FileInputStream)7 List (java.util.List)7 Node (org.dom4j.Node)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 URL (java.net.URL)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 Reader (java.io.Reader)5