1 17 package org.alfresco.repo.content.metadata; 18 19 import java.io.Serializable ; 20 import java.util.Collections ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.service.cmr.repository.ContentIOException; 26 import org.alfresco.service.cmr.repository.ContentReader; 27 import org.alfresco.service.cmr.repository.MimetypeService; 28 import org.alfresco.service.namespace.QName; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 36 abstract public class AbstractMetadataExtracter implements MetadataExtracter 37 { 38 private static Log logger = LogFactory.getLog(AbstractMetadataExtracter.class); 39 40 private MimetypeService mimetypeService; 41 private MetadataExtracterRegistry registry; 42 private Set <String > supportedMimetypes; 43 private double reliability; 44 private long extractionTime; 45 46 protected AbstractMetadataExtracter(String supportedMimetype, double reliability, long extractionTime) 47 { 48 this.supportedMimetypes = Collections.singleton(supportedMimetype); 49 this.reliability = reliability; 50 this.extractionTime = extractionTime; 51 } 52 53 protected AbstractMetadataExtracter(Set <String > supportedMimetypes, double reliability, long extractionTime) 54 { 55 this.supportedMimetypes = supportedMimetypes; 56 this.reliability = reliability; 57 this.extractionTime = extractionTime; 58 } 59 60 65 public void setRegistry(MetadataExtracterRegistry registry) 66 { 67 this.registry = registry; 68 } 69 70 75 public void setMimetypeService(MimetypeService mimetypeService) 76 { 77 this.mimetypeService = mimetypeService; 78 } 79 80 83 protected MimetypeService getMimetypeService() 84 { 85 return mimetypeService; 86 } 87 88 93 public void register() 94 { 95 if (registry == null) 96 { 97 logger.warn("Property 'registry' has not been set. Ignoring auto-registration: \n" + 98 " extracter: " + this); 99 return; 100 } 101 registry.register(this); 102 } 103 104 110 public double getReliability(String mimetype) 111 { 112 if (supportedMimetypes.contains(mimetype)) 113 return reliability; 114 else 115 return 0.0; 116 } 117 118 public long getExtractionTime() 119 { 120 return extractionTime; 121 } 122 123 129 protected void checkReliability(ContentReader reader) 130 { 131 String mimetype = reader.getMimetype(); 132 if (getReliability(mimetype) <= 0.0) 133 { 134 throw new AlfrescoRuntimeException( 135 "Metadata extracter does not support mimetype: \n" + 136 " reader: " + reader + "\n" + 137 " supported: " + supportedMimetypes + "\n" + 138 " extracter: " + this); 139 } 140 } 141 142 public final void extract(ContentReader reader, Map <QName, Serializable > destination) throws ContentIOException 143 { 144 checkReliability(reader); 146 147 try 148 { 149 extractInternal(reader, destination); 150 } 151 catch (Throwable e) 152 { 153 throw new ContentIOException("Metadata extraction failed: \n" + 154 " reader: " + reader, 155 e); 156 } 157 finally 158 { 159 if (!reader.isClosed()) 161 { 162 logger.error("Content reader not closed by metadata extracter: \n" + 163 " reader: " + reader + "\n" + 164 " extracter: " + this); 165 } 166 } 167 168 if (logger.isDebugEnabled()) 170 { 171 logger.debug("Completed metadata extraction: \n" + 172 " reader: " + reader + "\n" + 173 " extracter: " + this); 174 } 175 } 176 177 185 protected abstract void extractInternal(ContentReader reader, Map <QName, Serializable > destination) throws Throwable ; 186 187 196 protected boolean trimPut(QName prop, Object value, Map <QName, Serializable > destination) 197 { 198 if (value == null) 199 return false; 200 if (value instanceof String ) 201 { 202 String svalue = ((String ) value).trim(); 203 if (svalue.length() > 0) 204 { 205 destination.put(prop, svalue); 206 return true; 207 } 208 return false; 209 } 210 else if (value instanceof Serializable ) 211 { 212 destination.put(prop, (Serializable ) value); 213 } 214 else 215 { 216 destination.put(prop, value.toString()); 217 } 218 return true; 219 } 220 } 221 | Popular Tags |