|
| 1 | +/* |
| 2 | + * Copyright © 2024 MarkLogic Corporation. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.spark.writer.embedding; |
| 5 | + |
| 6 | +import com.marklogic.client.document.DocumentWriteOperation; |
| 7 | +import com.marklogic.client.impl.DocumentWriteOperationImpl; |
| 8 | +import com.marklogic.client.impl.HandleAccessor; |
| 9 | +import com.marklogic.client.io.DOMHandle; |
| 10 | +import com.marklogic.client.io.marker.AbstractWriteHandle; |
| 11 | +import com.marklogic.spark.ConnectorException; |
| 12 | +import org.w3c.dom.Document; |
| 13 | +import org.w3c.dom.Element; |
| 14 | +import org.w3c.dom.Node; |
| 15 | +import org.w3c.dom.NodeList; |
| 16 | +import org.xml.sax.InputSource; |
| 17 | + |
| 18 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 19 | +import javax.xml.xpath.XPathConstants; |
| 20 | +import javax.xml.xpath.XPathExpression; |
| 21 | +import javax.xml.xpath.XPathExpressionException; |
| 22 | +import javax.xml.xpath.XPathFactory; |
| 23 | +import java.io.StringReader; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +public class DOMChunkSelector implements ChunkSelector { |
| 28 | + |
| 29 | + private final XPathFactory xpathFactory; |
| 30 | + private final XPathExpression chunksExpression; |
| 31 | + private final String chunkTextExpression; |
| 32 | + private final DocumentBuilderFactory documentBuilderFactory; |
| 33 | + |
| 34 | + public DOMChunkSelector(String chunksExpression, String chunkTextExpression) { |
| 35 | + this.xpathFactory = XPathFactory.newInstance(); |
| 36 | + try { |
| 37 | + this.chunksExpression = this.xpathFactory.newXPath().compile(chunksExpression); |
| 38 | + } catch (XPathExpressionException e) { |
| 39 | + throw new ConnectorException(String.format( |
| 40 | + "Unable to compile XPath expression for selecting chunks: %s; cause: %s", chunksExpression, e.getMessage()), e); |
| 41 | + } |
| 42 | + this.chunkTextExpression = chunkTextExpression; |
| 43 | + this.documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public DocumentAndChunks selectChunks(DocumentWriteOperation sourceDocument) { |
| 48 | + Document doc = extractDocument(sourceDocument); |
| 49 | + |
| 50 | + NodeList chunkNodes = selectChunkNodes(doc); |
| 51 | + if (chunkNodes.getLength() == 0) { |
| 52 | + return new DocumentAndChunks(sourceDocument, null); |
| 53 | + } |
| 54 | + |
| 55 | + List<Chunk> chunks = makeChunks(sourceDocument, doc, chunkNodes); |
| 56 | + DocumentWriteOperation docToWrite = new DocumentWriteOperationImpl(sourceDocument.getUri(), |
| 57 | + sourceDocument.getMetadata(), new DOMHandle(doc)); |
| 58 | + return new DocumentAndChunks(docToWrite, chunks); |
| 59 | + } |
| 60 | + |
| 61 | + private Document extractDocument(DocumentWriteOperation sourceDocument) { |
| 62 | + AbstractWriteHandle handle = sourceDocument.getContent(); |
| 63 | + if (handle instanceof DOMHandle) { |
| 64 | + return ((DOMHandle) handle).get(); |
| 65 | + } |
| 66 | + String xml = HandleAccessor.contentAsString(handle); |
| 67 | + try { |
| 68 | + return documentBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); |
| 69 | + } catch (Exception e) { |
| 70 | + throw new ConnectorException(String.format("Unable to parse XML for document with URI: %s; cause: %s", |
| 71 | + sourceDocument.getUri(), e.getMessage()), e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private NodeList selectChunkNodes(Document doc) { |
| 76 | + try { |
| 77 | + return (NodeList) chunksExpression.evaluate(doc, XPathConstants.NODESET); |
| 78 | + } catch (XPathExpressionException e) { |
| 79 | + throw new ConnectorException(String.format( |
| 80 | + "Unable to evaluate XPath expression for selecting chunks: %s; cause: %s", chunksExpression, e.getMessage()), e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private List<Chunk> makeChunks(DocumentWriteOperation sourceDocument, Document document, NodeList chunkNodes) { |
| 85 | + List<Chunk> chunks = new ArrayList<>(); |
| 86 | + for (int i = 0; i < chunkNodes.getLength(); i++) { |
| 87 | + Node node = chunkNodes.item(i); |
| 88 | + if (node.getNodeType() != Node.ELEMENT_NODE) { |
| 89 | + throw new ConnectorException(String.format("XPath expression for selecting chunks must only " + |
| 90 | + "select elements; XPath: %s; document URI: %s", chunksExpression, sourceDocument.getUri())); |
| 91 | + } |
| 92 | + chunks.add(new DOMChunk(sourceDocument.getUri(), document, (Element) node, chunkTextExpression, xpathFactory)); |
| 93 | + } |
| 94 | + return chunks; |
| 95 | + } |
| 96 | +} |
0 commit comments