001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.LineNumberReader;
024    import java.io.Reader;
025    import java.io.StringReader;
026    import java.util.Arrays;
027    import java.util.HashSet;
028    import java.util.Set;
029    
030    public final class DsmScanner {
031    
032      private final LineNumberReader reader;
033      private static final String CELL_SEPARATOR = "|";
034      private static final char FEEDBACK_EDGE_FLAG = '*';
035      private final DirectedGraph<String, StringEdge> graph = DirectedGraph.createStringDirectedGraph();
036      private String[] vertices;
037      private Set<Edge> feedbackEdges = new HashSet<Edge>();
038    
039      private DsmScanner(Reader reader) {
040        this.reader = new LineNumberReader(reader);
041      }
042    
043      private Dsm<String> scan() {
044        try {
045          readColumnHeadersAndcreateDsmBuilder();
046          for (int i = 0; i < vertices.length; i++) {
047            readRow(i);
048          }
049        } catch (IOException e) {
050          throw new RuntimeException("Unable to read DSM content.", e); //NOSONAR
051        }
052        Dsm<String> dsm = new Dsm<String>(graph, graph.getVertices(), feedbackEdges);
053        DsmManualSorter.sort(dsm, Arrays.asList(vertices));
054        return dsm;
055      }
056    
057      private void readRow(int i) throws IOException {
058        String[] tokens = splitLine(reader.readLine());
059        if (tokens != null) {
060          for (int j = 1; j < tokens.length - 1; j++) {
061            int toVertexIndex = j - 1;
062            int weight = extractWeight(tokens[j]);
063            if (i != toVertexIndex) {
064              StringEdge edge = new StringEdge(vertices[toVertexIndex], vertices[i], weight);
065              if (isFeedbackEdge(tokens[j])) {
066                feedbackEdges.add(edge);
067              }
068              graph.addEdge(edge);
069            }
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        String[] tokens = line.split("\\" + CELL_SEPARATOR);
097        String[] result = new String[tokens.length];
098        for (int i = 0; i < tokens.length; i++) {
099          result[i] = tokens[i].trim();
100        }
101        return result;
102      }
103    
104      public static Dsm<String> scan(String textDsm) {
105        StringReader reader = new StringReader(textDsm);
106        return scan(reader);
107      }
108    
109      public static Dsm<String> scan(Reader dsmReader) {
110        DsmScanner scanner = new DsmScanner(dsmReader);
111        return scanner.scan();
112      }
113    }