1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.environment.ObjectModelHelper; 21 import org.apache.cocoon.environment.Request; 22 import org.apache.cocoon.environment.Session; 23 import org.apache.cocoon.environment.SourceResolver; 24 import org.apache.cocoon.xml.XMLUtils; 25 import org.apache.cocoon.xml.IncludeXMLConsumer; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.SAXException ; 28 29 import java.io.IOException ; 30 import java.util.Map ; 31 32 33 67 public class ReadDOMSessionTransformer extends AbstractTransformer { 68 69 public static final String ATTRIBUTE_NAME = "attribute-name"; 70 public static final String TRIGGER_ELEMENT = "trigger-element"; 71 72 76 public static final String POSITION = "position"; 77 78 Session session; 79 String attributeName; 80 String trigger; 81 String position; 82 83 84 public void setup(SourceResolver resolver, 85 Map objectModel, 86 String source, 87 Parameters parameters) 88 throws ProcessingException, SAXException , IOException { 89 Request request = ObjectModelHelper.getRequest(objectModel); 90 session = request.getSession(false); 91 if (session != null) { 92 if (getLogger().isDebugEnabled()) { 93 getLogger().debug("Session is available. ID=" + session.getId()); 94 } 95 this.attributeName = parameters.getParameter(ATTRIBUTE_NAME, null); 96 if (this.attributeName == null) { 97 this.attributeName = parameters.getParameter("dom-name", null); 99 } 100 101 this.trigger = parameters.getParameter(TRIGGER_ELEMENT, null); 102 this.position = parameters.getParameter(POSITION, "in"); 103 if (getLogger().isDebugEnabled()) { 104 getLogger().debug(ATTRIBUTE_NAME + "=" + attributeName + ", " 105 + TRIGGER_ELEMENT + "=" + trigger + ", " 106 + POSITION + "=" + position); 107 } 108 } else { 109 getLogger().warn("No session object: Nothing to do."); 110 } 111 } 112 113 114 115 public void startElement(String uri, String name, String raw, Attributes attributes) 116 throws SAXException { 117 if (name.equalsIgnoreCase(trigger)) { 119 getLogger().debug("Trigger encountered"); 120 if ("before".equalsIgnoreCase(position)) { 121 stream(); 122 super.contentHandler.startElement(uri,name,raw,attributes); 123 } else if ("in".equalsIgnoreCase(position)) { 124 super.contentHandler.startElement(uri,name,raw,attributes); 125 stream(); 126 } else if ("after".equalsIgnoreCase(position)) { 127 super.contentHandler.startElement(uri,name,raw,attributes); 128 } 129 } else { 130 super.contentHandler.startElement(uri,name,raw,attributes); 131 } 132 } 133 134 public void endElement(String uri,String name,String raw) 135 throws SAXException { 136 super.contentHandler.endElement(uri,name,raw); 137 if (name.equalsIgnoreCase(trigger)) { 138 if ("after".equalsIgnoreCase(position)) { 139 stream(); 140 } 141 } 142 } 143 144 145 146 private void stream() throws SAXException { 147 if (attributeName != null) { 148 Object node = session.getAttribute(attributeName); 149 if (node != null) { 150 getLogger().debug("Start streaming"); 151 XMLUtils.valueOf(new IncludeXMLConsumer(super.xmlConsumer), node); 152 } else { 153 getLogger().error("No attribute " + attributeName + " in session"); 154 } 155 } else { 156 getLogger().error("No "+ ATTRIBUTE_NAME + " parameter specified"); 157 } 158 } 159 } 160 | Popular Tags |