KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > metadata > AbstractMetadataExtracter


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.metadata;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
33  *
34  * @author Jesper Steen Møller
35  */

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 JavaDoc<String JavaDoc> supportedMimetypes;
43     private double reliability;
44     private long extractionTime;
45
46     protected AbstractMetadataExtracter(String JavaDoc 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 JavaDoc<String JavaDoc> supportedMimetypes, double reliability, long extractionTime)
54     {
55         this.supportedMimetypes = supportedMimetypes;
56         this.reliability = reliability;
57         this.extractionTime = extractionTime;
58     }
59
60     /**
61      * Set the registry to register with
62      *
63      * @param registry a metadata extracter registry
64      */

65     public void setRegistry(MetadataExtracterRegistry registry)
66     {
67         this.registry = registry;
68     }
69
70     /**
71      * Helper setter of the mimetype service. This is not always required.
72      *
73      * @param mimetypeService
74      */

75     public void setMimetypeService(MimetypeService mimetypeService)
76     {
77         this.mimetypeService = mimetypeService;
78     }
79
80     /**
81      * @return Returns the mimetype helper
82      */

83     protected MimetypeService getMimetypeService()
84     {
85         return mimetypeService;
86     }
87     
88     /**
89      * Registers this instance of the extracter with the registry.
90      *
91      * @see #setRegistry(MetadataExtracterRegistry)
92      */

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     /**
105      * Default reliability check that returns the reliability as configured by the contstructor
106      * if the mimetype is in the list of supported mimetypes.
107      *
108      * @param mimetype the mimetype to check
109      */

110     public double getReliability(String JavaDoc 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     /**
124      * Checks if the mimetype is supported.
125      *
126      * @param reader the reader to check
127      * @throws AlfrescoRuntimeException if the mimetype is not supported
128      */

129     protected void checkReliability(ContentReader reader)
130     {
131         String JavaDoc 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 JavaDoc<QName, Serializable JavaDoc> destination) throws ContentIOException
143     {
144         // check the reliability
145
checkReliability(reader);
146         
147         try
148         {
149             extractInternal(reader, destination);
150         }
151         catch (Throwable JavaDoc e)
152         {
153             throw new ContentIOException("Metadata extraction failed: \n" +
154                     " reader: " + reader,
155                     e);
156         }
157         finally
158         {
159             // check that the reader was closed
160
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         // done
169
if (logger.isDebugEnabled())
170         {
171             logger.debug("Completed metadata extraction: \n" +
172                     " reader: " + reader + "\n" +
173                     " extracter: " + this);
174         }
175     }
176
177     /**
178      * Override to provide the necessary extraction logic. Implementations must ensure that the reader
179      * is closed before the method exits.
180      *
181      * @param reader the source of the content
182      * @param destination the property map to fill
183      * @throws Throwable an exception
184      */

185     protected abstract void extractInternal(ContentReader reader, Map JavaDoc<QName, Serializable JavaDoc> destination) throws Throwable JavaDoc;
186     
187     /**
188      * Examines a value or string for nulls and adds it to the map (if
189      * non-empty)
190      *
191      * @param prop Alfresco's <code>ContentModel.PROP_</code> to set.
192      * @param value Value to set it to
193      * @param destination Map into which to set it
194      * @return true, if set, false otherwise
195      */

196     protected boolean trimPut(QName prop, Object JavaDoc value, Map JavaDoc<QName, Serializable JavaDoc> destination)
197     {
198         if (value == null)
199             return false;
200         if (value instanceof String JavaDoc)
201         {
202             String JavaDoc svalue = ((String JavaDoc) 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 JavaDoc)
211         {
212             destination.put(prop, (Serializable JavaDoc) value);
213         }
214         else
215         {
216             destination.put(prop, value.toString());
217         }
218         return true;
219     }
220 }
221
Popular Tags