Implementing GxResolver

A resolver takes a base-uri and an href and uses these two values to return a stream.

001 package org.gxml.book.common; 
002  
003 import java.io.File; 
004 import java.io.FileNotFoundException; 
005 import java.io.IOException; 
006 import java.io.InputStream; 
007 import java.net.URI; 
008 import java.net.URISyntaxException; 
009 import java.net.URL; 
010  
011 import org.gxml.xdm.Resolved; 
012 import org.gxml.xdm.Resolver; 
013  
014 import com.tibco.gxml.sa.api.common.util.PreCondition; 
015  
016 public final class SampleResolver implements Resolver 
017 { 
018     final URI baseURI; 
019  
020     public SampleResolver(final URI baseURI) 
021     { 
022         this.baseURI = PreCondition.assertArgumentNotNull(baseURI, "baseURI"); 
023     } 
024  
025     /** 
026      * Convert a URI relative to a base URI into an input source. 
027      * <p/> 
028      * This default implementation requires that neither parameter be null, and performs the expected action to retrieve 
029      * the input source (which may involve network access). 
030      * 
031      * @param baseURI 
032      *            the base URI against which the target is to be resolved; must not be null 
033      * @param location 
034      *            the URI to resolve; must not be null 
035      * @return a pair of InputStream and resolved URI. 
036      */ 
037     public Resolved<InputStream> resolveInputStream(final URI location) throws IOException 
038     { 
039         PreCondition.assertArgumentNotNull(location, "uri"); 
040         if (location.isAbsolute()) 
041         { 
042             return retrieve(location, location); 
043         } 
044         else 
045         { 
046             PreCondition.assertArgumentNotNull(baseURI, "baseURI"); 
047  
048             final URI base = baseURI.normalize(); 
049             final URI resolved = base.resolve(location); 
050  
051             return retrieve(location, resolved); 
052         } 
053     } 
054  
055     private Resolved<InputStream> retrieve(final URI location, final URI uri) throws IOException 
056     { 
057         PreCondition.assertArgumentNotNull(uri, "uri"); 
058  
059         final URL toRetrieve; 
060  
061         if (!uri.isAbsolute()) // assume local file 
062         { 
063             final File canonFile = new File(uri.toString()).getCanonicalFile(); 
064             toRetrieve = canonFile.toURI().toURL(); 
065         } 
066         else 
067         { 
068             toRetrieve = uri.toURL(); 
069         } 
070  
071         if (toRetrieve == null) 
072         { 
073             throw new FileNotFoundException(uri.toString()); 
074         } 
075  
076         final InputStream stream = toRetrieve.openStream(); 
077         if (stream == null) 
078         { 
079             throw new FileNotFoundException(toRetrieve.toString()); 
080         } 
081         try 
082         { 
083             return new Resolved<InputStream>(location, stream, toRetrieve.toURI()); 
084         } 
085         catch (final URISyntaxException e) 
086         { 
087             throw new AssertionError(e); 
088         } 
089     } 
090 }