1 17 package org.alfresco.repo.content.metadata; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.Serializable ; 22 import java.net.ConnectException ; 23 import java.util.Arrays ; 24 import java.util.HashSet ; 25 import java.util.Map ; 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 46 public class UnoMetadataExtracter extends AbstractMetadataExtracter 47 { 48 public static String [] SUPPORTED_MIMETYPES = new String [] { 49 MimetypeMap.MIMETYPE_STAROFFICE5_WRITER, 50 MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS, 51 MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER, 52 MimetypeMap.MIMETYPE_OPENOFFICE1_IMPRESS 53 }; 57 58 private String contentUrl; 59 private MyUnoConnection connection; 60 private boolean isConnected; 61 62 public UnoMetadataExtracter() 63 { 64 super(new HashSet <String >(Arrays.asList(SUPPORTED_MIMETYPES)), 1.00, 10000); 65 this.contentUrl = UnoConnection.DEFAULT_CONNECTION_STRING; 66 } 67 68 72 public void setContentUrl(String contentUrl) 73 { 74 this.contentUrl = contentUrl; 75 } 76 77 80 public synchronized void init() 81 { 82 connection = new MyUnoConnection(contentUrl); 83 try 85 { 86 connection.connect(); 87 isConnected = true; 88 super.register(); 90 } 91 catch (ConnectException e) 92 { 93 isConnected = false; 94 } 95 } 96 97 101 public boolean isConnected() 102 { 103 return isConnected; 104 } 105 106 public void extractInternal(ContentReader reader, final Map <QName, Serializable > destination) throws Throwable 107 { 108 String sourceMimetype = reader.getMimetype(); 109 110 File tempFromFile = TempFileProvider.createTempFile( 112 "UnoContentTransformer_", "." 113 + getMimetypeService().getExtension(sourceMimetype)); 114 reader.getContent(tempFromFile); 116 117 String sourceUrl = toUrl(tempFromFile, connection); 118 119 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 ("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 trimPut(ContentModel.PROP_TITLE, propSet.getPropertyValue("Title"), destination); 143 trimPut(ContentModel.PROP_DESCRIPTION, propSet.getPropertyValue("Subject"), destination); 144 145 trimPut(ContentModel.PROP_AUTHOR, propSet.getPropertyValue("Author"), destination); 149 } 154 finally 155 { 156 document.dispose(); 157 } 158 } 159 } 160 161 public String toUrl(File file, MyUnoConnection connection) throws ConnectException 162 { 163 Object 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 sourceMimetype) 170 { 171 if (isConnected()) 172 return super.getReliability(sourceMimetype); 173 else 174 return 0.0; 175 } 176 177 private static PropertyValue property(String name, Object 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 url) 188 { 189 super(url); 190 } 191 192 public Object getFileContentService() throws ConnectException 193 { 194 return getService("com.sun.star.ucb.FileContentProvider"); 195 } 196 } 197 } 198 | Popular Tags |