1 16 package org.outerj.daisy.frontend.components.daisysource; 17 18 import org.apache.excalibur.source.SourceFactory; 19 import org.apache.excalibur.source.Source; 20 import org.apache.excalibur.source.SourceException; 21 import org.apache.avalon.framework.context.Contextualizable; 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.thread.ThreadSafe; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.cocoon.components.ContextHelper; 29 import org.apache.cocoon.environment.Request; 30 import org.outerj.daisy.repository.Repository; 31 import org.outerj.daisy.repository.VariantKey; 32 import org.outerj.daisy.frontend.WikiHelper; 33 import org.outerj.daisy.frontend.components.siteconf.SiteConf; 34 import org.outerj.daisy.publisher.Publisher; 35 import org.outerj.daisy.publisher.BlobInfo; 36 37 import java.util.Map ; 38 import java.util.regex.Pattern ; 39 import java.util.regex.Matcher ; 40 import java.io.IOException ; 41 import java.net.MalformedURLException ; 42 43 50 public class DaisySourceFactory implements SourceFactory, Contextualizable, Serviceable, ThreadSafe { 51 public static final Pattern DAISY_SOURCE_PATTERN = Pattern.compile("^daisy:([0-9]+)(@([^:!#]*)(:([^:!#]*))?(:([^:!#]*))?)?(!(.*))?$"); 52 private Context context; 53 private ServiceManager serviceManager; 54 55 public void contextualize(Context context) throws ContextException { 56 this.context = context; 57 } 58 59 public void service(ServiceManager serviceManager) throws ServiceException { 60 this.serviceManager = serviceManager; 61 } 62 63 public Source getSource(String url, Map map) throws IOException , MalformedURLException { 64 Matcher matcher = DAISY_SOURCE_PATTERN.matcher(url); 65 if (!matcher.matches()) 66 throw new MalformedURLException ("Invalid daisy source URL: " + url); 67 68 Request request = ContextHelper.getRequest(context); 69 70 try { 71 Repository repository = WikiHelper.getRepository(request, serviceManager); 72 SiteConf siteConf = WikiHelper.getSiteConf(request); 73 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 74 75 long documentId = Long.parseLong(matcher.group(1)); 76 String branch = matcher.group(3); 77 long branchId = branch == null ? siteConf.getBranchId() : repository.getVariantManager().getBranch(branch, false).getId(); 78 String language = matcher.group(5); 79 long languageId = language == null ? siteConf.getLanguageId() : repository.getVariantManager().getLanguage(language, false).getId(); 80 String version = matcher.group(7); 81 if (version == null) 82 version = WikiHelper.getVersionMode(request).toString(); 83 String partType = matcher.group(9); 84 if (partType == null) 85 throw new MalformedURLException ("No part type specified in daisy URL: " + url); 86 87 BlobInfo blobInfo = publisher.getBlobInfo(new VariantKey(documentId, branchId, languageId), version, partType); 88 89 return new DaisySource(blobInfo, url); 90 } catch (MalformedURLException e) { 91 throw e; 92 } catch (Exception e) { 93 throw new SourceException("Error constructing daisy source for url " + url, e); 94 } 95 } 96 97 public void release(Source source) { 98 if (source instanceof DaisySource) { DaisySource daisySource = (DaisySource)source; 100 daisySource.dispose(); 101 } 102 } 103 } 104 | Popular Tags |