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.transformation;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.cocoon.ProcessingException;
021: import org.apache.cocoon.environment.ObjectModelHelper;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.cocoon.environment.Session;
024: import org.apache.cocoon.environment.SourceResolver;
025: import org.apache.cocoon.xml.XMLUtils;
026: import org.apache.cocoon.xml.IncludeXMLConsumer;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029:
030: import java.io.IOException;
031: import java.util.Map;
032:
033: /**
034: * @cocoon.sitemap.component.documentation
035: * With this transformer, an object that is stored in the session, can be inserted
036: * in the SAX stream at a given position, using usual <xsp:expr> rules.
037: * Object can be DOM Node, XMLizable, or any other object supported by <xsp:expr>.
038: *
039: * @cocoon.sitemap.component.name readDOMsession
040: * @cocoon.sitemap.component.logger sitemap.transformer.readDOMsession
041: *
042: * With this transformer, an object that is stored in the session, can be inserted
043: * in the SAX stream at a given position, using usual <xsp:expr> rules.
044: * Object can be DOM Node, XMLizable, or any other object supported by <xsp:expr>.
045: *
046: * Usage in sitemap:
047: * <pre>
048: * <map:transform type="read-session">
049: * <map:parameter name="attribute-name" value="companyInfo"/>
050: * <map:parameter name="trigger-element" value="company"/>
051: * <map:parameter name="position" value="after"/>
052: * </map:transform>
053: * </pre>
054: *
055: * Where:
056: * <ul>
057: * <li><b>attribute-name</b> is the name of the object in the session
058: * <li><b>trigger-element</b> is the element that we need to insert the SAX events
059: * <li><b>postion</b> is the actual place where the stream will be inserted, ie before, after or in
060: * the trigger-element
061: * </ul>
062: *
063: * @author <a href="mailto:sven.beauprez@the-ecorp.com">Sven Beauprez</a>
064: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
065: * @version CVS $Id: ReadDOMSessionTransformer.java 433543 2006-08-22 06:22:54Z crossley $
066: */
067: public class ReadDOMSessionTransformer extends AbstractTransformer {
068:
069: public static final String ATTRIBUTE_NAME = "attribute-name";
070: public static final String TRIGGER_ELEMENT = "trigger-element";
071:
072: /*
073: position where the sax events from the dom should be insterted
074: this can be: 'before', 'after' or 'in'
075: */
076: public static final String POSITION = "position";
077:
078: Session session;
079: String attributeName;
080: String trigger;
081: String position;
082:
083: /** BEGIN SitemapComponent methods **/
084: public void setup(SourceResolver resolver, Map objectModel,
085: String source, Parameters parameters)
086: throws ProcessingException, SAXException, IOException {
087: Request request = ObjectModelHelper.getRequest(objectModel);
088: session = request.getSession(false);
089: if (session != null) {
090: if (getLogger().isDebugEnabled()) {
091: getLogger().debug(
092: "Session is available. ID=" + session.getId());
093: }
094: this .attributeName = parameters.getParameter(
095: ATTRIBUTE_NAME, null);
096: if (this .attributeName == null) {
097: // Try old syntax
098: this .attributeName = parameters.getParameter(
099: "dom-name", null);
100: }
101:
102: this .trigger = parameters.getParameter(TRIGGER_ELEMENT,
103: null);
104: this .position = parameters.getParameter(POSITION, "in");
105: if (getLogger().isDebugEnabled()) {
106: getLogger().debug(
107: ATTRIBUTE_NAME + "=" + attributeName + ", "
108: + TRIGGER_ELEMENT + "=" + trigger
109: + ", " + POSITION + "=" + position);
110: }
111: } else {
112: getLogger().warn("No session object: Nothing to do.");
113: }
114: }
115:
116: /** END SitemapComponent methods **/
117:
118: /** BEGIN SAX ContentHandler handlers **/
119: public void startElement(String uri, String name, String raw,
120: Attributes attributes) throws SAXException {
121: // Start streaming after certain startelement is encountered
122: if (name.equalsIgnoreCase(trigger)) {
123: getLogger().debug("Trigger encountered");
124: if ("before".equalsIgnoreCase(position)) {
125: stream();
126: super .contentHandler.startElement(uri, name, raw,
127: attributes);
128: } else if ("in".equalsIgnoreCase(position)) {
129: super .contentHandler.startElement(uri, name, raw,
130: attributes);
131: stream();
132: } else if ("after".equalsIgnoreCase(position)) {
133: super .contentHandler.startElement(uri, name, raw,
134: attributes);
135: }
136: } else {
137: super .contentHandler.startElement(uri, name, raw,
138: attributes);
139: }
140: }
141:
142: public void endElement(String uri, String name, String raw)
143: throws SAXException {
144: super .contentHandler.endElement(uri, name, raw);
145: if (name.equalsIgnoreCase(trigger)) {
146: if ("after".equalsIgnoreCase(position)) {
147: stream();
148: }
149: }
150: }
151:
152: /** END SAX ContentHandler handlers **/
153:
154: /** own methods **/
155: private void stream() throws SAXException {
156: if (attributeName != null) {
157: Object node = session.getAttribute(attributeName);
158: if (node != null) {
159: getLogger().debug("Start streaming");
160: XMLUtils.valueOf(new IncludeXMLConsumer(
161: super .xmlConsumer), node);
162: } else {
163: getLogger()
164: .error(
165: "No attribute " + attributeName
166: + " in session");
167: }
168: } else {
169: getLogger().error(
170: "No " + ATTRIBUTE_NAME + " parameter specified");
171: }
172: }
173: }
|