Skip to content
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 @@ -190,12 +190,24 @@ protected List<Slot> extractMetadata(List<XPath> xPaths)
name = node.getDisplayName();
}
}
List<String> values = extractor.getValuesFromDoc(xpath.getValue());

// Check if this should be extracted as JSON (slot name ends with "_json")
List<String> values;
if (name != null && name.endsWith("_json")) {
// Extract as JSON
values = extractor.getValuesAsJsonFromDoc(xpath.getValue());
} else {
// Extract as regular text values
values = extractor.getValuesFromDoc(xpath.getValue());
}

if (values != null && (!values.isEmpty())) {
Slot slot = new Slot(name, values);
String unit = node.getAttributeValue("", Constants.UNIT);
if (unit != null) {
slot.setSlotType(unit);
if (node != null) {
String unit = node.getAttributeValue("", Constants.UNIT);
if (unit != null) {
slot.setSlotType(unit);
}
}
slots.add(slot);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gov.nasa.pds.harvest.search.util;

import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.transform.sax.SAXSource;
import javax.xml.xpath.XPathConstants;
Expand All @@ -16,12 +18,17 @@
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.xpath.XPathEvaluator;

import org.json.JSONObject;
import org.json.XML;
import org.xml.sax.InputSource;

/**
* Class to extract data from an XML file.
*/
public class XMLExtractor {
/** Logger instance */
private static final Logger log = Logger.getLogger(XMLExtractor.class.getName());

/** The DOM source. */
private DocumentInfo xml = null;

Expand Down Expand Up @@ -286,4 +293,84 @@ public List<String> getAttributeValuesFromItem(String expression, Object item)
}
return vals;
}

/**
* Gets the values of the given expression as JSON strings.
* Each matching node is converted to a JSON object and returned as a string.
*
* @param expression An XPath expression.
*
* @return A list of JSON strings, one for each matching node.
*
* @throws XPathExpressionException If the given expression was malformed.
*/
public List<String> getValuesAsJsonFromDoc(String expression)
throws XPathExpressionException {
return getValuesAsJsonFromItem(expression, xml);
}

/**
* Gets the values of the given expression as JSON strings.
* Each matching node is converted to a JSON object and returned as a string.
*
* @param expression An XPath expression.
* @param item The starting point from which to evaluate the XPath expression.
*
* @return A list of JSON strings, one for each matching node.
*
* @throws XPathExpressionException If the given expression was malformed.
*/
public List<String> getValuesAsJsonFromItem(String expression, Object item)
throws XPathExpressionException {
List<String> jsonStrings = new ArrayList<String>();
List<TinyElementImpl> nList = (List<TinyElementImpl>) xpath.evaluate(
expression, item, XPathConstants.NODESET);

if (nList != null) {
for (int i = 0, sz = nList.size(); i < sz; i++) {
TinyElementImpl node = nList.get(i);
try {
// Convert the node to an XML string
String xmlString = nodeToString(node);

// Convert XML to JSON using org.json library
JSONObject jsonObject = XML.toJSONObject(xmlString);

// Add the JSON string to the result list
jsonStrings.add(jsonObject.toString());
} catch (RuntimeException e) {
// Let RuntimeExceptions (programming errors) propagate
throw e;
} catch (Exception e) {
// If conversion fails, log and skip this node
log.warning("Failed to convert XML node to JSON: " + e.getMessage());
}
}
}
return jsonStrings;
}

/**
* Converts a TinyElementImpl node to an XML string.
*
* @param node The node to convert.
*
* @return An XML string representation of the node.
*
* @throws Exception If conversion fails.
*/
private String nodeToString(TinyElementImpl node) throws Exception {
// Use Saxon's built-in serialization
net.sf.saxon.s9api.Processor processor = new net.sf.saxon.s9api.Processor(false);
net.sf.saxon.s9api.Serializer serializer = processor.newSerializer();

StringWriter writer = new StringWriter();
serializer.setOutputWriter(writer);
serializer.setOutputProperty(net.sf.saxon.s9api.Serializer.Property.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty(net.sf.saxon.s9api.Serializer.Property.INDENT, "no");

serializer.serializeNode(new net.sf.saxon.s9api.XdmNode(node));

return writer.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,65 +75,18 @@
<field name="citation_editor_list" type="string">
<registryPath>citation_editor_list</registryPath>
</field>
<field name="citation_author_organization_name" type="string">
<registryPath>citation_author_organization_name</registryPath>
<!-- JSON fields for authors and editors to preserve nested structure -->
<field name="citation_author_person_json" type="string">
<registryPath>citation_author_person_json</registryPath>
</field>
<field name="citation_author_organization_rorid" type="string">
<registryPath>citation_author_organization_rorid</registryPath>
<field name="citation_author_organization_json" type="string">
<registryPath>citation_author_organization_json</registryPath>
</field>
<field name="citation_author_organization_contributor_type" type="string">
<registryPath>citation_author_organization_contributor_type</registryPath>
<field name="citation_editor_person_json" type="string">
<registryPath>citation_editor_person_json</registryPath>
</field>
<field name="citation_author_person_contributor_type" type="string">
<registryPath>citation_author_person_contributor_type</registryPath>
</field>
<field name="citation_author_person_display_full_name" type="string">
<registryPath>citation_author_person_display_full_name</registryPath>
</field>
<field name="citation_author_person_given_name" type="string">
<registryPath>citation_author_person_given_name</registryPath>
</field>
<field name="citation_author_person_family_name" type="string">
<registryPath>citation_author_person_family_name</registryPath>
</field>
<field name="citation_author_person_orcid" type="string">
<registryPath>citation_author_person_orcid</registryPath>
</field>
<field name="citation_author_person_affiliation_organization_name" type="string">
<registryPath>citation_author_person_affiliation_organization_name</registryPath>
</field>
<field name="citation_author_person_affiliation_organization_rorid" type="string">
<registryPath>citation_author_person_affiliation_organization_rorid</registryPath>
</field>
<field name="citation_editor_organization_name" type="string">
<registryPath>citation_editor_organization_name</registryPath>
</field>
<field name="citation_editor_organization_rorid" type="string">
<registryPath>citation_editor_organization_rorid</registryPath>
</field>
<field name="citation_editor_organization_contributor_type" type="string">
<registryPath>citation_editor_organization_contributor_type</registryPath>
</field>
<field name="citation_editor_person_contributor_type" type="string">
<registryPath>citation_editor_person_contributor_type</registryPath>
</field>
<field name="citation_editor_person_display_full_name" type="string">
<registryPath>citation_editor_person_display_full_name</registryPath>
</field>
<field name="citation_editor_person_given_name" type="string">
<registryPath>citation_editor_person_given_name</registryPath>
</field>
<field name="citation_editor_person_family_name" type="string">
<registryPath>citation_editor_person_family_name</registryPath>
</field>
<field name="citation_editor_person_orcid" type="string">
<registryPath>citation_editor_person_orcid</registryPath>
</field>
<field name="citation_editor_person_affiliation_organization_name" type="string">
<registryPath>citation_editor_person_affiliation_organization_name</registryPath>
</field>
<field name="citation_editor_person_affiliation_organization_rorid" type="string">
<registryPath>citation_editor_person_affiliation_organization_rorid</registryPath>
<field name="citation_editor_organization_json" type="string">
<registryPath>citation_editor_organization_json</registryPath>
</field>
<field name="citation_publication_year" type="string">
<registryPath>citation_publication_year</registryPath>
Expand Down
Loading