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

📄 digconnection.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            String message = (messages.getLength() > 0) ? ((Text) messages.item( 0 )).getNodeValue().trim() : "(no message)";

            // check for an error indicating an inconsistent KB
            if (message.equals( profile.getInconsistentKBMessage() )) {
                throw new DIGInconsistentKBException( message, msgAttr, code );
            }
            else {
                throw new DIGErrorResponseException( message, msgAttr, code );
            }
        }
    }


    /**
     * <p>Append any warning messages from this response to the list of recent warnings,
     * which is first cleared.</p>
     * @param response The response from the DIG server
     * @return True if any warnings were detected.
     */
    public boolean warningCheck( Document response ) {
        Element root = response.getDocumentElement();
        NodeList ok = root.getElementsByTagName( DIGProfile.OK );
        m_warnings.clear();

        if (ok != null && ok.getLength() > 0) {
            Element e = (Element) ok.item(0);
            NodeList warnings = e.getElementsByTagName( DIGProfile.WARNING );

            if (warnings != null && warnings.getLength() > 0) {
                for (int i = 0;  i < warnings.getLength(); i++) {
                    // append the warning message to the list
                    Element warn = (Element) warnings.item( i );
                    m_warnings.add( warn.getAttribute( DIGProfile.MESSAGE ) );
                }

                return true;
            }
        }

        return false;
    }


    /**
     * <p>Answer an iterator over the warnings received since the last tell operation</p>
     * @return An iterator over warnings
     */
    public Iterator getWarnings() {
        return m_warnings.iterator();
    }


    /**
     * <p>Release this connection back to the connection pool.</p>
     */
    public void release() {
        DIGConnectionPool.getInstance().release( this );
    }


    /**
     * <p>Answer the URL of the external reasoner this connection is bound to.</p>
     * @return The current external reasoner URL
     */
    public String getReasonerURL() {
        return m_extReasonerURL;
    }


    /**
     * <p>Set the URL of the external reasoner with which this connection communicates.</p>
     * @param url The URL of the new external reasoner connection point
     */
    public void setReasonerURL( String url ) {
        m_extReasonerURL = url;
        m_kbURI = null;
    }


    // Internal implementation methods
    //////////////////////////////////

    /**
     * <p>Answer the XML document object that resulted from the most recent request.</p>
     * @param conn The current HTTP connection
     * @return The response from the DIG reasoner, as an XML object
     * @exception DigReasonerException if the underling connection or XML parser raises
     * an error.
     */
    protected Document getDigResponse( HttpURLConnection conn ) {
        try {
            // check for successful response
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new DIGReasonerException( "DIG reasoner returned failure code " +
                                                conn.getResponseCode() + ": " +
                                                conn.getResponseMessage() );
            }

            BufferedInputStream in = new BufferedInputStream( conn.getInputStream() );
            StringBuffer buf = new StringBuffer();

            // read the return result into a string buffer before we parse it
            int ch = in.read();
            while (ch > 0) {
                buf.append( (char) ch );
                ch = in.read();
            }

            // TODO remove LogFactory.getLog( getClass() ).debug( "Response buffer = " + buf.toString() );
            // now parse into a document
            DocumentBuilder builder = m_factory.newDocumentBuilder();
            return builder.parse( new ByteArrayInputStream( buf.toString().getBytes() ) );
        }
        catch (Exception e) {
            e.printStackTrace( System.err );
            throw new DIGWrappedException( e );
        }
    }


    /**
     * <p>Answer an XML formatter object for the given document
     * @param doc The XML document to be serialised
     * @return An XML formatter object for the document
     */
    protected OutputFormat createXMLFormatter( Document doc ) {
        OutputFormat format = new OutputFormat( doc );
        format.setIndenting( true );
        format.setLineWidth( 0 );
        format.setPreserveSpace( false );

        return format;
    }


    /**
     * <p>Create a DIG verb as an xml element in a new document object.</p>
     * @param verbName The name of the DIG verb, as a string
     * @return An XML DOM element representing the DIG verb
     */
    protected Document createDigVerb( String verbName, DIGProfile profile ) {
        try {
            // initialise the XML DOM tree
            DocumentBuilder builder = m_factory.newDocumentBuilder();
            Document doc = builder.newDocument();

            // create the verb as the root element of the XML document
            Element root = doc.createElementNS( profile.getDIGNamespace(), verbName );
            doc.appendChild( root );

            // set the standard attributes
            root.setAttribute( "xmlns", profile.getDIGNamespace() );
            root.setAttribute( "xmlns:xsi", XSI );
            root.setAttributeNS( XSI, "xsi:schemaLocation",
                                 profile.getDIGNamespace() + " " + profile.getSchemaLocation() );
            if (m_kbURI != null) {
                root.setAttribute( DIGProfile.URI, m_kbURI );
            }

            return doc;
        }
        catch (FactoryConfigurationError e) {
            throw new DIGWrappedException( e );
        }
        catch (ParserConfigurationException e) {
            throw new DIGWrappedException( e );
        }
    }


    /**
     * <p>Log the messages going to and from DIG</p>
     * @param outgoing True for send, false for receive
     * @param msg The document sent or received.
     */
    protected void logMessage( boolean outgoing, Document msg ) {
        if (m_logCommunications) {
            StringWriter out = new StringWriter();
            serialiseDocument( msg, out );

            if (log.isDebugEnabled()) {
                log.debug( outgoing ? "Sending to DIG reasoner ..." : "Received from DIG reasoner ..." );
                log.debug( out );
            }
        }
    }


    //==============================================================================
    // Inner class definitions
    //==============================================================================

}


/*
 *  (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 + -