GridUtils { public static List<Grid> fromHtml( String html, String title ) throws Exception { if ( html == null || html.trim().isEmpty() ) { return null; } List<Grid> grids = new ArrayList<>(); Parser parser = Parser.createParser( html, "UTF-8" ); Node[] tables = parser.extractAllNodesThatMatch( new TagNameFilter( "table" ) ).toNodeArray(); for ( Node t : tables ) { Grid grid = new ListGrid(); grid.setTitle( title ); TableTag table = (TableTag) t; TableRow[] rows = table.getRows(); Integer firstColumnCount = null; for ( TableRow row : rows ) { if ( getColumnCount( row ) == 0 ) { log.warn( "Ignoring row with no columns" ); continue; } Node[] cells = row.getChildren().extractAllNodesThatMatch( HTML_ROW_FILTER ).toNodeArray(); if ( firstColumnCount == null ) { firstColumnCount = getColumnCount( row ); for ( Node c : cells ) { TagNode cell = (TagNode) c; grid.addHeader( new GridHeader( getValue( cell ), false, false ) ); Integer colSpan = MathUtils.parseInt( cell.getAttribute( "colspan" ) ); if ( colSpan != null && colSpan > 1 ) { grid.addEmptyHeaders( ( colSpan - 1 ) ); } } } else { if ( firstColumnCount != getColumnCount( row ) ) { log.warn( "Ignoring row which has " + row.getColumnCount() + " columns since table has " + firstColumnCount + " columns" ); continue; } grid.addRow(); for ( Node c : cells ) { TagNode cell = (TagNode) c; grid.addValue( getValue( cell ) ); Integer colSpan = MathUtils.parseInt( cell.getAttribute( "colspan" ) ); if ( colSpan != null && colSpan > 1 ) { grid.addEmptyValues( ( colSpan - 1 ) ); } } } } grids.add( grid ); } return grids; }  static void toPdf( Grid grid, OutputStream out ); static void toPdf( List<Grid> grids, OutputStream out ); static void toXls( List<Grid> grids, OutputStream out ); static void toXls( Grid grid, OutputStream out ); static void toCsv( Grid grid, Writer writer ); static void toJasperReport( Grid grid, Map<String, Object> params, OutputStream out ); static void toJrxml( Grid grid, Map<?, ?> params, Writer writer ); static void toHtml( Grid grid, Writer writer ); static void toHtmlCss( Grid grid, Writer writer ); static void toHtmlInlineCss( Grid grid, Writer writer ); static void toXml( Grid grid, OutputStream out ); static void addRows( Grid grid, SqlRowSet rs ); static int getGridIndexByDimensionItem( List<Object> row, List<DimensionalItemObject> items, int defaultIndex ); static List<Grid> fromHtml( String html, String title ); static String getValue( TagNode cell ); static Map<String, Object> getMetaValueMapping( Grid grid, int valueIndex );  }
@Test public void testFromHtml() throws Exception { String html = IOUtils.toString( new ClassPathResource( "customform.html" ).getInputStream(), StandardCharsets.UTF_8 ); List<Grid> grids = GridUtils.fromHtml( html, "TitleA" ); assertNotNull( grids ); assertEquals( 6, grids.size() ); assertEquals( "TitleA", grids.get( 0 ).getTitle() ); }