- Timestamp:
- 03/03/07 21:56:31 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Jav_Oo/com/scenari/s/oo/transform/TransformerExportOdfViaDraw.java
r7759 r8858 43 43 import java.io.InputStream; 44 44 45 import com.scenari.s.co.odstorage.OdFile; 46 import com.scenari.s.co.source.fs.WSourceElemFs; 47 import com.scenari.s.co.transform.HTransformParams; 48 import com.scenari.s.fw.log.HLogMgr; 49 import com.scenari.s.fw.utils.stream.HStream; 45 50 import com.sun.star.awt.Point; 46 51 import com.sun.star.awt.Size; … … 62 67 import com.sun.star.view.XSelectionSupplier; 63 68 64 import com.scenari.s.co.odstorage.OdFile;65 import com.scenari.s.co.source.fs.WSourceElemFs;66 import com.scenari.s.co.transform.HTransformParams;67 import com.scenari.s.fw.log.HLogMgr;68 import com.scenari.s.fw.utils.stream.HStream;69 70 69 /** 71 70 * Transforme un document ODF par les fonctions d'export d'openOffice via l'application draw. … … 102 101 public class TransformerExportOdfViaDraw extends TransformerExportImgBase { 103 102 104 protected static File sMasterOd = null; 105 106 /** 107 * 108 * @see com.scenari.s.co.transform.IHTransformer#hTransform(Object, Object, com.scenari.s.co.transform.HTransformParams) 109 */ 110 public void hTransform(Object pSrc, Object pRes, HTransformParams pParams) throws Exception { 111 File vOdgFile = null; 112 Object vDocDraw = null; 113 OdFile vOdFile = null; 114 try { 115 116 int vMarginTop = getMarginTop(pParams); 117 int vMarginLeft = getMarginLeft(pParams); 118 int vWMargin = vMarginLeft + getMarginRight(pParams); 119 int vHMargin = vMarginTop + getMarginBottom(pParams); 120 121 //Récupération du component loader 122 OoBootstrap.checkConnection(); 103 protected static File sMasterOd = null; 104 105 protected boolean fFromMml = false; 106 107 /** 108 * 109 * @see com.scenari.s.co.transform.IHTransformer#hTransform(Object, Object, com.scenari.s.co.transform.HTransformParams) 110 */ 111 public void hTransform(Object pSrc, Object pRes, HTransformParams pParams) throws Exception { 112 File vOdgFile = null; 113 File vTmpOdf = null; 114 Object vDocDraw = null; 115 OdFile vOdFile = null; 116 117 try { 118 119 int vMarginTop = getMarginTop(pParams); 120 int vMarginLeft = getMarginLeft(pParams); 121 int vWMargin = vMarginLeft + getMarginRight(pParams); 122 int vHMargin = vMarginTop + getMarginBottom(pParams); 123 124 //Récupération du component loader 125 OoBootstrap.checkConnection(); 123 126 XComponentLoader vCompLoader = OoBootstrap.getComponentLoader(); 124 127 125 vOdFile = new OdFile(); 126 vOdFile.initFromOdFile(getMaster()); 127 vOdFile.includeOd("Object 1/", new WSourceElemFs(((File) pSrc).getPath()), false); 128 vOdgFile = File.createTempFile("transformOdfViaDraw", ".odg"); 129 vOdFile.export(new WSourceElemFs(vOdgFile.getPath())); 130 131 // On charge le doc source 132 String vUrl = "file://".concat(vOdgFile.toURL().toExternalForm().substring(5)); 133 PropertyValue vProps[] = new PropertyValue[1]; 134 vProps[0] = new PropertyValue(); 135 vProps[0].Name = "Hidden"; 136 vProps[0].Value = Boolean.TRUE; 137 vDocDraw = vCompLoader.loadComponentFromURL(vUrl, "_blank", 0, vProps); 138 XDrawPagesSupplier vDrawPagesSupplier_DocDraw = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, vDocDraw); 139 XIndexAccess vIndexedDrawPages = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, vDrawPagesSupplier_DocDraw.getDrawPages()); 140 XDrawPage vDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, vIndexedDrawPages.getByIndex(0)); 141 142 //On sélectionne le shape OLE 143 XShape vOleShape = (XShape) UnoRuntime.queryInterface(XShape.class, vDrawPage.getByIndex(0)); 144 XController vController = ((XModel) UnoRuntime.queryInterface(XModel.class, vDocDraw)).getCurrentController(); 145 XFrame vFrameDoc = vController.getFrame(); 146 147 148 // NOTE : pour contourner un bug OO de blocage 149 long vOdFileSize = vOdgFile.length(); 150 long vTimeMs = vOdFileSize / 1000; 151 Thread.sleep(vTimeMs); 152 153 XSelectionSupplier vSelSupplier = (XSelectionSupplier) UnoRuntime.queryInterface(XSelectionSupplier.class, vController); 154 vSelSupplier.select(vOleShape); 155 156 //On rafarichit les dimensions du shape 157 XDispatchHelper vDipatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, OoBootstrap.createService("com.sun.star.frame.DispatchHelper")); 158 vDipatchHelper.executeDispatch((XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, vFrameDoc), ".uno:OriginalSize", "", 0, null); 159 160 //On met à jour les dimensions de la page 161 Size vSize = vOleShape.getSize(); 162 163 //On applique les transformations. 164 if (applyTransform(pParams, vSize)) { 165 //On réapplique les nouevlles Dim 166 vOleShape.setSize(vSize); 167 } 168 169 //On replace en fonction des margin 170 vOleShape.setPosition(new Point(vMarginTop, vMarginLeft)); 171 172 //On met à jour les dimensions de la page 173 XPropertySet vProps_DrawPage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, vDrawPage); 174 vProps_DrawPage.setPropertyValue("Width", new Integer(vSize.Width + vWMargin)); 175 vProps_DrawPage.setPropertyValue("Height", new Integer(vSize.Height + vHMargin)); 176 177 //On interroge le docDraw par son interface XStorable. 178 XStorable vStorableDocDraw = (XStorable) UnoRuntime.queryInterface(XStorable.class, vDocDraw); 179 180 // Propriétés de conversion 181 vProps = new PropertyValue[2]; 182 vProps[0] = new PropertyValue(); 183 vProps[0].Name = "Overwrite"; 184 vProps[0].Value = Boolean.TRUE; 185 vProps[1] = new PropertyValue(); 186 vProps[1].Name = "FilterName"; 187 vProps[1].Value = fFilterName; 188 189 //On transforme. 190 vStorableDocDraw.storeToURL("file://".concat(((File) pRes).toURL().toExternalForm().substring(5)), vProps); 191 192 /* 193 ######### GraphicExportFilter : impossible Flash impossible. ############### 194 Object vGraphicExportFilter = OoBootstrap.createService("com.sun.star.drawing.GraphicExportFilter"); 195 196 XExporter vExporter_GraphicExportFilter = (XExporter) UnoRuntime.queryInterface(XExporter.class, vGraphicExportFilter); 197 vExporter_GraphicExportFilter.setSourceDocument((XComponent) UnoRuntime.queryInterface(XComponent.class, vOle_Shape)); 198 XMimeTypeInfo vInfo = (XMimeTypeInfo) UnoRuntime.queryInterface(XMimeTypeInfo.class, vGraphicExportFilter); 199 String[] vList = vInfo.getSupportedMimeTypeNames(); 200 for (int i = 0; i < vList.length; i++) { 201 System.out.println(vList[i]); 202 } 203 204 vProps = new PropertyValue[3]; 205 vProps[0] = new PropertyValue(); 206 //vProps[0].Name = "MediaType"; 207 //vProps[0].Value = "image/png"; 208 //vProps[0].Value = "application/x-shockwave-flash"; 209 vProps[0].Name = "FilterName"; 210 vProps[0].Value = "WMF"; 211 //vProps[3] = new PropertyValue(); 212 //vProps[3].Name = "FilterName"; 213 //vProps[3].Value = fFilterName; 214 215 vProps[1] = new PropertyValue(); 216 vProps[1].Name = "URL"; 217 vProps[1].Value = "file://".concat(((File) pRes).toURL().toExternalForm().substring(5)); 218 vProps[2] = new PropertyValue(); 219 vProps[2].Name = "Overwrite"; 220 vProps[2].Value = Boolean.TRUE; 221 XFilter vFilter_GraphicExportFilter = (XFilter) UnoRuntime.queryInterface(XFilter.class, vGraphicExportFilter); 222 vFilter_GraphicExportFilter.filter(vProps); 223 */ 224 225 } catch (Exception e) { 226 throw (Exception) HLogMgr.hAddMessage(e, "Echec à la transformation d'une ressource OpenDocument."); 227 } finally { 228 try { 229 //On clos le doc. 230 XCloseable vCloseableDoc = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, vDocDraw); 231 vCloseableDoc.close(false); 232 } catch (Exception e) { 233 HLogMgr.hPublishException(e); 234 } 128 if (fFromMml) { 129 vTmpOdf = File.createTempFile("transformOdfViaDraw", ".odf"); 130 OdFile vOdf = new OdFile(); 131 vOdf.initEmptyFile(OdFile.MIME_ODF); 132 vOdf.addFile("content.xml", new WSourceElemFs(((File) pSrc).getPath())); 133 vOdf.export(new FileOutputStream(vTmpOdf)); 134 pSrc = vTmpOdf; 135 } 136 137 vOdFile = new OdFile(); 138 vOdFile.initFromOdFile(getMaster()); 139 vOdFile.includeOd("Object 1/", new WSourceElemFs(((File) pSrc).getPath()), false); 140 vOdgFile = File.createTempFile("transformOdfViaDraw", ".odg"); 141 vOdFile.export(new WSourceElemFs(vOdgFile.getPath())); 142 143 // On charge le doc source 144 String vUrl = "file://".concat(vOdgFile.toURL().toExternalForm().substring(5)); 145 PropertyValue vProps[] = new PropertyValue[1]; 146 vProps[0] = new PropertyValue(); 147 vProps[0].Name = "Hidden"; 148 vProps[0].Value = Boolean.TRUE; 149 vDocDraw = vCompLoader.loadComponentFromURL(vUrl, "_blank", 0, vProps); 150 XDrawPagesSupplier vDrawPagesSupplier_DocDraw = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, vDocDraw); 151 XIndexAccess vIndexedDrawPages = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, vDrawPagesSupplier_DocDraw.getDrawPages()); 152 XDrawPage vDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, vIndexedDrawPages.getByIndex(0)); 153 154 //On sélectionne le shape OLE 155 XShape vOleShape = (XShape) UnoRuntime.queryInterface(XShape.class, vDrawPage.getByIndex(0)); 156 XController vController = ((XModel) UnoRuntime.queryInterface(XModel.class, vDocDraw)).getCurrentController(); 157 XFrame vFrameDoc = vController.getFrame(); 158 159 // NOTE : pour contourner un bug OO de blocage 160 long vOdFileSize = vOdgFile.length(); 161 long vTimeMs = vOdFileSize / 1000; 162 Thread.sleep(vTimeMs); 163 164 XSelectionSupplier vSelSupplier = (XSelectionSupplier) UnoRuntime.queryInterface(XSelectionSupplier.class, vController); 165 vSelSupplier.select(vOleShape); 166 167 //On rafarichit les dimensions du shape 168 XDispatchHelper vDipatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, OoBootstrap.createService("com.sun.star.frame.DispatchHelper")); 169 vDipatchHelper.executeDispatch((XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, vFrameDoc), ".uno:OriginalSize", "", 0, null); 170 171 //On met à jour les dimensions de la page 172 Size vSize = vOleShape.getSize(); 173 174 //On applique les transformations. 175 if (applyTransform(pParams, vSize)) { 176 //On réapplique les nouevlles Dim 177 vOleShape.setSize(vSize); 178 } 179 180 //On replace en fonction des margin 181 vOleShape.setPosition(new Point(vMarginTop, vMarginLeft)); 182 183 //On met à jour les dimensions de la page 184 XPropertySet vProps_DrawPage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, vDrawPage); 185 vProps_DrawPage.setPropertyValue("Width", new Integer(vSize.Width + vWMargin)); 186 vProps_DrawPage.setPropertyValue("Height", new Integer(vSize.Height + vHMargin)); 187 188 //On interroge le docDraw par son interface XStorable. 189 XStorable vStorableDocDraw = (XStorable) UnoRuntime.queryInterface(XStorable.class, vDocDraw); 190 191 // Propriétés de conversion 192 vProps = new PropertyValue[2]; 193 vProps[0] = new PropertyValue(); 194 vProps[0].Name = "Overwrite"; 195 vProps[0].Value = Boolean.TRUE; 196 vProps[1] = new PropertyValue(); 197 vProps[1].Name = "FilterName"; 198 vProps[1].Value = fFilterName; 199 200 //On transforme. 201 vStorableDocDraw.storeToURL("file://".concat(((File) pRes).toURL().toExternalForm().substring(5)), vProps); 202 203 /* 204 ######### GraphicExportFilter : impossible Flash impossible. ############### 205 Object vGraphicExportFilter = OoBootstrap.createService("com.sun.star.drawing.GraphicExportFilter"); 206 207 XExporter vExporter_GraphicExportFilter = (XExporter) UnoRuntime.queryInterface(XExporter.class, vGraphicExportFilter); 208 vExporter_GraphicExportFilter.setSourceDocument((XComponent) UnoRuntime.queryInterface(XComponent.class, vOle_Shape)); 209 XMimeTypeInfo vInfo = (XMimeTypeInfo) UnoRuntime.queryInterface(XMimeTypeInfo.class, vGraphicExportFilter); 210 String[] vList = vInfo.getSupportedMimeTypeNames(); 211 for (int i = 0; i < vList.length; i++) { 212 System.out.println(vList[i]); 213 } 214 215 vProps = new PropertyValue[3]; 216 vProps[0] = new PropertyValue(); 217 //vProps[0].Name = "MediaType"; 218 //vProps[0].Value = "image/png"; 219 //vProps[0].Value = "application/x-shockwave-flash"; 220 vProps[0].Name = "FilterName"; 221 vProps[0].Value = "WMF"; 222 //vProps[3] = new PropertyValue(); 223 //vProps[3].Name = "FilterName"; 224 //vProps[3].Value = fFilterName; 225 226 vProps[1] = new PropertyValue(); 227 vProps[1].Name = "URL"; 228 vProps[1].Value = "file://".concat(((File) pRes).toURL().toExternalForm().substring(5)); 229 vProps[2] = new PropertyValue(); 230 vProps[2].Name = "Overwrite"; 231 vProps[2].Value = Boolean.TRUE; 232 XFilter vFilter_GraphicExportFilter = (XFilter) UnoRuntime.queryInterface(XFilter.class, vGraphicExportFilter); 233 vFilter_GraphicExportFilter.filter(vProps); 234 */ 235 236 } catch (Exception e) { 237 throw (Exception) HLogMgr.hAddMessage(e, "Echec à la transformation d'une ressource OpenDocument."); 238 } finally { 239 try { 240 //On clos le doc. 241 XCloseable vCloseableDoc = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, vDocDraw); 242 vCloseableDoc.close(false); 243 } catch (Exception e) { 244 HLogMgr.hPublishException(e); 245 } 235 246 if (vOdFile != null) { 236 247 try { … … 247 258 } 248 259 } 249 } 250 } 251 252 /** 253 * TEST 254 * 255 * java com.scenari.s.oo.transform.HTransformerExport "c:\test.odt" "c:\res.pdf" "writer_pdf_Export" "scale=1" 256 * 257 */ 258 public static void main(String pArgs[]) { 259 try { 260 TransformerExportOdfViaDraw vTransf = new TransformerExportOdfViaDraw(); 261 vTransf.wSetProperty("filterName", pArgs[2]); //"HTML (StarWriter)";//"XHTML Writer File"; //"writer_pdf_Export"; 262 263 //for (int i = 1; i < 1000; i++) { 264 // long vT0 = System.currentTimeMillis(); 265 266 vTransf.hTransform(new File(pArgs[0]), new File(pArgs[1]), HTransformParams.hNewParamsTransformByQueryString("transform=od2x&" + pArgs[3], "UTF-8")); 267 268 // System.out.println("Time "+i+" = "+((System.currentTimeMillis()-vT0))); 269 //} 270 271 System.exit(0); 272 } catch (Exception e) { 273 e.printStackTrace(); 274 System.exit(0); 275 } 276 } 277 278 protected static synchronized File getMaster() throws Exception { 279 if (sMasterOd == null || !sMasterOd.isFile()) { 280 InputStream vInput = TransformerExportOdfViaDraw.class.getResourceAsStream("FormulaInDrawMaster.odg"); 281 sMasterOd = File.createTempFile("masterOdf", ".odg"); 282 sMasterOd.deleteOnExit(); 283 HStream.hWrite(vInput, new FileOutputStream(sMasterOd)); 284 } 285 return sMasterOd; 286 } 287 260 if (vTmpOdf != null) { 261 try { 262 vTmpOdf.delete(); 263 } catch (Exception e) { 264 HLogMgr.hPublishException(e); 265 } 266 267 } 268 } 269 } 270 271 /** 272 * TEST 273 * 274 * java com.scenari.s.oo.transform.TransformerExportOdfViaDraw "c:\test.mml" "c:\res.odf" "draw_png_Export" "scale=1" 275 * 276 */ 277 public static void main(String pArgs[]) { 278 try { 279 TransformerExportOdfViaDraw vTransf = new TransformerExportOdfViaDraw(); 280 vTransf.fFromMml = true; 281 vTransf.wSetProperty("filterName", pArgs[2]); // "draw_flash_Export", "draw_pdf_Export", "draw_png_Export" 282 283 //for (int i = 1; i < 1000; i++) { 284 // long vT0 = System.currentTimeMillis(); 285 286 vTransf.hTransform(new File(pArgs[0]), new File(pArgs[1]), HTransformParams.hNewParamsTransformByQueryString("transform=od2x&" + pArgs[3], "UTF-8")); 287 288 // System.out.println("Time "+i+" = "+((System.currentTimeMillis()-vT0))); 289 //} 290 291 System.exit(0); 292 } catch (Exception e) { 293 e.printStackTrace(); 294 System.exit(0); 295 } 296 } 297 298 protected static synchronized File getMaster() throws Exception { 299 if (sMasterOd == null || !sMasterOd.isFile()) { 300 InputStream vInput = TransformerExportOdfViaDraw.class.getResourceAsStream("FormulaInDrawMaster.odg"); 301 sMasterOd = File.createTempFile("masterOdf", ".odg"); 302 sMasterOd.deleteOnExit(); 303 HStream.hWrite(vInput, new FileOutputStream(sMasterOd)); 304 } 305 return sMasterOd; 306 } 307 308 309 public void wSetProperty(String pKey, String pValue) { 310 if (pKey.equals("source") && pValue.equals("mml")) { 311 fFromMml = true; 312 } else { 313 super.wSetProperty(pKey, pValue); 314 } 315 } 288 316 }
Note: See TracChangeset
for help on using the changeset viewer.