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.portal.coplets.basket;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.components.source.SourceUtil;
027: import org.apache.cocoon.environment.SourceResolver;
028: import org.apache.cocoon.portal.coplet.CopletInstanceData;
029: import org.apache.cocoon.portal.generation.AbstractCopletGenerator;
030: import org.apache.cocoon.xml.SaxBuffer;
031: import org.apache.cocoon.xml.XMLUtils;
032: import org.apache.excalibur.source.Source;
033: import org.apache.excalibur.xml.sax.SAXParser;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * This generator displays the content of one item.
039: *
040: * @version CVS $Id: BasketContentGenerator.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: public class BasketContentGenerator extends AbstractCopletGenerator {
043:
044: /** This is the attribute name containing the content */
045: protected String attributeName;
046:
047: /* (non-Javadoc)
048: * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
049: */
050: public void setup(SourceResolver resolver, Map objectModel,
051: String src, Parameters par) throws ProcessingException,
052: SAXException, IOException {
053: super .setup(resolver, objectModel, src, par);
054:
055: this .attributeName = par.getParameter("attribute-name", null);
056: }
057:
058: /* (non-Javadoc)
059: * @see org.apache.cocoon.generation.Generator#generate()
060: */
061: public void generate() throws IOException, SAXException,
062: ProcessingException {
063: boolean streamed = false;
064: SAXParser parser = null;
065: try {
066: parser = (SAXParser) this .manager.lookup(SAXParser.ROLE);
067: if (this .attributeName != null) {
068: CopletInstanceData cid = this .getCopletInstanceData();
069: byte[] content = (byte[]) cid
070: .getAttribute(this .attributeName);
071: if (content == null) {
072: this .xmlConsumer.startDocument();
073: XMLUtils.createElement(this .xmlConsumer, "p");
074: this .xmlConsumer.endDocument();
075: return;
076: }
077: try {
078: InputSource is = new InputSource(
079: new ByteArrayInputStream(content));
080: SaxBuffer buffer = new SaxBuffer();
081: parser.parse(is, buffer);
082: streamed = true;
083: buffer.toSAX(this .xmlConsumer);
084: } catch (Exception ignore) {
085: // ignore
086: }
087: }
088: if (!streamed) {
089: Source source = null;
090: try {
091: source = this .resolver.resolveURI(this .source);
092: parser.parse(SourceUtil.getInputSource(source),
093: this .xmlConsumer);
094: } finally {
095: this .resolver.release(source);
096: }
097: }
098: } catch (ServiceException se) {
099: throw new ProcessingException("Unable to lookup parser.",
100: se);
101: } finally {
102: this.manager.release(parser);
103: }
104: }
105:
106: }
|