Skip to content

Commit

Permalink
Merge branch '0.9.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
eduramiba committed Nov 18, 2017
2 parents 2b9a7c8 + 56f48cd commit 5a789b1
Show file tree
Hide file tree
Showing 14 changed files with 7,707 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,19 @@ private Element createEdge(Document document, Edge e, Graph graph) throws Except
Element weightE = createEdgeWeight(document, e, graph);
edgeE.appendChild(weightE);

if (e.isDirected() && !graph.isDirected()) {
edgeE.setAttribute("type", "directed");
} else if (!e.isDirected() && graph.isDirected()) {
edgeE.setAttribute("type", "undirected");
boolean directedEdgeDefault = graph.isDirected() || graph.isMixed();
if (e.isDirected() && !directedEdgeDefault) {
edgeE.setAttribute("directed", "true");
} else if (!e.isDirected() && directedEdgeDefault) {
edgeE.setAttribute("directed", "false");
}

if (e.getTypeLabel() != null) {
//Edge labels not retained on graphml export https://github.com/gephi/gephi/issues/1516
String typeLabel = e.getTypeLabel().toString().trim();
if (!typeLabel.isEmpty()) {
edgeE.setAttribute("label", typeLabel);
}
}

//Attribute values
Expand Down
2 changes: 1 addition & 1 deletion modules/ExportPlugin/src/main/nbm/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Essential-Module: true
OpenIDE-Module-Localizing-Bundle: org/gephi/io/exporter/plugin/Bundle.properties
OpenIDE-Module-Specification-Version: ${gephi.modules.specification.version}
OpenIDE-Module-Specification-Version: 0.9.2.1
OpenIDE-Module-Display-Category: Plugin
OpenIDE-Module-Name: Export Plugin
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ public void addEdge(EdgeDraft edgeDraft) {
edgeMap.put(edgeDraft.getId(), index);
}

private void removeNode(NodeDraftImpl node) {
String id = node.getId();
if (!nodeMap.containsKey(id)) {
return;
}

int index = nodeMap.removeInt(id);
nodeList.set(index, null);
}

@Override
public void removeEdge(EdgeDraft edgeDraft) {
checkElementDraftImpl(edgeDraft);
Expand All @@ -343,6 +353,8 @@ public void removeEdge(EdgeDraft edgeDraft) {
if (!edgeMap.containsKey(id)) {
return;
}

boolean directed = edgeDraftImpl.getDirection() == EdgeDirection.DIRECTED;

if (edgeDraftImpl.getDirection() != null) {
//UnCounting
Expand All @@ -361,12 +373,12 @@ public void removeEdge(EdgeDraft edgeDraft) {
}

int edgeType = getEdgeType(edgeDraftImpl.getType());
long sourceTargetLong = getLongId(edgeDraftImpl);
long sourceTargetLong = getLongId(edgeDraftImpl.getSource(), edgeDraftImpl.getTarget(), directed);
ensureLongSetArraySize(edgeType);
Long2ObjectMap<int[]> edgeTypeSet = edgeTypeSets[edgeType];

//Get index
int index = edgeMap.remove(id);
int index = edgeMap.removeInt(id);

//Update edgeType set
int[] edges = edgeTypeSet.remove(sourceTargetLong);
Expand Down Expand Up @@ -727,14 +739,14 @@ public void closeLoader() {
if (directedEdgesCount > 0 && edgeDefault.equals(EdgeDirectionDefault.UNDIRECTED)) {
//Force undirected
for (EdgeDraftImpl edge : edgeList.toArray(new EdgeDraftImpl[0])) {
if (edge != null && edge.getDirection().equals(EdgeDirection.DIRECTED)) {
final boolean notAlreadyRemoved = edge != null
&& edgeMap.containsKey(edge.getId());

if (notAlreadyRemoved && edge.getDirection().equals(EdgeDirection.DIRECTED)) {
EdgeDraftImpl opposite = getOpposite(edge);
if (opposite != null) {
int oppositeIndex = edgeMap.getInt(opposite.getId());
mergeDirectedEdges(opposite, edge);

edgeMap.removeInt(opposite.getId());
edgeList.set(oppositeIndex, null);
removeEdge(opposite);
}
}
}
Expand All @@ -745,14 +757,12 @@ public void closeLoader() {
if (!allowAutoNode()) {
for (NodeDraftImpl node : nodeList) {
if (node != null && node.isCreatedAuto()) {
int index = nodeMap.removeInt(node.getId());
nodeList.set(index, null);
removeNode(node);
}
}
for (EdgeDraftImpl edge : edgeList) {
if (edge != null && (edge.getSource().isCreatedAuto() || edge.getTarget().isCreatedAuto())) {
int index = edgeMap.remove(edge.getId());
edgeList.set(index, null);
removeEdge(edge);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class ImporterGraphML implements FileImporter, LongTask {
private static final String EDGE_SOURCE = "source";
private static final String EDGE_TARGET = "target";
private static final String EDGE_DIRECTED = "directed";
private static final String EDGE_TYPE = "label";
private static final String ATTRIBUTE = "key";
private static final String ATTRIBUTE_ID = "id";
private static final String ATTRIBUTE_TITLE = "attr.name";
Expand Down Expand Up @@ -130,9 +131,6 @@ public ImporterGraphML() {
properties.addEdgePropertyAssociation(EdgeProperties.G, "g");
properties.addEdgePropertyAssociation(EdgeProperties.B, "b");
properties.addEdgePropertyAssociation(EdgeProperties.COLOR, "color");

nodePropertiesAttributes.put("d3", NodeProperties.LABEL);// Default node label used by yEd from yworks.com.
edgePropertiesAttributes.put("d7", EdgeProperties.LABEL);// Default edge label used by yEd from yworks.com.
}

@Override
Expand Down Expand Up @@ -197,7 +195,7 @@ private void readGraph(XMLStreamReader reader) throws Exception {
for (int i = 0; i < reader.getAttributeCount(); i++) {
String attName = reader.getAttributeName(i).getLocalPart();
if (GRAPH_DEFAULT_EDGETYPE.equalsIgnoreCase(attName)) {
defaultEdgeType = reader.getAttributeValue(i);
defaultEdgeType = reader.getAttributeValue(i).trim();
} else if (GRAPH_ID.equalsIgnoreCase(attName)) {
id = reader.getAttributeValue(i);
}
Expand Down Expand Up @@ -279,7 +277,7 @@ private void readNodeAttValue(XMLStreamReader reader, NodeDraft node) throws Exc
for (int i = 0; i < reader.getAttributeCount(); i++) {
String attName = reader.getAttributeName(i).getLocalPart();
if (ATTVALUE_FOR.equalsIgnoreCase(attName)) {
fore = reader.getAttributeValue(i);
fore = reader.getAttributeValue(i).trim();
}
}

Expand Down Expand Up @@ -374,6 +372,7 @@ private void readEdge(XMLStreamReader reader) throws Exception {
String source = "";
String target = "";
String directed = "";
String type = null;

//Attributes
for (int i = 0; i < reader.getAttributeCount(); i++) {
Expand All @@ -386,6 +385,8 @@ private void readEdge(XMLStreamReader reader) throws Exception {
id = reader.getAttributeValue(i);
} else if (EDGE_DIRECTED.equalsIgnoreCase(attName)) {
directed = reader.getAttributeValue(i);
} else if (EDGE_TYPE.equalsIgnoreCase(attName)) {
type = reader.getAttributeValue(i).trim();
}
}

Expand All @@ -401,6 +402,10 @@ private void readEdge(XMLStreamReader reader) throws Exception {
NodeDraft nodeTarget = container.getNode(target);
edge.setSource(nodeSource);
edge.setTarget(nodeTarget);
if (type != null && !type.isEmpty()) {
//Edge labels not retained on graphml export https://github.com/gephi/gephi/issues/1516
edge.setType(type);
}

//Type
if (!directed.isEmpty()) {
Expand All @@ -418,9 +423,9 @@ private void readEdge(XMLStreamReader reader) throws Exception {

boolean end = false;
while (reader.hasNext() && !end) {
int type = reader.next();
int elemType = reader.next();

switch (type) {
switch (elemType) {
case XMLStreamReader.START_ELEMENT:
if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
readEdgeAttValue(reader, edge);
Expand All @@ -444,7 +449,7 @@ private void readEdgeAttValue(XMLStreamReader reader, EdgeDraft edge) throws Exc
for (int i = 0; i < reader.getAttributeCount(); i++) {
String attName = reader.getAttributeName(i).getLocalPart();
if (ATTVALUE_FOR.equalsIgnoreCase(attName)) {
fore = reader.getAttributeValue(i);
fore = reader.getAttributeValue(i).trim();
}
}

Expand Down Expand Up @@ -533,13 +538,13 @@ private void readAttribute(XMLStreamReader reader) throws Exception {
for (int i = 0; i < reader.getAttributeCount(); i++) {
String attName = reader.getAttributeName(i).getLocalPart();
if (ATTRIBUTE_ID.equalsIgnoreCase(attName)) {
id = reader.getAttributeValue(i);
id = reader.getAttributeValue(i).trim();
} else if (ATTRIBUTE_TYPE.equalsIgnoreCase(attName)) {
type = reader.getAttributeValue(i);
type = reader.getAttributeValue(i).trim();
} else if (ATTRIBUTE_TITLE.equalsIgnoreCase(attName)) {
title = reader.getAttributeValue(i);
title = reader.getAttributeValue(i).trim();
} else if (ATTRIBUTE_FOR.equalsIgnoreCase(attName)) {
forStr = reader.getAttributeValue(i);
forStr = reader.getAttributeValue(i).trim();
}
}

Expand All @@ -565,7 +570,7 @@ private void readAttribute(XMLStreamReader reader) throws Exception {
property = true;
}
}

if (property) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.io.importer.api.Issue;
import org.gephi.io.importer.api.Report;
import org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser;
import org.gephi.utils.CharsetToolkit;
import org.openide.util.NbBundle;

/**
Expand Down Expand Up @@ -127,8 +128,24 @@ public static CSVParser configureCSVParser(File file, Character fieldSeparator,
csvFormat = csvFormat.withHeader((String[]) null).withSkipHeaderRecord(false);
}

boolean hasBOM = false;
try (FileInputStream is = new FileInputStream(file)) {
CharsetToolkit charsetToolkit = new CharsetToolkit(is);
hasBOM = charsetToolkit.hasUTF8Bom() || charsetToolkit.hasUTF16BEBom() || charsetToolkit.hasUTF16LEBom();
} catch (IOException e) {
//NOOP
}

FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader is = new InputStreamReader(fileInputStream, charset);
if (hasBOM) {
try {
is.read();
} catch (IOException e) {
// should never happen, as a file with no content
// but with a BOM has at least one char
}
}
return new CSVParser(is, csvFormat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.graph.api.types.IntervalSet;
import org.gephi.io.exporter.plugin.ExporterSpreadsheet;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDirectionDefault;
import org.gephi.io.importer.api.EdgeMergeStrategy;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.importer.plugin.file.spreadsheet.ImporterSpreadsheetCSV;
Expand Down Expand Up @@ -343,7 +344,7 @@ public void testEdgesTableWithTimeset_Interval() throws FileNotFoundException, I

@Test
public void testEdgesTableDynamicWeightsMerged() throws FileNotFoundException, IOException {
File file = FileUtil.archiveOrDirForURL(SpreadsheetNGTest.class.getResource("/org/gephi/io/importer/plugin/file/spreadsheet/edgest_table_dynamic_weights.csv"));
File file = FileUtil.archiveOrDirForURL(SpreadsheetNGTest.class.getResource("/org/gephi/io/importer/plugin/file/spreadsheet/edges_table_dynamic_weights.csv"));

ImporterSpreadsheetCSV importer = new ImporterSpreadsheetCSV();

Expand Down Expand Up @@ -491,6 +492,60 @@ public void testUTF8Chars() throws FileNotFoundException, IOException {

checkNodesSpreadsheet();
}

@Test
public void testUTF8CharsWithBOM() throws FileNotFoundException, IOException {
File file = FileUtil.archiveOrDirForURL(SpreadsheetNGTest.class.getResource("/org/gephi/io/importer/plugin/file/spreadsheet/test_utf8_chars_with_bom.csv"));

ImporterSpreadsheetCSV importer = new ImporterSpreadsheetCSV();

importer.setFile(file);

Assert.assertEquals(importer.getMode(), Mode.NODES_TABLE);
Assert.assertEquals(importer.getCharset().name(), "UTF-8");

Container container = importController.importFile(
file, importer
);
Assert.assertNotNull(container);

importController.process(container, new DefaultProcessor(), workspace);

checkNodesSpreadsheet();
}

@Test
public void testEdgesTableOppositeForceUndirected_Merged() throws FileNotFoundException, IOException {
File file = FileUtil.archiveOrDirForURL(SpreadsheetNGTest.class.getResource("/org/gephi/io/importer/plugin/file/spreadsheet/edges_table_opposite_force_undirected_merged.csv"));

Container container = importController.importFile(file);
Assert.assertNotNull(container);

//Force undirected:
container.getLoader().setEdgeDefault(EdgeDirectionDefault.UNDIRECTED);
container.getLoader().setEdgesMergeStrategy(EdgeMergeStrategy.SUM);

importController.process(container, new DefaultProcessor(), workspace);

checkEdgesSpreadsheet();
}

@Test
public void testEdgesTableOppositeForceUndirected_Issue1848() throws FileNotFoundException, IOException {
//https://github.com/gephi/gephi/issues/1848
File file = FileUtil.archiveOrDirForURL(SpreadsheetNGTest.class.getResource("/org/gephi/io/importer/plugin/file/spreadsheet/edges_table_opposite_force_undirected_issue_1848.csv"));

Container container = importController.importFile(file);
Assert.assertNotNull(container);

//Force undirected:
container.getLoader().setEdgeDefault(EdgeDirectionDefault.UNDIRECTED);
container.getLoader().setEdgesMergeStrategy(EdgeMergeStrategy.SUM);

importController.process(container, new DefaultProcessor(), workspace);

checkEdgesSpreadsheet();
}

private void checkEdgesSpreadsheet() throws IOException {
checkEdgesSpreadsheet(true);
Expand Down
Loading

0 comments on commit 5a789b1

Please sign in to comment.