KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.net.ConnectException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import net.sf.joott.uno.UnoConnection;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.repo.content.MimetypeMap;
31 import org.alfresco.service.cmr.repository.ContentReader;
32 import org.alfresco.service.namespace.QName;
33 import org.alfresco.util.TempFileProvider;
34
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.document.XDocumentInfoSupplier;
38 import com.sun.star.frame.XComponentLoader;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.ucb.XFileIdentifierConverter;
41 import com.sun.star.uno.UnoRuntime;
42
43 /**
44  * @author Jesper Steen Møller
45  */

46 public class UnoMetadataExtracter extends AbstractMetadataExtracter
47 {
48     public static String JavaDoc[] SUPPORTED_MIMETYPES = new String JavaDoc[] {
49         MimetypeMap.MIMETYPE_STAROFFICE5_WRITER,
50         MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS,
51         MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER,
52         MimetypeMap.MIMETYPE_OPENOFFICE1_IMPRESS
53     // Add the other OpenOffice.org stuff here
54
// In fact, other types may apply as well, but should be counted as lower
55
// quality since they involve conversion.
56
};
57
58     private String JavaDoc contentUrl;
59     private MyUnoConnection connection;
60     private boolean isConnected;
61
62     public UnoMetadataExtracter()
63     {
64         super(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(SUPPORTED_MIMETYPES)), 1.00, 10000);
65         this.contentUrl = UnoConnection.DEFAULT_CONNECTION_STRING;
66     }
67
68     /**
69      *
70      * @param contentUrl the URL to connect to
71      */

72     public void setContentUrl(String JavaDoc contentUrl)
73     {
74         this.contentUrl = contentUrl;
75     }
76
77     /**
78      * Initialises the bean by establishing an UNO connection
79      */

80     public synchronized void init()
81     {
82         connection = new MyUnoConnection(contentUrl);
83         // attempt to make an connection
84
try
85         {
86             connection.connect();
87             isConnected = true;
88             // register
89
super.register();
90         }
91         catch (ConnectException JavaDoc e)
92         {
93             isConnected = false;
94         }
95     }
96
97     /**
98      * @return Returns true if a connection to the Uno server could be
99      * established
100      */

101     public boolean isConnected()
102     {
103         return isConnected;
104     }
105
106     public void extractInternal(ContentReader reader, final Map JavaDoc<QName, Serializable JavaDoc> destination) throws Throwable JavaDoc
107     {
108         String JavaDoc sourceMimetype = reader.getMimetype();
109
110         // create temporary files to convert from and to
111
File JavaDoc tempFromFile = TempFileProvider.createTempFile(
112                 "UnoContentTransformer_", "."
113                 + getMimetypeService().getExtension(sourceMimetype));
114         // download the content from the source reader
115
reader.getContent(tempFromFile);
116
117         String JavaDoc sourceUrl = toUrl(tempFromFile, connection);
118
119         // UNO Interprocess Bridge *should* be thread-safe, but...
120
synchronized (connection)
121         {
122             XComponentLoader desktop = connection.getDesktop();
123             XComponent document = desktop.loadComponentFromURL(
124                     sourceUrl,
125                     "_blank",
126                     0,
127                     new PropertyValue[] { property("Hidden", Boolean.TRUE) });
128             if (document == null)
129             {
130                 throw new FileNotFoundException JavaDoc("could not open source document: " + sourceUrl);
131             }
132             try
133             {
134                 XDocumentInfoSupplier infoSupplier = (XDocumentInfoSupplier) UnoRuntime.queryInterface(
135                         XDocumentInfoSupplier.class, document);
136                 XPropertySet propSet = (XPropertySet) UnoRuntime.queryInterface(
137                         XPropertySet.class,
138                         infoSupplier
139                         .getDocumentInfo());
140
141                 // Titled aspect
142
trimPut(ContentModel.PROP_TITLE, propSet.getPropertyValue("Title"), destination);
143                 trimPut(ContentModel.PROP_DESCRIPTION, propSet.getPropertyValue("Subject"), destination);
144
145                 // Auditable aspect
146
// trimPut(ContentModel.PROP_CREATED,
147
// si.getCreateDateTime(), destination);
148
trimPut(ContentModel.PROP_AUTHOR, propSet.getPropertyValue("Author"), destination);
149                 // trimPut(ContentModel.PROP_MODIFIED,
150
// si.getLastSaveDateTime(), destination);
151
// trimPut(ContentModel.PROP_MODIFIER, si.getLastAuthor(),
152
// destination);
153
}
154             finally
155             {
156                 document.dispose();
157             }
158         }
159     }
160
161     public String JavaDoc toUrl(File JavaDoc file, MyUnoConnection connection) throws ConnectException JavaDoc
162     {
163         Object JavaDoc fcp = connection.getFileContentService();
164         XFileIdentifierConverter fic = (XFileIdentifierConverter) UnoRuntime.queryInterface(
165                 XFileIdentifierConverter.class, fcp);
166         return fic.getFileURLFromSystemPath("", file.getAbsolutePath());
167     }
168
169     public double getReliability(String JavaDoc sourceMimetype)
170     {
171         if (isConnected())
172             return super.getReliability(sourceMimetype);
173         else
174             return 0.0;
175     }
176
177     private static PropertyValue property(String JavaDoc name, Object JavaDoc value)
178     {
179         PropertyValue property = new PropertyValue();
180         property.Name = name;
181         property.Value = value;
182         return property;
183     }
184
185     static class MyUnoConnection extends UnoConnection
186     {
187         public MyUnoConnection(String JavaDoc url)
188         {
189             super(url);
190         }
191
192         public Object JavaDoc getFileContentService() throws ConnectException JavaDoc
193         {
194             return getService("com.sun.star.ucb.FileContentProvider");
195         }
196     }
197 }
198
Popular Tags