1 package org.sapia.resource.include; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.URI ; 6 import java.net.URISyntaxException ; 7 8 import org.sapia.resource.Resource; 9 import org.sapia.resource.ResourceNotFoundException; 10 import org.sapia.resource.Utils; 11 12 26 public class IncludeContext { 27 28 private IncludeContext _parent; 29 private String _uri; 30 private IncludeConfig _conf; 31 private Resource _resource; 32 33 void setParent(IncludeContext parent){ 34 _parent = parent; 35 } 36 37 void setUri(String uri){ 38 _uri = uri; 39 } 40 41 void setConfig(IncludeConfig conf){ 42 _conf = conf; 43 } 44 45 public IncludeConfig getConfig(){ 46 return _conf; 47 } 48 49 53 public IncludeContext getParent(){ 54 return _parent; 55 } 56 57 60 public String getUri(){ 61 return _uri; 62 } 63 64 69 public String getCanonicalUri() throws ResourceNotFoundException, IOException , Exception { 70 return resolve().getURI(); 71 } 72 73 87 public Object include() throws ResourceNotFoundException, IOException , Exception { 88 InputStream is = null; 89 try{ 90 Resource res = resolve(); 91 is = res.getInputStream(); 92 return doInclude(is, null); 93 }finally{ 94 if(is != null) 95 is.close(); 96 pop(); 97 } 98 } 99 100 107 public Object include(Object context) throws ResourceNotFoundException, IOException , Exception { 108 InputStream is = null; 109 try{ 110 Resource res = resolve(); 111 is = res.getInputStream(); 112 return doInclude(is, context); 113 }finally{ 114 if(is != null) 115 is.close(); 116 pop(); 117 } 118 } 119 120 130 public InputStream includeStream() throws ResourceNotFoundException, IOException , Exception { 131 try{ 132 Resource res = resolve(); 133 return res.getInputStream(); 134 }finally{ 135 pop(); 136 } 137 } 138 139 140 150 public Resource includeResource() throws ResourceNotFoundException, IOException , Exception { 151 try{ 152 return resolve(); 153 }finally{ 154 pop(); 155 } 156 } 157 158 165 public Resource getResource() throws ResourceNotFoundException, IOException , Exception { 166 return resolve(); 167 } 168 169 181 public Resource getRelative(String uri) throws ResourceNotFoundException, IOException , Exception { 182 try{ 183 if(_uri == null){ 184 return _conf.getResources().resolveResource(uri); 185 } 186 boolean hasScheme = Utils.hasScheme(uri); 187 if(hasScheme && Utils.isAbsolute(new URI (uri))){ 188 return _conf.getResources().resolveResource(uri); 189 } 190 else if(!hasScheme && 191 Utils.isAbsolute( 192 new URI (new StringBuffer ("scheme:").append(uri).toString()) 193 )){ 194 return _conf.getResources().resolveResource(uri); 195 } 196 else{ 197 return resolve().getRelative(uri); 198 } 199 }catch(URISyntaxException e){ 200 throw new IOException ("Invalid URI: " + uri + " - " + e.getMessage()); 201 } 202 } 203 204 217 protected Object doInclude(InputStream is, Object context) throws IOException , Exception { 218 return is; 219 } 220 221 Resource resolve() throws ResourceNotFoundException, IOException , Exception { 222 if(_resource != null){ 223 return _resource; 224 } 225 if(_parent != null && _parent._uri != null){ 226 Resource res = _parent.resolve(); 227 boolean hasScheme = Utils.hasScheme(uri()); 228 if(hasScheme && Utils.isAbsolute(new URI (uri()))){ 229 _resource = _conf.getResources().resolveResource(uri()); 230 } 231 else if(!hasScheme && 232 Utils.isAbsolute( 233 new URI (new StringBuffer ("scheme:").append(uri()).toString()) 234 )){ 235 _resource = _conf.getResources().resolveResource(uri()); 236 } 237 else{ 238 _resource = res.getRelative(uri()); 239 } 240 } 241 else{ 242 _resource = _conf.getResources().resolveResource(uri()); 243 } 244 return _resource; 245 } 246 247 private void pop(){ 248 IncludeState.popContext(_conf.getAppKey()); 249 } 250 251 private String uri(){ 252 return _uri; 253 } 254 } 255 | Popular Tags |