001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.template;
018:
019: import java.io.IOException;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.environment.SourceResolver;
029: import org.apache.cocoon.template.environment.ParsingContext;
030: import org.apache.cocoon.template.expression.StringTemplateParser;
031: import org.apache.cocoon.template.script.InstructionFactory;
032: import org.apache.cocoon.template.script.Parser;
033: import org.apache.cocoon.transformation.ServiceableTransformer;
034: import org.apache.cocoon.xml.XMLConsumer;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * Adapter that makes this generator usable as a transformer (Note there is a
039: * performance penalty for this however: you effectively recompile the template
040: * for every instance document)
041: *
042: * @version $Id: TransformerAdapter.java 449189 2006-09-23 06:52:29Z crossley $
043: */
044: public class TransformerAdapter extends ServiceableTransformer {
045:
046: static class TemplateConsumer extends Parser implements
047: XMLConsumer, Serviceable, Disposable {
048:
049: private ServiceManager manager;
050:
051: private final JXTemplateGenerator gen;
052:
053: public TemplateConsumer() {
054: this .gen = new JXTemplateGenerator();
055: }
056:
057: public void setup(SourceResolver resolver, Map objectModel,
058: String src, Parameters parameters)
059: throws ProcessingException, SAXException, IOException {
060: this .gen.setup(resolver, objectModel, null, parameters);
061: }
062:
063: /**
064: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
065: */
066: public void service(ServiceManager manager)
067: throws ServiceException {
068: this .manager = manager;
069: this .gen.service(manager);
070: setParsingContext(new ParsingContext(
071: (StringTemplateParser) this .manager
072: .lookup(StringTemplateParser.ROLE),
073: (InstructionFactory) this .manager
074: .lookup(InstructionFactory.ROLE)));
075: }
076:
077: /**
078: * @see org.apache.avalon.framework.activity.Disposable#dispose()
079: */
080: public void dispose() {
081: this .manager.release(this .parsingContext
082: .getInstructionFactory());
083: this .manager.release(this .parsingContext
084: .getStringTemplateParser());
085: setParsingContext(null);
086: }
087:
088: public void endDocument() throws SAXException {
089: super .endDocument();
090: gen.performGeneration(getStartEvent(), null);
091: }
092:
093: void setConsumer(XMLConsumer consumer) {
094: gen.setConsumer(consumer);
095: }
096:
097: protected void recycle() {
098: super .recycle();
099: gen.recycle();
100: }
101: }
102:
103: protected final TemplateConsumer templateConsumer = new TemplateConsumer();
104:
105: /**
106: * @see org.apache.cocoon.transformation.ServiceableTransformer#recycle()
107: */
108: public void recycle() {
109: super .recycle();
110: templateConsumer.recycle();
111: }
112:
113: /**
114: * @see org.apache.cocoon.transformation.ServiceableTransformer#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
115: */
116: public void setup(SourceResolver resolver, Map objectModel,
117: String src, Parameters parameters)
118: throws ProcessingException, SAXException, IOException {
119: super .setup(resolver, objectModel, src, parameters);
120: templateConsumer.setup(resolver, objectModel, src, parameters);
121: }
122:
123: /**
124: * @see org.apache.cocoon.transformation.ServiceableTransformer#service(org.apache.avalon.framework.service.ServiceManager)
125: */
126: public void service(ServiceManager manager) throws ServiceException {
127: super .service(manager);
128: templateConsumer.service(manager);
129: }
130:
131: /**
132: * @see org.apache.cocoon.transformation.ServiceableTransformer#dispose()
133: */
134: public void dispose() {
135: templateConsumer.dispose();
136: super .dispose();
137: }
138:
139: /**
140: * @see org.apache.cocoon.xml.AbstractXMLProducer#setConsumer(org.apache.cocoon.xml.XMLConsumer)
141: */
142: public void setConsumer(XMLConsumer xmlConsumer) {
143: super.setConsumer(templateConsumer);
144: templateConsumer.setConsumer(xmlConsumer);
145: }
146: }
|