1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package graphical; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.util.ArrayList; 27cdf0e10cSrcweir 28cdf0e10cSrcweir /** 29cdf0e10cSrcweir * 30cdf0e10cSrcweir * @author ll93751 31cdf0e10cSrcweir */ 32cdf0e10cSrcweir public class Office implements IOffice 33cdf0e10cSrcweir { 34cdf0e10cSrcweir private ParameterHelper m_aParameterHelper; 35cdf0e10cSrcweir private String m_sDocumentName; 36cdf0e10cSrcweir private String m_sResult; 37cdf0e10cSrcweir private IOffice m_aOffice = null; 38cdf0e10cSrcweir Office(ParameterHelper _aParam, String _sResult)39cdf0e10cSrcweir public Office(ParameterHelper _aParam, String _sResult) 40cdf0e10cSrcweir { 41cdf0e10cSrcweir m_aParameterHelper = _aParam; 42cdf0e10cSrcweir m_sResult = _sResult; 43cdf0e10cSrcweir 44cdf0e10cSrcweir if (_aParam.getReferenceType().toLowerCase().equals("ooo") || 45cdf0e10cSrcweir _aParam.getReferenceType().toLowerCase().equals("o3") || 46cdf0e10cSrcweir _aParam.getReferenceType().toLowerCase().equals("ps") || 47cdf0e10cSrcweir _aParam.getReferenceType().toLowerCase().equals("pdf")) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir m_aOffice = new OpenOfficePostscriptCreator(_aParam, m_sResult); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir else if (_aParam.getReferenceType().toLowerCase().equals("msoffice")) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir m_aOffice = new MSOfficePostscriptCreator(_aParam, m_sResult); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir } 56cdf0e10cSrcweir 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** 59cdf0e10cSrcweir * Load a document with an already started Office. 60cdf0e10cSrcweir * @param _sDocumentName 61cdf0e10cSrcweir * @throws graphical.OfficeException 62cdf0e10cSrcweir */ load(String _sDocumentName)63cdf0e10cSrcweir public void load(String _sDocumentName) throws OfficeException 64cdf0e10cSrcweir { 65cdf0e10cSrcweir m_sDocumentName = _sDocumentName; 66cdf0e10cSrcweir // check if given file is a picture, then do nothing 67cdf0e10cSrcweir String sDocumentSuffix = FileHelper.getSuffix(m_sDocumentName); 68cdf0e10cSrcweir if (sDocumentSuffix.toLowerCase().endsWith(".png") || 69cdf0e10cSrcweir sDocumentSuffix.toLowerCase().endsWith(".gif") || 70cdf0e10cSrcweir sDocumentSuffix.toLowerCase().endsWith(".jpg") || 71cdf0e10cSrcweir sDocumentSuffix.toLowerCase().endsWith(".bmp")) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir throw new OfficeException("The given document is not a document type."); 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir // TODO: we should start the office after we know if we really need an Office. 77cdf0e10cSrcweir if (m_aOffice != null) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir if (sDocumentSuffix.toLowerCase().endsWith(".odb")) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir if (m_aParameterHelper.getReferenceType().toLowerCase().equals("msoffice")) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir // we can't handle .odb with msoffice 84cdf0e10cSrcweir return; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir // TODO: run through all documents which exists as reports in odb files 87cdf0e10cSrcweir OpenOfficeDatabaseReportExtractor aExtractor = new OpenOfficeDatabaseReportExtractor(m_aParameterHelper); 88cdf0e10cSrcweir ArrayList aList = aExtractor.load(m_sDocumentName); 89cdf0e10cSrcweir if (aList != null) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir // remove the whole section about the 'name'.odb there are no information we need 92cdf0e10cSrcweir // we will create a new one. 93cdf0e10cSrcweir String sIniFile = FileHelper.appendPath(m_sResult, "index.ini"); 94cdf0e10cSrcweir IniFile aIniFile2 = new IniFile(sIniFile); 95cdf0e10cSrcweir String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file 96cdf0e10cSrcweir aIniFile2.removeSection(sSection); 97cdf0e10cSrcweir aIniFile2.close(); 98cdf0e10cSrcweir 99cdf0e10cSrcweir for (int i=0; i<aList.size();i++) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir String sDocumentName = (String)aList.get(i); 102cdf0e10cSrcweir m_aOffice.load(sDocumentName); 103cdf0e10cSrcweir m_aOffice.storeAsPostscript(); 104cdf0e10cSrcweir 105cdf0e10cSrcweir 106cdf0e10cSrcweir // foreach Report found in the .odb file, create an entry 'report'<number> in the original <name>.odb Section 107cdf0e10cSrcweir // so it is possible to run through all reports by the given .odb name 108cdf0e10cSrcweir IniFile aIniFile = new IniFile(sIniFile); 109cdf0e10cSrcweir // String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file 110cdf0e10cSrcweir int nFileCount = aIniFile.getIntValue(sSection, "reportcount", 0); 111cdf0e10cSrcweir String sValue = FileHelper.getBasename(sDocumentName); // name of the corresponding report 112cdf0e10cSrcweir aIniFile.insertValue(sSection, "report" + nFileCount, sValue); 113cdf0e10cSrcweir aIniFile.insertValue(sSection, "reportcount", nFileCount + 1); 114cdf0e10cSrcweir aIniFile.close(); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir } 117cdf0e10cSrcweir else 118cdf0e10cSrcweir { 119cdf0e10cSrcweir throw new OfficeException("Can't open the document " + m_sDocumentName); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir else 123cdf0e10cSrcweir { 124cdf0e10cSrcweir m_aOffice.load(_sDocumentName); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } 128cdf0e10cSrcweir storeAsPostscript()129cdf0e10cSrcweir public void storeAsPostscript() throws OfficeException 130cdf0e10cSrcweir { 131cdf0e10cSrcweir if (m_aOffice != null) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir if (m_sDocumentName.endsWith(".odb")) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir // this has already be done by load() for odb files. 136cdf0e10cSrcweir } 137cdf0e10cSrcweir else 138cdf0e10cSrcweir { 139cdf0e10cSrcweir m_aOffice.storeAsPostscript(); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir // FileHelper.addBasenameToIndex(sOutputFilename); 143cdf0e10cSrcweir } 144cdf0e10cSrcweir } 145cdf0e10cSrcweir start()146cdf0e10cSrcweir public void start() throws OfficeException 147cdf0e10cSrcweir { 148cdf0e10cSrcweir if (m_aOffice != null) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir m_aOffice.start(); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir } 153cdf0e10cSrcweir close()154cdf0e10cSrcweir public void close() throws OfficeException 155cdf0e10cSrcweir { 156cdf0e10cSrcweir if (m_aOffice != null) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir m_aOffice.close(); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir 163cdf0e10cSrcweir 164cdf0e10cSrcweir 165cdf0e10cSrcweir } 166