KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > ARPResource


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  
27  * * $Id: ARPResource.java,v 1.12 2005/02/21 12:08:59 andy_seaborne Exp $
28    
29    AUTHOR: Jeremy J. Carroll
30 */

31 /*
32  * ARPResource.java
33  *
34  * Created on June 25, 2001, 9:57 PM
35  */

36
37 package com.hp.hpl.jena.rdf.arp;
38
39
40
41 /**
42  *
43  * @author jjc
44  */

45 class ARPResource implements AResourceInternal {
46     // Constants cribbed from com.megginson.sax.rdf.RDFFilter
47
private final static String JavaDoc RDFNS =
48     "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
49     private final static int RDFNS_LENGTH = RDFNS.length();
50    /*
51     private final static String XMLNS =
52     "http://www.w3.org/XML/1998/namespace";
53 */

54
55                 // Element names
56
private static AResourceInternal RDF_STATEMENT ;
57     static URIReference RDF_BAG;
58     static URIReference RDF_SEQ ;
59     static URIReference RDF_ALT ;
60   // private static String RDF_LI ;
61
private static AResourceInternal RDF_TYPE ;
62     private static AResourceInternal RDF_SUBJECT ;
63     private static AResourceInternal RDF_PREDICATE ;
64     private static AResourceInternal RDF_OBJECT ;
65     static {
66         try {
67         RDF_STATEMENT =
68     new URIReference((RDFNS + "Statement").intern());
69     RDF_BAG =
70     new URIReference((RDFNS + "Bag").intern());
71     RDF_SEQ =
72     new URIReference((RDFNS + "Seq").intern());
73     RDF_ALT =
74     new URIReference((RDFNS + "Alt").intern());
75  // RDF_LI =
76
// (RDFNS + "li").intern();
77
RDF_TYPE =
78     new URIReference((RDFNS + "type").intern());
79     RDF_SUBJECT =
80     new URIReference((RDFNS + "subject").intern());
81     RDF_PREDICATE =
82     new URIReference((RDFNS + "predicate").intern());
83     RDF_OBJECT =
84     new URIReference((RDFNS + "object").intern());
85         }
86         catch ( MalformedURIException e ) {
87             System.err.println("Internal error: " + e.toString());
88             e.printStackTrace();
89         }
90     }
91     
92     private XMLHandler arp;
93     
94     String JavaDoc nodeID = null;
95     
96     // The uri is the document uri.
97
// If the uri is null then
98
// we are an anonymous resource , number genId.
99
private String JavaDoc uri; // maybe the
100
static private int genIdCounter = 0;
101     final private int genId = genIdCounter++;
102     private int liCounter = 1;
103     static String JavaDoc dummy = "http://jena.hpl.hp.com/arp/not/a/real/uri/";
104     static String JavaDoc nullDummy = "nullpointerexception://jena.hpl.hp.com/arp/";
105     
106     ARPResource(XMLHandler parent) {
107         arp = parent;
108     }
109     
110     
111     void setAbout(URIReference uri){
112         setAbout(uri.getURI());
113     }
114     
115     void setAbout(String JavaDoc uri){
116         this.uri = uri.startsWith( dummy ) ? uri.substring( dummy.length() ) : uri;
117     }
118     
119     void setLiObject(Object JavaDoc v, String JavaDoc reify) {
120         setPredObject( rdf_n(liCounter++), v, reify(reify) );
121     }
122   
123     void setPredicateObject(AResourceInternal p, ARPString v) {
124         setPredObject(p,v,null);
125     }
126     
127     void setPredicateObject(AResourceInternal p, Object JavaDoc v, String JavaDoc reify) {
128         setPredObject(p,v,reify(reify));
129     }
130     
131     AResourceInternal reify(String JavaDoc reify) {
132         ARPResource reification = null;
133         if ( reify != null ) {
134                 reification = new ARPResource(arp);
135                 reification.setAbout(reify);
136         }
137         return reification;
138     }
139     
140      
141     void setType(URIReference r) {
142         setPredObject(RDF_TYPE, r, null );
143     }
144     
145     private void setPredObject( AResourceInternal pred, Object JavaDoc obj, AResourceInternal reify ) {
146         // The basic triple.
147
triple( this, pred, obj );
148         
149       
150         
151         if ( reify != null ) {
152             tripleRes( reify, RDF_TYPE, RDF_STATEMENT );
153             tripleRes( reify, RDF_SUBJECT, this );
154             tripleRes( reify, RDF_PREDICATE, pred );
155             triple( reify, RDF_OBJECT, obj );
156         }
157         
158     }
159     
160     private void triple( AResourceInternal subj, AResourceInternal pred, Object JavaDoc obj ) {
161         int sw = 0;
162         if ( obj instanceof AResourceInternal ) {
163             sw += 1;
164         }
165         if ( obj instanceof ALiteral ) {
166             sw += 2;
167         }
168         switch (sw) {
169             case 1:
170                 tripleRes(subj,pred,(AResourceInternal)obj);
171                 break;
172             case 2:
173                 tripleLit(subj,pred,(ALiteral)obj);
174                 break;
175             case 0:
176             case 3:
177             default:
178                 throw new RuntimeException JavaDoc("Assertion failure: sw = " + sw);
179         }
180     }
181     
182     private void tripleRes(final AResourceInternal s,final AResourceInternal p,final AResourceInternal o ) {
183         s.setHasBeenUsed();
184         o.setHasBeenUsed();
185         arp.getStatementHandler().statement(s,p,o);
186         
187     }
188     
189     private void tripleLit( AResourceInternal s, final AResourceInternal p, final ALiteral o ) {
190            s.setHasBeenUsed();
191        arp.getStatementHandler().statement(s,p,o);
192     }
193     
194     static private URIReference _rdf_n[] = new URIReference[0];
195     
196     static private URIReference rdf_n(int i) {
197         if (i>=_rdf_n.length) {
198             int newLength = (i+10)*3/2;
199             URIReference new_rdf_n[] = new URIReference[newLength];
200             System.arraycopy(_rdf_n,0,new_rdf_n,0,_rdf_n.length);
201             for (int j=_rdf_n.length;j<newLength;j++) {
202                 try {
203                 new_rdf_n[j] = new URIReference(RDFNS+"_"+j);
204                 }
205                 catch (MalformedURIException disaster) {
206                     throw new RuntimeException JavaDoc("Impossible: RDF_nnn handling logic error.");
207                 }
208             }
209             _rdf_n = new_rdf_n;
210         }
211         return _rdf_n[i];
212     }
213     
214     static int isRDF_N(AResourceInternal r) {
215         if ( !r.isAnonymous() ) {
216             String JavaDoc uri = r.getURI();
217             if ( uri.length() > RDFNS_LENGTH && uri.charAt(RDFNS_LENGTH) == '_' ) {
218                 try {
219                     String JavaDoc sub = uri.substring(RDFNS_LENGTH+1);
220                     int rslt = Integer.parseInt(sub);
221                     if ( rslt>0 && uri.startsWith(RDFNS) ) {
222                         return rslt;
223                     }
224                 }
225                 catch (NumberFormatException JavaDoc e) {
226                 }
227             }
228         }
229         return -1;
230     }
231     
232     // AResource interface.
233
public boolean isAnonymous() {
234         return uri == null;
235     }
236     
237     public String JavaDoc getAnonymousID() {
238         return nodeID==null
239                 ? ( "A" + Integer.toString(genId) )
240                 : "U" + nodeID;
241     }
242     
243     void setNodeId(String JavaDoc s) {
244         nodeID = s;
245     }
246     
247     public String JavaDoc getURI() {
248         return this.uri;
249     }
250     
251     public String JavaDoc toString() {
252         return uri==null?("_:"+getAnonymousID()):("<"+uri+">");
253     }
254     
255     public int hashCode() {
256         if ( uri==null && nodeID==null)
257             return genId;
258         else if ( uri != null )
259             return uri.hashCode();
260         else
261            return nodeID.hashCode();
262     }
263     
264     public boolean equals(Object JavaDoc o) {
265         if ( o == null || !(o instanceof AResourceInternal))
266             return false;
267         if ( this == o)
268           return true;
269         AResourceInternal a=(AResourceInternal)o;
270         if ( uri != null )
271               return (!a.isAnonymous()) && uri.equals(a.getURI());
272             ARPResource aa = (ARPResource)a;
273         return nodeID != null && nodeID.equals(aa.nodeID);
274     }
275     
276     private Object JavaDoc userData;
277     
278     public Object JavaDoc getUserData() {
279         if ( uri != null )
280           throw new IllegalStateException JavaDoc("User data only supported on blank nodes");
281         if ( nodeID != null )
282            return arp.getUserData(nodeID);
283         else
284            return userData;
285     }
286     
287     public void setUserData(Object JavaDoc d) {
288         if ( uri != null )
289           throw new IllegalStateException JavaDoc("User data only supported on blank nodes");
290         if ( nodeID == null )
291             userData = d;
292         else
293             arp.setUserData(nodeID,d);
294     }
295
296     /* (non-Javadoc)
297      * @see com.hp.hpl.jena.rdf.arp.AResource#hasNodeID()
298      */

299     public boolean hasNodeID() {
300         return nodeID!=null;
301     }
302   private boolean used = false;
303
304     /* (non-Javadoc)
305      * @see com.hp.hpl.jena.rdf.arp.AResourceInternal#setHasBeenUsed()
306      */

307     public void setHasBeenUsed() {
308         used = true;
309     }
310
311
312     /* (non-Javadoc)
313      * @see com.hp.hpl.jena.rdf.arp.AResourceInternal#getHasBeenUsed()
314      */

315     public boolean getHasBeenUsed() {
316         return used;
317     }
318     
319 }
320
Popular Tags