xref: /AOO41X/main/qadevOOo/runner/graphical/Office.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*
2*cdf0e10cSrcweir  * ************************************************************************
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir  *
16*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir  *
22*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir  *
27*cdf0e10cSrcweir  * ***********************************************************************
28*cdf0e10cSrcweir  */
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir package graphical;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import java.util.ArrayList;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /**
35*cdf0e10cSrcweir  *
36*cdf0e10cSrcweir  * @author ll93751
37*cdf0e10cSrcweir  */
38*cdf0e10cSrcweir public class Office implements IOffice
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir     private ParameterHelper m_aParameterHelper;
41*cdf0e10cSrcweir     private String m_sDocumentName;
42*cdf0e10cSrcweir     private String m_sResult;
43*cdf0e10cSrcweir     private IOffice m_aOffice = null;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir     public Office(ParameterHelper _aParam, String _sResult)
46*cdf0e10cSrcweir     {
47*cdf0e10cSrcweir         m_aParameterHelper = _aParam;
48*cdf0e10cSrcweir         m_sResult = _sResult;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir         if (_aParam.getReferenceType().toLowerCase().equals("ooo") ||
51*cdf0e10cSrcweir             _aParam.getReferenceType().toLowerCase().equals("o3") ||
52*cdf0e10cSrcweir             _aParam.getReferenceType().toLowerCase().equals("ps") ||
53*cdf0e10cSrcweir             _aParam.getReferenceType().toLowerCase().equals("pdf"))
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir             m_aOffice = new OpenOfficePostscriptCreator(_aParam, m_sResult);
56*cdf0e10cSrcweir         }
57*cdf0e10cSrcweir         else if (_aParam.getReferenceType().toLowerCase().equals("msoffice"))
58*cdf0e10cSrcweir         {
59*cdf0e10cSrcweir             m_aOffice = new MSOfficePostscriptCreator(_aParam, m_sResult);
60*cdf0e10cSrcweir         }
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     /**
65*cdf0e10cSrcweir      * Load a document with an already started Office.
66*cdf0e10cSrcweir      * @param _sDocumentName
67*cdf0e10cSrcweir      * @throws graphical.OfficeException
68*cdf0e10cSrcweir      */
69*cdf0e10cSrcweir     public void load(String _sDocumentName) throws OfficeException
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir         m_sDocumentName = _sDocumentName;
72*cdf0e10cSrcweir         // check if given file is a picture, then do nothing
73*cdf0e10cSrcweir         String sDocumentSuffix = FileHelper.getSuffix(m_sDocumentName);
74*cdf0e10cSrcweir         if (sDocumentSuffix.toLowerCase().endsWith(".png") ||
75*cdf0e10cSrcweir             sDocumentSuffix.toLowerCase().endsWith(".gif") ||
76*cdf0e10cSrcweir             sDocumentSuffix.toLowerCase().endsWith(".jpg") ||
77*cdf0e10cSrcweir             sDocumentSuffix.toLowerCase().endsWith(".bmp"))
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             throw new OfficeException("The given document is not a document type.");
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         // TODO: we should start the office after we know if we really need an Office.
83*cdf0e10cSrcweir         if (m_aOffice != null)
84*cdf0e10cSrcweir         {
85*cdf0e10cSrcweir             if (sDocumentSuffix.toLowerCase().endsWith(".odb"))
86*cdf0e10cSrcweir             {
87*cdf0e10cSrcweir                 if (m_aParameterHelper.getReferenceType().toLowerCase().equals("msoffice"))
88*cdf0e10cSrcweir                 {
89*cdf0e10cSrcweir                     // we can't handle .odb with msoffice
90*cdf0e10cSrcweir                     return;
91*cdf0e10cSrcweir                 }
92*cdf0e10cSrcweir                 // TODO: run through all documents which exists as reports in odb files
93*cdf0e10cSrcweir                 OpenOfficeDatabaseReportExtractor aExtractor = new OpenOfficeDatabaseReportExtractor(m_aParameterHelper);
94*cdf0e10cSrcweir                 ArrayList aList = aExtractor.load(m_sDocumentName);
95*cdf0e10cSrcweir                 if (aList != null)
96*cdf0e10cSrcweir                 {
97*cdf0e10cSrcweir                     // remove the whole section about the 'name'.odb there are no information we need
98*cdf0e10cSrcweir                     // we will create a new one.
99*cdf0e10cSrcweir                     String sIniFile = FileHelper.appendPath(m_sResult, "index.ini");
100*cdf0e10cSrcweir                     IniFile aIniFile2 = new IniFile(sIniFile);
101*cdf0e10cSrcweir                     String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
102*cdf0e10cSrcweir                     aIniFile2.removeSection(sSection);
103*cdf0e10cSrcweir                     aIniFile2.close();
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir                     for (int i=0; i<aList.size();i++)
106*cdf0e10cSrcweir                     {
107*cdf0e10cSrcweir                         String sDocumentName = (String)aList.get(i);
108*cdf0e10cSrcweir                         m_aOffice.load(sDocumentName);
109*cdf0e10cSrcweir                         m_aOffice.storeAsPostscript();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir                         // foreach Report found in the .odb file, create an entry 'report'<number> in the original <name>.odb Section
113*cdf0e10cSrcweir                         // so it is possible to run through all reports by the given .odb name
114*cdf0e10cSrcweir                         IniFile aIniFile = new IniFile(sIniFile);
115*cdf0e10cSrcweir                         // String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
116*cdf0e10cSrcweir                         int nFileCount = aIniFile.getIntValue(sSection, "reportcount", 0);
117*cdf0e10cSrcweir                         String sValue = FileHelper.getBasename(sDocumentName);    // name of the corresponding report
118*cdf0e10cSrcweir                         aIniFile.insertValue(sSection, "report" + nFileCount, sValue);
119*cdf0e10cSrcweir                         aIniFile.insertValue(sSection, "reportcount", nFileCount + 1);
120*cdf0e10cSrcweir                         aIniFile.close();
121*cdf0e10cSrcweir                     }
122*cdf0e10cSrcweir                 }
123*cdf0e10cSrcweir                 else
124*cdf0e10cSrcweir                 {
125*cdf0e10cSrcweir                     throw new OfficeException("Can't open the document " + m_sDocumentName);
126*cdf0e10cSrcweir                 }
127*cdf0e10cSrcweir             }
128*cdf0e10cSrcweir             else
129*cdf0e10cSrcweir             {
130*cdf0e10cSrcweir                 m_aOffice.load(_sDocumentName);
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir         }
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     public void storeAsPostscript() throws OfficeException
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         if (m_aOffice != null)
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             if (m_sDocumentName.endsWith(".odb"))
140*cdf0e10cSrcweir             {
141*cdf0e10cSrcweir                 // this has already be done by load() for odb files.
142*cdf0e10cSrcweir             }
143*cdf0e10cSrcweir             else
144*cdf0e10cSrcweir             {
145*cdf0e10cSrcweir                 m_aOffice.storeAsPostscript();
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //          FileHelper.addBasenameToIndex(sOutputFilename);
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     public void start() throws OfficeException
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         if (m_aOffice != null)
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             m_aOffice.start();
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     public void close() throws OfficeException
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         if (m_aOffice != null)
163*cdf0e10cSrcweir         {
164*cdf0e10cSrcweir             m_aOffice.close();
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir }
172