001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: PolyadicPrefixMappingImpl.java,v 1.12 2008/01/02 12:10:19 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.compose;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.Graph;
012: import com.hp.hpl.jena.shared.PrefixMapping;
013: import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
014: import com.hp.hpl.jena.util.CollectionFactory;
015:
016: public class PolyadicPrefixMappingImpl extends PrefixMappingImpl
017: implements PrefixMapping {
018: private Polyadic poly;
019: private PrefixMapping pending = new PrefixMappingImpl();
020:
021: public PolyadicPrefixMappingImpl(Polyadic p) {
022: poly = p;
023: }
024:
025: protected boolean equals(PrefixMappingImpl other) {
026: return equalsByMap(other);
027: }
028:
029: protected boolean sameAs(PrefixMappingImpl other) {
030: return equalsByMap(other);
031: }
032:
033: private PrefixMapping getBaseMapping() {
034: Graph base = poly.getBaseGraph();
035: return base == null ? pending : base.getPrefixMapping();
036: }
037:
038: public PrefixMapping setNsPrefix(String prefix, String uri) {
039: checkUnlocked();
040: getBaseMapping().setNsPrefix(prefix, uri);
041: return this ;
042: }
043:
044: public PrefixMapping removeNsPrefix(String prefix) {
045: checkUnlocked();
046: getBaseMapping().removeNsPrefix(prefix);
047: return this ;
048: }
049:
050: /**
051: Add the bindings of other to our own. We defer to the general case
052: because we have to ensure the URIs are checked.
053:
054: @param other the PrefixMapping whose bindings we are to add to this.
055: */
056: public PrefixMapping setNsPrefixes(PrefixMapping other) {
057: return setNsPrefixes(other.getNsPrefixMap());
058: }
059:
060: /**
061: Add the bindings in the map to our own. This will fail with a ClassCastException
062: if any key or value is not a String; we make no guarantees about order or
063: completeness if this happens. It will fail with an IllegalPrefixException if
064: any prefix is illegal; similar provisos apply.
065:
066: @param other the Map whose bindings we are to add to this.
067: */
068: public PrefixMapping setNsPrefixes(Map other) {
069: checkUnlocked();
070: getBaseMapping().setNsPrefixes(other);
071: return this ;
072: }
073:
074: public String getNsPrefixURI(String prefix) {
075: PrefixMapping bm = getBaseMapping();
076: String s = bm.getNsPrefixURI(prefix);
077: if (s == null && prefix.length() > 0) {
078: List graphs = poly.getSubGraphs();
079: for (int i = 0; i < graphs.size(); i += 1) {
080: String ss = ((Graph) graphs.get(i)).getPrefixMapping()
081: .getNsPrefixURI(prefix);
082: if (ss != null)
083: return ss;
084: }
085: }
086: return s;
087: }
088:
089: public Map getNsPrefixMap() {
090: Map result = CollectionFactory.createHashedMap();
091: List graphs = poly.getSubGraphs();
092: for (int i = 0; i < graphs.size(); i += 1)
093: result.putAll(((Graph) graphs.get(i)).getPrefixMapping()
094: .getNsPrefixMap());
095: result.putAll(getBaseMapping().getNsPrefixMap());
096: return result;
097: }
098:
099: public String getNsURIPrefix(String uri) {
100: String s = getBaseMapping().getNsURIPrefix(uri);
101: if (s == null) {
102: List graphs = poly.getSubGraphs();
103: for (int i = 0; i < graphs.size(); i += 1) {
104: String ss = ((Graph) graphs.get(i)).getPrefixMapping()
105: .getNsURIPrefix(uri);
106: if (ss != null && ss.length() > 0)
107: return ss;
108: }
109: }
110: return s;
111: }
112:
113: /**
114: Expand a prefixed URI. There's an assumption that any URI of the form
115: Head:Tail is subject to mapping if Head is in the prefix mapping. So, if
116: someone takes it into their heads to define eg "http" or "ftp" we have problems.
117: */
118: public String expandPrefix(String prefixed) {
119: String s = getBaseMapping().expandPrefix(prefixed);
120: if (s.equals(prefixed)) {
121: List graphs = poly.getSubGraphs();
122: for (int i = 0; i < graphs.size(); i += 1) {
123: String ss = ((Graph) graphs.get(i)).getPrefixMapping()
124: .expandPrefix(prefixed);
125: if (!ss.equals(prefixed))
126: return ss;
127: }
128: }
129: return s;
130: }
131:
132: /**
133: Answer a readable (we hope) representation of this prefix mapping.
134: */
135: public String toString() {
136: return "<polyadic prefix map>";
137: }
138:
139: /**
140: Compress the URI using the prefix mapping. This version of the code looks
141: through all the maplets and checks each candidate prefix URI for being a
142: leading substring of the argument URI. There's probably a much more
143: efficient algorithm available, preprocessing the prefix strings into some
144: kind of search table, but for the moment we don't need it.
145: */
146: public String shortForm(String uri) {
147: String s = getBaseMapping().shortForm(uri);
148: if (s.equals(uri)) {
149: List graphs = poly.getSubGraphs();
150: for (int i = 0; i < graphs.size(); i += 1) {
151: String ss = ((Graph) graphs.get(i)).getPrefixMapping()
152: .shortForm(uri);
153: if (!ss.equals(uri))
154: return ss;
155: }
156: }
157: return s;
158: }
159:
160: public String qnameFor(String uri) {
161: String result = getBaseMapping().qnameFor(uri);
162: if (result == null) {
163: List graphs = poly.getSubGraphs();
164: for (int i = 0; i < graphs.size(); i += 1) {
165: String ss = ((Graph) graphs.get(i)).getPrefixMapping()
166: .qnameFor(uri);
167: if (ss != null)
168: return ss;
169: }
170: }
171: return result;
172: }
173: }
174:
175: /*
176: * (c) Copyright 2004, 2005, 2006, 2007, 2008 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: */
|