📄 ntriplesparser.java
字号:
else if (c != '.') { _throwParseException("Expected '.', found: " + (char)c); } c = _skipLine(c); _statementHandler.handleStatement(_subject, _predicate, _object); _subject = null; _predicate = null; _object = null; return c; } private int _parseSubject(int c) throws IOException, ParseException { StringBuffer buf = new StringBuffer(100); // subject is either an uriref (<foo://bar>) or a nodeID (_:node1) if (c == '<') { // subject is an uriref c = _parseUriRef(c, buf); _subject = _createURI(buf.toString()); } else if (c == '_') { // subject is a bNode c = _parseNodeID(c, buf); _subject = _createBNode(buf.toString()); } else if (c == -1) { _throwEOFException(); } else { _throwParseException("Expected '<' or '_', found: " + (char)c); } return c; } private int _parsePredicate(int c) throws IOException, ParseException { StringBuffer buf = new StringBuffer(100); // predicate must be an uriref (<foo://bar>) if (c == '<') { // predicate is an uriref c = _parseUriRef(c, buf); _predicate = _createURI(buf.toString()); } else if (c == -1) { _throwEOFException(); } else { _throwParseException("Expected '<', found: " + (char)c); } return c; } private int _parseObject(int c) throws IOException, ParseException { StringBuffer buf = new StringBuffer(100); // object is either an uriref (<foo://bar>), a nodeID (_:node1) or a // literal ("foo"-en or "1"^^<xsd:integer>). if (c == '<') { // object is an uriref c = _parseUriRef(c, buf); _object = _createURI(buf.toString()); } else if (c == '_') { // object is a bNode c = _parseNodeID(c, buf); _object = _createBNode(buf.toString()); } else if (c == '"') { // object is a literal StringBuffer lang = new StringBuffer(8); StringBuffer datatype = new StringBuffer(40); c = _parseLiteral(c, buf, lang, datatype); _object = _createLiteral(buf.toString(), lang.toString(), datatype.toString()); } else if (c == -1) { _throwEOFException(); } else { _throwParseException("Expected '<', '_' or '\"', found: " + (char)c); } return c; } private int _parseUriRef(int c, StringBuffer uriRef) throws IOException, ParseException { // Supplied char is '<', ignore it. // Read up to the next '>' character c = _reader.read(); while (c != '>') { if (c == -1) { _throwEOFException(); } uriRef.append( (char)c ); c = _reader.read(); } // c == '>', read next char c = _reader.read(); return c; } private int _parseNodeID(int c, StringBuffer name) throws IOException, ParseException { // Supplied char is '_', ignore it. c = _reader.read(); if (c == -1) { _throwEOFException(); } else if (c != ':') { _throwParseException("Expected ':', found: " + (char)c); } c = _reader.read(); if (c == -1) { _throwEOFException(); } else if (!NTriplesUtil.isLetter(c)) { _throwParseException("Expected a letter, found: " + (char)c); } name.append( (char)c ); // Read all following letter and numbers, they are part of the name c = _reader.read(); while (c != -1 && NTriplesUtil.isLetterOrNumber(c)) { name.append( (char)c ); c = _reader.read(); } return c; } private int _parseLiteral( int c, StringBuffer value, StringBuffer lang, StringBuffer datatype) throws IOException, ParseException { // Supplied char is '"', ignore it. // Read up to the next '"' character c = _reader.read(); while (c != '"') { if (c == -1) { _throwEOFException(); } value.append( (char)c ); if (c == '\\') { // This escapes the next character, which might be a double quote c = _reader.read(); if (c == -1) { _throwEOFException(); } value.append( (char)c ); } c = _reader.read(); } // c == '"', read next char c = _reader.read(); if (c == '@') { // Read language c = _reader.read(); while (c != -1 && c != '.' && c != '^' && c != ' ' && c != '\t') { lang.append( (char)c ); c = _reader.read(); } } else if (c == '^') { // Read datatype c = _reader.read(); // c should be another '^' if (c == -1) { _throwEOFException(); } else if (c != '^') { _throwParseException("Expected '^', found: " + (char)c); } c = _reader.read(); // c should be a '<' if (c == -1) { _throwEOFException(); } else if (c != '<') { _throwParseException("Expected '<', found: " + (char)c); } c = _parseUriRef(c, datatype); } return c; } private URI _createURI(String uri) throws ParseException { try { uri = NTriplesUtil.unescapeString(uri); } catch (IllegalArgumentException e) { _throwParseException(e.getMessage()); } try { return _valFactory.createURI(uri); } catch (Exception e) { _throwParseException(e); return null; } } private BNode _createBNode(String nodeID) throws ParseException { // Maybe the node ID has been used before: BNode result = (BNode)_bNodeIdMap.get(nodeID); if (result == null) { // This is a new node ID, create a new BNode object for it try { if (_preserveBNodeIds) { result = _valFactory.createBNode(nodeID); } else { result = _valFactory.createBNode(); } } catch (Exception e) { _throwParseException(e); } // Remember it, the nodeID might occur again. _bNodeIdMap.put(nodeID, result); } return result; } private Literal _createLiteral(String label, String lang, String datatype) throws ParseException { try { label = NTriplesUtil.unescapeString(label); } catch (IllegalArgumentException e) { _throwParseException(e.getMessage()); } if (lang.length() == 0) { lang = null; } if (datatype.length() == 0) { datatype = null; } URI dtURI = null; if (datatype != null) { dtURI = _createURI(datatype); if (_datatypeHandling == DT_VERIFY) { if (!XmlDatatypeUtil.isValidValue(label, datatype)) { _throwParseException("'" + label + "' is not a valid value for datatype " + datatype); } } else if (_datatypeHandling == DT_NORMALIZE) { try { label = XmlDatatypeUtil.normalize(label, datatype); } catch (IllegalArgumentException e) { _throwParseException("'" + label + "' is not a valid value for datatype " + datatype + ": " + e.getMessage()); } } } try { if (dtURI != null) { return _valFactory.createLiteral(label, dtURI); } else if (lang != null) { return _valFactory.createLiteral(label, lang); } else { return _valFactory.createLiteral(label); } } catch (Exception e) { _throwParseException(e); return null; } } private void _throwParseException(String msg) throws ParseException { if (_errListener != null) { _errListener.fatalError(msg, _lineNo, -1); } throw new ParseException(msg, _lineNo, -1); } private void _throwParseException(Exception e) throws ParseException { if (e instanceof ParseException) { throw (ParseException)e; } else { if (_errListener != null) { _errListener.fatalError(e.getMessage(), _lineNo, -1); } throw new ParseException(e, _lineNo, -1); } } private void _throwEOFException() throws ParseException { _throwParseException("Unexpected end of file"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -