KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > DBProp


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4 */

5
6 package com.hp.hpl.jena.db.impl;
7
8 import java.net.UnknownHostException JavaDoc;
9 import java.rmi.server.UID JavaDoc;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.util.iterator.*;
13 import com.hp.hpl.jena.vocabulary.DB;
14
15
16 /**
17  *
18  * A base class for DB property information in a persistent store.
19  *
20  * This is written in the style of enhanced nodes - no state is
21  * stored in the DBStoreDesc, instead all state is in the
22  * underlying graph and this is just provided as a convenience.
23  *
24  * (We don't use enhanced nodes because, since we control everything
25  * in the persistent store system description, we can avoid any
26  * need to handle polymorhphism).
27  *
28  *
29  * @author csayers
30  * @version $Revision: 1.15 $
31  */

32 public abstract class DBProp {
33
34     protected SpecializedGraph graph = null;
35     protected Node self = null;
36     
37     public DBProp( SpecializedGraph g) {
38         graph = g;
39         self = generateNodeURI();
40     }
41     
42     public DBProp( SpecializedGraph g, Node n) {
43         graph = g;
44         self = n;
45     }
46     
47     public Node getNode() { return self; }
48     
49     /**
50         Utility method for creating a CompletionFlag; its value lies in having a short name!
51         @return a new SpecializedGraph.CompletionFlag() set to <code>false</code>.
52     */

53     protected static SpecializedGraph.CompletionFlag newComplete()
54         { return new SpecializedGraph.CompletionFlag(); }
55     
56     protected void putPropString( Node_URI predicate, String JavaDoc value) {
57         putPropNode( predicate, Node.createLiteral( value ) );
58     }
59     
60     protected void putPropNode( Node_URI predicate, Node node) {
61         graph.add( Triple.create( self, predicate, node ), newComplete() );
62     }
63     
64     protected String JavaDoc getPropString( Node_URI predicate) {
65         ClosableIterator it = graph.find(self, predicate, null, newComplete() );
66         if( !it.hasNext() ) {
67             it.close();
68             return null;
69         }
70         Node result = ((Triple)it.next()).getObject();
71         it.close();
72         return result.getLiteral().getLexicalForm();
73     }
74     
75     protected void remove() {
76         SpecializedGraph.CompletionFlag complete = newComplete();
77         ClosableIterator it = graph.find( self, null, null, complete);
78         while( it.hasNext() ) graph.delete( (Triple) it.next(), complete );
79         it.close();
80         self = null;
81         graph = null;
82     }
83     
84     void showGraph()
85         {
86         SpecializedGraph.CompletionFlag complete = newComplete();
87         ExtendedIterator it = graph.find( self, null, null, complete );
88         while (it.hasNext()) System.err.println( ">> " + it.next() );
89         }
90     
91     public static ExtendedIterator listTriples( SpecializedGraph g, Node self ) {
92         // Get all the triples about the requested node.
93
return g.find( self, null, null, newComplete() );
94     }
95         
96     protected static Node findProperty( Graph graph, Node_URI predicate ) {
97         ClosableIterator it = graph.find( null, predicate, null );
98         Node result = null;
99         if( it.hasNext() ) result = ((Triple) it.next()).getObject();
100         it.close();
101         return result;
102     }
103
104     public static String JavaDoc generateUniqueID() {
105         UID JavaDoc uid = new UID JavaDoc();
106         String JavaDoc hostname;
107         try {
108             hostname = java.net.InetAddress.getLocalHost().getHostAddress().toString();
109         } catch (UnknownHostException JavaDoc e) {
110             hostname = "localhost";
111         }
112         return (hostname + uid.toString()).replace('.','_').replace(':','_').replace('-','_');
113     }
114
115     public static Node generateNodeURI() {
116         String JavaDoc generateUniqueID = null;
117         return Node.createURI( DB.uri + generateUniqueID() );
118     }
119     
120
121 }
122
123 /*
124  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
125  * All rights reserved.
126  *
127  * Redistribution and use in source and binary forms, with or without
128  * modification, are permitted provided that the following conditions
129  * are met:
130  * 1. Redistributions of source code must retain the above copyright
131  * notice, this list of conditions and the following disclaimer.
132  * 2. Redistributions in binary form must reproduce the above copyright
133  * notice, this list of conditions and the following disclaimer in the
134  * documentation and/or other materials provided with the distribution.
135  * 3. The name of the author may not be used to endorse or promote products
136  * derived from this software without specific prior written permission.
137
138  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
139  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
140  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
141  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
142  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
143  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
144  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
145  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
146  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
147  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
148  */
Popular Tags