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:
021: import org.xml.sax.SAXException;
022:
023: import de.mcs.jmeasurement.DefaultMeasurePoint;
024: import de.mcs.jmeasurement.MeasureData;
025: import de.mcs.jmeasurement.MeasureFactory;
026: import de.mcs.jmeasurement.MeasurePoint;
027: import de.mcs.jmeasurement.MeasurementException;
028: import de.mcs.jmeasurement.MemoryHelper;
029: import de.mcs.jmeasurement.SnapShot;
030: import de.mcs.utils.Files;
031: import de.mcs.utils.StringUtils;
032:
033: /**
034: * This class is used to render the output of the MaesurementFactory into a
035: * simple html-based report. Header, Footer and page oriented information are
036: * used for creating the html side. The <code>MeasureDataRenderer</code>,
037: * <code>MeasureDataRendererColumnHeader</code> and the
038: * <code>MeasureDataRendererPage</code> interfaces are implemented.
039: *
040: * @author w.klaas
041: */
042: public class DefaultHTMLRenderer implements MeasureDataRenderer,
043: MeasureDataRendererColumnHeader, MeasureDataRendererPage,
044: MeasureDataRendererSnapshot {
045:
046: /** the CR LF characters. */
047: private static final String CRLF = "\r\n";
048:
049: /** default buffer size. */
050: private static final int BUFFER_SIZE = 1024;
051:
052: /** line counter to do some pretty background coloring in the table. */
053: private int lineCount;
054:
055: /**
056: * This methode will generate the string representation of the desired
057: * renderer for one measure point.
058: *
059: * @param point
060: * goes in.
061: * @param jumpPrefix
062: * the prefix needed for jumping in the html file.
063: * @return String the string representation of the values
064: * @see de.mcs.jmeasurement.renderer.MeasureDataRenderer#getDataAsString(MeasurePoint,
065: * String)
066: */
067: public final String getDataAsString(final MeasurePoint point,
068: final String jumpPrefix) {
069: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
070: MeasureData[] datas = point.getData();
071: // String[] values = new String[datas.length];
072: if ((lineCount % 2) > 0) {
073: stringBuffer.append("<tr bgcolor=\"#AACCCC\">");
074:
075: } else {
076: stringBuffer.append("<tr bgcolor=\"#DDEEEE\">");
077:
078: }
079: stringBuffer.append(CRLF);
080: String[] exceptions = new String[0];
081: for (int j = 0; j < datas.length; j++) {
082: MeasureData data = datas[j];
083: stringBuffer.append("<td>");
084: if (data.getName().equals(
085: DefaultMeasurePoint.DATA_KEY_EXCEPTION_LIST)) {
086: exceptions = StringUtils.csvStringToArray(data
087: .getAsString(), ',', '"');
088: if (exceptions.length > 0) {
089: stringBuffer.append("<a href=\"#e");
090: stringBuffer.append(jumpPrefix);
091: stringBuffer.append("lc");
092: stringBuffer.append(Integer.toString(lineCount));
093: stringBuffer
094: .append("\" onClick=\"getElementById('x");
095: stringBuffer.append(jumpPrefix);
096: stringBuffer.append("lc");
097: stringBuffer.append(Integer.toString(lineCount));
098: stringBuffer
099: .append("').style.display='block'\">View</a>");
100: } else {
101: stringBuffer.append(" ");
102: }
103: } else {
104: String string = data.getAsString();
105: if ((string == null) || string.equals("")) {
106: string = " ";
107: }
108: stringBuffer.append(string);
109: }
110: stringBuffer.append("</td>");
111: stringBuffer.append(CRLF);
112: }
113: stringBuffer.append("</tr>");
114: stringBuffer.append(CRLF);
115: if (exceptions.length > 0) {
116: stringBuffer.append("<tr bgcolor=\"#FFDDDD\" id=\"x");
117: stringBuffer.append(jumpPrefix);
118: stringBuffer.append("lc");
119: stringBuffer.append(Integer.toString(lineCount));
120: stringBuffer.append("\" style=\"display:none;\">\r\n");
121: stringBuffer.append("<td width=\"50%\" colspan=\"");
122: stringBuffer.append(Integer.toString(datas.length));
123: stringBuffer.append("\">");
124: stringBuffer.append("<a name=\"e");
125: stringBuffer.append(jumpPrefix);
126: stringBuffer.append("lc");
127: stringBuffer.append(Integer.toString(lineCount));
128: stringBuffer.append("\"/>");
129: stringBuffer.append("<pre>");
130: stringBuffer.append("Stacktraces: \r\n");
131: for (int i = 0; i < exceptions.length; i++) {
132: String string = exceptions[i];
133: stringBuffer.append("Exception " + Integer.toString(i));
134: stringBuffer.append(CRLF);
135: stringBuffer.append(string);
136: stringBuffer.append(CRLF);
137: }
138: stringBuffer.append("</pre>");
139: stringBuffer.append(CRLF);
140: stringBuffer.append("</td>");
141: stringBuffer.append(CRLF);
142: stringBuffer.append("</tr>");
143: stringBuffer.append(CRLF);
144: }
145: lineCount++;
146: return stringBuffer.toString();
147: }
148:
149: /**
150: * This methode will generate the string representation of the header of the
151: * desired renderer for measure points.
152: *
153: * @param point
154: * goes in.
155: * @return String the string representation of the column headers
156: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererColumnHeader#getColumnHeaderAsString(MeasurePoint)
157: */
158: public final String getColumnHeaderAsString(final MeasurePoint point) {
159: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
160: MeasureData[] datas = point.getData();
161: // String[] values = new String[datas.length];
162: stringBuffer.append("<thead>");
163: stringBuffer.append(CRLF);
164: stringBuffer.append("<tr bgcolor=\"#88AAAA\">");
165: stringBuffer.append(CRLF);
166: for (int j = 0; j < datas.length; j++) {
167: MeasureData data = datas[j];
168: stringBuffer.append("<td>");
169: stringBuffer.append(data.getName());
170: stringBuffer.append("</td>");
171: stringBuffer.append(CRLF);
172: }
173: stringBuffer.append("</tr>");
174: stringBuffer.append(CRLF);
175: stringBuffer.append("</thead>");
176: stringBuffer.append(CRLF);
177: return stringBuffer.toString();
178: }
179:
180: /**
181: * @return getting the header for an report
182: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#getReportHeader()
183: */
184: public final String getReportHeader() {
185: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
186: stringBuffer.append("<html>");
187: stringBuffer.append(CRLF);
188: stringBuffer.append("<head>");
189: stringBuffer.append(CRLF);
190: stringBuffer
191: .append(
192: "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-15\">")
193: .append(CRLF);
194: stringBuffer.append("<title>measurement report for ");
195: stringBuffer.append(MeasureFactory.getApplicationName());
196: stringBuffer.append("</title>");
197: stringBuffer.append(CRLF);
198: stringBuffer.append("</head>");
199: stringBuffer.append(CRLF);
200: stringBuffer.append("<body>");
201: stringBuffer.append(CRLF);
202: stringBuffer.append("<h1> JMeasurement HTML Report for ");
203: stringBuffer.append(MeasureFactory.getApplicationName());
204: stringBuffer.append("</h1><br/>");
205: return stringBuffer.toString();
206: }
207:
208: /**
209: * @return getting the footer for an report
210: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#getReportFooter()
211: */
212: public final String getReportFooter() {
213: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
214: stringBuffer.append("</body>");
215: stringBuffer.append(CRLF);
216: stringBuffer.append("</html>");
217: return stringBuffer.toString();
218: }
219:
220: /**
221: * @return getting a header for an page
222: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#beginPage()
223: */
224: public final String beginPage() {
225: lineCount = 0;
226: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
227: stringBuffer
228: .append("<table width=\"100%\" border=1 cellpadding=5 cellspacing=0>");
229: stringBuffer.append(CRLF);
230: return stringBuffer.toString();
231: }
232:
233: /**
234: * @return getting a footer for an page
235: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#endPage()
236: */
237: public final String endPage() {
238: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
239: stringBuffer.append("</table>");
240: stringBuffer.append(CRLF);
241: return stringBuffer.toString();
242: }
243:
244: /**
245: * @param snapShot
246: * the snapshot to start with.
247: * @return String
248: * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
249: */
250: public final String startSnapShot(final SnapShot snapShot) {
251: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
252: stringBuffer.append("<h2>Snapshot: ");
253: stringBuffer.append(snapShot.getName());
254: stringBuffer.append("</h2>");
255: stringBuffer.append(CRLF);
256: stringBuffer.append("max memory: ");
257: stringBuffer.append(MemoryHelper.toGUIString(snapShot
258: .getMaxMemory()));
259: stringBuffer.append(", bytes: ");
260: stringBuffer.append(Long.toString(snapShot.getMaxMemory()));
261: stringBuffer.append("<br/>");
262: stringBuffer.append(CRLF);
263: stringBuffer.append("free memory: ");
264: stringBuffer.append(MemoryHelper.toGUIString(snapShot
265: .getFreeMemory()));
266: stringBuffer.append(", bytes: ");
267: stringBuffer.append(Long.toString(snapShot.getFreeMemory()));
268: stringBuffer.append("<br/>");
269: stringBuffer.append(CRLF);
270: stringBuffer.append("total memory: ");
271: stringBuffer.append(MemoryHelper.toGUIString(snapShot
272: .getTotalMemory()));
273: stringBuffer.append(", bytes: ");
274: stringBuffer.append(Long.toString(snapShot.getTotalMemory()));
275: stringBuffer.append("<br/>");
276: stringBuffer.append(CRLF);
277: return stringBuffer.toString();
278: }
279:
280: /**
281: * @param snapShot
282: * the snapshot to start with.
283: * @return String
284: * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
285: */
286: public final String endSnapShot(final SnapShot snapShot) {
287: return "";
288: }
289:
290: /**
291: * generating a report from persistence data.
292: *
293: * @param args
294: * first argument is the file with the persistence data. Second
295: * must be the file the report should be exported to.
296: * @throws IOException
297: * if something goes wrong.
298: * @throws SAXException
299: * if something goes wrong.
300: * @throws MeasurementException
301: * if something goes wrong.
302: */
303:
304: public static void main(final String[] args) throws IOException,
305: SAXException, MeasurementException {
306: String infile = args[0];
307: String outfile = args[1];
308: MeasureFactory.loadFromXMLFile(infile, true);
309: String report = MeasureFactory
310: .getReport(new DefaultHTMLRenderer());
311: Files.writeStringToFile(outfile, report);
312: }
313: }
|