📄 parserbase.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.turtle;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.Triple ;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.datatypes.xsd.* ;
import com.hp.hpl.jena.n3.RelURI;
import com.hp.hpl.jena.n3.RelURI.JenaURIException;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
import com.hp.hpl.jena.vocabulary.RDF;
public class ParserBase
{
protected final Node XSD_TRUE = Node.createLiteral("true", null, XSDDatatype.XSDboolean) ;
protected final Node XSD_FALSE = Node.createLiteral("false", null, XSDDatatype.XSDboolean) ;
protected final Node nRDFtype = RDF.type.asNode() ;
protected final Node nRDFnil = RDF.nil.asNode() ;
protected final Node nRDFfirst = RDF.first.asNode() ;
protected final Node nRDFrest = RDF.rest.asNode() ;
protected final Node nRDFsubject = RDF.subject.asNode() ;
protected final Node nRDFpredicate = RDF.predicate.asNode() ;
protected final Node nRDFobject = RDF.object.asNode() ;
protected final String SWAP_NS = "http://www.w3.org/2000/10/swap/" ;
protected final String SWAP_LOG_NS = "http://www.w3.org/2000/10/swap/log#" ;
protected final Node nLogImplies = Node.createURI(SWAP_LOG_NS+"implies") ;
protected boolean strictTurtle = true ;
protected String baseURI = null ;
protected String getBaseURI() { return baseURI ; }
public void setBaseURI(String u) { baseURI = u ; }
protected PrefixMapping pmap = new PrefixMappingImpl() ;
public PrefixMapping getPrefixMapping() { return pmap ; }
// label => bNode for construct templates patterns
LabelToNodeMap bNodeLabels = new LabelToNodeMap() ;
TripleHandler handler = null ;
public void setTripleHandler(TripleHandler h) { handler = h ; }
protected void emitTriple(int line, int col, Triple triple)
{
handler.triple(line, col, triple) ;
}
protected void startFormula(int line, int col)
{ handler.startFormula(line, col) ; }
protected void endFormula(int line, int col)
{handler.endFormula(line, col) ; }
protected void setPrefix(String prefix, String uri)
{
pmap.setNsPrefix(prefix, uri) ;
}
protected int makePositiveInteger(String lexicalForm)
{
if ( lexicalForm == null )
return -1 ;
return Integer.parseInt(lexicalForm) ;
}
protected Node makeNodeInteger(String lexicalForm)
{
return Node.createLiteral(lexicalForm, null, XSDDatatype.XSDinteger) ;
}
protected Node makeNodeDouble(String lexicalForm)
{
return Node.createLiteral(lexicalForm, null, XSDDatatype.XSDdouble) ;
}
protected Node makeNodeDecimal(String lexicalForm)
{
return Node.createLiteral(lexicalForm, null, XSDDatatype.XSDdecimal) ;
}
protected Node makeNode(String lexicalForm, String langTag, Node datatype)
{
String uri = (datatype==null) ? null : datatype.getURI() ;
return makeNode(lexicalForm, langTag, uri) ;
}
protected Node makeNode(String lexicalForm, String langTag, String datatypeURI)
{
Node n = null ;
// Can't have type and lang tag.
if ( datatypeURI != null)
{
RDFDatatype dType = TypeMapper.getInstance().getSafeTypeByName(datatypeURI) ;
n = Node.createLiteral(lexicalForm, null, dType) ;
}
else
n = Node.createLiteral(lexicalForm, langTag, null) ;
return n ;
}
protected long integerValue(String s)
{
if ( s.startsWith("+") )
s = s.substring(1) ;
if ( s.startsWith("0x") )
{
// Hex
s = s.substring(2) ;
return Long.parseLong(s, 16) ;
}
return Long.parseLong(s) ;
}
protected double doubleValue(String s)
{
if ( s.startsWith("+") )
s = s.substring(1) ;
double valDouble = Double.parseDouble(s) ;
return valDouble ;
}
protected String stripQuotes(String s)
{
return s.substring(1,s.length()-1) ;
}
protected String stripQuotes3(String s)
{
return s.substring(3,s.length()-3) ;
}
protected String stripChars(String s, int n)
{
return s.substring(n, s.length()) ;
}
final static String bNodeLabelStart = "_:" ;
protected Node createVariable(String s, int line, int column)
{
s = s.substring(1) ; // Drop the marker
return Node.createVariable(s) ;
}
protected Node createURIfromQName(String s, int line, int column)
{
s = fixupQName(s, line, column) ;
return Node.createURI(s) ;
}
protected Node createNodeFromURI(String s, int line, int column)
{
s = stripQuotes(s) ;
String uriStr = s ; // Mutated
try {
uriStr = RelURI.resolve(uriStr, getBaseURI()) ;
} catch (JenaURIException ex)
{
throw new TurtleParseException(exMsg(ex.getMessage(), line, column)) ;
}
return Node.createURI(uriStr) ;
}
protected void raiseException(String s , int line, int column)
{
throw new TurtleParseException(exMsg(s, line, column)) ;
}
// Unlabelled bNode.
protected Node createBNode() { return bNodeLabels.allocNode() ; }
// Labelled bNode.
protected Node createBNode(String label, int line, int column)
{
return bNodeLabels.asNode(label) ;
}
protected String fixupQName(String qname, int line, int column)
{
String s = this.getPrefixMapping().expandPrefix(qname) ;
if ( s.equals(qname) )
{
String msg = "Line " + line + ", column " + column;
throw new TurtleParseException(msg+": Unresolved qname: "+qname) ;
}
return s ;
}
protected String fixupPrefix(String prefix, int line, int column)
{
if ( prefix.endsWith(":") )
prefix = prefix.substring(0, prefix.length()-1) ;
return prefix ;
}
// Utilities to remove escapes
// Testing interface
public static String unescapeStr(String s)
{ return unescape(s, '\\', false, 1, 1) ; }
// public static String unescapeCodePoint(String s)
// { return unescape(s, '\\', true, 1, 1) ; }
//
// protected String unescapeCodePoint(String s, int line, int column)
// { return unescape(s, '\\', true, line, column) ; }
protected String unescapeStr(String s, int line, int column)
{ return unescape(s, '\\', false, line, column) ; }
// Worker function
private static String unescape(String s, char escape, boolean pointCodeOnly, int line, int column)
{
int i = s.indexOf(escape) ;
if ( i == -1 )
return s ;
// Dump the initial part straight into the string buffer
StringBuffer sb = new StringBuffer(s.substring(0,i)) ;
for ( ; i < s.length() ; i++ )
{
char ch = s.charAt(i) ;
// Keep line and column numbers.
switch (ch)
{
case '\n':
case '\r':
line++ ;
column = 1 ;
break ;
default:
column++ ;
break ;
}
if ( ch != escape )
{
sb.append(ch) ;
continue ;
}
// Escape
if ( i >= s.length()-1 )
throw new TurtleParseException(exMsg("Illegal escape at end of string", line, column)) ;
char ch2 = s.charAt(i+1) ;
column = column+1 ;
i = i + 1 ;
// \\u and \\U
if ( ch2 == 'u' )
{
// i points to the \ so i+6 is next character
if ( i+4 >= s.length() )
throw new TurtleParseException(exMsg("\\u escape too short", line, column)) ;
int x = hex(s, i+1, 4, line, column) ;
sb.append((char)x) ;
// Jump 1 2 3 4 -- already skipped \ and u
i = i+4 ;
column = column+4 ;
continue ;
}
if ( ch2 == 'U' )
{
// i points to the \ so i+6 is next character
if ( i+8 >= s.length() )
throw new TurtleParseException(exMsg("\\U escape too short", line, column)) ;
int x = hex(s, i+1, 8, line, column) ;
sb.append((char)x) ;
// Jump 1 2 3 4 5 6 7 8 -- already skipped \ and u
i = i+8 ;
column = column+8 ;
continue ;
}
// Are we doing just point code escapes?
// If so, \X-anything else is legal as a literal "\" and "X"
if ( pointCodeOnly )
{
sb.append('\\') ;
sb.append(ch2) ;
i = i + 1 ;
continue ;
}
// Not just codepoints. Must be a legal escape.
char ch3 = 0 ;
switch (ch2)
{
case 'n': ch3 = '\n' ; break ;
case 't': ch3 = '\t' ; break ;
case 'r': ch3 = '\r' ; break ;
case 'b': ch3 = '\b' ; break ;
case 'f': ch3 = '\f' ; break ;
case '\'': ch3 = '\'' ; break ;
case '\"': ch3 = '\"' ; break ;
case '\\': ch3 = '\\' ; break ;
default:
throw new TurtleParseException(exMsg("Unknown escape: \\"+ch2, line, column)) ;
}
sb.append(ch3) ;
}
return sb.toString() ;
}
// Line and column that started the escape
static private int hex(String s, int i, int len, int line, int column)
{
if ( i+len >= s.length() )
{
}
int x = 0 ;
for ( int j = i ; j < i+len ; j++ )
{
char ch = s.charAt(j) ;
column++ ;
int k = 0 ;
switch (ch)
{
case '0': k = 0 ; break ;
case '1': k = 1 ; break ;
case '2': k = 2 ; break ;
case '3': k = 3 ; break ;
case '4': k = 4 ; break ;
case '5': k = 5 ; break ;
case '6': k = 6 ; break ;
case '7': k = 7 ; break ;
case '8': k = 8 ; break ;
case '9': k = 9 ; break ;
case 'A': case 'a': k = 10 ; break ;
case 'B': case 'b': k = 11 ; break ;
case 'C': case 'c': k = 12 ; break ;
case 'D': case 'd': k = 13 ; break ;
case 'E': case 'e': k = 14 ; break ;
case 'F': case 'f': k = 15 ; break ;
default:
throw new TurtleParseException(exMsg("Illegal hex escape: "+ch, line, column)) ;
}
x = (x<<4)+k ;
}
return x ;
}
protected static String exMsg(String msg, int line, int column)
{
return "Line " + line + ", column " + column + ": " + msg ;
}
}
/*
* (c) Copyright 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 + -