KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > components > daisysource > DaisySourceFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
38 import java.util.regex.Pattern JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42
43 /**
44  * Cocoon source which reads its data from a part in a Daisy document. The format of the
45  * source is the usual daisy link format (which is not really a valid URL given the invalid
46  * use of the @ and : symbols, hope this doesn't give problems). The part type must be
47  * specified by adding an "!" to the URL, followed by the name of the part type,
48  * e.g. daisy:123!ImageData (branch, language and version can of course also be specified).
49  */

50 public class DaisySourceFactory implements SourceFactory, Contextualizable, Serviceable, ThreadSafe {
51     public static final Pattern JavaDoc 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 JavaDoc url, Map JavaDoc map) throws IOException JavaDoc, MalformedURLException JavaDoc {
64         Matcher JavaDoc matcher = DAISY_SOURCE_PATTERN.matcher(url);
65         if (!matcher.matches())
66             throw new MalformedURLException JavaDoc("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 JavaDoc branch = matcher.group(3);
77             long branchId = branch == null ? siteConf.getBranchId() : repository.getVariantManager().getBranch(branch, false).getId();
78             String JavaDoc language = matcher.group(5);
79             long languageId = language == null ? siteConf.getLanguageId() : repository.getVariantManager().getLanguage(language, false).getId();
80             String JavaDoc version = matcher.group(7);
81             if (version == null)
82                 version = WikiHelper.getVersionMode(request).toString();
83             String JavaDoc partType = matcher.group(9);
84             if (partType == null)
85                 throw new MalformedURLException JavaDoc("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 JavaDoc e) {
91             throw e;
92         } catch (Exception JavaDoc 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) { // should always be
99
DaisySource daisySource = (DaisySource)source;
100             daisySource.dispose();
101         }
102     }
103 }
104
Popular Tags