public static Iterable < String > renderSolutionTable ( final List < String > variables , final Iterable < ? extends BindingSet > solutions ) { final List < String > actualVariables ; if ( variables != null ) { actualVariables = ImmutableList . copyOf ( variables ) ; } else { final Set < String > variableSet = Sets . newHashSet ( ) ; for ( final BindingSet solution : solutions ) { variableSet . addAll ( solution . getBindingNames ( ) ) ; } actualVariables = Ordering . natural ( ) . sortedCopy ( variableSet ) ; } final int width = 75 / actualVariables . size ( ) ; final StringBuilder builder = new StringBuilder ( ) ; builder . append ( "<table class=\"sparql table table-condensed tablesorter\"><thead>\n<tr>" ) ; for ( final String variable : actualVariables ) { builder . append ( "<th style=\"width: " ) . append ( width ) . append ( "%\">" ) . append ( escapeHtml ( variable ) ) . append ( "</th>" ) ; } final Iterable < String > header = ImmutableList . of ( builder . toString ( ) ) ; final Iterable < String > footer = ImmutableList . of ( "</tbody></table>" ) ; final Function < BindingSet , String > renderer = new Function < BindingSet , String > ( ) { @ Override public String apply ( final BindingSet bindings ) { if ( Thread . interrupted ( ) ) { throw new IllegalStateException ( "Interrupted" ) ; } final StringBuilder builder = new StringBuilder ( ) ; builder . append ( "<tr>" ) ; for ( final String variable : actualVariables ) { builder . append ( "<td>" ) ; try { render ( bindings . getValue ( variable ) , builder ) ; } catch ( final IOException ex ) { throw new Error ( ex ) ; } builder . append ( "</td>" ) ; } builder . append ( "</tr>\n" ) ; return builder . toString ( ) ; } } ; return Iterables . concat ( header , Iterables . transform ( solutions , renderer ) , footer ) ; }
Render in a streaming - way the solutions of a SPARQL SELECT query to an HTML table emitting an iterable with of HTML fragments .