1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.InputStreamReader ; 20 import java.io.OutputStreamWriter ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 import java.util.Map ; 24 25 import org.alfresco.repo.content.MimetypeMap; 26 import org.alfresco.service.cmr.repository.ContentReader; 27 import org.alfresco.service.cmr.repository.ContentWriter; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 38 public class StringExtractingContentTransformer extends AbstractContentTransformer 39 { 40 public static final String PREFIX_TEXT = "text/"; 41 42 private static final Log logger = LogFactory.getLog(StringExtractingContentTransformer.class); 43 44 51 public double getReliability(String sourceMimetype, String targetMimetype) 52 { 53 if (!targetMimetype.equals(MimetypeMap.MIMETYPE_TEXT_PLAIN)) 54 { 55 return 0.0; 57 } 58 else if (sourceMimetype.equals(MimetypeMap.MIMETYPE_TEXT_PLAIN)) 59 { 60 return 1.0; 62 } 63 else if (sourceMimetype.startsWith(PREFIX_TEXT)) 64 { 65 return 0.1; 67 } 68 else 69 { 70 return 0.0; 72 } 73 } 74 75 83 @Override 84 public void transformInternal(ContentReader reader, ContentWriter writer, Map <String , Object > options) 85 throws Exception 86 { 87 transformText(reader, writer); 89 } 90 91 94 private void transformText(ContentReader reader, ContentWriter writer) throws Exception 95 { 96 Reader charReader = null; 98 Writer charWriter = null; 99 try 100 { 101 if (reader.getEncoding() == null) 102 { 103 charReader = new InputStreamReader (reader.getContentInputStream()); 104 } 105 else 106 { 107 charReader = new InputStreamReader (reader.getContentInputStream(), reader.getEncoding()); 108 } 109 if (writer.getEncoding() == null) 110 { 111 charWriter = new OutputStreamWriter (writer.getContentOutputStream()); 112 } 113 else 114 { 115 charWriter = new OutputStreamWriter (writer.getContentOutputStream(), writer.getEncoding()); 116 } 117 char[] buffer = new char[1024]; 119 int readCount = 0; 120 while (readCount > -1) 121 { 122 charWriter.write(buffer, 0, readCount); 124 readCount = charReader.read(buffer); 126 } 127 } 128 finally 129 { 130 if (charReader != null) 131 { 132 try { charReader.close(); } catch (Throwable e) { logger.error(e); } 133 } 134 if (charWriter != null) 135 { 136 try { charWriter.close(); } catch (Throwable e) { logger.error(e); } 137 } 138 } 139 } 141 } 142 | Popular Tags |