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 package convwatch;
25 
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.RandomAccessFile;
29 import java.lang.Double;
30 
31 
32 public class PerformanceContainer /* extends *//* implements */ {
33     private long m_nStartTime;
34 
35     /*
36       simple helper functions to start/stop a timer, to know how long a process need in milliseconds
37      */
getStartTime()38     public long getStartTime()
39         {
40             return System.currentTimeMillis();
41         }
setStartTime(long _nStartTime)42     public void setStartTime(long _nStartTime)
43         {
44             m_nStartTime = _nStartTime;
45         }
46 
47     /*
48       return the time, which is done until last startTime()
49      */
meanTime(long _nCurrentTimer)50     private long meanTime(long _nCurrentTimer)
51         {
52             if (_nCurrentTimer == 0)
53             {
54                 GlobalLogWriter.get().println("Forgotten to initialise a start timer.");
55                 return 0;
56             }
57             long nMeanTime = System.currentTimeMillis();
58             return nMeanTime - _nCurrentTimer;
59         }
60 
61     /*
62       public long stopTimer()
63         {
64             if (m_nStartTime == 0)
65             {
66                 System.out.println("Forgotten to initialise start timer.");
67                 return 0;
68             }
69             long nStopTime = System.currentTimeMillis();
70             return nStopTime - m_nStartTime;
71         }
72     */
73 
74     final static int Load = 0;
75     final static int Store = 1;
76     final static int Print = 2;
77     final static int OfficeStart = 3;
78     final static int StoreAsPDF = 4;
79 
80     private long m_nTime[];
81     private String m_sMSOfficeVersion;
82 
PerformanceContainer()83     public PerformanceContainer()
84         {
85             m_nTime = new long[5];
86             // @todo: is this need?
87             for (int i=0;i<5;i++)
88             {
89                 m_nTime[i] = 0;
90             }
91         }
92 
setTime(int _nIndex, long _nValue)93     public void setTime(int _nIndex, long _nValue)
94         {
95             m_nTime[_nIndex] = _nValue;
96         }
getTime(int _nIndex)97     public long getTime(int _nIndex)
98         {
99             return m_nTime[_nIndex];
100         }
101 
startTime(int _nIndex)102     public void startTime(int _nIndex)
103         {
104             m_nTime[_nIndex] = getStartTime();
105         }
106 
stopTime(int _nIndex)107     public void stopTime(int _nIndex)
108         {
109             m_nTime[_nIndex] = meanTime(m_nTime[_nIndex]);
110         }
111 
getMSOfficeVersion()112     public String getMSOfficeVersion()
113     {
114         return m_sMSOfficeVersion;
115     }
print(FileWriter out)116     public void print(FileWriter out) throws java.io.IOException
117         {
118             String ls = System.getProperty("line.separator");
119 
120             out.write("loadtime="        + String.valueOf(m_nTime[ Load ]) + ls);
121             out.write("storetime="       + String.valueOf(m_nTime[ Store ]) + ls);
122             out.write("printtime="       + String.valueOf(m_nTime[ Print ]) + ls);
123             out.write("officestarttime=" + String.valueOf(m_nTime[ OfficeStart ]) + ls);
124             out.write("storeaspdftime="  + String.valueOf(m_nTime[ StoreAsPDF ]) + ls);
125         }
126 
stringToDouble(String _sStr)127     public static double stringToDouble(String _sStr)
128         {
129             double nValue = 0;
130             try
131             {
132                 nValue = Double.parseDouble( _sStr );
133             }
134             catch (NumberFormatException e)
135             {
136                 GlobalLogWriter.get().println("Can't convert string to double " + _sStr);
137             }
138             return nValue;
139         }
140 
secondsToMilliSeconds(double _nSeconds)141     public static long secondsToMilliSeconds(double _nSeconds)
142         {
143             return (long)(_nSeconds * 1000.0);
144         }
145 
146     /*
147       Helper function, which read some values from a given file
148 
149       sample of wordinfofile
150     name=c:\doc-pool\wntmsci\samples\msoffice\word\LineSpacing.doc
151     WordVersion=11.0
152     WordStartTime=0.340490102767944
153     WordLoadTime=0.650935888290405
154     WordPrintTime=0.580835103988647
155      */
readWordValuesFromFile(String sFilename)156     public void readWordValuesFromFile(String sFilename)
157         {
158             File aFile = new File(sFilename);
159             if (! aFile.exists())
160             {
161                 GlobalLogWriter.get().println("couldn't find file " + sFilename);
162                 return;
163             }
164 
165             RandomAccessFile aRandomAccessFile = null;
166             try
167             {
168                 aRandomAccessFile = new RandomAccessFile(aFile,"r");
169                 String sLine = "";
170                 while (sLine != null)
171                 {
172                     sLine = aRandomAccessFile.readLine();
173                     if ( (sLine != null) &&
174 						 (! (sLine.length() < 2) ) &&
175 						 (! sLine.startsWith("#")))
176 					{
177                         if (sLine.startsWith("WordStartTime="))
178                         {
179                             String sTime = sLine.substring(14);
180                             m_nTime[OfficeStart] = secondsToMilliSeconds(stringToDouble(sTime));
181                         }
182                         else if (sLine.startsWith("WordLoadTime="))
183                         {
184                             String sTime = sLine.substring(13);
185                             m_nTime[Load] = secondsToMilliSeconds(stringToDouble(sTime));
186                         }
187                         else if (sLine.startsWith("WordPrintTime="))
188                         {
189                             String sTime = sLine.substring(14);
190                             m_nTime[Print] = secondsToMilliSeconds(stringToDouble(sTime));
191                         }
192                         else if (sLine.startsWith("WordVersion="))
193                         {
194                             String sMSOfficeVersion = sLine.substring(12);
195                             m_sMSOfficeVersion = "Word:" + sMSOfficeVersion;
196                         }
197                         else if (sLine.startsWith("ExcelVersion="))
198                         {
199                             String sMSOfficeVersion = sLine.substring(13);
200                             m_sMSOfficeVersion = "Excel:" + sMSOfficeVersion;
201                         }
202                         else if (sLine.startsWith("PowerPointVersion="))
203                         {
204                             String sMSOfficeVersion = sLine.substring(18);
205                             m_sMSOfficeVersion = "PowerPoint:" + sMSOfficeVersion;
206                         }
207 					}
208 				}
209             }
210             catch (java.io.FileNotFoundException fne)
211             {
212                 GlobalLogWriter.get().println("couldn't open file " + sFilename);
213                 GlobalLogWriter.get().println("Message: " + fne.getMessage());
214             }
215             catch (java.io.IOException ie)
216             {
217                 GlobalLogWriter.get().println("Exception while reading file " + sFilename);
218                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
219             }
220             try
221             {
222                 aRandomAccessFile.close();
223             }
224             catch (java.io.IOException ie)
225             {
226                 GlobalLogWriter.get().println("Couldn't close file " + sFilename);
227                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
228             }
229         }
230 
231 //    public static void main(String[] args) {
232 //
233 ///*
234 // BorderRemover a = new BorderRemover();
235 //        try
236 //        {
237 //            a.createNewImageWithoutBorder(args[0], args[1]);
238 //        }
239 //        catch(java.io.IOException e)
240 //        {
241 //            System.out.println("Exception caught.");
242 //        }
243 // */
244 //    }
245 
246 }
247