⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queryresultsformatter.java

📁 Java实现的数据库和RDF之间的转换程序。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                StringBuffer sbuff = new StringBuffer(120) ;
                String rVar = (String)queryResults.getResultVars().get(col) ;
                sbuff.append('?') ;
                sbuff.append(rVar) ;
                sbuff.append(' ') ;
                String s = getVarAsString(env, rVar) ;

                int pad = colWidths[col] ;
                sbuff.append(s) ;

                for ( int j = 0 ; j < pad-s.length() ; j++ )
                    sbuff.append(' ') ;
                // Always has a trailing space
                sbuff.append(' ') ;
                pw.print(sbuff) ;
            }
            pw.println(" .") ;
        }
        all.close() ;
        pw.flush() ;
    }

    /** Textual representation : default layout using " | " to separate columns
     *  @param printwriter Output
     */
    public void printAll(PrintWriter printwriter) { printAll(printwriter, " | ", null) ; }

    /** Textual representation : layout using given separator
     *  @param printwriter Output
     *  @param colSep      Column separator
     */
    public void printAll(PrintWriter printwriter, String colSep) { printAll(printwriter, colSep, null) ; }

    /** Textual representation : layout using given separator
     *  @param printwriter Output
     *  @param colSep      Column separator
     *  @param lineEnd     String to add to end of lines
     */
    public void printAll(PrintWriter printwriter, String colSep, String lineEnd)
    {
        if ( queryResults.getResultVars().size() == 0 )
        {
            printwriter.println("==== No variables ====") ;
            db_rdf.information.resultStr +="==== No variables ====";
            printwriter.flush() ;
            return ;
        }

     	if ( all == null )
    		all = new QueryResultsMem(queryResults) ;

        if ( colWidths == null )
            colWidths() ;

        String row[] = new String[numCols] ;
        int lineWidth = 0 ;
        for ( int col = 0 ; col < numCols ; col++ )
        {
            String rVar = (String)queryResults.getResultVars().get(col) ;
            row[col] = rVar ;
            lineWidth += colWidths[col] ;
            if ( col > 0 )
                lineWidth += colSep.length() ;
        }
        printRow(printwriter, row, colSep, lineEnd) ;

        for ( int i = 0 ; i < lineWidth ; i++ )
        {
            printwriter.print('=');
             db_rdf.information.resultStr +="=";
        }
        printwriter.println() ;
         db_rdf.information.resultStr +="\n";

        for ( Iterator tableIter = all ; tableIter.hasNext() ; )
        {
            ResultBinding env = (ResultBinding)tableIter.next() ;
            for ( int col = 0 ; col < numCols ; col++ )
            {
                String rVar = (String)queryResults.getResultVars().get(col) ;
                row[col] = this.getVarAsString(env, rVar );
            }
            printRow(printwriter, row, colSep, lineEnd) ;
           db_rdf.information.resultStr +=db_rdf.information.rightQueryEnd;     //正确的查询结果结束标志
        }
        all.reset() ;
        printwriter.flush() ;
    }


    private void printRow(PrintWriter pw, String[] row, String colSep, String lineEnd)
    {
        if ( row.length != numCols )
            throw new RDQL_InternalErrorException("QueryResultsFormatter.printRow: Row length ("+row.length+") != numCols ("+numCols+")") ;

        for ( int col = 0 ; col < numCols ; col++ )
        {
            String s = row[col] ;
            int pad = colWidths[col] ;
            StringBuffer sbuff = new StringBuffer(120) ;

            if ( col > 0 )
                sbuff.append(colSep) ;

            sbuff.append(s) ;
            for ( int j = 0 ; j < pad-s.length() ; j++ )
                sbuff.append(' ') ;

            pw.print(sbuff) ;
            db_rdf.information.resultStr +=sbuff;
        }
        if ( lineEnd != null )
        {
            pw.print(lineEnd);
            db_rdf.information.resultStr += lineEnd;
        }
        pw.println() ;
        db_rdf.information.resultStr +="\n";
    }

    /** HTML representation */

    public void printHTML(PrintWriter pw)
    {
    	if ( all == null )
    	{
    		all = new QueryResultsMem(queryResults) ;
            numRows = all.size() ;
        }

        pw.println("<table>");
        // Column headings
        pw.println("  <tr>");
        for ( int ii = 0 ; ii < queryResults.getResultVars().size() ; ii++ )
        {
            String tmp = (String)queryResults.getResultVars().get(ii) ;
            pw.print("    <th>") ;
            pw.print(tmp) ;
            pw.print("</th>") ;
            pw.println() ;
        }
        pw.println("  </tr>");

        for ( ; all.hasNext() ; )
        {
            pw.println("  <tr>");
            ResultBinding env = (ResultBinding)all.next() ;
            for ( int col = 0 ; col < queryResults.getResultVars().size() ; col++ )
            {
                String rVar = (String)queryResults.getResultVars().get(col) ;
                Value val = env.getValue(rVar) ;
                // Use the unquoted form here - should also XML-escape it.
                String s = (val==null)? notThere : val.toString() ;

                pw.print("    <td>") ;
                pw.print(s) ;
                pw.print("</td>") ;
                pw.println() ;
            }
            pw.println("  </tr>");
        }
        pw.println("</table>");
        pw.flush() ;
        all.reset() ;
    }

    /** This operation faithfully walks the results but does nothing with them.
     *  Used in timing operations.  Be careful that a compiler does
     *  not optimize some or all of it away!
     */

    public void consume()
    {
        //numCols = queryResults.getResultVars().size() ;
        for ( Iterator rowIter = queryResults ; rowIter.hasNext() ; )
        {
            ResultBinding result = (ResultBinding)rowIter.next() ;

            for ( ResultBinding.ResultBindingIterator iter = result.iterator() ; iter.hasNext() ; )
            {
                iter.next() ;
                String rVar = iter.varName() ;
                Value val = iter.value() ;
                //String valStr = (val==null) ? null : val.toString() ;
            }
        }
    }

    private String getVarAsString(ResultBinding env, String varName)
    {
        // Without adornment.
        //Value val = env.getValue(rVar) ;
        //String s = (val==null)? notThere : val.asQuotedString() ;
        //return s ;

        // Print in all details
        Object obj = env.get(varName) ;

        String s = notThere ;
        if ( obj != null )
        {
            if ( ! ( obj instanceof RDFNode ) )
                s = "Found a "+(obj.getClass().getName()) ;
            else if ( obj instanceof Literal )
            {
                Literal l = (Literal)obj ;
                StringBuffer sb = new StringBuffer() ;
                sb.append('"').append(l.getLexicalForm()).append('"') ;

                if ( ! l.getLanguage().equals(""))
                    sb.append("@").append(l.getLanguage()) ;
                if ( l.getDatatype() != null )
                    sb.append("^^<").append(l.getDatatypeURI()).append(">") ;
                s = sb.toString() ;
            }
            else
                s = env.getValue(varName).asQuotedString() ;
        }
        return s ;
    }
}

/*
 *  (c) Copyright 2001, 2002, 2003 Hewlett-Packard Development Company, LP
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -