Java: List cleanup
[smartapi.git] / Common / Java / SmartAPI / src / smartapi / rdf / ItemizedList.java
1 package smartapi.rdf;
2
3 import org.apache.jena.datatypes.xsd.XSDDatatype;
4 import org.apache.jena.rdf.model.Model;
5 import org.apache.jena.rdf.model.Property;
6 import org.apache.jena.rdf.model.RDFNode;
7 import org.apache.jena.rdf.model.Resource;
8 import org.apache.jena.rdf.model.Statement;
9 import org.apache.jena.rdf.model.StmtIterator;
10
11 import smartapi.common.PROPERTY;
12 import smartapi.common.Tools;
13 import smartapi.model.Obj;
14
15 public class ItemizedList extends List {
16
17         public ItemizedList()
18         {
19                 super();
20         }
21
22         public ItemizedList(String identifierUri)
23         {
24                 super(identifierUri);
25         }
26
27         public Resource getSerialization(Model model)
28         {
29                 Resource resource = super.getSerialization(model);
30                 Resource first = model.createResource();
31
32                 Property entriesProp = model.createProperty( PROPERTY.getUri(PROPERTY.ENTRIES) );
33                 Property firstProp = model.createProperty( PROPERTY.getUri(PROPERTY.FIRST) );   
34                 Property valueProp = model.createProperty( PROPERTY.getUri(PROPERTY.RDF_VALUE) );
35                 first.addProperty(model.createProperty( PROPERTY.getUri(PROPERTY.SIZE) ), model.createTypedLiteral(this.getItems().size(), XSDDatatype.XSDinteger));
36                 Resource arrayResource = model.createResource();
37                 for( int i = 0; i < this.list.size(); i++ ) {
38                         Object o = this.list.get(i);
39                         if ( o instanceof Obj ) {
40                                 arrayResource.addProperty(valueProp, ((Obj)o).serialize(model));
41                         }
42                         if ( o instanceof Variant ) {
43                                 arrayResource.addProperty(valueProp, ((Variant)o).serialize(model));
44                         }
45                 }
46                 first.addProperty(model.createProperty( PROPERTY.getUri(PROPERTY.ARRAY) ), arrayResource);
47                 resource.addProperty(entriesProp, first);
48                 return resource;
49         }
50
51         public static ItemizedList parse(Resource resource)
52         {
53                 ItemizedList list;
54                 if ( !resource.isAnon() )
55                         list = new ItemizedList(resource.toString());
56                 else
57                         list = new ItemizedList();
58
59                 list.clearTypes();
60                 StmtIterator i = resource.listProperties();
61                 while( i.hasNext() ) {
62                         Statement statement = i.next();
63                         // parse statement
64                         list.parse(statement);
65                 }
66                 return list;
67         }
68
69         public void parse(Statement statement)
70         {
71                 // get predicate
72                 String predicate = statement.getPredicate().toString();
73                 int propertyIndex = PROPERTY.add(statement.getPredicate().toString());
74
75                 if ( propertyIndex == PROPERTY.ENTRIES ) {
76                         try {
77                                 Resource first = statement.getResource();
78                                 Property sizeProp = first.getModel().createProperty( PROPERTY.getUri(PROPERTY.SIZE) );
79                                 Property arrayProp = first.getModel().createProperty( PROPERTY.getUri(PROPERTY.ARRAY) );
80                                 Property valueProp = first.getModel().createProperty( PROPERTY.getUri(PROPERTY.RDF_VALUE) );
81                                 
82                                 int size = -1;
83                                 Statement sizeStmt = first.getProperty(sizeProp);
84                                 if ( sizeStmt != null ) {
85                                         try {
86                                                 size = sizeStmt.getInt();
87                                         } catch ( Exception e ) {
88                                                 System.err.println("Unable to interpret itemized list size property value as integer.");
89                                                 e.printStackTrace();
90                                         }
91                                 }
92                                 
93                                 Resource array = first.getPropertyResourceValue(arrayProp);
94                                 if ( array == null ) {
95                                         System.err.println("Unable to find array while parsing object into an itemized list.");
96                                 } else {
97                                         StmtIterator i = array.listProperties(valueProp);
98                                         java.util.List<Statement> stmtList = i.toList();
99                                         if ( size != stmtList.size() ) {
100                                                 System.err.println("Error while parsing itemized list. List has " + stmtList.size() + " values, but the value of smartapi:size property is " + size + ".");
101                                         }
102                                         //Object[] values = new Object[stmtList.size()];
103                                         for ( Statement stmt : stmtList ) {
104                                                 RDFNode node = stmt.getObject();
105                                                 if ( node.isResource() ) {
106                                                         this.addItems(Tools.toObj(node.asResource()));
107                                                 }
108                                                 if ( node.isLiteral() ) {
109                                                         this.addItems(Variant.parse(stmt));
110                                                 }
111                                         }
112                                         return;
113                                 }
114
115                         } catch (Exception e) {
116                                 System.err.println("Unable to parse smartapi:list property value.");
117                                 e.printStackTrace();
118                         }
119                         return;
120                 }
121                 if ( propertyIndex != PROPERTY.REST ) {
122                         // pass on to Object
123                         super.parse(statement);
124                 }
125         }
126 }