View Javadoc

1   package org.e2etrace.formatter;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  
22  import org.e2etrace.trace.ITraceSession;
23  import org.e2etrace.trace.ITraceStep;
24  
25  
26  /**
27   * Base class for trace formatters.
28   * <p>
29   *
30   * This class provides the logic for traversing a tree of trace steps. For each
31   * step the call-back <code>formatSingleStep</code> is invoked and the result
32   * written to the output stream. Additionally, <code>formatHeader</code> and
33   * <code>formatFooter</code> are invoked to generate formatter header and
34   * footer information.
35   * <p>
36   *
37   * @author Gunther Popp
38   *
39   */
40  public abstract class AbstractTraceFormatter implements ITraceFormatter {
41  
42    /**
43     * Default constructor.
44     * <p>
45     *
46     */
47    protected AbstractTraceFormatter() {
48  
49    }
50  
51    /**
52     * CALL-BACK: Generate formatted output for a single trace step.
53     * <p>
54     *
55     * @param step trace step
56     * @param level current call level (starting with 0)
57     * @return formatted output
58     */
59    protected abstract String formatSingleStep(ITraceStep step, int level);
60  
61    /** {@inheritDoc} */
62    public void format(ITraceSession session, Writer toWriter) throws IOException {
63      writeHeader(session, toWriter);
64      if(session.getRootStep() != null ) {
65        writeSteps(session.getRootStep(), toWriter, 0);
66      }
67      writeFooter(session, toWriter);
68  
69    }
70  
71    /**
72     * Returns the platform dependent newline character(s).<p>
73     *
74     * @return newline character(s)
75     */
76    protected String getNewLine() {
77      return System.getProperty("line.separator");
78    }
79  
80    /**
81     * CALL-BACK: Write the footer of the trace output.
82     * <p>
83     *
84     * @param session trace session
85     * @param toWriter output writer
86     * @throws IOException A problem occured sending the output to toWriter
87     */
88    protected abstract void writeFooter(ITraceSession session, Writer toWriter)
89        throws IOException;
90  
91    /**
92     * CALL-BACK: Write the header of the trace output.
93     * <p>
94     *
95     * @param session trace session
96     * @param toWriter output writer
97     * @throws IOException A problem occured sending the output to toWriter
98     */
99    protected abstract void writeHeader(ITraceSession session, Writer toWriter)
100       throws IOException;
101 
102   /**
103    * Writes output of a trace step using a given call level and forwards the
104    * call to all child steps.
105    * <p>
106    *
107    * @param step current step
108    * @param toWriter toWriter Writer to send the output to
109    * @param level current call level (starting with 0)
110    * @throws IOException A problem occured sending the output to toWriter
111    */
112   protected void writeSteps(ITraceStep step, Writer toWriter, int level)
113       throws IOException {
114     ITraceStep childSteps[];
115 
116     if (step != null) {
117       toWriter.write(formatSingleStep(step, level));
118 
119       childSteps = step.getChildren();
120       level = level + 1;
121       for (int i = 0; i < childSteps.length; i++) {
122         writeSteps(childSteps[i], toWriter, level);
123       }
124 
125     }
126   }
127 }