📄 rdfxmlparser.java
字号:
if ( ((NodeElement)topElement).isVolatile() ) { _elementStack.pop(); } } else { // topElement instanceof PropertyElement PropertyElement predicate = (PropertyElement)topElement; if (predicate.parseCollection()) { Resource lastListResource = predicate.getLastListResource(); if (lastListResource == null) { // no last list resource, list must have been empty. NodeElement subject = (NodeElement)_peekStack(1); _reportStatement( subject.getResource(), predicate.getURI(), RDF_NIL); _handleReification(RDF_NIL); } else { // Generate the final tail of the list. _reportStatement(lastListResource, RDF_REST, RDF_NIL); } } } _elementStack.pop(); } void emptyElement(String namespaceURI, String localName, String qName, Atts atts) throws SAXException { if (_topIsProperty()) { // this element represents the subject and/or object of a statement _processNodeElt(namespaceURI, localName, qName, atts, true); } else { // this element represents a property _processPropertyElt(namespaceURI, localName, qName, atts, true); } } void text(String text) throws SAXException { if (!_topIsProperty()) { throw new SAXException("unexpected literal"); } PropertyElement propEl = (PropertyElement)_peekStack(0); String datatype = propEl.getDatatype(); Literal lit = _createLiteral(text, _xmlLang, datatype); NodeElement subject = (NodeElement)_peekStack(1); PropertyElement predicate = (PropertyElement)_peekStack(0); _reportStatement(subject.getResource(), predicate.getURI(), lit); _handleReification(lit); }/*------------------------------------------------------+| RDF processing methods |+------------------------------------------------------*/ /* Process a node element (can be both subject and object) */ private void _processNodeElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws SAXException { if (_verifyData) { // Check the element name _checkNodeEltName(namespaceURI, localName, qName); } Resource nodeResource = _getNodeResource(atts); NodeElement nodeElement = new NodeElement(nodeResource); if (!_elementStack.isEmpty()) { // node can be object of a statement, or part of an rdf:List NodeElement subject = (NodeElement)_peekStack(1); PropertyElement predicate = (PropertyElement)_peekStack(0); if (predicate.parseCollection()) { Resource lastListRes = predicate.getLastListResource(); BNode newListRes = _createBNode(); if (lastListRes == null) { // first element in the list _reportStatement(subject.getResource(), predicate.getURI(), newListRes); _handleReification(newListRes); } else { // not the first element in the list _reportStatement(lastListRes, RDF_REST, newListRes); } _reportStatement(newListRes, RDF_FIRST, nodeResource); predicate.setLastListResource(newListRes); } else { _reportStatement( subject.getResource(), predicate.getURI(), nodeResource); _handleReification(nodeResource); } } if (!localName.equals("Description") || !namespaceURI.equals(RDF.NAMESPACE)) { // element name is uri's type URI className = null; if ("".equals(namespaceURI)) { // No namespace, use base URI className = _buildResourceFromLocalName(localName); } else { className = _createURI( namespaceURI + localName ); } _reportStatement(nodeResource, RDF_TYPE, className); } Att type = atts.removeAtt(RDF.NAMESPACE, "type"); if (type != null) { // rdf:type attribute, value is a URI-reference URI className = _buildURIFromReference(type.getValue()); _reportStatement(nodeResource, RDF_TYPE, className); } if (_verifyData) { _checkRdfAtts(atts); } _processSubjectAtts(nodeElement, atts); if (!isEmptyElt) { _elementStack.push(nodeElement); } } /** * Retrieves the resource of a node element (subject or object) using * relevant attributes (rdf:ID, rdf:about and rdf:nodeID) from its * attributes list. * * @return a resource or a bNode. **/ private Resource _getNodeResource(Atts atts) throws SAXException { Att id = atts.removeAtt(RDF.NAMESPACE, "ID"); Att about = atts.removeAtt(RDF.NAMESPACE, "about"); Att nodeID = atts.removeAtt(RDF.NAMESPACE, "nodeID"); if (_verifyData) { int definedAttsCount = 0; if (id != null) { definedAttsCount++; } if (about != null) { definedAttsCount++; } if (nodeID != null) { definedAttsCount++; } if (definedAttsCount > 1) { sendError("Only one of the attributes rdf:ID, rdf:about or rdf:nodeID can be used here"); } } Resource result = null; if (id != null) { result = _buildURIFromID(id.getValue()); } else if (about != null) { result = _buildURIFromReference(about.getValue()); } else if (nodeID != null) { result = _createBNode(nodeID.getValue()); } else { // No resource specified, generate a bNode result = _createBNode(); } return result; } /** processes subject attributes. **/ private void _processSubjectAtts(NodeElement nodeElt, Atts atts) throws SAXException { Resource subject = nodeElt.getResource(); Iterator iter = atts.iterator(); while (iter.hasNext()) { Att att = (Att)iter.next(); URI predicate = _createURI( att.getURI() ); Literal lit = _createLiteral(att.getValue(), _xmlLang, null); _reportStatement(subject, predicate, lit); } } private void _processPropertyElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws SAXException { if (_verifyData) { _checkPropertyEltName(namespaceURI, localName, qName); } // Get the URI of the property URI propURI = null; if (namespaceURI.equals("")) { // no namespace URI sendError("unqualified property element <" + qName + "> not allowed"); // Use base URI as namespace: propURI = _buildResourceFromLocalName(localName); } else { propURI = _createURI( namespaceURI + localName ); } // List expansion rule if (propURI.equals(RDF_LI)) { NodeElement subject = (NodeElement)_peekStack(0); propURI = _createURI( RDF.NAMESPACE + "_" + subject.getNextLiCounter() ); } // Push the property on the stack. PropertyElement predicate = new PropertyElement(propURI); _elementStack.push(predicate); // Check if property has a reification ID Att id = atts.removeAtt(RDF.NAMESPACE, "ID"); if (id != null) { URI reifURI = _buildURIFromID(id.getValue()); predicate.setReificationURI(reifURI); } // Check for presence of rdf:parseType attribute Att parseType = atts.removeAtt(RDF.NAMESPACE, "parseType"); if (parseType != null) { if (_verifyData) { _checkNoMoreAtts(atts); } String parseTypeValue = parseType.getValue(); if (parseTypeValue.equals("Resource")) { BNode objectResource = _createBNode(); NodeElement subject = (NodeElement)_peekStack(1); _reportStatement( subject.getResource(), propURI, objectResource); if (isEmptyElt) { _handleReification(objectResource); } else { NodeElement object = new NodeElement(objectResource); object.setIsVolatile(true); _elementStack.push(object); } } else if (parseTypeValue.equals("Collection")) { if (isEmptyElt) { NodeElement subject = (NodeElement)_peekStack(1); _reportStatement(subject.getResource(), propURI, RDF_NIL); _handleReification(RDF_NIL); } else { predicate.setParseCollection(true); } } else { // other parseType if (!parseTypeValue.equals("Literal")) { sendWarning("unknown parseType: " + parseType.getValue()); } if (isEmptyElt) { NodeElement subject = (NodeElement)_peekStack(1); Literal lit = _createLiteral("", null, RDF.XMLLITERAL); _reportStatement(subject.getResource(), propURI, lit); _handleReification(lit); } else { // The next string is an rdf:XMLLiteral predicate.setDatatype(RDF.XMLLITERAL); _saxFilter.setParseLiteralMode(); } } } // parseType == null else if (isEmptyElt) { // empty element without an rdf:parseType attribute // Note: we handle rdf:datatype attributes here to allow datatyped // empty strings in documents. The current spec does have a // production rule that matches this, which is likely to be an // omission on its part. Att datatype = atts.getAtt(RDF.NAMESPACE, "datatype"); if (atts.size() == 0 || atts.size() == 1 && datatype != null) { // element had no attributes, or only the optional // rdf:ID and/or rdf:datatype attributes. NodeElement subject = (NodeElement)_peekStack(1); Literal lit = (datatype == null) ? _createLiteral("", _xmlLang, null) : _createLiteral("", null, datatype.getValue()); _reportStatement(subject.getResource(), propURI, lit); _handleReification(lit); } else { // Create resource for the statement's object. Resource resourceRes = _getPropertyResource(atts); // All special rdf attributes have been checked/removed. if (_verifyData) { _checkRdfAtts(atts); } NodeElement resourceElt = new NodeElement(resourceRes); NodeElement subject = (NodeElement)_peekStack(1); _reportStatement(subject.getResource(), propURI, resourceRes); _handleReification(resourceRes); Att type = atts.removeAtt(RDF.NAMESPACE, "type"); if (type != null) { // rdf:type attribute, value is a URI-reference URI className = _buildURIFromReference(type.getValue()); _reportStatement(resourceRes, RDF_TYPE, className); } _processSubjectAtts(resourceElt, atts); } // Empty element has been pushed on the stack already, remove it. _elementStack.pop(); } else { // Not an empty element, sub elements will follow. // Check for rdf:datatype attribute Att datatype = atts.removeAtt(RDF.NAMESPACE, "datatype"); if (datatype != null) { predicate.setDatatype(datatype.getValue()); } // No more attributes are expected. if (_verifyData) { _checkNoMoreAtts(atts); } } } /** * Retrieves the object resource of a property element using relevant attributes * (rdf:resource and rdf:nodeID) from its attributes list. * * @return a resource or a bNode. **/ private Resource _getPropertyResource(Atts atts) throws SAXException { Att resource = atts.removeAtt(RDF.NAMESPACE, "resource"); Att nodeID = atts.removeAtt(RDF.NAMESPACE, "nodeID"); if (_verifyData) { int definedAttsCount = 0; if (resource != null) { definedAttsCount++; } if (nodeID != null) { definedAttsCount++; } if (definedAttsCount > 1) { sendError("Only one of the attributes rdf:resource or rdf:nodeID can be used here"); } } Resource result = null; if (resource != null) { result = _buildURIFromReference(resource.getValue()); } else if (nodeID != null) { result = _createBNode(nodeID.getValue()); } else { // No resource specified, generate a bNode result = _createBNode(); } return result; } /* * Processes any rdf:ID attributes that generate reified statements. This * method assumes that a PropertyElement (which can have an rdf:ID * attribute) is on top of the stack, and a NodeElement is below that. */ private void _handleReification(Value value) throws SAXException { PropertyElement predicate = (PropertyElement)_peekStack(0); if (predicate.isReified()) { NodeElement subject = (NodeElement)_peekStack(1); URI reifRes = predicate.getReificationURI(); _reifyStatement(reifRes, subject.getResource(), predicate.getURI(), value); } } private void _reifyStatement(Resource reifNode, Resource subj, URI pred, Value obj) throws SAXException { _reportStatement(reifNode, RDF_TYPE, RDF_STATEMENT); _reportStatement(reifNode, RDF_SUBJECT, subj); _reportStatement(reifNode, RDF_PREDICATE, pred); _reportStatement(reifNode, RDF_OBJECT, obj); } /** * Builds a Resource from a non-qualified localname. **/ private URI _buildResourceFromLocalName(String localName) throws SAXException { // Resolve the relative URI against the base URI String uriString = _baseURI.resolve("#" + localName).toString(); return _createURI(uriString); } /** * Builds a Resource from the value of an rdf:ID attribute. **/ private URI _buildURIFromID(String id) throws SAXException { if (_verifyData) { // Check if 'id' is a legal NCName if (!XmlUtil.isNCName(id)) { sendError("Not an XML Name: " + id); } } // Resolve the relative URI against the base URI String uriString = _baseURI.resolve("#" + id).toString(); if (_verifyData) { // uriString should be unique in the current document if (!_usedIDs.add(uriString)) { // uriString was not added because the set already contained // an equal string. sendError("'" + id + "' already used as ID value, values of rdf:ID attributes should be unique"); } } return _createURI(uriString); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -