Example usage for org.apache.poi.ss.usermodel WorkbookFactory create

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory create.

Prototype

public static Workbook create(File file) throws IOException, EncryptedDocumentException 

Source Link

Document

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and be readable.

Usage

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testDictionaryDefaultMaxLength() throws Exception {

    GcpShapeConfig.init();/*from  w w  w.  j a  va2 s. com*/
    InputStream input = getClass().getClassLoader().getResourceAsStream("dictionary-max-length.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    loader.setFailOnErrors(true);
    loader.load(book, graph);

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();

    URI shapeId = uri("https://schema.pearson.com/shapes/PERSON_STG_Shape");

    ShapeManager shapeManager = loader.getShapeManager();

    URI predicate = uri("https://schema.pearson.com/ns/alias/FIRST_NAME");
    Shape shape = shapeManager.getShapeById(shapeId);

    PropertyConstraint p = shape.getPropertyConstraint(predicate);
    Integer maxLength = p.getMaxLength();
    assertTrue(maxLength != null);
    assertEquals(new Integer(256), maxLength);

    Integer minLength = p.getMinLength();
    assertTrue(minLength != null);
    assertEquals(new Integer(0), minLength);
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testSecurityClassificationByName() throws Exception {

    GcpShapeConfig.init();//  w  w  w. j a  v a  2 s .c om
    InputStream input = getClass().getClassLoader().getResourceAsStream("security-classification-by-name.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    loader.setFailOnErrors(true);
    loader.load(book, graph);

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();

    URI shapeId = uri("https://schema.pearson.com/shapes/PERSON_STG_Shape");

    ShapeManager shapeManager = loader.getShapeManager();

    URI predicate = uri("https://schema.pearson.com/ns/alias/FIRST_NAME");
    Shape shape = shapeManager.getShapeById(shapeId);

    PropertyConstraint p = shape.getPropertyConstraint(predicate);

    List<URI> list = p.getQualifiedSecurityClassification();
    URI dcl3 = uri("https://schema.pearson.com/ns/dcl/DCL3");
    URI pii = uri("https://schema.pearson.com/ns/dcl/PII");
    assertTrue(list.contains(dcl3));
    assertTrue(list.contains(pii));

}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testCurrentStateView() throws Exception {

    GcpShapeConfig.init();//from  ww  w . j  a  v  a  2s .  c  o m
    InputStream input = getClass().getClassLoader().getResourceAsStream("current-state-view.xlsx");
    File file = new File("src/test/resources/current-state-view.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    loader.setFailOnErrors(true);
    loader.load(book, graph, file);

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();

    URI shapeId = uri("http://example.com/shapes/PersonShape");

    ShapeManager shapeManager = loader.getShapeManager();
    Shape shape = shapeManager.getShapeById(shapeId);
    assertTrue(shape != null);

    GoogleBigQueryView datasource = shape.getShapeDataSource().stream()
            .filter(ds -> ds instanceof GoogleBigQueryView).map(ds -> (GoogleBigQueryView) ds).findFirst()
            .get();

    assertTrue(datasource != null);

}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testBigQueryTableIdRegex() throws Exception {

    GcpShapeConfig.init();//from  www .j  a  va2  s .  co  m
    InputStream input = getClass().getClassLoader().getResourceAsStream("bigQueryTableId-regex.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    loader.setFailOnErrors(true);
    File file = new File("src/test/resources/bigQueryTableId-regex.xlsx");
    loader.load(book, graph, file);

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();

    URI shapeId = uri("http://example.com/shapes/BqPersonShape");

    ShapeManager shapeManager = loader.getShapeManager();
    Shape shape = shapeManager.getShapeById(shapeId);
    assertTrue(shape != null);

    assertTrue(shape.getShapeDataSource() != null);

    List<DataSource> dsList = shape.getShapeDataSource().stream()
            .filter(ds -> ds.isA(Konig.GoogleBigQueryTable)).collect(Collectors.toList());

    assertEquals(1, dsList.size());

    DataSource ds = dsList.get(0);
    assertTrue(ds instanceof GoogleBigQueryTable);
    GoogleBigQueryTable table = (GoogleBigQueryTable) ds;
    String tableId = table.getTableReference().getTableId();
    assertTrue(tableId != null);
    assertEquals("BqPerson", tableId);
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testGoogleCloudSql() throws Exception {

    InputStream input = getClass().getClassLoader().getResourceAsStream("google-cloud-sql.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/google-cloud-sql.xlsx");
    loader.load(book, graph, file);/*from  w w w  .j a v  a2 s .c o  m*/

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();

    // TODO: Add assertions
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testBigQueryTransform() throws Exception {

    InputStream input = getClass().getClassLoader().getResourceAsStream("bigquery-transform.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/bigquery-transform.xlsx");
    loader.load(book, graph, file);//from   ww  w. j av a 2s.  co  m

    ShapeManager shapeManager = loader.getShapeManager();

    Shape shape = shapeManager.getShapeById(uri("http://example.com/shapes/OriginProductShape"));
    URI predicate = uri("http://example.com/ns/alias/PRD_PRICE");
    PropertyConstraint p = shape.getPropertyConstraint(predicate);
    assertTrue(p != null);
    Path path = p.getEquivalentPath();
    assertEquals("/schema:offers[schema:priceCurrency \"USD\"]/schema:price", path.toSimpleString());
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testCustomTableName() throws Exception {

    InputStream input = getClass().getClassLoader().getResourceAsStream("custom-tablename.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/custom-tablename.xlsx");
    loader.load(book, graph, file);/*from   w w w .  j  a  va  2 s  .  c o  m*/

    StringWriter writer = new StringWriter();
    RdfUtil.prettyPrintTurtle(graph, writer);

    writer.close();
    URI shapeId = uri("https://schema.pearson.com/shapes/OriginSales_TeamShape");

    ShapeManager s = loader.getShapeManager();

    Shape shape = s.getShapeById(shapeId);
    assertTrue(shape != null);
    List<DataSource> list = shape.getShapeDataSource();
    assertEquals(2, list.size());

    DataSource ds = list.get(0);
    // CASE 1 : Custom Table Name from the Datasource Function Parameter
    assertEquals("http://www.konig.io/ns/aws/host/${awsAuroraHost}/databases/schema1/tables/P6_Sales_Team",
            ds.getId().stringValue());

    URI originCompanySubTypeShapeId = uri("https://schema.pearson.com/shapes/OriginCompanySubTypeShape");

    Shape originCompanySubTypeShape = s.getShapeById(originCompanySubTypeShapeId);
    assertTrue(originCompanySubTypeShape != null);
    List<DataSource> originCompanySubTypeList = originCompanySubTypeShape.getShapeDataSource();
    assertEquals(2, originCompanySubTypeList.size());

    // CASE 2 : Custom Table Name from the global settings
    DataSource ds1 = originCompanySubTypeList.get(0);
    assertEquals("http://www.konig.io/ns/aws/host/${awsAuroraHost}/databases/schema1/tables/CompanySubType",
            ds1.getId().stringValue());

    URI creativeWorkShapeShapeId = uri("https://schema.pearson.com/shapes/CreativeWorkShape");
    Shape creativeWorkShape = s.getShapeById(creativeWorkShapeShapeId);
    List<DataSource> dsList = creativeWorkShape.getShapeDataSource();

    // CASE 3 : Custom Table Name from the default settings.properties
    DataSource ds2 = dsList.get(0);
    assertEquals(
            "https://www.googleapis.com/bigquery/v2/projects/${gcpProjectId}/datasets/schema/tables/CreativeWork",
            ds2.getId().stringValue());

}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testInvalidOntologyNamespace() throws Exception {

    InputStream input = getClass().getClassLoader().getResourceAsStream("invalid-ontology-namespace.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    Throwable error = null;//from  www .  ja  v a 2 s .c  o m
    try {
        File file = new File("src/test/resources/invalid-ontology-namespace.xlsx");
        loader.load(book, graph, file);
    } catch (Throwable e) {
        error = e;
    }
    assertTrue(error != null);
    assertTrue(error.getMessage().contains("Namespace must end with '/' or '#' but found: http://schema.org"));
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testGoogleCloudSqlTable() throws Exception {

    InputStream input = getClass().getClassLoader().getResourceAsStream("google-cloud-sql-table.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/google-cloud-sql-table.xlsx");
    loader.load(book, graph, file);//  w  w  w  . j ava  2  s  .  c o m
    input.close();

    URI shapeId = uri("http://example.com/shapes/PersonShape");

    ShapeManager shapeManager = loader.getShapeManager();

    Shape shape = shapeManager.getShapeById(shapeId);
    assertTrue(shape != null);
    List<DataSource> list = shape.getShapeDataSource();
    assertEquals(1, list.size());

    DataSource ds = list.get(0);
    assertEquals(
            "https://www.googleapis.com/sql/v1beta4/projects/${gcpProjectId}/instances/schema/databases/schema/tables/PersonShape",
            ds.getId().stringValue());

    assertTrue(ds.isA(Konig.GoogleCloudSqlTable));
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testDatasourceParamsGoogleBucket() throws Exception {
    GcpShapeConfig.init();//  w w w  . j a v  a2 s  . com
    InputStream input = new FileInputStream(new File("src/test/resources/test-datasource-params-bucket.xlsx"));
    Workbook book = WorkbookFactory.create(input);

    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/test-datasource-params-bucket.xlsx");
    loader.load(book, graph, file);
    input.close();

    URI shapeId = uri("http://example.com/shapes/ProductShape");

    ShapeManager shapeManager = loader.getShapeManager();
    Shape shape = shapeManager.getShapeById(shapeId);
    assertTrue(shape != null);

    List<String> eventTypes = shape.getShapeDataSource().stream()
            .filter(e -> e instanceof GoogleCloudStorageBucket).map(e -> (GoogleCloudStorageBucket) e)
            .findFirst().get().getNotificationInfo().get(0).getEventTypes();

    assertEquals(3, eventTypes.size());
    assertEquals("OBJECT_METADATA_UPDATE", eventTypes.get(0));
}