*/
package org.sonar.server.source;
-import com.google.common.base.Strings;
-import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
-import org.sonar.core.source.SnapshotDataTypes;
-import org.sonar.core.source.db.SnapshotDataDto;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.Collection;
import java.util.List;
public class HtmlSourceDecorator implements ServerComponent {
- private static final String SINGLE_LINE_SYMBOLS = "single_line_symbols";
-
@CheckForNull
public String getDecoratedSourceAsHtml(@Nullable String sourceLine, @Nullable String highlighting, @Nullable String symbols) {
- Collection<SnapshotDataDto> snapshotDataEntries = Lists.newArrayList();
- if (highlighting != null) {
- SnapshotDataDto highlightingDto = new SnapshotDataDto();
- highlightingDto.setData(highlighting);
- highlightingDto.setDataType(SnapshotDataTypes.SYNTAX_HIGHLIGHTING);
- snapshotDataEntries.add(highlightingDto);
+ if (sourceLine == null) {
+ return null;
+ }
+ DecorationDataHolder decorationDataHolder = new DecorationDataHolder();
+ if (StringUtils.isNotBlank(highlighting)) {
+ decorationDataHolder.loadSyntaxHighlightingData(highlighting);
}
- if (symbols != null) {
- SnapshotDataDto symbolsDto = new SnapshotDataDto();
- symbolsDto.setData(symbols);
- symbolsDto.setDataType(SINGLE_LINE_SYMBOLS);
- snapshotDataEntries.add(symbolsDto);
+ if (StringUtils.isNotBlank(symbols)) {
+ decorationDataHolder.loadLineSymbolReferences(symbols);
}
- List<String> decoratedSource = decorate(sourceLine, snapshotDataEntries, 1, 1);
+ HtmlTextDecorator textDecorator = new HtmlTextDecorator();
+ List<String> decoratedSource = textDecorator.decorateTextWithHtml(sourceLine, decorationDataHolder, 1, 1);
if (decoratedSource == null) {
return null;
} else {
}
}
- @CheckForNull
- private List<String> decorate(@Nullable String snapshotSource, Collection<SnapshotDataDto> snapshotDataEntries, @Nullable Integer from, @Nullable Integer to) {
- if (snapshotSource != null) {
- DecorationDataHolder decorationDataHolder = new DecorationDataHolder();
- for (SnapshotDataDto snapshotDataEntry : snapshotDataEntries) {
- loadSnapshotData(decorationDataHolder, snapshotDataEntry);
- }
-
- HtmlTextDecorator textDecorator = new HtmlTextDecorator();
- return textDecorator.decorateTextWithHtml(snapshotSource, decorationDataHolder, from, to);
- }
- return null;
- }
-
- private void loadSnapshotData(DecorationDataHolder dataHolder, SnapshotDataDto entry) {
- if (!Strings.isNullOrEmpty(entry.getData())) {
- if (SnapshotDataTypes.SYNTAX_HIGHLIGHTING.equals(entry.getDataType())) {
- dataHolder.loadSyntaxHighlightingData(entry.getData());
- } else if (SnapshotDataTypes.SYMBOL_HIGHLIGHTING.equals(entry.getDataType())) {
- dataHolder.loadSymbolReferences(entry.getData());
- } else if (SINGLE_LINE_SYMBOLS.equals(entry.getDataType())) {
- dataHolder.loadLineSymbolReferences(entry.getData());
- }
- }
- }
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.index;
-
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.source.db.SnapshotDataDao;
-import org.sonar.core.source.db.SnapshotDataDto;
-
-import java.util.Map;
-
-public class ComponentDataPersister implements ScanPersister {
- private final ComponentDataCache data;
- private final SnapshotCache snapshots;
- private final SnapshotDataDao dao;
- private final MyBatis mybatis;
-
- public ComponentDataPersister(ComponentDataCache data, SnapshotCache snapshots,
- SnapshotDataDao dao, MyBatis mybatis) {
- this.data = data;
- this.snapshots = snapshots;
- this.dao = dao;
- this.mybatis = mybatis;
- }
-
- @Override
- public void persist() {
- DbSession session = mybatis.openSession(true);
- try {
- for (Map.Entry<String, Snapshot> componentEntry : snapshots.snapshots()) {
- String componentKey = componentEntry.getKey();
- Snapshot snapshot = componentEntry.getValue();
- for (Cache.Entry<Data> dataEntry : data.entries(componentKey)) {
- Data value = dataEntry.value();
- if (value != null) {
- SnapshotDataDto dto = new SnapshotDataDto();
- dto.setSnapshotId(snapshot.getId());
- dto.setResourceId(snapshot.getResourceId());
- dto.setDataType(dataEntry.key()[1].toString());
- dto.setData(value.writeString());
- dao.insert(session, dto);
- }
- }
- }
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.index;
+
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.source.SnapshotDataTypes;
+import org.sonar.core.source.db.SnapshotDataDao;
+import org.sonar.core.source.db.SnapshotDataDto;
+
+import java.util.Map;
+
+/**
+ * Store file hashes in snapshot_data for reuse in next analysis to know if a file is modified
+ */
+public class FileHashesPersister implements ScanPersister {
+ private final ComponentDataCache data;
+ private final SnapshotCache snapshots;
+ private final SnapshotDataDao dao;
+ private final MyBatis mybatis;
+
+ public FileHashesPersister(ComponentDataCache data, SnapshotCache snapshots,
+ SnapshotDataDao dao, MyBatis mybatis) {
+ this.data = data;
+ this.snapshots = snapshots;
+ this.dao = dao;
+ this.mybatis = mybatis;
+ }
+
+ @Override
+ public void persist() {
+ DbSession session = mybatis.openSession(true);
+ try {
+ for (Map.Entry<String, Snapshot> componentEntry : snapshots.snapshots()) {
+ String componentKey = componentEntry.getKey();
+ Snapshot snapshot = componentEntry.getValue();
+ String fileHashesdata = data.getStringData(componentKey, SnapshotDataTypes.FILE_HASHES);
+ if (fileHashesdata != null) {
+ SnapshotDataDto dto = new SnapshotDataDto();
+ dto.setSnapshotId(snapshot.getId());
+ dto.setResourceId(snapshot.getResourceId());
+ dto.setDataType(SnapshotDataTypes.FILE_HASHES);
+ dto.setData(fileHashesdata);
+ dao.insert(session, dto);
+ }
+ }
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+}
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.index.Caches;
import org.sonar.batch.index.ComponentDataCache;
-import org.sonar.batch.index.ComponentDataPersister;
+import org.sonar.batch.index.FileHashesPersister;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.DefaultPersistenceManager;
import org.sonar.batch.index.DefaultResourcePersister;
SnapshotCache.class,
ResourceCache.class,
ComponentDataCache.class,
- ComponentDataPersister.class,
+ FileHashesPersister.class,
DefaultUserFinder.class,
// file system
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.index;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.source.db.SnapshotDataDao;
-
-public class ComponentDataPersisterTest extends AbstractDaoTestCase {
-
- @ClassRule
- public static TemporaryFolder temp = new TemporaryFolder();
-
- SnapshotCache snapshots = new SnapshotCache();
- ComponentDataCache data;
- Caches caches;
-
- @Before
- public void start() throws Exception {
- caches = CachesTest.createCacheOnTemp(temp);
- caches.start();
- }
-
- @After
- public void stop() {
- caches.stop();
- }
-
- @Test
- public void should_persist_component_data() throws Exception {
- setupData("should_persist_component_data");
- Snapshot snapshot = new Snapshot();
- snapshot.setId(100);
- snapshot.setResourceId(200);
- snapshots.put("org/struts/Action.java", snapshot);
-
- data = new ComponentDataCache(caches);
- data.setStringData("org/struts/Action.java", "SYMBOL", "content of symbol");
- data.setStringData("org/struts/Action.java", "SYNTAX", "content of syntax");
- data.setStringData("org/struts/Other.java", "SYMBOL", "unregistered component, should not be persisted");
-
- SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis());
- ComponentDataPersister persister = new ComponentDataPersister(data, snapshots, dataDao, getMyBatis());
- persister.persist();
-
- checkTables("should_persist_component_data", new String[] {"id", "created_at", "updated_at"}, "snapshot_data");
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.index;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.source.SnapshotDataTypes;
+import org.sonar.core.source.db.SnapshotDataDao;
+
+public class FileHashesPersisterTest extends AbstractDaoTestCase {
+
+ @ClassRule
+ public static TemporaryFolder temp = new TemporaryFolder();
+
+ SnapshotCache snapshots = new SnapshotCache();
+ ComponentDataCache data;
+ Caches caches;
+
+ @Before
+ public void start() throws Exception {
+ caches = CachesTest.createCacheOnTemp(temp);
+ caches.start();
+ }
+
+ @After
+ public void stop() {
+ caches.stop();
+ }
+
+ @Test
+ public void should_persist_component_data() throws Exception {
+ setupData("should_persist_component_data");
+ Snapshot snapshot = new Snapshot();
+ snapshot.setId(100);
+ snapshot.setResourceId(200);
+ snapshots.put("myProject", snapshot);
+
+ data = new ComponentDataCache(caches);
+ data.setStringData("myProject", SnapshotDataTypes.FILE_HASHES, "org/struts/Action.java=123ABC");
+
+ SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis());
+ FileHashesPersister persister = new FileHashesPersister(data, snapshots, dataDao, getMyBatis());
+ persister.persist();
+
+ checkTables("should_persist_component_data", new String[] {"id", "created_at", "updated_at"}, "snapshot_data");
+ }
+}
+++ /dev/null
-<dataset>
- <snapshots id="100" project_id="200" islast="[true]"/>
- <snapshot_data id="1" snapshot_id="100" resource_id="200" snapshot_data="content of symbol" data_type="SYMBOL" created_at="[null]" updated_at="[null]"/>
- <snapshot_data id="2" snapshot_id="100" resource_id="200" snapshot_data="content of syntax" data_type="SYNTAX" created_at="[null]" updated_at="[null]" />
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <snapshots id="100" project_id="200" islast="[true]"/>
-</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <snapshots id="100" project_id="200" islast="[true]"/>
+ <snapshot_data id="1" snapshot_id="100" resource_id="200" snapshot_data="org/struts/Action.java=123ABC" data_type="file_hashes" created_at="[null]" updated_at="[null]"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <snapshots id="100" project_id="200" islast="[true]"/>
+</dataset>
\ No newline at end of file
String SYNTAX_HIGHLIGHTING = "highlight_syntax";
String SYMBOL_HIGHLIGHTING = "symbol";
- String TOKEN = "token";
/**
* Key-values [relative path, hash] of all files. Stored on modules.