KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > examples > rdf > Tutorial10


1 /*
2  * (c) Copyright 2003, 2004, Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  * $Id: Tutorial10.java,v 1.1 2005/03/12 13:52:28 andy_seaborne Exp $
6  */

7 package jena.examples.rdf ;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.util.FileManager;
11 import com.hp.hpl.jena.vocabulary.*;
12
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16
17 /** Tutorial 10 - demonstrate a container
18  *
19  * @author bwm - updated by kers/Daniel
20  * @version Release='$Name: $' Revision='$Revision: 1.1 $' Date='$Date: 2005/03/12 13:52:28 $'
21  */

22 public class Tutorial10 extends Object JavaDoc {
23     
24     static final String JavaDoc inputFileName = "vc-db-1.rdf";
25     
26     public static void main (String JavaDoc args[]) {
27         // create an empty model
28
Model model = ModelFactory.createDefaultModel();
29        
30         // use the class loader to find the input file
31
InputStream JavaDoc in = FileManager.get().open( inputFileName );
32         if (in == null) {
33             throw new IllegalArgumentException JavaDoc( "File: " + inputFileName + " not found");
34         }
35         
36         // read the RDF/XML file
37
model.read(new InputStreamReader JavaDoc(in), "");
38         
39         // create a bag
40
Bag smiths = model.createBag();
41         
42         // select all the resources with a VCARD.FN property
43
// whose value ends with "Smith"
44
StmtIterator iter = model.listStatements(
45             new
46                 SimpleSelector(null, VCARD.FN, (RDFNode) null) {
47                     public boolean selects(Statement s) {
48                             return s.getString().endsWith("Smith");
49                     }
50                 });
51         // add the Smith's to the bag
52
while (iter.hasNext()) {
53             smiths.add( iter.nextStatement().getSubject());
54         }
55         
56         // print the graph as RDF/XML
57
model.write(new PrintWriter JavaDoc(System.out));
58         System.out.println();
59         
60         // print out the members of the bag
61
NodeIterator iter2 = smiths.iterator();
62         if (iter2.hasNext()) {
63             System.out.println("The bag contains:");
64             while (iter2.hasNext()) {
65                 System.out.println(" " +
66                     ((Resource) iter2.next())
67                                      .getRequiredProperty(VCARD.FN)
68                                      .getString());
69             }
70         } else {
71             System.out.println("The bag is empty");
72         }
73     }
74 }
75
76 /*
77  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
78  * All rights reserved.
79  *
80  * Redistribution and use in source and binary forms, with or without
81  * modification, are permitted provided that the following conditions
82  * are met:
83  * 1. Redistributions of source code must retain the above copyright
84  * notice, this list of conditions and the following disclaimer.
85  * 2. Redistributions in binary form must reproduce the above copyright
86  * notice, this list of conditions and the following disclaimer in the
87  * documentation and/or other materials provided with the distribution.
88  * 3. The name of the author may not be used to endorse or promote products
89  * derived from this software without specific prior written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
92  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
93  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
94  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
95  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
96  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
97  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
98  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
99  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
100  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101  */

102
Popular Tags