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.MeasureData;
024: import de.mcs.jmeasurement.MeasureFactory;
025: import de.mcs.jmeasurement.MeasurePoint;
026: import de.mcs.jmeasurement.MeasurementException;
027: import de.mcs.utils.Files;
028: import de.mcs.utils.StringUtils;
029:
030: /**
031: * This class is used to render the output of the MaesurementFactory into a
032: * simple csv-based report. No header, footer and page oriented information are
033: * used. Only the <code>MeasureDataRenderer</code> and the
034: * <code>MeasureDataRendererColumnHeader</code> interfaces are implemented.
035: *
036: * @author w.klaas
037: */
038: public class DefaultCSVDataRenderer implements MeasureDataRenderer,
039: MeasureDataRendererColumnHeader {
040:
041: /** default buffer size. */
042: private static final int BUFFER_SIZE = 1024;
043:
044: /** separator for this csv report. */
045: private char localSeparator;
046:
047: /** delimiter for this csv report. */
048: private char localDelimiter;
049:
050: /**
051: * default constructor. Using <code><"></code> as field delimiter and
052: * <code><,></code> as field separator.
053: */
054: public DefaultCSVDataRenderer() {
055: this (',', '"');
056: }
057:
058: /**
059: * Constructor to define field separator and delimitter.
060: *
061: * @param separator
062: * the field separator, default is <,>
063: * @param delimiter
064: * the field delimiter, default is <">
065: */
066: public DefaultCSVDataRenderer(final char separator,
067: final char delimiter) {
068: localSeparator = separator;
069: localDelimiter = delimiter;
070: }
071:
072: /**
073: * This methode will generate the string representation of the header of the
074: * desired renderer for measure points.
075: *
076: * @param point
077: * goes in.
078: * @return String the string representation of the column headers
079: * @see de.mcs.jmeasurement.renderer.MeasureDataRendererColumnHeader#getColumnHeaderAsString(MeasurePoint)
080: */
081: public final String getColumnHeaderAsString(final MeasurePoint point) {
082: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
083: MeasureData[] datas = point.getData();
084: String[] values = new String[datas.length];
085: for (int j = 0; j < datas.length; j++) {
086: MeasureData data = datas[j];
087: values[j] = data.getName();
088: }
089: stringBuffer.append(StringUtils.arrayToCSVString(values,
090: localSeparator, localDelimiter));
091: stringBuffer.append("\r\n");
092: return stringBuffer.toString();
093: }
094:
095: /**
096: * This methode will generate the string representation of the desired
097: * renderer for one measure point.
098: *
099: * @param point
100: * goes in.
101: * @param prefix
102: * not used in this renderer
103: * @return String the string representation of the values
104: * @see de.mcs.jmeasurement.renderer.MeasureDataRenderer#getDataAsString(MeasurePoint,
105: * String)
106: */
107: public final String getDataAsString(final MeasurePoint point,
108: final String prefix) {
109: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
110: MeasureData[] datas = point.getData();
111: String[] values = new String[datas.length];
112: for (int j = 0; j < datas.length; j++) {
113: MeasureData data = datas[j];
114: values[j] = data.getAsString();
115: }
116: stringBuffer.append(StringUtils.arrayToCSVString(values,
117: localSeparator, localDelimiter));
118: stringBuffer.append("\r\n");
119: return stringBuffer.toString();
120: }
121:
122: /**
123: * generating a report from persistence data.
124: *
125: * @param args
126: * first argument is the file with the persistence data. Second
127: * must be the file the report should be exported to.
128: * @throws IOException
129: * if something goes wrong.
130: * @throws SAXException
131: * if something goes wrong.
132: * @throws MeasurementException
133: * if something goes wrong.
134: */
135:
136: public static void main(final String[] args) throws IOException,
137: SAXException, MeasurementException {
138: String infile = args[0];
139: String outfile = args[1];
140: MeasureFactory.loadFromXMLFile(infile, true);
141: String report = MeasureFactory
142: .getReport(new DefaultCSVDataRenderer());
143: Files.writeStringToFile(outfile, report);
144: }
145: }
|