01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.editor;
17:
18: import org.apache.cocoon.forms.datatype.convertor.ConvertorBuilder;
19: import org.apache.cocoon.forms.datatype.convertor.Convertor;
20: import org.apache.cocoon.forms.util.DomHelper;
21: import org.apache.cocoon.forms.CacheManager;
22: import org.apache.cocoon.components.source.SourceUtil;
23: import org.apache.avalon.framework.service.Serviceable;
24: import org.apache.avalon.framework.service.ServiceManager;
25: import org.apache.avalon.framework.service.ServiceException;
26: import org.apache.excalibur.source.SourceResolver;
27: import org.apache.excalibur.source.Source;
28: import org.w3c.dom.Element;
29: import org.outerj.daisy.htmlcleaner.HtmlCleanerTemplate;
30: import org.outerj.daisy.htmlcleaner.HtmlCleanerFactory;
31: import org.xml.sax.InputSource;
32:
33: /**
34: * Builds {@link org.outerj.daisy.frontend.editor.HtmlCleaningConvertor}s.
35: */
36: public class HtmlCleaningConvertorBuilder implements ConvertorBuilder,
37: Serviceable {
38: private ServiceManager serviceManager;
39:
40: public void service(ServiceManager serviceManager)
41: throws ServiceException {
42: this .serviceManager = serviceManager;
43: }
44:
45: public Convertor build(Element element) throws Exception {
46: String config = DomHelper.getAttribute(element, "config");
47:
48: Source source = null;
49: SourceResolver sourceResolver = null;
50: CacheManager cacheManager = null;
51: try {
52: cacheManager = (CacheManager) serviceManager
53: .lookup(CacheManager.ROLE);
54: sourceResolver = (SourceResolver) serviceManager
55: .lookup(SourceResolver.ROLE);
56: source = sourceResolver.resolveURI(config);
57:
58: String prefix = HtmlCleanerTemplate.class.getName();
59: HtmlCleanerTemplate template = (HtmlCleanerTemplate) cacheManager
60: .get(source, prefix);
61: if (template == null) {
62: HtmlCleanerFactory factory = new HtmlCleanerFactory();
63: InputSource is = SourceUtil.getInputSource(source);
64: template = factory.buildTemplate(is);
65: cacheManager.set(template, source, prefix);
66: }
67:
68: return new HtmlCleaningConvertor(template);
69: } finally {
70: if (source != null)
71: sourceResolver.release(source);
72: if (sourceResolver != null)
73: serviceManager.release(sourceResolver);
74: if (cacheManager != null)
75: serviceManager.release(cacheManager);
76: }
77: }
78: }
|