Skip to content

Commit

Permalink
Merge pull request jgrapht#647 from d-michail/sax-characters-fix
Browse files Browse the repository at this point in the history
Fixes issue with multiple characters() method calls in some SAX parsers.
  • Loading branch information
jsichi authored Aug 3, 2018
2 parents 2d2f355 + 6a028e0 commit 5e3c38f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
12 changes: 10 additions & 2 deletions jgrapht-io/src/main/java/org/jgrapht/io/GraphMLImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,17 @@ public void characters(char ch[], int start, int length)
throws SAXException
{
if (insideDefault) {
currentKey.defaultValue = new String(ch, start, length);
if (currentKey.defaultValue != null) {
currentKey.defaultValue += new String(ch, start, length);
} else {
currentKey.defaultValue = new String(ch, start, length);
}
} else if (insideData) {
currentData.value = new String(ch, start, length);
if (currentData.value != null) {
currentData.value += new String(ch, start, length);
} else {
currentData.value = new String(ch, start, length);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private class GraphMLHandler
private E currentEdge;
private Key currentKey;
private String currentDataKey;
private String currentDataValue;
private StringBuilder currentDataValue;
private Map<String, Key> nodeValidKeys;
private Map<String, Key> edgeValidKeys;
private Map<String, Key> graphValidKeys;
Expand All @@ -329,7 +329,7 @@ public void startDocument()
currentEdge = null;
currentKey = null;
currentDataKey = null;
currentDataValue = null;
currentDataValue = new StringBuilder();
nodeValidKeys = new HashMap<>();
edgeValidKeys = new HashMap<>();
graphValidKeys = new HashMap<>();
Expand Down Expand Up @@ -438,7 +438,7 @@ public void endElement(String uri, String localName, String qName)
case DATA:
if (--insideData == 0) {
notifyData();
currentDataValue = null;
currentDataValue.setLength(0);
currentDataKey = null;
}
break;
Expand All @@ -452,7 +452,7 @@ public void characters(char ch[], int start, int length)
throws SAXException
{
if (insideData == 1) {
currentDataValue = new String(ch, start, length);
currentDataValue.append(ch, start, length);
}
}

Expand Down Expand Up @@ -488,7 +488,7 @@ private Optional<String> findAttribute(String localName, Attributes attributes)

private void notifyData()
{
if (currentDataKey == null || currentDataValue == null) {
if (currentDataKey == null || currentDataValue.length() == 0) {
return;
}

Expand All @@ -497,7 +497,7 @@ private void notifyData()
if (key != null) {
notifyVertex(
currentNode, key.attributeName,
new DefaultAttribute<>(currentDataValue, key.type));
new DefaultAttribute<>(currentDataValue.toString(), key.type));
}
}
if (currentEdge != null) {
Expand All @@ -508,21 +508,21 @@ private void notifyData()
*/
if (isWeighted && key.attributeName.equals(edgeWeightAttributeName)) {
try {
graph.setEdgeWeight(currentEdge, Double.parseDouble(currentDataValue));
graph.setEdgeWeight(currentEdge, Double.parseDouble(currentDataValue.toString()));
} catch (NumberFormatException e) {
// ignore
}
}
notifyEdge(
currentEdge, key.attributeName,
new DefaultAttribute<>(currentDataValue, key.type));
new DefaultAttribute<>(currentDataValue.toString(), key.type));
}
}
if (graph != null) {
Key key = graphValidKeys.get(currentDataKey);
if (key != null) {
notifyGraph(
key.attributeName, new DefaultAttribute<>(currentDataValue, key.type));
key.attributeName, new DefaultAttribute<>(currentDataValue.toString(), key.type));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.*;
import java.nio.charset.*;
Expand Down

0 comments on commit 5e3c38f

Please sign in to comment.