1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package convwatch;
29 
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.RandomAccessFile;
33 import java.lang.Double;
34 
35 
36 public class PerformanceContainer /* extends *//* implements */ {
37     private long m_nStartTime;
38 
39     /*
40       simple helper functions to start/stop a timer, to know how long a process need in milliseconds
41      */
42     public long getStartTime()
43         {
44             return System.currentTimeMillis();
45         }
46     public void setStartTime(long _nStartTime)
47         {
48             m_nStartTime = _nStartTime;
49         }
50 
51     /*
52       return the time, which is done until last startTime()
53      */
54     private long meanTime(long _nCurrentTimer)
55         {
56             if (_nCurrentTimer == 0)
57             {
58                 GlobalLogWriter.get().println("Forgotten to initialise a start timer.");
59                 return 0;
60             }
61             long nMeanTime = System.currentTimeMillis();
62             return nMeanTime - _nCurrentTimer;
63         }
64 
65     /*
66       public long stopTimer()
67         {
68             if (m_nStartTime == 0)
69             {
70                 System.out.println("Forgotten to initialise start timer.");
71                 return 0;
72             }
73             long nStopTime = System.currentTimeMillis();
74             return nStopTime - m_nStartTime;
75         }
76     */
77 
78     final static int Load = 0;
79     final static int Store = 1;
80     final static int Print = 2;
81     final static int OfficeStart = 3;
82     final static int StoreAsPDF = 4;
83 
84     private long m_nTime[];
85     private String m_sMSOfficeVersion;
86 
87     public PerformanceContainer()
88         {
89             m_nTime = new long[5];
90             // @todo: is this need?
91             for (int i=0;i<5;i++)
92             {
93                 m_nTime[i] = 0;
94             }
95         }
96 
97     public void setTime(int _nIndex, long _nValue)
98         {
99             m_nTime[_nIndex] = _nValue;
100         }
101     public long getTime(int _nIndex)
102         {
103             return m_nTime[_nIndex];
104         }
105 
106     public void startTime(int _nIndex)
107         {
108             m_nTime[_nIndex] = getStartTime();
109         }
110 
111     public void stopTime(int _nIndex)
112         {
113             m_nTime[_nIndex] = meanTime(m_nTime[_nIndex]);
114         }
115 
116     public String getMSOfficeVersion()
117     {
118         return m_sMSOfficeVersion;
119     }
120     public void print(FileWriter out) throws java.io.IOException
121         {
122             String ls = System.getProperty("line.separator");
123 
124             out.write("loadtime="        + String.valueOf(m_nTime[ Load ]) + ls);
125             out.write("storetime="       + String.valueOf(m_nTime[ Store ]) + ls);
126             out.write("printtime="       + String.valueOf(m_nTime[ Print ]) + ls);
127             out.write("officestarttime=" + String.valueOf(m_nTime[ OfficeStart ]) + ls);
128             out.write("storeaspdftime="  + String.valueOf(m_nTime[ StoreAsPDF ]) + ls);
129         }
130 
131     public static double stringToDouble(String _sStr)
132         {
133             double nValue = 0;
134             try
135             {
136                 nValue = Double.parseDouble( _sStr );
137             }
138             catch (NumberFormatException e)
139             {
140                 GlobalLogWriter.get().println("Can't convert string to double " + _sStr);
141             }
142             return nValue;
143         }
144 
145     public static long secondsToMilliSeconds(double _nSeconds)
146         {
147             return (long)(_nSeconds * 1000.0);
148         }
149 
150     /*
151       Helper function, which read some values from a given file
152 
153       sample of wordinfofile
154     name=c:\doc-pool\wntmsci\samples\msoffice\word\LineSpacing.doc
155     WordVersion=11.0
156     WordStartTime=0.340490102767944
157     WordLoadTime=0.650935888290405
158     WordPrintTime=0.580835103988647
159      */
160     public void readWordValuesFromFile(String sFilename)
161         {
162             File aFile = new File(sFilename);
163             if (! aFile.exists())
164             {
165                 GlobalLogWriter.get().println("couldn't find file " + sFilename);
166                 return;
167             }
168 
169             RandomAccessFile aRandomAccessFile = null;
170             try
171             {
172                 aRandomAccessFile = new RandomAccessFile(aFile,"r");
173                 String sLine = "";
174                 while (sLine != null)
175                 {
176                     sLine = aRandomAccessFile.readLine();
177                     if ( (sLine != null) &&
178 						 (! (sLine.length() < 2) ) &&
179 						 (! sLine.startsWith("#")))
180 					{
181                         if (sLine.startsWith("WordStartTime="))
182                         {
183                             String sTime = sLine.substring(14);
184                             m_nTime[OfficeStart] = secondsToMilliSeconds(stringToDouble(sTime));
185                         }
186                         else if (sLine.startsWith("WordLoadTime="))
187                         {
188                             String sTime = sLine.substring(13);
189                             m_nTime[Load] = secondsToMilliSeconds(stringToDouble(sTime));
190                         }
191                         else if (sLine.startsWith("WordPrintTime="))
192                         {
193                             String sTime = sLine.substring(14);
194                             m_nTime[Print] = secondsToMilliSeconds(stringToDouble(sTime));
195                         }
196                         else if (sLine.startsWith("WordVersion="))
197                         {
198                             String sMSOfficeVersion = sLine.substring(12);
199                             m_sMSOfficeVersion = "Word:" + sMSOfficeVersion;
200                         }
201                         else if (sLine.startsWith("ExcelVersion="))
202                         {
203                             String sMSOfficeVersion = sLine.substring(13);
204                             m_sMSOfficeVersion = "Excel:" + sMSOfficeVersion;
205                         }
206                         else if (sLine.startsWith("PowerPointVersion="))
207                         {
208                             String sMSOfficeVersion = sLine.substring(18);
209                             m_sMSOfficeVersion = "PowerPoint:" + sMSOfficeVersion;
210                         }
211 					}
212 				}
213             }
214             catch (java.io.FileNotFoundException fne)
215             {
216                 GlobalLogWriter.get().println("couldn't open file " + sFilename);
217                 GlobalLogWriter.get().println("Message: " + fne.getMessage());
218             }
219             catch (java.io.IOException ie)
220             {
221                 GlobalLogWriter.get().println("Exception while reading file " + sFilename);
222                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
223             }
224             try
225             {
226                 aRandomAccessFile.close();
227             }
228             catch (java.io.IOException ie)
229             {
230                 GlobalLogWriter.get().println("Couldn't close file " + sFilename);
231                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
232             }
233         }
234 
235 //    public static void main(String[] args) {
236 //
237 ///*
238 // BorderRemover a = new BorderRemover();
239 //        try
240 //        {
241 //            a.createNewImageWithoutBorder(args[0], args[1]);
242 //        }
243 //        catch(java.io.IOException e)
244 //        {
245 //            System.out.println("Exception caught.");
246 //        }
247 // */
248 //    }
249 
250 }
251