📄 reluri.java
字号:
/*
* (c) Copyright 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/
package com.hp.hpl.jena.n3;
/*
* (c) Copyright 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.regex.Pattern;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.shared.JenaException;
import com.hp.hpl.jena.util.FileUtils;
import com.hp.hpl.jena.util.cache.Cache;
/** RelURI copied to Jena
*
* @author Andy Seaborne
* @version $Id: RelURI.java,v 1.12 2007/02/05 12:41:11 andy_seaborne Exp $
*/
public class RelURI
{
public static class JenaURIException extends JenaException
{
public JenaURIException(String msg) { super(msg) ; }
}
public static class RelativeURIException extends JenaURIException
{
public RelativeURIException(String msg) { super(msg) ; }
}
static private String globalBase = null ;
static Cache baseCache = new Cache1() ;
static Pattern patternHttp = Pattern.compile("^http://[^/]*/[^/]+") ;
static Pattern patternFile = Pattern.compile("^file:/*[^/]+/") ;
/** Create resolve a URI agaisnt a base.
* Returns null if the result is not absolute.
* @param relStr
* @param baseStr
* @return String An absolute URI
* @throws JenaURIException Unacceptable base URI string
* @throws RelativeURIException Base is relative or opaque
*/
static public String resolve(String relStr, String baseStr)
{
// Special case (GNUClassPath workaround)
if ( relStr.equals(".") )
{
if ( baseStr.startsWith("http://") || baseStr.startsWith("file:/") )
{
if ( baseStr.endsWith("/") )
return baseStr ;
if ( patternHttp.matcher(baseStr).find() )
{
int j = baseStr.lastIndexOf("/") ;
return baseStr.substring(0, j+1) ;
}
if ( patternFile.matcher(baseStr).find())
{
int j = baseStr.lastIndexOf("/") ;
return baseStr.substring(0, j+1) ;
}
}
// Can't shortcut - drop through anyway.
}
// Encode spaces (for filenames)
baseStr = encode(baseStr) ;
relStr = encode(relStr) ;
// "Adapt" URIs with spaces
String s = _resolve(relStr, baseStr) ;
s = decode(s) ;
return s ;
}
static Pattern patEnc1 = Pattern.compile("_") ;
static String encStr1 = "__" ;
static Pattern patEnc2 = Pattern.compile(" ") ;
static String encStr2 = "_20" ;
static private String encode(String s)
{
if ( s == null ) return s ;
s = patEnc1.matcher(s).replaceAll(encStr1) ;
s = patEnc2.matcher(s).replaceAll(encStr2) ;
// Not Java 1.4
// s = s.replace("_", "__") ;
// s = s.replace(" ", "_20") ;
return s ;
}
static Pattern patDec1 = Pattern.compile("_20") ;
static String decStr1 = " " ;
static Pattern patDec2 = Pattern.compile("__") ;
static String decStr2 = "_" ;
static private String decode(String s)
{
s = patDec1.matcher(s).replaceAll(decStr1) ;
s = patDec2.matcher(s).replaceAll(decStr2) ;
// s = s.replace("_20", " ") ;
// s = s.replace("__", "_") ;
return s ;
}
static private String _resolve(String relStr, String baseStr)
{
URI rel = null ;
try { rel = new URI(relStr) ; }
catch (java.net.URISyntaxException ex)
{ throw new JenaURIException("Illegal URI: "+relStr) ; }
if ( rel.isAbsolute() )
{
String s = rel.getScheme() ;
// Corner case : relStr is the strictly absolute URI with an incomplete
// scheme specific part -- example: "file:x"
if ( rel.getScheme().equals("file") )
return resolveFileURL(relStr) ;
return relStr ;
}
if ( baseStr == null )
{
// Null base - relStr not absolute
//return relStr ;
if ( rel.isAbsolute() )
return relStr ;
throw new JenaURIException("Null base for relative URI resolution: "+relStr) ;
}
if ( baseStr.length() == 0 )
throw new JenaURIException("Empty base for relative URI resolution") ;
// if ( baseStr.endsWith("#") )
// throw new JenaURIException("Base URI ends in #") ;
// RFC 3986 says any URI to be used as a base URI is stripped of its fragment.
if ( baseStr.indexOf('#') >= 0 )
{
int i = baseStr.indexOf('#') ;
baseStr = baseStr.substring(0,i) ;
}
// Check for just "scheme:"
if ( baseStr.endsWith(":") )
{
// Slightly confusingly, this leads to a slightly different exception
// for ("base:" "#") and ("base:x" "#")
return resolve(baseStr+relStr) ;
}
if ( relStr.equals("") )
return baseStr ;
// Cache this? One slot cache.
URI base = (URI)baseCache.get(baseStr) ;
if ( base == null )
{
try {
base = new URI(baseStr) ;
} catch (java.net.URISyntaxException ex)
{ throw new JenaURIException("Illegal URI (base): "+baseStr) ; }
if ( ! base.isAbsolute() )
throw new RelativeURIException("Relative URI for base: "+baseStr) ;
if ( base.isOpaque() )
{
// The case of file:A and #x
if ( base.getScheme().equals("file") && relStr.startsWith("#") )
return baseStr+relStr ;
// tag: and urn: but also anOther:...
//return baseStr+relURI ;
throw new RelativeURIException("Can't resolve a relative URI against an opaque URI: rel="+relStr+" : base="+baseStr) ;
}
baseCache.put(baseStr, base) ;
}
if ( base.getPath().length() == 0 && !relStr.startsWith("/") )
{
// No path in base. Unrooted relative string.
// Fudge - make base have a slash
try {
base = new URI(baseStr+"/") ;
} catch (java.net.URISyntaxException ex)
{
LogFactory.getLog(RelURI.class).fatal("Base now illegal fixing up path-less base URI ("+baseStr+")") ;
throw new JenaURIException("Illegal URI (base) ptII: "+baseStr) ;
}
}
URI abs = resolve(rel, base) ;
if ( abs == null )
return null ;
// Finally, sort out file URLs
// 1 - file:filename => file:///dir/filename
// 2 - file:/ => file:///
// Fix for file names
String s = abs.toString() ;
if ( s.startsWith("file:") )
s = resolveFileURL(s) ;
return s ;
}
private static URI resolve(URI rel, URI base)
{
try {
URI abs = base.resolve(rel) ;
if ( ! abs.isAbsolute() )
return null ;
return abs ;
} catch (RuntimeException ex)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -