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 */
020package org.sonar.graph;
021
022import org.apache.commons.lang.ArrayUtils;
023
024import java.io.IOException;
025import java.io.LineNumberReader;
026import java.io.Reader;
027import java.io.StringReader;
028import java.util.Arrays;
029import java.util.HashSet;
030import java.util.Set;
031
032public final class DsmScanner {
033
034  private final LineNumberReader reader;
035  private static final String CELL_SEPARATOR = "|";
036  private static final char FEEDBACK_EDGE_FLAG = '*';
037  private final DirectedGraph<String, StringEdge> graph = DirectedGraph.createStringDirectedGraph();
038  private String[] vertices;
039  private Set<Edge> feedbackEdges = new HashSet<Edge>();
040
041  private DsmScanner(Reader reader) {
042    this.reader = new LineNumberReader(reader);
043  }
044
045  private Dsm<String> scan() {
046    try {
047      readColumnHeadersAndcreateDsmBuilder();
048      for (int i = 0; i < vertices.length; i++) {
049        readRow(i);
050      }
051    } catch (IOException e) {
052      throw new RuntimeException("Unable to read DSM content.", e); //NOSONAR
053    }
054    Dsm<String> dsm = new Dsm<String>(graph, graph.getVertices(), feedbackEdges);
055    DsmManualSorter.sort(dsm, Arrays.asList(vertices));
056    return dsm;
057  }
058
059  private void readRow(int i) throws IOException {
060    String[] tokens = splitLine(reader.readLine());
061    for (int j = 1; j < tokens.length - 1; j++) {
062      int toVertexIndex = j - 1;
063      int weight = extractWeight(tokens[j]);
064      if (i != toVertexIndex) {
065        StringEdge edge = new StringEdge(vertices[toVertexIndex], vertices[i], weight);
066        if (isFeedbackEdge(tokens[j])) {
067          feedbackEdges.add(edge);
068        }
069        graph.addEdge(edge);
070      }
071    }
072  }
073
074  private boolean isFeedbackEdge(String cellContent) {
075    return cellContent.indexOf(FEEDBACK_EDGE_FLAG) != -1;
076  }
077
078  private int extractWeight(String stringContent) {
079    try {
080      return Integer.valueOf(stringContent.replace(FEEDBACK_EDGE_FLAG, ' ').trim());
081    } catch (NumberFormatException e) {
082      return 0;
083    }
084  }
085
086  private void readColumnHeadersAndcreateDsmBuilder() throws IOException {
087    String[] tokens = splitLine(reader.readLine());
088    if (tokens != null) {
089      vertices = new String[tokens.length - 2];
090      System.arraycopy(tokens, 1, vertices, 0, tokens.length - 2);
091      graph.addVertices(Arrays.asList(vertices));
092    }
093  }
094
095  private String[] splitLine(String line) {
096    if (line == null) {
097      return ArrayUtils.EMPTY_STRING_ARRAY;
098    }
099    String[] tokens = line.split("\\" + CELL_SEPARATOR);
100    String[] result = new String[tokens.length];
101    for (int i = 0; i < tokens.length; i++) {
102      result[i] = tokens[i].trim();
103    }
104    return result;
105  }
106
107  public static Dsm<String> scan(String textDsm) {
108    StringReader reader = new StringReader(textDsm);
109    return scan(reader);
110  }
111
112  public static Dsm<String> scan(Reader dsmReader) {
113    DsmScanner scanner = new DsmScanner(dsmReader);
114    return scanner.scan();
115  }
116}