summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/data/CGDocument.java
blob: eeb009ce028350dc97b1ebbf0e22355dec558f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: CGDocument.java,v $
 * $Revision: 1.8 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package com.sun.star.wizards.web.data;

import java.io.File;
import java.io.FileNotFoundException;

import org.w3c.dom.Node;

import com.sun.star.beans.PropertyValue;
import com.sun.star.document.MacroExecMode;
import com.sun.star.document.UpdateDocMode;
import com.sun.star.document.XDocumentProperties;
import com.sun.star.document.XDocumentPropertiesSupplier;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.DateTime;
import com.sun.star.wizards.common.Desktop;
import com.sun.star.wizards.common.FileAccess;
import com.sun.star.wizards.common.JavaTools;
import com.sun.star.wizards.common.Properties;
import com.sun.star.wizards.common.XMLHelper;
import com.sun.star.wizards.common.XMLProvider;
import com.sun.star.wizards.document.OfficeDocument;
import com.sun.star.wizards.ui.event.Task;

/**
 * About the member fields Title, Decription and Author:
 * There is an ambiguicy in handling these fields.
 * On the one hand, the user can set those fields to a custom value,
 * relevant only for the web-wizard.
 * On the other-hand, the web-wizard can read those fields automatically
 * from SO documents.
 * In order to avoid conflict by saving, I use two fields: "cp_title", which
 * should be read from the configuratin (saved session), and "title" which is
 * the value read from the document.
 * The *cp_title* "overrides" *title*, if exists. if not, *title* is used.
 * The same is valid for *description* and *author*.
 * @author  rpiterman
 */
public class CGDocument extends ConfigSetItem implements XMLProvider
{

    public static final int PAGE_TYPE_UNKNOWN = 0;
    public static final int PAGE_TYPE_PAGE = 1;
    public static final int PAGE_TYPE_SLIDE = 2;
    public String cp_Title = "";
    public String cp_Description = "";
    public String cp_URL;
    public String cp_Author = "";
    public String cp_Exporter;
    /**
     * the destination filename to which this
     * document will be exported to.
     */
    public String localFilename = "";
    public String urlFilename = "";
    public String title = "";
    public String description = "";
    public String author = "";
    public DateTime createDate;
    public DateTime updateDate;
    public int sizeBytes = -1;
    public int pages = -1;
    /**
     * The contents subdirectory name in which the document is in.
     * This subdirectory will be created in the content's "docs"
     * subdirectory.
     */
    public String dirName;
    /**
     * contains an identifier of the type of this document.
     */
    public String appType;
    public PropertyValue[] mediaDescriptor;
    /**
     * Is this document an original SO Document (different versions).
     */
    public boolean isSODocument;
    /**
     * can this document be opend through SO?
     */
    public boolean isSOOpenable;
    /**
     * This flag is set to true after the validate method is called.
     */
    public boolean valid = false;
    public String targetTypeName;

    public CGDocument()
    {
    }

    public CGDocument(String url, XMultiServiceFactory xmsf, Task task) throws Exception
    {
        cp_URL = getSettings().getFileAccess(xmsf).getURL(url);
        if (task == null)
        {
            task = new Task("", "", 5);
        }
        validate(xmsf, task);
    }

    /**
     * the task will advance 5 times during validate.
     * @param xmsf
     * @param task
     * @throws Exception
     */
    public void validate(XMultiServiceFactory xmsf, Task task)
            throws FileNotFoundException,
            IllegalArgumentException,
            Exception
    {

        if (!getSettings().getFileAccess(xmsf).exists(cp_URL, false))
        {
            throw new FileNotFoundException("The given URL does not point to a file");
        }
        if (getSettings().getFileAccess(xmsf).isDirectory(cp_URL))
        {
            throw new IllegalArgumentException("The given URL points to a directory");        //create a TypeDetection service
        }
        mediaDescriptor = OfficeDocument.getFileMediaDecriptor(xmsf, cp_URL);

        task.advance(true); //1

        //System.out.println(Properties.getPropertyValue(mediaDescriptor,"UIName"));

        analyzeFileType(mediaDescriptor);

        task.advance(true); //2

        String path = getSettings().getFileAccess(xmsf).getPath(cp_URL, "");
        localFilename = FileAccess.getFilename(path, File.separator);

        /* if the type is a star office convertable document
         * We try to open the document to get some properties
         */

        XDocumentProperties xProps = null;

        task.advance(true); //3

        if (isSOOpenable)
        { // for documents which are openable through SO, use DocumentProperties service.
            XDesktop desktop = Desktop.getDesktop(xmsf);
            PropertyValue[] props = new PropertyValue[3];
            props[0] = Properties.createProperty("Hidden", Boolean.TRUE);
            props[1] = Properties.createProperty("MacroExecutionMode", new Short(MacroExecMode.NEVER_EXECUTE));
            props[2] = Properties.createProperty("UpdateDocMode", new Short(UpdateDocMode.NO_UPDATE));
            XComponent component = ((XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop)).loadComponentFromURL(cp_URL, "_default", 0, props);
            xProps = ((XDocumentPropertiesSupplier) UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, component)).getDocumentProperties();
        }

        task.advance(true); //4

        //now use the object to read some document properties.
        if (xProps != null)
        {
            title = xProps.getTitle();
            description = xProps.getDescription();
            author = xProps.getAuthor();
            createDate = xProps.getCreationDate();
            updateDate = xProps.getModificationDate();
        }
        else
        { //get some information from OS.
            title = localFilename;
            updateDate = getSettings().getFileAccess(xmsf).getLastModified(cp_URL);
        }

        task.advance(true); //5

        valid = true;

        if (cp_Title.equals(""))
        {
            cp_Title = title;
        }
        if (cp_Title.equals(""))
        {
            cp_Title = localFilename;
        }
        if (cp_Description.equals(""))
        {
            cp_Description = description;
        }
        if (cp_Author.equals(""))
        {
            cp_Author = author;
        }
        if (cp_Exporter == null || cp_Exporter.equals(""))
        {
            cp_Exporter = (String) getSettings().cp_Exporters.getKey(
                    getSettings().getExporters(appType)[0]);
        }
    }

    /**
     * Analyzes a type-detection string, returned from the TypeDetection service,
     * and sets the appType, isSOOpenable and isSODocument members.
     */
    private void analyzeFileType(PropertyValue[] mediaDesc)
    {

        if (mediaDesc == null)
        {
        }

        String media = (mediaDesc == null)
                ? "" : (String) Properties.getPropertyValue(mediaDescriptor, "Name");
        appType = getDocType(media);

        //System.out.println(appType);

        isSOOpenable =
                (appType == TypeDetection.WRITER_DOC || appType == TypeDetection.CALC_DOC || appType == TypeDetection.IMPRESS_DOC || appType == TypeDetection.DRAW_DOC) || appType == TypeDetection.HTML_DOC;

//        String[] parts = media.split("_");    // line removed because of compatibility to JDK13
        String[] parts = JavaTools.ArrayoutofString(media, "_");


        isSODocument = parts.length < 2 ? false : isSOOpenable && (parts[1].startsWith("Star"));

    }

    /**
     * @param media is the media description string returned by an UNO TypeDetection object.
     * @return one of the constants in the interface TypeDetection.
     *
     */
    private String getDocType(String media)
    {
        if (media.equals(""))
        {
            return TypeDetection.NO_TYPE;
        }
        if (media.startsWith("writer"))
        {
            if (media.startsWith("writer_web_HTML"))
            {
                return TypeDetection.HTML_DOC;
            }
            else
            {
                return TypeDetection.WRITER_DOC;
            }
        }
        else if (media.startsWith("calc"))
        {
            return TypeDetection.CALC_DOC;
        }
        else if (media.startsWith("draw"))
        {
            return TypeDetection.DRAW_DOC;
        }
        else if (media.startsWith("impress"))
        {
            return TypeDetection.IMPRESS_DOC;
        }
        else if (media.startsWith("pdf"))
        {
            return TypeDetection.PDF_DOC;
        }
        else if (media.startsWith("gif") || media.startsWith("jpg"))
        {
            return TypeDetection.WEB_GRAPHICS;
        }
        else if (media.startsWith("wav"))
        {
            return TypeDetection.SOUND_FILE;
        }
        else
        {
            return TypeDetection.NO_TYPE;
        }
    }

    public Node createDOM(Node parent)
    {
        CGDesign d = getSettings().cp_DefaultSession.cp_Design;
        CGExporter exp = (CGExporter) getSettings().cp_Exporters.getElement(cp_Exporter);

        return XMLHelper.addElement(parent, "document",
                new String[]
                {
                    "title", "description", "author", "format", "filename", "create-date", "update-date", "pages", "size", "icon", "dir", "fn"
                },
                new String[]
                {
                    d.cp_DisplayTitle ? cp_Title : "",
                    d.cp_DisplayDescription ? cp_Description : "",
                    d.cp_DisplayAuthor ? cp_Author : "",
                    d.cp_DisplayFileFormat ? getTargetTypeName(exp) : "",
                    d.cp_DisplayFilename ? localFilename : "",
                    d.cp_DisplayCreateDate ? createDate() : "",
                    d.cp_DisplayUpdateDate ? updateDate() : "",
                    d.cp_DisplayPages && (pages > -1) ? "" + pages() : "", //TODO when do i calculate pages?
                    d.cp_DisplaySize ? sizeKB() : "",//TODO when do i calculate size?
                    d.cp_DisplayFormatIcon ? getIcon(exp) : "",
                    dirName, urlFilename
                });
    }

    private String updateDate()
    {
        if (this.updateDate == null)
        {
            return "";
        }
        return getSettings().formatter.formatCreated(this.updateDate);
    }

    private String createDate()
    {
        if (this.createDate == null)
        {
            return "";
        }
        return getSettings().formatter.formatCreated(this.createDate);
    }

    private String sizeKB()
    {
        if (sizeBytes == -1)
        {
            return "";
        }
        else
        {
            return getSettings().formatter.formatFileSize(sizeBytes);
        }
    }

    private String pages()
    {
        return pages == -1 ? ""
                : JavaTools.replaceSubString(pagesTemplate(), "" + pages, "%NUMBER");
    }

    private String pagesTemplate()
    {
        int pagesType = ((CGExporter) getSettings().cp_Exporters.getElement(cp_Exporter)).cp_PageType;
        switch (pagesType)
        {
            case PAGE_TYPE_UNKNOWN:
                return "";
            case PAGE_TYPE_PAGE:
                return getSettings().resources[CGSettings.RESOURCE_PAGES_TEMPLATE];
            case PAGE_TYPE_SLIDE:
                return getSettings().resources[CGSettings.RESOURCE_SLIDES_TEMPLATE];
            default:
                return "";
        }
    }

    private String getTargetTypeName(CGExporter exp)
    {
        return (exp.targetTypeName.equals(""))
                ? (String) Properties.getPropertyValue(mediaDescriptor, "UIName")
                : exp.targetTypeName;
    }

    private String getIcon(CGExporter exporter)
    {
        return exporter.cp_Icon.equals("") ? getIcon(this.appType) : exporter.cp_Icon;
    }

    private String getIcon(String appType)
    {
        //System.out.println("Getting Icon for: " + appType);
        return appType + ".gif";
    }

    /**
     * This method is used by the DataAware model
     * and returns the index of the exporter of
     * this document, <b>in the list of the
     * exporters available for this type of document</b>
     * @return
     */
    public short[] getExporter()
    {
        return new short[]
                {
                    getExporterIndex()
                };
    }

    /**
     * @see getExporter()
     */
    public void setExporter(short[] exporter_)
    {
        Object exp = getSettings().getExporters(appType)[exporter_[0]];
        cp_Exporter = (String) getSettings().cp_Exporters.getKey(exp);
    }

    /**
     * @see getExporter()
     * @return
     */
    private short getExporterIndex()
    {
        if (cp_Exporter == null)
        {
            return 0;
        }
        Object exporter = getSettings().cp_Exporters.getElement(cp_Exporter);
        Object[] exporters = getSettings().getExporters(appType);
        for (short i = 0; i < exporters.length; i++)
        {
            if (exporters[i] == exporter)
            {
                return i;
            }
        }
        return -1;
    }

    public String toString()
    {
        return localFilename;
    }
}