KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Copyright 2003, 2004, Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  * $Id: Tutorial03.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.vocabulary.*;
11
12
13 /** Tutorial 3 Statement attribute accessor methods
14  *
15  * @author bwm - updated by kers/Daniel
16  * @version Release='$Name: $' Revision='$Revision: 1.1 $' Date='$Date: 2005/03/12 13:52:28 $'
17  */

18 public class Tutorial03 extends Object JavaDoc {
19     public static void main (String JavaDoc args[]) {
20     
21         // some definitions
22
String JavaDoc personURI = "http://somewhere/JohnSmith";
23         String JavaDoc givenName = "John";
24         String JavaDoc familyName = "Smith";
25         String JavaDoc fullName = givenName + " " + familyName;
26         // create an empty model
27
Model model = ModelFactory.createDefaultModel();
28
29         // create the resource
30
// and add the properties cascading style
31
Resource johnSmith
32           = model.createResource(personURI)
33                  .addProperty(VCARD.FN, fullName)
34                  .addProperty(VCARD.N,
35                               model.createResource()
36                                    .addProperty(VCARD.Given, givenName)
37                                    .addProperty(VCARD.Family, familyName));
38         
39         // list the statements in the graph
40
StmtIterator iter = model.listStatements();
41         
42         // print out the predicate, subject and object of each statement
43
while (iter.hasNext()) {
44             Statement stmt = iter.nextStatement(); // get next statement
45
Resource subject = stmt.getSubject(); // get the subject
46
Property predicate = stmt.getPredicate(); // get the predicate
47
RDFNode object = stmt.getObject(); // get the object
48

49             System.out.print(subject.toString());
50             System.out.print(" " + predicate.toString() + " ");
51             if (object instanceof Resource) {
52                 System.out.print(object.toString());
53             } else {
54                 // object is a literal
55
System.out.print(" \"" + object.toString() + "\"");
56             }
57             System.out.println(" .");
58         }
59     }
60 }
61
62 /*
63  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
64  * All rights reserved.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
69  * 1. Redistributions of source code must retain the above copyright
70  * notice, this list of conditions and the following disclaimer.
71  * 2. Redistributions in binary form must reproduce the above copyright
72  * notice, this list of conditions and the following disclaimer in the
73  * documentation and/or other materials provided with the distribution.
74  * 3. The name of the author may not be used to endorse or promote products
75  * derived from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
78  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
79  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
80  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
81  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
82  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
86  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87  */

88
Popular Tags