KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > compose > PolyadicPrefixMappingImpl


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: PolyadicPrefixMappingImpl.java,v 1.6 2005/02/21 11:52:04 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.compose;
8
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.Graph;
12 import com.hp.hpl.jena.shared.PrefixMapping;
13 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
14 import com.hp.hpl.jena.util.CollectionFactory;
15
16
17 public class PolyadicPrefixMappingImpl extends PrefixMappingImpl implements PrefixMapping
18     {
19     private Polyadic poly;
20     private PrefixMapping pending = new PrefixMappingImpl();
21     
22     public PolyadicPrefixMappingImpl( Polyadic p )
23         { poly = p;
24         }
25            
26     private PrefixMapping getBaseMapping()
27         {
28         Graph base = poly.getBaseGraph();
29         return base == null ? pending : base.getPrefixMapping();
30         }
31     
32     public PrefixMapping setNsPrefix( String JavaDoc prefix, String JavaDoc uri )
33         {
34         checkUnlocked();
35         getBaseMapping().setNsPrefix( prefix, uri );
36         return this;
37         }
38     
39     public PrefixMapping removeNsPrefix( String JavaDoc prefix )
40         {
41         checkUnlocked();
42         getBaseMapping().removeNsPrefix( prefix );
43         return this;
44         }
45         
46     /**
47         Add the bindings of other to our own. We defer to the general case
48         because we have to ensure the URIs are checked.
49         
50         @param other the PrefixMapping whose bindings we are to add to this.
51     */

52     public PrefixMapping setNsPrefixes( PrefixMapping other )
53         { return setNsPrefixes( other.getNsPrefixMap() ); }
54         
55     /**
56         Add the bindings in the map to our own. This will fail with a ClassCastException
57         if any key or value is not a String; we make no guarantees about order or
58         completeness if this happens. It will fail with an IllegalPrefixException if
59         any prefix is illegal; similar provisos apply.
60         
61          @param other the Map whose bindings we are to add to this.
62     */

63     public PrefixMapping setNsPrefixes( Map other )
64         {
65         checkUnlocked();
66         getBaseMapping().setNsPrefixes( other );
67         return this;
68         }
69          
70     public String JavaDoc getNsPrefixURI( String JavaDoc prefix )
71         {
72         PrefixMapping bm = getBaseMapping();
73         String JavaDoc s = bm.getNsPrefixURI( prefix );
74         if (s == null)
75             {
76             List graphs = poly.getSubGraphs();
77             for (int i = 0; i < graphs.size(); i += 1)
78                 {
79                 String JavaDoc ss = ((Graph) graphs.get(i)).getPrefixMapping().getNsPrefixURI( prefix );
80                 if (ss != null) return ss;
81                 }
82             }
83         return s;
84         }
85         
86     public Map getNsPrefixMap()
87         {
88         Map result = CollectionFactory.createHashedMap();
89         List graphs = poly.getSubGraphs();
90         for (int i = 0; i < graphs.size(); i += 1)
91             result.putAll( ((Graph) graphs.get(i)).getPrefixMapping().getNsPrefixMap() );
92         result.putAll( getBaseMapping().getNsPrefixMap() );
93         return result;
94         }
95         
96     public String JavaDoc getNsURIPrefix( String JavaDoc uri )
97         {
98         String JavaDoc s = getBaseMapping().getNsURIPrefix( uri );
99         if (s == null)
100             {
101             List graphs = poly.getSubGraphs();
102             for (int i = 0; i < graphs.size(); i += 1)
103                 {
104                 String JavaDoc ss = ((Graph) graphs.get(i)).getPrefixMapping().getNsURIPrefix( uri );
105                 if (ss != null) return ss;
106                 }
107             }
108         return s;
109         }
110         
111     /**
112         Expand a prefixed URI. There's an assumption that any URI of the form
113         Head:Tail is subject to mapping if Head is in the prefix mapping. So, if
114         someone takes it into their heads to define eg "http" or "ftp" we have problems.
115     */

116     public String JavaDoc expandPrefix( String JavaDoc prefixed )
117         {
118         String JavaDoc s = getBaseMapping().expandPrefix( prefixed );
119         if (s.equals( prefixed ))
120             {
121             List graphs = poly.getSubGraphs();
122             for (int i = 0; i < graphs.size(); i += 1)
123                 {
124                 String JavaDoc ss = ((Graph) graphs.get(i)).getPrefixMapping().expandPrefix( prefixed );
125                 if (!ss.equals( prefixed )) return ss;
126                 }
127             }
128         return s;
129         }
130         
131     /**
132         Answer a readable (we hope) representation of this prefix mapping.
133     */

134     public String JavaDoc toString()
135         { return "<polyadic prefix map>"; }
136         
137     /**
138         Compress the URI using the prefix mapping. This version of the code looks
139         through all the maplets and checks each candidate prefix URI for being a
140         leading substring of the argument URI. There's probably a much more
141         efficient algorithm available, preprocessing the prefix strings into some
142         kind of search table, but for the moment we don't need it.
143     */

144     public String JavaDoc shortForm( String JavaDoc uri )
145         {
146         String JavaDoc s = getBaseMapping().shortForm( uri );
147         if (s.equals( uri ))
148             {
149             List graphs = poly.getSubGraphs();
150             for (int i = 0; i < graphs.size(); i += 1)
151                 {
152                 String JavaDoc ss = ((Graph) graphs.get(i)).getPrefixMapping().shortForm( uri );
153                 if (!ss.equals( uri )) return ss;
154                 }
155             }
156         return s;
157         }
158     
159     public String JavaDoc qnameFor( String JavaDoc uri )
160         {
161         String JavaDoc result = getBaseMapping().qnameFor( uri );
162         if (result == null)
163             {
164             List graphs = poly.getSubGraphs();
165             for (int i = 0; i < graphs.size(); i += 1)
166                 {
167                 String JavaDoc ss = ((Graph) graphs.get(i)).getPrefixMapping().qnameFor( uri );
168                 if (ss != null) return ss;
169                 }
170             }
171         return result;
172         }
173     }
174
175 /*
176  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
177     All rights reserved.
178
179     Redistribution and use in source and binary forms, with or without
180     modification, are permitted provided that the following conditions
181     are met:
182
183     1. Redistributions of source code must retain the above copyright
184        notice, this list of conditions and the following disclaimer.
185
186     2. Redistributions in binary form must reproduce the above copyright
187        notice, this list of conditions and the following disclaimer in the
188        documentation and/or other materials provided with the distribution.
189
190     3. The name of the author may not be used to endorse or promote products
191        derived from this software without specific prior written permission.
192
193     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
201     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
202     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203 */
Popular Tags