📄 n3jenawritercommon.java
字号:
{
if ( r.isAnon() )
{
if ( ! alwaysAllocateBNodeLabel )
{
// Does anything point to it?
StmtIterator sIter = r.getModel().listStatements(null, null, r) ;
if ( ! sIter.hasNext() )
{
sIter.close() ;
// This bNode is not referenced so don't need the bNode Id.
// Must be a subject - indent better be zero!
// This only happens for subjects because object bNodes
// referred to once (the other case for [] syntax)
// are handled elsewhere (by oneRef set)
// Later: use [ prop value ] for this.
return "[]" ;
}
sIter.close() ;
}
if ( ! bNodesMap.containsKey(r) )
bNodesMap.put(r, "_:b"+(++bNodeCounter)) ;
return (String)bNodesMap.get(r) ;
}
// It has a URI.
if ( r.equals(RDF.nil) )
return "()" ;
return formatURI(r.getURI()) ;
}
protected String formatLiteral(Literal literal)
{
String datatype = literal.getDatatypeURI() ;
String lang = literal.getLanguage() ;
String s = literal.getLexicalForm() ;
if ( datatype != null )
{
// Special form we know how to handle?
// Assume valid text
if ( datatype.equals(XSD.integer.getURI()) )
{
try {
new java.math.BigInteger(s) ;
return s ;
} catch (NumberFormatException nfe) {}
// No luck. Continue.
// Continuing is always safe.
}
if ( datatype.equals(XSD.decimal.getURI()) )
{
// Must have ., can't have e or E
if ( s.indexOf('.') >= 0 &&
s.indexOf('e') == -1 && s.indexOf('E') == -1 )
{
// See if parsable.
try {
BigDecimal d = new BigDecimal(s) ;
return s ;
} catch (NumberFormatException nfe) {}
}
}
if ( this.allowDoubles && datatype.equals(XSD.xdouble.getURI()) )
{
// Must have 'e' or 'E' (N3 and Turtle now read 2.3 as a decimal).
if ( s.indexOf('e') >= 0 ||
s.indexOf('E') >= 0 )
{
try {
// Validate it.
Double.parseDouble(s) ;
return s ;
} catch (NumberFormatException nfe) {}
// No luck. Continue.
}
}
}
// Format the text - with escaping.
StringBuffer sbuff = new StringBuffer() ;
boolean singleQuoteLiteral = true ;
String quoteMarks = "\"" ;
// Things that force the use of """ strings
if ( this.allowTripleQuotedStrings &&
( s.indexOf("\n") != -1 ||
s.indexOf("\r") != -1 ||
s.indexOf("\f") != -1 ) )
{
quoteMarks = "\"\"\"" ;
singleQuoteLiteral = false ;
}
sbuff.append(quoteMarks);
string(sbuff, s, singleQuoteLiteral) ;
sbuff.append(quoteMarks);
// Format the language tag
if ( lang != null && lang.length()>0)
{
sbuff.append("@") ;
sbuff.append(lang) ;
}
// Format the datatype
if ( datatype != null )
{
sbuff.append("^^") ;
sbuff.append(formatURI(datatype)) ;
}
return sbuff.toString() ;
}
protected String formatProperty(Property p)
{
String prop = p.getURI() ;
if ( this.useWellKnownPropertySymbols && wellKnownPropsMap.containsKey(prop) )
return (String)wellKnownPropsMap.get(prop);
return formatURI(prop) ;
}
protected String formatURI(String uriStr)
{
String matchURI = "" ;
String matchPrefix = null ;
if ( doAbbreviatedBaseURIref && uriStr.equals(baseURIref) )
return "<>" ;
// Try for a prefix and write as qname. Find the longest if several.
// Possible optimization: split URI and have URI=> ns: map.
// Ordering prefixes by length, then first hit is better.
//
// Also: could just assume that the split is on / or #
// Means we need to find a prefix just once.
for ( Iterator pIter = prefixMap.keySet().iterator() ; pIter.hasNext() ; )
{
String p = (String)pIter.next() ;
String u = (String)prefixMap.get(p) ;
if ( uriStr.startsWith(u) )
if ( matchURI.length() < u.length() )
{
matchPrefix = p ;
matchURI = u ;
}
}
if ( matchPrefix != null )
{
String localname = uriStr.substring(matchURI.length()) ;
if ( checkQName(matchPrefix, localname) )
return matchPrefix+":"+localname ;
// Continue and return quoted URIref
}
// Not as a qname - write as a quoted URIref
// Should we unicode escape here?
// It should be right - the writer should be UTF-8 on output.
return "<"+uriStr+">" ;
}
// Qnames in N3 aren't really qnames
// No dots; digit can be first
// These tests must agree, or be more restrictive, than the parser.
static boolean checkQName(String ns, String local)
{
return checkQNameNamespace(ns) && checkQNameLocalname(local) ;
}
static boolean checkQNameNamespace(String s)
{
return checkQNamePart(s) ;
}
static boolean checkQNameLocalname(String s)
{
return checkQNamePart(s) ;
}
static boolean checkQNamePart(String s)
{
boolean isOK = true ;
CharacterIterator cIter = new StringCharacterIterator(s) ;
for ( char ch = cIter.first() ;
ch != java.text.CharacterIterator.DONE ;
ch = cIter.next() )
{
if ( Character.isLetterOrDigit(ch) )
continue ;
switch (ch)
{
case '_': case '-':
continue ;
}
// Not an acceptable characters
isOK = false ;
break ;
}
return isOK ;
}
final static String WS = "\n\r\t" ;
protected static void string(StringBuffer sbuff, String s, boolean singleQuoteLiteral)
{
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// Escape escapes and quotes
if (c == '\\' || c == '"' )
{
sbuff.append('\\') ;
sbuff.append(c) ;
continue ;
}
// Characters to literally output.
// This would generate 7-bit safe files
// if (c >= 32 && c < 127)
// {
// sbuff.append(c) ;
// continue;
// }
// Whitespace
if ( singleQuoteLiteral && ( c == '\n' || c == '\r' || c == '\f' ) )
{
if (c == '\n') sbuff.append("\\n");
if (c == '\t') sbuff.append("\\t");
if (c == '\r') sbuff.append("\\r");
if (c == '\f') sbuff.append("\\f");
continue ;
}
// Output as is (subject to UTF-8 encoding on output that is)
sbuff.append(c) ;
// // Unicode escapes
// // c < 32, c >= 127, not whitespace or other specials
// String hexstr = Integer.toHexString(c).toUpperCase();
// int pad = 4 - hexstr.length();
// sbuff.append("\\u");
// for (; pad > 0; pad--)
// sbuff.append("0");
// sbuff.append(hexstr);
}
}
protected int calcPropertyPadding(String propStr)
{
int padding = propertyCol - propStr.length();
if (padding < minGap)
padding = minGap;
return padding ;
}
protected static String pad(int cols)
{
StringBuffer sb = new StringBuffer() ;
for ( int i = 0 ; i < cols ; i++ )
sb.append(' ') ;
return sb.toString() ;
}
// Utilities
protected int countProperties(Resource r)
{
int numProp = 0 ;
StmtIterator sIter = r.listProperties() ;
for ( ; sIter.hasNext() ; )
{
sIter.nextStatement() ;
numProp++ ;
}
sIter.close() ;
return numProp ;
}
protected int countProperties(Resource r, Property p)
{
int numProp = 0 ;
StmtIterator sIter = r.listProperties(p) ;
for ( ; sIter.hasNext() ; )
{
sIter.nextStatement() ;
numProp++ ;
}
sIter.close() ;
return numProp ;
}
protected int countArcsTo(Resource resource)
{
return countArcsTo(null, resource) ;
}
protected int countArcsTo(Property prop, Resource resource)
{
int numArcs = 0 ;
StmtIterator sIter = resource.getModel().listStatements(null, prop, resource) ;
for ( ; sIter.hasNext() ; )
{
sIter.nextStatement() ;
numArcs++ ;
}
sIter.close() ;
return numArcs ;
}
protected Iterator rdfListIterator(Resource r)
{
List list = new ArrayList() ;
for ( ; ! r.equals(RDF.nil); )
{
StmtIterator sIter = r.getModel().listStatements(r, RDF.first, (RDFNode)null) ;
list.add(sIter.nextStatement().getObject()) ;
if ( sIter.hasNext() )
// @@ need to cope with this (unusual) case
throw new JenaException("N3: Multi valued list item") ;
sIter = r.getModel().listStatements(r, RDF.rest, (RDFNode)null) ;
r = (Resource)sIter.nextStatement().getObject() ;
if ( sIter.hasNext() )
throw new JenaException("N3: List has two tails") ;
}
return list.iterator() ;
}
// Convenience operations for accessing system properties.
protected String getStringValue(String prop, String defaultValue)
{
String p = getPropValue(prop) ;
if ( p == null )
return defaultValue ;
return p ;
}
protected boolean getBooleanValue(String prop, boolean defaultValue)
{
String p = getPropValue(prop) ;
if ( p == null )
return defaultValue ;
if ( p.equalsIgnoreCase("true") )
return true ;
if ( p.equals("1") )
return true ;
return false ;
}
protected int getIntValue(String prop, int defaultValue)
{
String p = getPropValue(prop) ;
if ( p == null )
return defaultValue ;
try {
return Integer.parseInt(p) ;
} catch (NumberFormatException ex)
{
logger.warn("Format error for property: "+prop) ;
return defaultValue ;
}
}
// May be the absolute or local form of the property name
protected String getPropValue(String prop)
{
prop = absolutePropName(prop) ;
if ( writerPropertyMap != null && writerPropertyMap.containsKey(prop) )
{
Object obj = writerPropertyMap.get(prop) ;
if ( ! ( obj instanceof String ) )
logger.warn("getPropValue: N3 Property for '"+prop+"' is not a string") ;
return (String)obj ;
}
String s = JenaRuntime.getSystemProperty(prop) ;
if ( s == null )
s = JenaRuntime.getSystemProperty(localPropName(prop)) ;
return s ;
}
protected String absolutePropName(String propName)
{
if ( propName.indexOf(':') == -1 )
return N3JenaWriter.propBase + propName ;
return propName ;
}
protected String localPropName(String propName)
{
if ( propName.startsWith(N3JenaWriter.propBase) )
propName = propName.substring(N3JenaWriter.propBase.length()) ;
return propName ;
}
private boolean isOpaque(String uri)
{
try {
return new URI(uri).isOpaque() ;
} catch (URISyntaxException ex) { return true ; }
}
}
/*
* (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 + -