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.plugins.squid.bridges;
021
022import org.sonar.api.resources.Resource;
023import org.sonar.graph.Dsm;
024import org.sonar.graph.DsmCell;
025import org.sonar.squid.api.SourceCode;
026
027public final class DsmSerializer {
028
029  private Dsm dsm;
030  private StringBuilder json;
031  private DependencyIndex dependencyIndex;
032  private ResourceIndex resourceIndex;
033
034  private DsmSerializer(Dsm<SourceCode> dsm, DependencyIndex dependencyIndex, ResourceIndex resourceIndex) {
035    this.dsm = dsm;
036    this.json = new StringBuilder();
037    this.dependencyIndex = dependencyIndex;
038    this.resourceIndex = resourceIndex;
039  }
040
041  private String serialize() {
042    json.append('[');
043    serializeRows();
044    json.append(']');
045    return json.toString();
046  }
047
048  private void serializeRows() {
049    for (int y = 0; y < dsm.getDimension(); y++) {
050      if (y > 0) {
051        json.append(',');
052      }
053      serializeRow(y);
054    }
055  }
056
057  private void serializeRow(int y) {
058    SourceCode squidResource = (SourceCode) dsm.getVertex(y);
059    Resource sonarResource = resourceIndex.get(squidResource);
060
061    json.append("{");
062    if (sonarResource != null) {
063      json.append("\"i\":");
064      json.append(sonarResource.getId());
065      json.append(",\"n\":\"");
066      json.append(sonarResource.getName());
067      json.append("\",\"q\":\"");
068      json.append(sonarResource.getQualifier());
069      json.append("\",\"v\":[");
070      for (int x = 0; x < dsm.getDimension(); x++) {
071        if (x > 0) {
072          json.append(',');
073        }
074        serializeCell(y, x);
075      }
076      json.append("]");
077    }
078    json.append("}");
079  }
080
081  private void serializeCell(int y, int x) {
082    DsmCell cell = dsm.getCell(x, y);
083    json.append('{');
084    if (cell.getEdge() != null && cell.getWeight() > 0) {
085      json.append("\"i\":");
086      json.append(dependencyIndex.get(cell.getEdge()).getId());
087      json.append(",\"w\":");
088      json.append(cell.getWeight());
089    }
090    json.append('}');
091  }
092
093  public static String serialize(Dsm<SourceCode> dsm, DependencyIndex dependencyIndex, ResourceIndex resourceIndex) {
094    return new DsmSerializer(dsm, dependencyIndex, resourceIndex).serialize();
095  }
096}