001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 23.04.2005 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.jmeasurement.renderer;
018:
019: import java.io.IOException;
020: import java.text.DateFormat;
021:
022: import org.xml.sax.SAXException;
023:
024: import de.mcs.jmeasurement.DefaultMeasurePoint;
025: import de.mcs.jmeasurement.MeasureData;
026: import de.mcs.jmeasurement.MeasureFactory;
027: import de.mcs.jmeasurement.MeasurePoint;
028: import de.mcs.jmeasurement.MeasurementException;
029: import de.mcs.jmeasurement.MemoryHelper;
030: import de.mcs.jmeasurement.SnapShot;
031: import de.mcs.utils.Files;
032: import de.mcs.utils.StringUtils;
033:
034: /**
035: * The default text renderer is a renderer which will render the data into a
036: * normal text based format. Every line represent one measurepoint. The data
037: * will be seperated with <TAB> or <SPACE> character. All fields
038: * will be extactly <code>fieldSize</code> characters long. There is the
039: * possibility to determine the fieldsize from the names of the data values, or
040: * you can force the fieldsizes.
041: *
042: * @author w.klaas
043: */
044: public class DefaultTextRenderer implements MeasureDataRenderer,
045: MeasureDataRendererColumnHeader, MeasureDataRendererSnapshot {
046:
047: /** default line count. */
048: private static final int DEFAULT_LINE_COUNT = 0;
049:
050: /** default field size. */
051: private static final int DEFAULT_FIELD_SIZE = 20;
052:
053: /** default page size. */
054: private static final int DEFAULT_PAGE_SIZE = 80;
055:
056: /** default buffer size. */
057: private static final int BUFFER_SIZE = 1024;
058:
059: /** CRLF Character. */
060: private static final String CRLF = "\n";
061:
062: /** FF Character. */
063: private static final String FF = "\f";
064:
065: /** FF Character. */
066: private static final String TAB = "\t";
067:
068: /** SPCAE Character. */
069: private static final String SPACE = " ";
070:
071: /**
072: * page size in lines, after this count a FF will be added and a new header
073: * will be written.
074: */
075: private int pageSize = DEFAULT_PAGE_SIZE;
076:
077: /** internal counter of lines. */
078: private int lineCount = DEFAULT_LINE_COUNT;
079:
080: /** exact size of the field data. */
081: private int fieldsize = DEFAULT_FIELD_SIZE;
082:
083: /** holding the empty measurepoint for adding header to page begins. */
084: private MeasurePoint emptyPoint = null;
085:
086: /** array with all sizes of the fields. */
087: private int[] fieldSizes;
088:
089: /** using the tab character for field separation. */
090: private boolean useTab;
091:
092: /**
093: * Construct this renderer with the default fieldsize of 20. Fieldseparation
094: * will be set to TAB.
095: *
096: * @param aPageSize
097: * count of lines until a FF is made.
098: */
099: public DefaultTextRenderer(final int aPageSize) {
100: this (aPageSize, DEFAULT_FIELD_SIZE, true);
101: }
102:
103: /**
104: * Construct this renderer. If you set fieldSize to -1 then the fieldsize
105: * will be evaluated from the length of the field name.
106: *
107: * @param aPageSize
108: * count of lines until a FF is made.
109: * @param aFieldSize
110: * count of characters for a field
111: * @param aUseTab
112: * use tab character for field separation
113: */
114: public DefaultTextRenderer(final int aPageSize,
115: final int aFieldSize, final boolean aUseTab) {
116: this .pageSize = aPageSize;
117: this .fieldsize = aFieldSize;
118: this .fieldSizes = new int[0];
119: this .useTab = aUseTab;
120: }
121:
122: /**
123: * Construct this renderer. The fieldsize will be evaluated from the
124: * aFieldSizes array. Field separation will set to SPACE character.
125: *
126: * @param aPageSize
127: * count of lines until a FF is made.
128: * @param aFieldSizes
129: * array with values of fieldsizes
130: */
131: public DefaultTextRenderer(final int aPageSize,
132: final int[] aFieldSizes) {
133: this .pageSize = aPageSize;
134: this .fieldSizes = aFieldSizes.clone();
135: this .useTab = false;
136: }
137:
138: /**
139: * @see de.mcs.jmeasurement.renderer.MeasureDataRenderer#getDataAsString(MeasurePoint,
140: * String)
141: * @param point
142: * goes in.
143: * @param prefix
144: * not used in this renderer
145: * @return String the string representation of the values
146: */
147: public final String getDataAsString(final MeasurePoint point,
148: final String prefix) {
149: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
150: stringBuffer.append(checkLineCount());
151: MeasureData[] datas = point.getData();
152: // String[] values = new String[datas.length];
153: boolean first = true;
154: for (int j = 0; j < datas.length; j++) {
155: MeasureData data = datas[j];
156: if (!first) {
157: if (useTab) {
158: stringBuffer.append(TAB);
159: } else {
160: stringBuffer.append(SPACE);
161: }
162: } else {
163: first = false;
164: }
165: if (data.getName().equals(
166: DefaultMeasurePoint.DATA_KEY_EXCEPTION_LIST)) {
167: String[] exceptions = StringUtils.csvStringToArray(data
168: .getAsString(), ',', '"');
169: if (exceptions.length > 0) {
170: stringBuffer.append(formatFieldData(exceptions[0],
171: j));
172: }
173: } else {
174: stringBuffer.append(formatFieldData(data.getAsString(),
175: j));
176: }
177: }
178: stringBuffer.append(CRLF);
179: lineCount++;
180: return stringBuffer.toString();
181: }
182:
183: /**
184: * Getting the value formattet to the actual fieldsize.
185: *
186: * @param aValue
187: * value to format
188: * @param pos
189: * array index of this field
190: * @return String
191: */
192: private String formatFieldData(final String aValue, final int pos) {
193: StringBuffer value = new StringBuffer(aValue);
194: int size = fieldsize;
195: if (fieldSizes.length > 0) {
196: if (pos < fieldSizes.length) {
197: size = fieldSizes[pos];
198: }
199: }
200: while (value.length() < size) {
201: value.append(SPACE);
202: }
203: if (value.length() > size) {
204: value = new StringBuffer(value.substring(0, size));
205: }
206: return value.toString();
207: }
208:
209: /**
210: * checking if a new page has to begin.
211: *
212: * @return String <code>empty</code> or the header line
213: */
214: private String checkLineCount() {
215: if (lineCount >= pageSize) {
216: lineCount = 0;
217: return FF + getPageHeader();
218: } else {
219: return "";
220: }
221: }
222:
223: /**
224: * @see MeasureDataRendererColumnHeader#getColumnHeaderAsString(de.mcs.jmeasurement.MeasurePoint)
225: * @param point
226: * goes in.
227: * @return String the string representation of the column headers
228: */
229: public final String getColumnHeaderAsString(final MeasurePoint point) {
230: if (null == emptyPoint) {
231: emptyPoint = point;
232: if (fieldsize < 0) {
233: // getting the column sizes
234: MeasureData[] datas = emptyPoint.getData();
235: fieldSizes = new int[datas.length];
236: for (int j = 0; j < datas.length; j++) {
237: MeasureData data = datas[j];
238: fieldSizes[j] = data.getName().length();
239: }
240: }
241: }
242: return getPageHeader();
243: }
244:
245: /**
246: * writing the output.
247: *
248: * @return String header information
249: */
250: private String getPageHeader() {
251: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
252: MeasureData[] datas = emptyPoint.getData();
253: // String[] values = new String[datas.length];
254: boolean first = true;
255: for (int j = 0; j < datas.length; j++) {
256: MeasureData data = datas[j];
257: if (!first) {
258: if (useTab) {
259: stringBuffer.append(TAB);
260: } else {
261: stringBuffer.append(SPACE);
262: }
263: } else {
264: first = false;
265: }
266: stringBuffer.append(formatFieldData(data.getName(), j));
267: }
268: stringBuffer.append(CRLF);
269: lineCount++;
270: return stringBuffer.toString();
271: }
272:
273: /**
274: * @return Returns the fieldsize.
275: */
276: public final int getFieldsize() {
277: return fieldsize;
278: }
279:
280: /**
281: * @param aFieldsize
282: * The fieldsize to set.
283: */
284: public final void setFieldsize(final int aFieldsize) {
285: this .fieldsize = aFieldsize;
286: }
287:
288: /**
289: * @return Returns the pageSize.
290: */
291: public final int getPageSize() {
292: return pageSize;
293: }
294:
295: /**
296: * @param aPageSize
297: * The pageSize to set.
298: */
299: public final void setPageSize(final int aPageSize) {
300: this .pageSize = aPageSize;
301: }
302:
303: /**
304: * @return Returns the lineCount.
305: */
306: public final int getLineCount() {
307: return lineCount;
308: }
309:
310: /**
311: * @param snapShot
312: * the snapshot to start with.
313: * @return String
314: * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
315: */
316: public final String startSnapShot(final SnapShot snapShot) {
317: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
318: stringBuffer.append("Snapshot: ");
319: stringBuffer.append(snapShot.getName());
320: stringBuffer.append(CRLF);
321: stringBuffer.append("date: ");
322: stringBuffer.append(DateFormat.getDateTimeInstance().format(
323: snapShot.getDate()));
324: stringBuffer.append(CRLF);
325: stringBuffer.append("max memory: ");
326: stringBuffer.append(MemoryHelper.toGUIString(snapShot
327: .getMaxMemory()));
328: stringBuffer.append(", bytes: ");
329: stringBuffer.append(Long.toString(snapShot.getMaxMemory()));
330: stringBuffer.append(CRLF);
331: stringBuffer.append("free memory: ");
332: stringBuffer.append(MemoryHelper.toGUIString(snapShot
333: .getFreeMemory()));
334: stringBuffer.append(", bytes: ");
335: stringBuffer.append(Long.toString(snapShot.getFreeMemory()));
336: stringBuffer.append(CRLF);
337: stringBuffer.append("total memory: ");
338: stringBuffer.append(MemoryHelper.toGUIString(snapShot
339: .getTotalMemory()));
340: stringBuffer.append(", bytes: ");
341: stringBuffer.append(Long.toString(snapShot.getTotalMemory()));
342: stringBuffer.append(CRLF);
343: return stringBuffer.toString();
344: }
345:
346: /**
347: * @param snapShot
348: * the snapshot to start with.
349: * @return String
350: * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
351: */
352: public final String endSnapShot(final SnapShot snapShot) {
353: return "";
354: }
355:
356: /**
357: * generating a report from persistence data.
358: *
359: * @param args
360: * first argument is the file with the persistence data. Second
361: * must be the file the report should be exported to.
362: * @throws IOException
363: * if something goes wrong.
364: * @throws SAXException
365: * if something goes wrong.
366: * @throws MeasurementException
367: * if something goes wrong.
368: */
369:
370: public static void main(final String[] args) throws IOException,
371: SAXException, MeasurementException {
372: String infile = args[0];
373: String outfile = args[1];
374: MeasureFactory.loadFromXMLFile(infile, true);
375: String report = MeasureFactory
376: .getReport(new DefaultTextRenderer(80));
377: Files.writeStringToFile(outfile, report);
378: }
379: }
|