Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation #165

Merged
merged 2 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
Expand Down Expand Up @@ -66,14 +71,8 @@ private void loadLibrary(String directory, StringBuilder styleSheetBuilder) {
String resourceName = directory + "/" + s.getFileName();
LOGGER.debug("Reading subComponent {}", resourceName);
SVGOMDocument doc = svgLoadDoc.read(resourceName);
Map<String, SVGOMDocument> mapSubDoc;
if (!svgDocuments.containsKey(componentType)) {
mapSubDoc = new TreeMap<>();
svgDocuments.put(componentType, mapSubDoc);
} else {
mapSubDoc = svgDocuments.get(componentType);
}
mapSubDoc.put(s.getName(), doc);
cleanEmptyTextNodes(doc, resourceName);
svgDocuments.computeIfAbsent(componentType, k -> new TreeMap<>()).put(s.getName(), doc);
});
components.put(componentType, c);
});
Expand All @@ -86,6 +85,22 @@ private void loadLibrary(String directory, StringBuilder styleSheetBuilder) {
}
}

private static void cleanEmptyTextNodes(Node parentNode, String resourceName) {
try {
// Find empty text nodes
NodeList nl = (NodeList) XPathFactory.newInstance().newXPath()
.evaluate("//text()[normalize-space(.)='']", parentNode, XPathConstants.NODESET);

// Remove the found nodes
for (int i = 0; i < nl.getLength(); ++i) {
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}
} catch (XPathExpressionException e) {
LOGGER.warn("Exception occurred while cleaning the subcomponent {} from empty text nodes", resourceName);
}
}

@Override
public Map<String, SVGOMDocument> getSvgDocument(String type) {
Objects.requireNonNull(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,8 @@ private void insertClonedSubcomponent(Element g, BiConsumer<Element, String> ele
for (int i = 0; i < subComponentChildren.getLength(); i++) {
org.w3c.dom.Node n = subComponentChildren.item(i).cloneNode(true);
if (n instanceof Element) {
elementAttributesSetter.accept((Element) n, subComponentName);
setAttributesAndInsertElement(g, elementAttributesSetter, subComponentName, (Element) n);
}
g.getOwnerDocument().adoptNode(n);
g.appendChild(n);
}
}

Expand Down
589 changes: 293 additions & 296 deletions single-line-diagram-core/src/test/resources/substation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading