Search in sources :

Example 76 with Document

use of com.google.cloud.documentai.v1beta2.Document in project atlasmap by atlasmap.

the class CsvService method inspect.

/**
 * Inspect a CSV instance and return a Document object.
 * @param request request
 * @param format format
 * @param delimiter delimiter
 * @param firstRecordAsHeader first record as header
 * @param skipHeaderRecord skip header record
 * @param headers headers
 * @param commentMarker comment marker
 * @param escape escape
 * @param ignoreEmptyLines ignore empty lines
 * @param ignoreHeaderCase ignore header case
 * @param ignoreSurroundingSpaces ignore surrounding spaces
 * @param nullString null string
 * @param quote quote
 * @param allowDuplicateHeaderNames allow duplicate header names
 * @param allowMissingColumnNames allow missing column names
 * @return {@link CsvInspectionResponse}
 * @throws IOException unexpected error
 */
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path("/inspect")
@Operation(summary = "Inspect CSV", description = "Inspect a CSV instance and return a Document object")
@RequestBody(description = "Csv", content = @Content(mediaType = "text/csv", schema = @Schema(implementation = String.class)))
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = CsvInspectionResponse.class)), description = "Return a Document object"))
public Response inspect(InputStream request, @QueryParam("format") String format, @QueryParam("delimiter") String delimiter, @QueryParam("firstRecordAsHeader") Boolean firstRecordAsHeader, @QueryParam("skipRecordHeader") Boolean skipHeaderRecord, @QueryParam("headers") String headers, @QueryParam("commentMarker") String commentMarker, @QueryParam("escape") String escape, @QueryParam("ignoreEmptyLines") Boolean ignoreEmptyLines, @QueryParam("ignoreHeaderCase") Boolean ignoreHeaderCase, @QueryParam("ignoreSurroundingSpaces") Boolean ignoreSurroundingSpaces, @QueryParam("nullString") String nullString, @QueryParam("quote") String quote, @QueryParam("allowDuplicateHeaderNames") Boolean allowDuplicateHeaderNames, @QueryParam("allowMissingColumnNames") Boolean allowMissingColumnNames) throws IOException {
    long startTime = System.currentTimeMillis();
    CsvInspectionResponse response = new CsvInspectionResponse();
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Options: delimiter={}, firstRecordAsHeader={}", delimiter, firstRecordAsHeader);
        }
        CsvConfig csvConfig = new CsvConfig(format);
        if (delimiter != null) {
            csvConfig.setDelimiter(delimiter.charAt(0));
        }
        csvConfig.setFirstRecordAsHeader(firstRecordAsHeader);
        csvConfig.setSkipHeaderRecord(skipHeaderRecord);
        csvConfig.setHeaders(headers);
        if (commentMarker != null) {
            csvConfig.setCommentMarker(commentMarker.charAt(0));
        }
        if (escape != null) {
            csvConfig.setEscape(escape.charAt(0));
        }
        csvConfig.setIgnoreEmptyLines(ignoreEmptyLines);
        csvConfig.setIgnoreHeaderCase(ignoreHeaderCase);
        csvConfig.setIgnoreSurroundingSpaces(ignoreSurroundingSpaces);
        csvConfig.setNullString(nullString);
        if (quote != null) {
            csvConfig.setQuote(quote.charAt(0));
        }
        csvConfig.setAllowDuplicateHeaderNames(allowDuplicateHeaderNames);
        csvConfig.setAllowMissingColumnNames(allowMissingColumnNames);
        CsvFieldReader csvFieldReader = new CsvFieldReader(csvConfig);
        csvFieldReader.setDocument(request);
        Document document = csvFieldReader.readSchema();
        response.setCsvDocument(document);
        request.close();
    } catch (Exception e) {
        LOG.error("Error inspecting CSV: " + e.getMessage(), e);
        response.setErrorMessage(e.getMessage());
    } finally {
        request.close();
        ;
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(("Response: {}" + new ObjectMapper().writeValueAsString(response)));
    }
    return Response.ok().entity(toJson(response)).build();
}
Also used : CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) CsvConfig(io.atlasmap.csv.core.CsvConfig) Document(io.atlasmap.v2.Document) CsvFieldReader(io.atlasmap.csv.core.CsvFieldReader) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 77 with Document

use of com.google.cloud.documentai.v1beta2.Document in project atlasmap by atlasmap.

the class AtlasUtilTest method testExcludeNotRequestedFieldsShouldIncludeAllIfIncludePathsNull.

@Test
public void testExcludeNotRequestedFieldsShouldIncludeAllIfIncludePathsNull() {
    Document document = new Document();
    Fields fields = new Fields();
    fields.getField().add(newField("/A", newField("/A/A", newField("/A/A/A"), newField("/A/A/B")), newField("/A/B", newField("/A/B/A", newField("/A/B/A/A")))));
    document.setFields(fields);
    AtlasUtil.excludeNotRequestedFields(document, null);
    assertEquals("/A{/A/A{/A/A/A{}, /A/A/B{}, }, /A/B{/A/B/A{/A/B/A/A{}, }, }, }, ", fields.getField().get(0).toString());
}
Also used : Fields(io.atlasmap.v2.Fields) Document(io.atlasmap.v2.Document) Test(org.junit.jupiter.api.Test)

Example 78 with Document

use of com.google.cloud.documentai.v1beta2.Document in project atlasmap by atlasmap.

the class AtlasUtilTest method testExcludeNotRequestedFieldsShouldIncludeAllIntermediateLevels.

@Test
public void testExcludeNotRequestedFieldsShouldIncludeAllIntermediateLevels() {
    Document document = new Document();
    Fields fields = new Fields();
    fields.getField().add(newField("/A", newField("/A/A", newField("/A/A/A"), newField("/A/A/B")), newField("/A/B", newField("/A/B/A", newField("/A/B/A/A")))));
    document.setFields(fields);
    AtlasUtil.excludeNotRequestedFields(document, Arrays.asList("/A/A/B"));
    assertEquals("/A{/A/A{/A/A/A{}, /A/A/B{}, }, /A/B{}, }, ", fields.getField().get(0).toString());
}
Also used : Fields(io.atlasmap.v2.Fields) Document(io.atlasmap.v2.Document) Test(org.junit.jupiter.api.Test)

Example 79 with Document

use of com.google.cloud.documentai.v1beta2.Document in project atlasmap by atlasmap.

the class AtlasUtilTest method testExcludeNotRequestedFieldsShouldIncludeTwoDifferentLevels.

@Test
public void testExcludeNotRequestedFieldsShouldIncludeTwoDifferentLevels() {
    Document document = new Document();
    Fields fields = new Fields();
    fields.getField().add(newField("/A", newField("/A/A", newField("/A/A/A"), newField("/A/A/B")), newField("/A/B", newField("/A/B/A", newField("/A/B/A/A")))));
    document.setFields(fields);
    AtlasUtil.excludeNotRequestedFields(document, Arrays.asList("/A/A", "/A/B"));
    assertEquals("/A{/A/A{/A/A/A{}, /A/A/B{}, }, /A/B{/A/B/A{}, }, }, ", fields.getField().get(0).toString());
}
Also used : Fields(io.atlasmap.v2.Fields) Document(io.atlasmap.v2.Document) Test(org.junit.jupiter.api.Test)

Example 80 with Document

use of com.google.cloud.documentai.v1beta2.Document in project atlasmap by atlasmap.

the class AtlasUtilTest method testExcludeNotRequestedFieldsShouldIncludeAllIfIncludePathsEmpty.

@Test
public void testExcludeNotRequestedFieldsShouldIncludeAllIfIncludePathsEmpty() {
    Document document = new Document();
    Fields fields = new Fields();
    fields.getField().add(newField("/A", newField("/A/A", newField("/A/A/A"), newField("/A/A/B")), newField("/A/B", newField("/A/B/A", newField("/A/B/A/A")))));
    document.setFields(fields);
    AtlasUtil.excludeNotRequestedFields(document, Collections.emptyList());
    assertEquals("/A{/A/A{/A/A/A{}, /A/A/B{}, }, /A/B{/A/B/A{/A/B/A/A{}, }, }, }, ", fields.getField().get(0).toString());
}
Also used : Fields(io.atlasmap.v2.Fields) Document(io.atlasmap.v2.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.jdom2.Document)559 Element (org.jdom2.Element)330 Test (org.junit.Test)170 SAXBuilder (org.jdom2.input.SAXBuilder)160 IOException (java.io.IOException)146 JDOMException (org.jdom2.JDOMException)94 File (java.io.File)84 XMLOutputter (org.jdom2.output.XMLOutputter)80 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)48 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)34 ArrayList (java.util.ArrayList)33 HashMap (java.util.HashMap)32 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)31 MCRContent (org.mycore.common.content.MCRContent)30 MCRObject (org.mycore.datamodel.metadata.MCRObject)30 MCRException (org.mycore.common.MCRException)28 InputStream (java.io.InputStream)27 StringReader (java.io.StringReader)26 Path (java.nio.file.Path)26 URL (java.net.URL)24