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

📄 n3tordf.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
		catch (JenaException rdfEx)
		{
			error("Line "+line+": JenaException: " + rdfEx);
		}
	}
	
	private Map bNodeMap = new HashMap() ;
    
	private RDFNode createNode(int line, AST thing) 
	{
		//String tokenType = N3AntlrParser._tokenNames[thing.getType()] ;
		//System.out.println("Token type: "+tokenType) ;
		String text = thing.getText() ;
		switch (thing.getType())
		{
            case N3Parser.NUMBER :
                Resource xsdType = XSD.integer ;
                if ( text.indexOf('.') >= 0 )
                    // The choice of XSD:decimal is for compatibility with N3/cwm.
                    xsdType = XSD.decimal ;
                if ( text.indexOf('e') >= 0 || text.indexOf('E') >= 0 )
                    xsdType = XSD.xdouble ;
                return model.createTypedLiteral(text, xsdType.getURI());
                
			case N3Parser.LITERAL :
				// Literals have three part: value (string), lang tag, datatype
                // Can't have a lang tag and a data type - if both, just use the datatype
                
                AST a1 = thing.getNextSibling() ;
                AST a2 = (a1==null?null:a1.getNextSibling()) ;
                AST datatype = null ;
                AST lang = null ;

                if ( a2 != null )
                {
                    if ( a2.getType() == N3Parser.DATATYPE )
                        datatype = a2.getFirstChild() ;
                    else
                        lang = a2 ;
                }
                // First takes precedence over second.
                if ( a1 != null )
                {
                    if ( a1.getType() == N3Parser.DATATYPE )
                        datatype = a1.getFirstChild() ;
                    else
                        lang = a1 ;
                }

                // Chop leading '@'
                String langTag = (lang!=null)?lang.getText().substring(1):null ;
                
                if ( datatype == null )
                    return model.createLiteral(text, langTag) ;
                
                // If there is a datatype, it takes predence over lang tag.
                String typeURI = datatype.getText();

                if ( datatype.getType() != N3Parser.QNAME &&
                     datatype.getType() != N3Parser.URIREF )
                {
                    error("Line "+ line+ ": N3toRDF: Must use URIref or QName datatype URI: "
                            + text+ "^^"+ typeURI+"("+N3Parser.getTokenNames()[datatype.getType()]+")");
                    return model.createLiteral("Illegal literal: " + text + "^^" + typeURI);
 
                }
                
                // Can't have bNodes here so the code is slightly different for expansion
                
                if ( datatype.getType() == N3Parser.QNAME )
                {
                    if (typeURI.startsWith("_:") || typeURI.startsWith("=:"))
                    {
                        error("Line "+ line+ ": N3toRDF: Can't use bNode for datatype URI: "
                                + text+ "^^"+ typeURI);
                        return model.createLiteral("Illegal literal: " + text + "^^" + typeURI);
                    }

                    String typeURI2 = expandPrefix(model, typeURI) ;
                    if ( typeURI2 == typeURI )
                    {
                        error("Line "+line+": N3toRDF: Undefined qname namespace in datatype: " + typeURI);
                    }
                    
                    typeURI = typeURI2 ;
                }

                typeURI = expandURIRef(typeURI, line);
                // 2003-08 - Ignore lang tag when there is an type. 
                return model.createTypedLiteral(text, typeURI) ;

			case N3Parser.QNAME :
				// Is it a labelled bNode?
                // Check if _ has been defined.
				if ( text.startsWith("_:") && ( model.getNsPrefixURI("_") == null ) )
				{
					if ( ! bNodeMap.containsKey(text) )
						bNodeMap.put(text, model.createResource()) ;
					return (Resource)bNodeMap.get(text) ;
				}
			
                String uriref = expandPrefix(model, text) ;
                if ( uriref == text )
                {
                    error("Line "+line+": N3toRDF: Undefined qname namespace: " + text);
                    return null ;
                }
                return model.createResource(expandURIRef(uriref, line)) ;

            // Normal URIref - may be <> or <#>
            case N3Parser.URIREF :
                return model.createResource(expandURIRef(text, line)) ;

            // Lists
            case N3Parser.TK_LIST_NIL:
                return RDF.nil ;
            case N3Parser.TK_LIST:
                return RDF.List ;

			case N3Parser.ANON:			// bNodes via [] or [:- ] QNAME starts "=:"
				if ( ! bNodeMap.containsKey(text) )
					bNodeMap.put(text, model.createResource()) ;
				return (Resource)bNodeMap.get(text) ;

            case N3Parser.UVAR:
                error("Line "+line+": N3toRDF: Can't map variables to RDF: "+text) ;
                break ;

			default:
				error("Line "+line+": N3toRDF: Can't map to a resource or literal: "+AntlrUtils.ast(thing)) ;
                break ;
		}
		return null ;
	}

    private String expandURIRef(String text, int line)
    {
        try {
            return RelURI.resolve(text, base) ;
        } catch (Exception ex)
        { 
            error("Line "+line+": N3toRDF: Bad URI: "+text+ " ("+ex.getMessage()+")") ;
        }
        return null ;
    }
    
    private boolean hasURIscheme(String text)
    {
        for ( int i = 0 ; i < text.length() ; i++ )
        {
            char ch = text.charAt(i) ;
            if ( ch == ':' )
                return true ;
            if ( ( ch >= 'a' && ch <= 'z' ) ||
                 ( ch >= 'A' && ch <= 'Z' ) ||
                 ( ch >= '0' && ch <= '9' ) ||
                 ch == '+' || ch == '-' | ch== '.' ) 
                continue ;
            return false ;
        }
        return false ;
    }
    
    private void setPrefixMapping(Model m, String prefix, String uriref)
    {
        try { model.setNsPrefix(prefix, uriref); }
        catch (PrefixMapping.IllegalPrefixException ex)
        {
            warning("Prefix mapping '" + prefix + "' illegal: used but not recorded in model");
        }
        myPrefixMapping.put(prefix, uriref);
    }
    
    private String expandPrefix(Model m, String prefixed)
    {
        // Code from shared.impl.PrefixMappingImpl ...
        // Needed a copy as we store unchecked prefixes for N3.
        int colon = prefixed.indexOf( ':' );
        if (colon < 0) 
            return prefixed;
        else
        {
            String prefix = prefixed.substring( 0, colon );
            String uri = (String) myPrefixMapping.get( prefix );
            if ( uri == null )
                return prefixed ;
            return uri + prefixed.substring( colon + 1 ) ;
        }
        // Not this - model may already have prefixes defined;
        // we allow "illegal" prefixes (e.g. starts with a number)
        // for compatibility 
        //return model.expandPrefix(prefix) ;
    }
}

/*
 *  (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 + -