public static ArrayList<RdfModel> processRDF(InputStream in) { Model model = ModelFactory.createDefaultModel(); ArrayList<RdfModel> result = new ArrayList<RdfModel>(); if (in != null) { model.read(in, "RDF/XML"); // Now, I only care these propties: has-title, year-of, full-name. All three of them must // exist' for (final ResIterator it = model.listSubjectsWithProperty(RdfPropertyList.p_hasTitle); it.hasNext(); ) { RdfModel rm = new RdfModel(); try { final Resource node = it.next().asResource(); // node is a resource which has title property rm.setHasTitle(node.getProperty(RdfPropertyList.p_hasTitle).getString()); StringBuilder authors = new StringBuilder(); StringBuilder dates = new StringBuilder(); for (final StmtIterator all_props = node.listProperties(); all_props.hasNext(); ) { try { Resource all_res = all_props.next().getObject().asResource(); StmtIterator fullnames = all_res.listProperties(RdfPropertyList.p_fullName); StmtIterator years = all_res.listProperties(RdfPropertyList.p_year); // Just for now I may have mutiple author or dates in a String, seperated by comma RdfProcess newprocess = new RdfProcess(); while (fullnames.hasNext()) { String fullname = newprocess.getValue(fullnames.next().getObject()); if (!fullname.equals("Invalid/Lack of Information")) { authors.append(fullname + " , "); } } while (years.hasNext()) { String year = newprocess.getValue(years.next().getObject()); if (!year.equals("Invalid/Lack of Information")) { dates.append(year + " , "); } } } catch (Exception e) { } } rm.setHasDate(dates.toString()); rm.setHasAuthor(authors.toString()); } catch (Exception e) { } result.add(rm); } } return result; }
private Model JoinMap(Model map) { Model newMap = ModelFactory.createDefaultModel(); StmtIterator stmts = map.listStatements(); while (stmts.hasNext()) { Statement s = stmts.next(); if (s.getSubject().isURIResource() || s.getObject().isURIResource()) { s = newMap.createStatement( checkUri(s.getSubject().asNode(), newMap).asResource(), s.getPredicate(), checkUri(s.getObject().asNode(), newMap)); } newMap.add(s); } return newMap; }