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 graphical;
25 
26 import java.io.File;
27 // import java.io.FileWriter;
28 // import java.io.OutputStream;
29 // import java.io.PrintStream;
30 import java.io.PrintStream;
31 import java.io.RandomAccessFile;
32 // import java.io.StringWriter;
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      */
getStartTime()42     public long getStartTime()
43         {
44             return System.currentTimeMillis();
45         }
setStartTime(long _nStartTime)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      */
meanTime(long _nCurrentTimer)54     private long meanTime(long _nCurrentTimer)
55         {
56             if (_nCurrentTimer == 0)
57             {
58                 GlobalLogWriter.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     final static int OfficeStop = 5;
84     final static int AllTime = 6;
85     final static int LAST_VALUE = 7; // THIS MUST BE ALWAYS THE LAST AND THE BIGGEST VALUE!
86 
87     private long m_nTime[];
88     private String m_sMSOfficeVersion;
89 
PerformanceContainer()90     public PerformanceContainer()
91         {
92             m_nTime = new long[LAST_VALUE];
93             // @todo: is this need?
94             for (int i=0;i<LAST_VALUE;i++)
95             {
96                 m_nTime[i] = 0;
97             }
98         }
99 
setTime(int _nIndex, long _nValue)100     public void setTime(int _nIndex, long _nValue)
101         {
102             m_nTime[_nIndex] = _nValue;
103         }
getTime(int _nIndex)104     public long getTime(int _nIndex)
105         {
106             return m_nTime[_nIndex];
107         }
108 
startTime(int _nIndex)109     public void startTime(int _nIndex)
110         {
111             m_nTime[_nIndex] = getStartTime();
112         }
113 
stopTime(int _nIndex)114     public void stopTime(int _nIndex)
115         {
116             m_nTime[_nIndex] = meanTime(m_nTime[_nIndex]);
117         }
118 
getMSOfficeVersion()119     public String getMSOfficeVersion()
120     {
121         return m_sMSOfficeVersion;
122     }
123 
print(PrintStream out)124     public void print(PrintStream out)
125         {
126             // String ls = System.getProperty("line.separator");
127             // out.
128             out.println("loadtime="        + String.valueOf(m_nTime[ Load ]));
129             out.println("storetime="       + String.valueOf(m_nTime[ Store ]));
130             out.println("printtime="       + String.valueOf(m_nTime[ Print ]));
131             out.println("officestarttime=" + String.valueOf(m_nTime[ OfficeStart ]));
132             out.println("officestoptime="  + String.valueOf(m_nTime[ OfficeStop ]));
133             out.println("storeaspdftime="  + String.valueOf(m_nTime[ StoreAsPDF ]));
134             out.println("alltime="         + String.valueOf(m_nTime[ AllTime ]));
135         }
136 
print(IniFile _aIniFile, String _sSection)137     public void print(IniFile _aIniFile, String _sSection)
138         {
139             // String ls = System.getProperty("line.separator");
140             // out.
141             _aIniFile.insertValue(_sSection, "loadtime"        , String.valueOf(m_nTime[ Load ]));
142             _aIniFile.insertValue(_sSection, "storetime"       , String.valueOf(m_nTime[ Store ]));
143             _aIniFile.insertValue(_sSection, "printtime"       , String.valueOf(m_nTime[ Print ]));
144             _aIniFile.insertValue(_sSection, "officestarttime" , String.valueOf(m_nTime[ OfficeStart ]));
145             _aIniFile.insertValue(_sSection, "officestoptime"  , String.valueOf(m_nTime[ OfficeStop ]));
146             _aIniFile.insertValue(_sSection, "storeaspdftime"  , String.valueOf(m_nTime[ StoreAsPDF ]));
147             _aIniFile.insertValue(_sSection, "alltime"         , String.valueOf(m_nTime[ AllTime ]));
148         }
149 
stringToDouble(String _sStr)150     public static double stringToDouble(String _sStr)
151         {
152             double nValue = 0;
153             try
154             {
155                 nValue = Double.parseDouble( _sStr );
156             }
157             catch (NumberFormatException e)
158             {
159                 GlobalLogWriter.println("Can't convert string to double " + _sStr);
160             }
161             return nValue;
162         }
163 
secondsToMilliSeconds(double _nSeconds)164     public static long secondsToMilliSeconds(double _nSeconds)
165         {
166             return (long)(_nSeconds * 1000.0);
167         }
168 
169     /*
170       Helper function, which read some values from a given file
171 
172       sample of wordinfofile
173     name=c:\doc-pool\wntmsci\samples\msoffice\word\LineSpacing.doc
174     WordVersion=11.0
175     WordStartTime=0.340490102767944
176     WordLoadTime=0.650935888290405
177     WordPrintTime=0.580835103988647
178      */
readWordValuesFromFile(String sFilename)179     public void readWordValuesFromFile(String sFilename)
180         {
181             File aFile = new File(sFilename);
182             if (! aFile.exists())
183             {
184                 GlobalLogWriter.println("couldn't find file " + sFilename);
185                 return;
186             }
187 
188             RandomAccessFile aRandomAccessFile = null;
189             try
190             {
191                 aRandomAccessFile = new RandomAccessFile(aFile,"r");
192                 String sLine = "";
193                 while (sLine != null)
194                 {
195                     sLine = aRandomAccessFile.readLine();
196                     if ( (sLine != null) &&
197 						 (! (sLine.length() < 2) ) &&
198 						 (! sLine.startsWith("#")))
199 					{
200                         if (sLine.startsWith("WordStartTime="))
201                         {
202                             String sTime = sLine.substring(14);
203                             m_nTime[OfficeStart] = secondsToMilliSeconds(stringToDouble(sTime));
204                         }
205                         else if (sLine.startsWith("WordLoadTime="))
206                         {
207                             String sTime = sLine.substring(13);
208                             m_nTime[Load] = secondsToMilliSeconds(stringToDouble(sTime));
209                         }
210                         else if (sLine.startsWith("WordPrintTime="))
211                         {
212                             String sTime = sLine.substring(14);
213                             m_nTime[Print] = secondsToMilliSeconds(stringToDouble(sTime));
214                         }
215                         else if (sLine.startsWith("WordVersion="))
216                         {
217                             String sMSOfficeVersion = sLine.substring(12);
218                             m_sMSOfficeVersion = "Word:" + sMSOfficeVersion;
219                         }
220                         else if (sLine.startsWith("ExcelVersion="))
221                         {
222                             String sMSOfficeVersion = sLine.substring(13);
223                             m_sMSOfficeVersion = "Excel:" + sMSOfficeVersion;
224                         }
225                         else if (sLine.startsWith("PowerPointVersion="))
226                         {
227                             String sMSOfficeVersion = sLine.substring(18);
228                             m_sMSOfficeVersion = "PowerPoint:" + sMSOfficeVersion;
229                         }
230 					}
231 				}
232             }
233             catch (java.io.FileNotFoundException fne)
234             {
235                 GlobalLogWriter.println("couldn't open file " + sFilename);
236                 GlobalLogWriter.println("Message: " + fne.getMessage());
237             }
238             catch (java.io.IOException ie)
239             {
240                 GlobalLogWriter.println("Exception while reading file " + sFilename);
241                 GlobalLogWriter.println("Message: " + ie.getMessage());
242             }
243             try
244             {
245                 aRandomAccessFile.close();
246             }
247             catch (java.io.IOException ie)
248             {
249                 GlobalLogWriter.println("Couldn't close file " + sFilename);
250                 GlobalLogWriter.println("Message: " + ie.getMessage());
251             }
252         }
253 
254 //    public static void main(String[] args)
255 //    {
256 //        BorderRemover a = new BorderRemover();
257 //        try
258 //        {
259 //            a.createNewImageWithoutBorder(args[0], args[1]);
260 //        }
261 //        catch(java.io.IOException e)
262 //        {
263 //            System.out.println("Exception caught.");
264 //        }
265 //
266 //    }
267 }
268