diff -r 1feed5fbf147 -r e1244384cc6f src/org/sonews/storage/DrupalMessage.java --- a/src/org/sonews/storage/DrupalMessage.java Wed Oct 12 00:25:14 2011 +0200 +++ b/src/org/sonews/storage/DrupalMessage.java Wed Oct 12 23:10:31 2011 +0200 @@ -19,12 +19,16 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.mail.Header; import javax.mail.MessagingException; import javax.mail.Multipart; @@ -33,6 +37,11 @@ import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import org.sonews.util.io.Resource; /** * This is MimeMessage which enables custom Message-ID header @@ -46,6 +55,7 @@ */ public class DrupalMessage extends MimeMessage { + private static final Logger log = Logger.getLogger(DrupalMessage.class.getName()); private static final String MESSAGE_ID_HEADER = "Message-ID"; private static final String CRLF = "\r\n"; public static final String CHARSET = "UTF-8"; @@ -62,12 +72,12 @@ addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain)); addHeader("Newsgroups", rs.getString("group_name")); - setFrom(new InternetAddress("anonym@example.com", rs.getString("sender_name"))); + setFrom(new InternetAddress(rs.getString("sender_email"), rs.getString("sender_name"))); setSubject(rs.getString("subject")); setSentDate(new Date(rs.getLong("created"))); - - Integer parentID = rs.getInt("parent_id"); - if (parentID != null && parentID > 0) { + + int parentID = rs.getInt("parent_id"); + if (parentID > 0) { String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain); addHeader("In-Reply-To", parentMessageID); addHeader("References", parentMessageID); @@ -77,12 +87,12 @@ Multipart multipart = new MimeMultipart("alternative"); setContent(multipart); - /** TODO: Plain text part */ + /** Plain text part */ MimeBodyPart textPart = new MimeBodyPart(); multipart.addBodyPart(textPart); textPart.setText(readPlainText(rs)); - /** TODO: XHTML part */ + /** XHTML part */ MimeBodyPart htmlPart = new MimeBodyPart(); multipart.addBodyPart(htmlPart); htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE); @@ -100,9 +110,29 @@ private String readXhtmlText(ResultSet rs) { /** - * TODO: převést na XHTML + * TODO: znovupoužívat XSL transformér */ - return "
TODO: tady bude nějaký (X)HTML"; + try { + String originalText = rs.getString("text"); + StringReader input = new StringReader("" + originalText + ""); + StringWriter output = new StringWriter(); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer t = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart.xsl"))); + t.setParameter("isRoot", (rs.getInt("parent_id") == 0)); + t.setParameter("title", rs.getString("subject")); + t.setParameter("urlBase", rs.getString("urlBase")); + t.setParameter("wwwRead", rs.getString("wwwRead")); + t.setParameter("wwwPost", rs.getString("wwwPost")); + t.transform(new StreamSource(input), new StreamResult(output)); + + return output.toString(); + } catch (Exception e) { + /** + * TODO: lepší ošetření chyby + */ + log.log(Level.WARNING, "Error while transforming article to XHTML", e); + return "Při transformaci příspěvku bohužel došlo k chybě.
"; + } } private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {