1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 /*
24  * AbstractExporter.java
25  *
26  * Created on 1. Oktober 2003, 16:12
27  */
28 package com.sun.star.wizards.web.export;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.document.MacroExecMode;
32 import com.sun.star.document.UpdateDocMode;
33 import com.sun.star.frame.XComponentLoader;
34 import com.sun.star.frame.XDesktop;
35 import com.sun.star.frame.XStorable;
36 import com.sun.star.io.IOException;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.util.XCloseable;
40 import com.sun.star.wizards.common.Desktop;
41 import com.sun.star.wizards.common.FileAccess;
42 import com.sun.star.wizards.common.Properties;
43 import com.sun.star.wizards.document.OfficeDocument;
44 import com.sun.star.wizards.text.TextDocument;
45 import com.sun.star.wizards.web.data.CGArgument;
46 import com.sun.star.wizards.web.data.CGDocument;
47 import com.sun.star.wizards.web.data.CGExporter;
48 import com.sun.star.wizards.web.data.TypeDetection;
49 
50 /**
51  *
52  * @author  rpiterman
53  */
54 public abstract class AbstractExporter implements Exporter
55 {
56 
57     protected CGExporter exporter;
58     protected FileAccess fileAccess;
59 
storeToURL(Object officeDocument, Properties props, String targetUrl, String filterName, PropertyValue[] filterData)60     protected void storeToURL(Object officeDocument, Properties props, String targetUrl, String filterName, PropertyValue[] filterData)
61             throws IOException
62     {
63 
64         props = new Properties();
65         props.put("FilterName", filterName);
66 
67         if (filterData.length > 0)
68         {
69             props.put("FilterData", filterData);
70         }
71         XStorable xs = UnoRuntime.queryInterface(XStorable.class, officeDocument);
72         PropertyValue[] o = props.getProperties();
73         xs.storeToURL(targetUrl, o);
74     }
75 
storeToURL(Object officeDocument, String targetUrl, String filterName, PropertyValue[] filterData)76     protected void storeToURL(Object officeDocument, String targetUrl, String filterName, PropertyValue[] filterData)
77             throws IOException
78     {
79 
80         storeToURL(officeDocument, new Properties(), targetUrl, filterName, filterData);
81     }
82 
storeToURL(Object officeDocument, String targetUrl, String filterName)83     protected void storeToURL(Object officeDocument, String targetUrl, String filterName)
84             throws IOException
85     {
86 
87         storeToURL(officeDocument, new Properties(), targetUrl, filterName, new PropertyValue[0]);
88 
89     }
90 
getArgument(String name, CGExporter p)91     protected String getArgument(String name, CGExporter p)
92     {
93         return ((CGArgument) p.cp_Arguments.getElement(name)).cp_Value;
94     }
95 
openDocument(CGDocument doc, XMultiServiceFactory xmsf)96     protected Object openDocument(CGDocument doc, XMultiServiceFactory xmsf)
97             throws com.sun.star.io.IOException
98     {
99         Object document = null;
100         //open the document.
101         try
102         {
103             XDesktop desktop = Desktop.getDesktop(xmsf);
104             Properties props = new Properties();
105             props.put("Hidden", Boolean.TRUE);
106             props.put("MacroExecutionMode", new Short(MacroExecMode.NEVER_EXECUTE));
107             props.put("UpdateDocMode", new Short(UpdateDocMode.NO_UPDATE));
108             document = UnoRuntime.queryInterface(XComponentLoader.class, desktop).loadComponentFromURL(doc.cp_URL, "_blank", 0, props.getProperties());
109         }
110         catch (com.sun.star.lang.IllegalArgumentException iaex)
111         {
112         }
113         //try to get the number of pages in the document;
114         try
115         {
116             pageCount(doc, document);
117         }
118         catch (Exception ex)
119         {
120             //Here i do nothing since pages is not *so* important.
121         }
122         return document;
123     }
124 
closeDocument(Object doc, XMultiServiceFactory xmsf)125     protected void closeDocument(Object doc, XMultiServiceFactory xmsf)
126     {
127         /*OfficeDocument.dispose(
128         xmsf,
129         (XComponent) UnoRuntime.queryInterface(XComponent.class, doc));*/
130         try
131         {
132             XCloseable xc = UnoRuntime.queryInterface(XCloseable.class, doc);
133             xc.close(false);
134         }
135         catch (Exception ex)
136         {
137             ex.printStackTrace();
138         }
139     }
140 
pageCount(CGDocument doc, Object document)141     private void pageCount(CGDocument doc, Object document)
142     {
143         if (doc.appType.equals(TypeDetection.WRITER_DOC))
144         {
145             doc.pages = TextDocument.getPageCount(document);
146         }
147         else if (doc.appType.equals(TypeDetection.IMPRESS_DOC))
148         {
149             doc.pages = OfficeDocument.getSlideCount(document);
150         }
151         else if (doc.appType.equals(TypeDetection.DRAW_DOC))
152         {
153             doc.pages = OfficeDocument.getSlideCount(document);
154         }
155     }
156 
init(CGExporter exporter_)157     public void init(CGExporter exporter_)
158     {
159         exporter = exporter_;
160     }
161 
getFileAccess(XMultiServiceFactory xmsf)162     protected FileAccess getFileAccess(XMultiServiceFactory xmsf)
163     {
164         if (fileAccess == null)
165         {
166             try
167             {
168                 fileAccess = new FileAccess(xmsf);
169             }
170             catch (Exception ex)
171             {
172             }
173         }
174         return fileAccess;
175     }
176 
calcFileSize(CGDocument doc, String url, XMultiServiceFactory xmsf)177     protected void calcFileSize(CGDocument doc, String url, XMultiServiceFactory xmsf)
178     {
179         /*if the exporter exports to a
180          * binary format, get the size of the destination.
181          */
182         if (exporter.cp_Binary)
183         {
184             doc.sizeBytes = getFileAccess(xmsf).getSize(url);
185         }
186     }
187 }
188