001: /*
002: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.n3;
008:
009: import com.hp.hpl.jena.iri.IRI;
010: import com.hp.hpl.jena.iri.IRIException;
011: import com.hp.hpl.jena.iri.IRIFactory;
012: import com.hp.hpl.jena.util.FileUtils;
013:
014: /** A simple class to access IRI resolution
015: *
016: * @author Andy Seaborne, Jeremy Carroll
017: * */
018:
019: public class IRIResolver {
020: /**
021: * The current working directory, as a string.
022: */
023: static private String globalBase = "http://localhost/LocalHostBase/";
024:
025: // Try to set the global base from the current directory.
026: // Security (e.g. Tomcat) may prevent this in which case we
027: // use a common default set above.
028: static {
029: try {
030: globalBase = FileUtils.toURL(".");
031: } catch (Throwable th) {
032: }
033: }
034:
035: /**
036: * The current working directory, as an IRI.
037: */
038: static final IRI cwd;
039:
040: /**
041: * An IRIFactory appropriately configuired.
042: */
043: static final IRIFactory factory = new IRIFactory(IRIFactory
044: .jenaImplementation());
045: static {
046: factory.setSameSchemeRelativeReferences("file");
047: }
048:
049: static {
050:
051: IRI cwdx;
052: try {
053: cwdx = factory.construct(globalBase);
054: } catch (IRIException e) {
055: System.err
056: .println("Unexpected IRIException in initializer: "
057: + e.getMessage());
058: cwdx = factory.create("file:///");
059: }
060: cwd = cwdx;
061: }
062:
063: /**
064: * Turn a filename into a well-formed file: URL relative to the working
065: * directory.
066: *
067: * @param filename
068: * @return String The filename as an absolute URL
069: */
070: static public String resolveFileURL(String filename)
071: throws IRIException {
072: IRI r = cwd.resolve(filename);
073: if (!r.getScheme().equalsIgnoreCase("file")) {
074: return resolveFileURL("./" + filename);
075: }
076: return r.toString();
077: }
078:
079: /**
080: * Create resolve a URI against a base. If baseStr is a relative file IRI
081: * then it is first resolved against the current working directory.
082: *
083: * @param relStr
084: * @param baseStr
085: * Can be null if relStr is absolute
086: * @return String An absolute URI
087: * @throws JenaURIException
088: * If result would not be legal, absolute IRI
089: */
090: static public String resolve(String relStr, String baseStr)
091: throws JenaURIException {
092: return exceptions(resolveIRI(relStr, baseStr)).toString();
093: }
094:
095: /*
096: * No exception thrown by this method.
097: */
098: static private IRI resolveIRI(String relStr, String baseStr) {
099: IRI i = factory.create(relStr);
100: if (i.isAbsolute())
101: // removes excess . segments
102: return cwd.create(i);
103:
104: IRI base = factory.create(baseStr);
105:
106: if ("file".equalsIgnoreCase(base.getScheme()))
107: return cwd.create(base).create(i);
108: return base.create(i);
109: }
110:
111: final private IRI base;
112:
113: /**
114: * Construct an IRIResolver with base as the
115: * current working directory.
116: *
117: */
118: public IRIResolver() {
119: this (null);
120: }
121:
122: /**
123: * Construct an IRIResolver with base determined
124: * by the argument URI. If this is relative,
125: * it is relative against the current working directory.
126: * @param baseS
127: *
128: * @throws JenaURIException
129: * If resulting base would not be legal, absolute IRI
130: */
131: public IRIResolver(String baseS) {
132: if (baseS == null)
133: baseS = chooseBaseURI();
134: // IRI aaa = RelURI.factory.construct(baseS);
135: base = exceptions(cwd.create(baseS));
136: }
137:
138: /**
139: * The base of this IRIResolver.
140: * @return
141: */
142: public String getBaseIRI() {
143: return base.toString();
144: }
145:
146: /**
147: * Resolve the relative URI against the base of
148: * this IRIResolver.
149: * @param relURI
150: * @return the resolved IRI
151: * @throws JenaURIException
152: * If resulting URI would not be legal, absolute IRI
153:
154: */
155: public String resolve(String relURI) {
156: return exceptions(base.resolve(relURI)).toString();
157: }
158:
159: /**
160: * Throw any exceptions resulting from IRI.
161: * @param iri
162: * @return iri
163: */
164: static private IRI exceptions(IRI iri) {
165: if (iri.hasViolation(false)) {
166: try {
167: cwd.construct(iri);
168: } catch (IRIException e) {
169: throw new JenaURIException(e);
170: }
171: }
172: return iri;
173: }
174:
175: /**
176: * Resolve the relative URI str against the current
177: * working directory.
178: * @param str
179: * @return
180: */
181: public static String resolveGlobal(String str) {
182: return exceptions(cwd.resolve(str)).toString();
183: }
184:
185: /**
186: * Choose a base URI based on the current directory
187: *
188: * @return String Absolute URI
189: */
190:
191: static public String chooseBaseURI() {
192: return chooseBaseURI(null);
193: }
194:
195: /**
196: * Choose a baseURI based on a suggestion
197: *
198: * @return String URI (if relative, relative to current working directory).
199: */
200:
201: static public String chooseBaseURI(String baseURI) {
202: if (baseURI == null)
203: baseURI = "file:.";
204: return resolveGlobal(baseURI);
205: }
206:
207: }
208:
209: /*
210: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
211: * reserved.
212: *
213: * Redistribution and use in source and binary forms, with or without
214: * modification, are permitted provided that the following conditions are met:
215: * 1. Redistributions of source code must retain the above copyright notice,
216: * this list of conditions and the following disclaimer. 2. Redistributions in
217: * binary form must reproduce the above copyright notice, this list of
218: * conditions and the following disclaimer in the documentation and/or other
219: * materials provided with the distribution. 3. The name of the author may not
220: * be used to endorse or promote products derived from this software without
221: * specific prior written permission.
222: *
223: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
224: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
225: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
226: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
227: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
228: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
229: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
231: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
232: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233: */
|