1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PerformanceContainer.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 import java
.io
.FileWriter
;
35 import java
.io
.RandomAccessFile
;
36 import java
.lang
.Double
;
39 public class PerformanceContainer
/* extends *//* implements */ {
40 private long m_nStartTime
;
43 simple helper functions to start/stop a timer, to know how long a process need in milliseconds
45 public long getStartTime()
47 return System
.currentTimeMillis();
49 public void setStartTime(long _nStartTime
)
51 m_nStartTime
= _nStartTime
;
55 return the time, which is done until last startTime()
57 private long meanTime(long _nCurrentTimer
)
59 if (_nCurrentTimer
== 0)
61 GlobalLogWriter
.get().println("Forgotten to initialise a start timer.");
64 long nMeanTime
= System
.currentTimeMillis();
65 return nMeanTime
- _nCurrentTimer
;
69 public long stopTimer()
71 if (m_nStartTime == 0)
73 System.out.println("Forgotten to initialise start timer.");
76 long nStopTime = System.currentTimeMillis();
77 return nStopTime - m_nStartTime;
81 final static int Load
= 0;
82 final static int Store
= 1;
83 final static int Print
= 2;
84 final static int OfficeStart
= 3;
85 final static int StoreAsPDF
= 4;
87 private long m_nTime
[];
88 private String m_sMSOfficeVersion
;
90 public PerformanceContainer()
92 m_nTime
= new long[5];
93 // @todo: is this need?
100 public void setTime(int _nIndex
, long _nValue
)
102 m_nTime
[_nIndex
] = _nValue
;
104 public long getTime(int _nIndex
)
106 return m_nTime
[_nIndex
];
109 public void startTime(int _nIndex
)
111 m_nTime
[_nIndex
] = getStartTime();
114 public void stopTime(int _nIndex
)
116 m_nTime
[_nIndex
] = meanTime(m_nTime
[_nIndex
]);
119 public String
getMSOfficeVersion()
121 return m_sMSOfficeVersion
;
123 public void print(FileWriter out
) throws java
.io
.IOException
125 String ls
= System
.getProperty("line.separator");
127 out
.write("loadtime=" + String
.valueOf(m_nTime
[ Load
]) + ls
);
128 out
.write("storetime=" + String
.valueOf(m_nTime
[ Store
]) + ls
);
129 out
.write("printtime=" + String
.valueOf(m_nTime
[ Print
]) + ls
);
130 out
.write("officestarttime=" + String
.valueOf(m_nTime
[ OfficeStart
]) + ls
);
131 out
.write("storeaspdftime=" + String
.valueOf(m_nTime
[ StoreAsPDF
]) + ls
);
134 public static double stringToDouble(String _sStr
)
139 nValue
= Double
.parseDouble( _sStr
);
141 catch (NumberFormatException e
)
143 GlobalLogWriter
.get().println("Can't convert string to double " + _sStr
);
148 public static long secondsToMilliSeconds(double _nSeconds
)
150 return (long)(_nSeconds
* 1000.0);
154 Helper function, which read some values from a given file
156 sample of wordinfofile
157 name=c:\doc-pool\wntmsci\samples\msoffice\word\LineSpacing.doc
159 WordStartTime=0.340490102767944
160 WordLoadTime=0.650935888290405
161 WordPrintTime=0.580835103988647
163 public void readWordValuesFromFile(String sFilename
)
165 File aFile
= new File(sFilename
);
166 if (! aFile
.exists())
168 GlobalLogWriter
.get().println("couldn't find file " + sFilename
);
172 RandomAccessFile aRandomAccessFile
= null;
175 aRandomAccessFile
= new RandomAccessFile(aFile
,"r");
177 while (sLine
!= null)
179 sLine
= aRandomAccessFile
.readLine();
180 if ( (sLine
!= null) &&
181 (! (sLine
.length() < 2) ) &&
182 (! sLine
.startsWith("#")))
184 if (sLine
.startsWith("WordStartTime="))
186 String sTime
= sLine
.substring(14);
187 m_nTime
[OfficeStart
] = secondsToMilliSeconds(stringToDouble(sTime
));
189 else if (sLine
.startsWith("WordLoadTime="))
191 String sTime
= sLine
.substring(13);
192 m_nTime
[Load
] = secondsToMilliSeconds(stringToDouble(sTime
));
194 else if (sLine
.startsWith("WordPrintTime="))
196 String sTime
= sLine
.substring(14);
197 m_nTime
[Print
] = secondsToMilliSeconds(stringToDouble(sTime
));
199 else if (sLine
.startsWith("WordVersion="))
201 String sMSOfficeVersion
= sLine
.substring(12);
202 m_sMSOfficeVersion
= "Word:" + sMSOfficeVersion
;
204 else if (sLine
.startsWith("ExcelVersion="))
206 String sMSOfficeVersion
= sLine
.substring(13);
207 m_sMSOfficeVersion
= "Excel:" + sMSOfficeVersion
;
209 else if (sLine
.startsWith("PowerPointVersion="))
211 String sMSOfficeVersion
= sLine
.substring(18);
212 m_sMSOfficeVersion
= "PowerPoint:" + sMSOfficeVersion
;
217 catch (java
.io
.FileNotFoundException fne
)
219 GlobalLogWriter
.get().println("couldn't open file " + sFilename
);
220 GlobalLogWriter
.get().println("Message: " + fne
.getMessage());
222 catch (java
.io
.IOException ie
)
224 GlobalLogWriter
.get().println("Exception while reading file " + sFilename
);
225 GlobalLogWriter
.get().println("Message: " + ie
.getMessage());
229 aRandomAccessFile
.close();
231 catch (java
.io
.IOException ie
)
233 GlobalLogWriter
.get().println("Couldn't close file " + sFilename
);
234 GlobalLogWriter
.get().println("Message: " + ie
.getMessage());
238 public static void main(String
[] args
) {
241 BorderRemover a = new BorderRemover();
244 a.createNewImageWithoutBorder(args[0], args[1]);
246 catch(java.io.IOException e)
248 System.out.println("Exception caught.");