Skip to content

Commit

Permalink
SCUFL2-122 Include prefixes for rdf attributes
Browse files Browse the repository at this point in the history
git-svn-id: https://taverna.googlecode.com/svn/scufl2/trunk@15058 bf327186-88b3-11dd-a302-d386e5130c1c
  • Loading branch information
stian@mygrid.org.uk committed Jul 9, 2012
1 parent ac3e38b commit fdd2655
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import uk.org.taverna.scufl2.api.property.PropertyResource.PropertyVisit;

public class PropertyResourceSerialiser extends VisitorWithPath {
public static final String ABOUT = "about";
public static final String DESCRIPTION = "Description";
public static final String RESOURCE = "resource";
public static final String LITERAL = "Literal";
Expand All @@ -30,6 +31,15 @@ public class PropertyResourceSerialiser extends VisitorWithPath {
public static final String PARSE_TYPE = "parseType";
public static final String COLLECTION = "Collection";
public static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

public static final String RDF_ = "rdf:";
public static final String RDF_ABOUT = RDF_ + ABOUT;
public static final String RDF_DESCRIPTION = RDF_ + DESCRIPTION ;
public static final String RDF_DATATYPE = RDF_ + DATATYPE;
public static final String RDF_LI = RDF_ + LI;
public static final String RDF_PARSE_TYPE = RDF_ + PARSE_TYPE;
private static final String RDF_RESOURCE = RDF_ + RESOURCE;

protected Stack<Element> elementStack = new Stack<Element>();
protected DocumentBuilder docBuilder;
protected Document doc;
Expand Down Expand Up @@ -96,19 +106,19 @@ public Element getRootElement() {

protected void list(PropertyList node) {
Element element = elementStack.peek();
element.setAttributeNS(RDF, PARSE_TYPE, COLLECTION);
element.setAttributeNS(RDF, RDF_PARSE_TYPE, COLLECTION);
}

protected void literal(PropertyLiteral node) {
Element element = elementStack.peek();
if (node.getLiteralType().equals(PropertyLiteral.XML_LITERAL)) {
Element nodeElement = node.getLiteralValueAsElement();
element.appendChild(doc.importNode(nodeElement, true));
element.setAttributeNS(RDF, PARSE_TYPE, LITERAL);
element.setAttributeNS(RDF, RDF_PARSE_TYPE, LITERAL);
} else {
element.setTextContent(node.getLiteralValue());
if (!node.getLiteralType().equals(PropertyLiteral.XSD_STRING)) {
element.setAttributeNS(RDF, DATATYPE, node.getLiteralType()
element.setAttributeNS(RDF, RDF_DATATYPE, node.getLiteralType()
.toASCIIString());
}
}
Expand All @@ -121,7 +131,7 @@ protected void property(PropertyVisit node) {

protected void reference(PropertyReference node) {
Element element = elementStack.peek();
element.setAttributeNS(RDF, RESOURCE, node.getResourceURI()
element.setAttributeNS(RDF, RDF_RESOURCE, node.getResourceURI()
.toASCIIString());
}

Expand All @@ -132,10 +142,10 @@ protected void resource(PropertyResource node) {
element = uriToElement(typeUri);
} else {
// Anonymous - give warning?
element = doc.createElementNS(RDF, DESCRIPTION);
element = doc.createElementNS(RDF, RDF_DESCRIPTION);
}
if (node.getResourceURI() != null) {
element.setAttributeNS(RDF, "about", node.getResourceURI()
element.setAttributeNS(RDF, RDF_ABOUT, node.getResourceURI()
.toASCIIString());
}
addElement(element);
Expand Down Expand Up @@ -182,7 +192,7 @@ public boolean visit() {
Element element = uriToElement(propertyVisit.getPredicateUri());
addElement(element);
} else if (parent instanceof PropertyList) {
addElement(doc.createElementNS(RDF, LI));
addElement(doc.createElementNS(RDF, RDF_LI));
}
}
if (node instanceof PropertyList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import uk.org.taverna.scufl2.api.property.PropertyList;
import uk.org.taverna.scufl2.api.property.PropertyLiteral;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void propertyAnonymous() throws Exception {

propResource.accept(serialiser);
Element elem = serialiser.getRootElement();
assertEquals("Description", elem.getTagName());
assertEquals("rdf:Description", elem.getTagName());
assertEquals(PropertyResourceSerialiser.RDF, elem.getNamespaceURI());
assertEquals(0, elem.getAttributes().getLength());
assertEquals(0, elem.getChildNodes().getLength());
Expand All @@ -142,7 +143,7 @@ public void propertyAnonymousWithAbout() throws Exception {

propResource.accept(serialiser);
Element elem = serialiser.getRootElement();
assertEquals("Description", elem.getTagName());
assertEquals("rdf:Description", elem.getTagName());
assertEquals(PropertyResourceSerialiser.RDF, elem.getNamespaceURI());
assertEquals(1, elem.getAttributes().getLength());
assertEquals(0, elem.getChildNodes().getLength());
Expand Down Expand Up @@ -234,6 +235,45 @@ public void literalInteger() throws Exception {
assertEquals(1, elem.getAttributes().getLength());
assertEquals(1, elem.getChildNodes().getLength());
}



@Test
public void literalIntegerInList() throws Exception {

PropertyList propList = new PropertyList();
propResource.addProperty(property.resolve("#list"), propList);

PropertyLiteral literal = new PropertyLiteral(1337);
propList.add(literal);

propResource.accept(serialiser);
Element elem = serialiser.getRootElement();
elem = (Element) elem.getChildNodes().item(0);
assertEquals("list", elem.getTagName());
assertEquals("http://example.com/property#", elem.getNamespaceURI());
assertEquals(1, elem.getAttributes().getLength());
Attr collection = elem.getAttributeNodeNS(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#", "parseType");
assertEquals("Collection", collection.getValue());
assertEquals("rdf", collection.getPrefix());

assertEquals(1, elem.getChildNodes().getLength());
Element liElem = (Element) elem.getChildNodes().item(0);
assertEquals("li", liElem.getLocalName());
assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#", liElem.getNamespaceURI());
assertEquals("rdf", liElem.getPrefix());
assertEquals("rdf:li", liElem.getTagName());
System.out.println(liElem.getAttributes().item(0).getPrefix());
assertEquals("1337", elem.getTextContent());
Attr datatype = liElem.getAttributeNodeNS(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#", "datatype");
assertEquals("rdf", datatype.getPrefix());
assertEquals(PropertyLiteral.XSD_INT.toASCIIString(), datatype.getValue());
assertEquals(1, liElem.getAttributes().getLength());
assertEquals(1, liElem.getChildNodes().getLength());
}


@Test
public void literalDate() throws Exception {
Expand Down

0 comments on commit fdd2655

Please sign in to comment.