
org.n52.youngs.transform.impl.EntryMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of main Show documentation
Show all versions of main Show documentation
A mapping platform to load CSW records into Elasticsearch
The newest version!
/*
* Copyright 2015-2019 52°North Initiative for Geospatial Open Source
* Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.n52.youngs.transform.impl;
import com.google.common.base.Charsets;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import org.n52.youngs.transform.MappingEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author Matthes Rieke
*/
public class EntryMapper {
private static final Logger log = LoggerFactory.getLogger(EntryMapper.class);
private static final Map DEFAULT_OUTPUT_PROPERTIES = ImmutableMap.of(
OutputKeys.OMIT_XML_DECLARATION, "no",
OutputKeys.INDENT, "no",
OutputKeys.ENCODING, Charsets.UTF_8.name());
private final Optional stripspaceTransformer;
private final Transformer defaultTransformer;
public EntryMapper() {
this(Optional.empty(), null);
}
public EntryMapper(Optional stripspaceTransformer, Transformer defaultTransformer) {
this.stripspaceTransformer = stripspaceTransformer;
this.defaultTransformer = defaultTransformer;
}
public Optional mapEntry(MappingEntry entry, final Node node) {
log.trace("Applying field mapping '{}' to node: {}", entry.getFieldName(), node);
Optional result = Optional.empty();
// try nodeset first
try {
if (entry.hasCondition() && !assertCondition(node, entry.getCondition())) {
log.info("Condition '{}' not matched, skipping", entry.getCondition());
return Optional.empty();
}
Object nodesetResult = entry.getXPath().evaluate(node, XPathConstants.NODESET);
if (entry.getChildren() != null && !entry.getChildren().isEmpty()) {
result = mapChildren(nodesetResult, entry);
}
else {
result = Optional.ofNullable(handleEvaluationResult(nodesetResult, entry.getFieldName()));
}
if (result.isPresent()) {
log.trace("Found nodeset result: {}", result.get());
}
} catch (XPathExpressionException e) {
log.debug("Error selecting field {} as nodeset, could be XPath 2.0 expression... trying evaluation to string."
+ " Error was: {}", entry.getFieldName(), e.getMessage());
log.trace("Error selecting field {} as nodeset", entry.getFieldName(), e);
}
// try string eval if nodeset did not work
if (!result.isPresent()) {
try {
String stringResult = (String) entry.getXPath().evaluate(node, XPathConstants.STRING);
result = Optional.ofNullable(handleEvaluationResult(stringResult, entry.getFieldName()));
if (result.isPresent()) {
log.trace("Found string result: {}", result.get());
}
} catch (XPathExpressionException e) {
log.warn("Error selecting field {} as string: {}", entry.getFieldName(), e.getMessage());
log.trace("Error selecting field {} as string", entry.getFieldName(), e);
}
}
if (result.isPresent()) {
EvalResult er = result.get();
if (entry.hasReplacements()) {
er = handleReplacements(entry, er);
}
if (entry.hasSplit()) {
er = handleSplit(entry, er);
}
result = Optional.of(er);
}
return result;
}
private Optional mapChildren(Object nodesetResult, MappingEntry entry) {
Optional result;
// special case: children --> nested object
List nodes;
if (nodesetResult instanceof NodeList) {
NodeList tmp = (NodeList) nodesetResult;
nodes = new ArrayList<>(tmp.getLength());
for (int i = 0; i < tmp.getLength(); i++) {
nodes.add(tmp.item(i));
}
}
else if (nodesetResult instanceof Node) {
nodes = Collections.singletonList((Node) nodesetResult);
}
else {
log.warn("nodesetResult type {} not supported", nodesetResult.getClass());
nodes = Collections.emptyList();
}
//for each parent node, parse the children
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy