src/org/sonews/storage/DrupalMessage.java
changeset 89 c60625d58158
parent 87 d4435ac082dc
child 100 08c9fb6fb017
     1.1 --- a/src/org/sonews/storage/DrupalMessage.java	Mon Oct 17 11:16:18 2011 +0200
     1.2 +++ b/src/org/sonews/storage/DrupalMessage.java	Mon Oct 17 18:14:34 2011 +0200
     1.3 @@ -98,7 +98,9 @@
     1.4  
     1.5  			/** Plain text part */
     1.6  			MimeBodyPart textPart = new MimeBodyPart();
     1.7 -			textPart.setText(readPlainText(rs, xhtmlText));
     1.8 +			String plainText = readPlainText(rs, xhtmlText);
     1.9 +			textPart.setText(plainText);
    1.10 +			//addHeader("Lines", String.valueOf(plainText.split("\n").length));
    1.11  
    1.12  			/**
    1.13  			 * Thunderbirdu záleží, v jakém pořadí části jsou 
    1.14 @@ -114,10 +116,22 @@
    1.15  	}
    1.16  
    1.17  	private String readPlainText(ResultSet rs, String xhtmlText) {
    1.18 -		/**
    1.19 -		 * TODO: převést na prostý text
    1.20 -		 */
    1.21 -		return "TODO: obyčejný text\n(zatím čtěte XHTML verzi)";
    1.22 +		try {
    1.23 +			TransformerFactory tf = TransformerFactory.newInstance();
    1.24 +			Transformer textTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeTextPart.xsl")));
    1.25 +
    1.26 +			StringReader input = new StringReader(xhtmlText);
    1.27 +			StringWriter output = new StringWriter(xhtmlText.length());
    1.28 +			textTransformer.transform(new StreamSource(input), new StreamResult(output));
    1.29 +
    1.30 +			return output.toString();
    1.31 +		} catch (Exception e) {
    1.32 +			/**
    1.33 +			 * TODO: lepší ošetření chyby
    1.34 +			 */
    1.35 +			log.log(Level.WARNING, "Error while transforming article to plain text", e);
    1.36 +			return makeSimpleXHTML("Při transformaci příspěvku bohužel došlo k chybě.");
    1.37 +		}
    1.38  	}
    1.39  
    1.40  	private String readXhtmlText(ResultSet rs) {
    1.41 @@ -177,7 +191,13 @@
    1.42  	 * TODO: refaktorovat, přesunout
    1.43  	 */
    1.44  	private static String tidyXhtml(String inputText) throws IOException {
    1.45 -		// https://sourceforge.net/tracker/index.php?func=detail&aid=3424437&group_id=27659&atid=390966
    1.46 +		/*
    1.47 +		 * Viz https://sourceforge.net/tracker/index.php?func=detail&aid=3424437&group_id=27659&atid=390966
    1.48 +		 *
    1.49 +		 * TODO:
    1.50 +		 *		- použít delší zástupný řetězec, ne jen jeden znak
    1.51 +		 *		- umísťovat ho jen tam, kde už nějaký text je (ne mezi >\s*<)
    1.52 +		 */
    1.53  		inputText = inputText.replaceAll("\\n", "◆\n");
    1.54  
    1.55  		Runtime r = Runtime.getRuntime();