001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.graph;
021    
022    import java.io.IOException;
023    import java.io.StringWriter;
024    import java.io.Writer;
025    
026    public final class DsmPrinter {
027    
028      private final Writer writer;
029      private final Dsm dsm;
030      private static final String CELL_SEPARATOR = "| ";
031      private static final String FEEDBACK_EDGE_FLAG = "*";
032      private final boolean displayColumnHeaders;
033    
034      private DsmPrinter(Writer writer, Dsm dsm, boolean displayColumnHeaders) {
035        this.writer = writer;
036        this.dsm = dsm;
037        this.displayColumnHeaders = displayColumnHeaders;
038      }
039    
040      private void print() {
041        try {
042          if (displayColumnHeaders) {
043            printColumnHeaders();
044          }
045          for (int y = 0; y < dsm.getDimension(); y++) {
046            printRow(y);
047          }
048          writer.flush();
049          
050        } catch (IOException e) {
051          throw new RuntimeException("Unable to print the desired DSM.", e); // NOSONAR
052        }
053      }
054    
055      public static String print(Dsm dsm, boolean displayColumnHeaders) {
056        StringWriter writer = new StringWriter();
057        print(writer, dsm, displayColumnHeaders);
058        return writer.toString();
059      }
060    
061      public static String print(Dsm dsm) {
062        return print(dsm, true);
063      }
064    
065      public static void print(Writer writer, Dsm dsm, boolean displayColumnHeaders) {
066        DsmPrinter printer = new DsmPrinter(writer, dsm, displayColumnHeaders);
067        printer.print();
068      }
069    
070      private void printRow(int y) throws IOException {
071        printRowHeader(y);
072        for (int x = 0; x < dsm.getDimension(); x++) {
073          printCell(y, x);
074        }
075        writer.append((char) Character.LINE_SEPARATOR);
076      }
077    
078      private void printCell(int y, int x) throws IOException {
079        if (dsm.getCell(x, y).getWeight() == 0) {
080          writer.append(" ");
081        } else {
082          writer.append("").append(String.valueOf(dsm.getCell(x, y).getWeight()));
083        }
084        if (dsm.getCell(x, y).isFeedbackEdge()) {
085          writer.append(FEEDBACK_EDGE_FLAG);
086        } else {
087          writer.append(' ');
088        }
089        writer.append(CELL_SEPARATOR);
090      }
091    
092      private void printRowHeader(int y) throws IOException {
093        writer.append(String.valueOf(dsm.getVertex(y))).append(" " + CELL_SEPARATOR);
094      }
095    
096      private void printColumnHeaders() throws IOException {
097        writer.append("  " + CELL_SEPARATOR);
098        for (int i = 0; i < dsm.getDimension(); i++) {
099          printRowHeader(i);
100        }
101        writer.append((char) Character.LINE_SEPARATOR);
102      }
103    
104    }