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.dom.DOMBuilder; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.SAXException ; 27 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 34 57 58 public class WriteDOMSessionTransformer 59 extends AbstractTransformer { 60 61 public static final String DOM_NAME = "dom-name"; 62 public static final String DOM_ROOT_ELEMENT = "dom-root-element"; 63 64 private boolean buildDom = false; 65 private boolean sessionAvailable = false; 66 67 private Session session; 68 private DOMBuilder builder; 69 70 private String DOMName; 71 private String rootElement; 72 private Map storedPrefixMap; 73 74 77 public void recycle() { 78 super.recycle(); 79 this.session = null; 80 this.builder = null; 81 this.buildDom = false; 82 this.sessionAvailable = false; 83 } 84 85 86 87 public void setup(SourceResolver resolver, Map objectModel, 88 String source, Parameters parameters) 89 throws ProcessingException, SAXException , IOException { 90 getLogger().debug("WriteSessionTransformer: setup"); 91 Request request = ObjectModelHelper.getRequest(objectModel); 92 session = request.getSession(false); 93 if (session != null) { 94 DOMName = parameters.getParameter(WriteDOMSessionTransformer.DOM_NAME,null); 95 rootElement = parameters.getParameter(WriteDOMSessionTransformer.DOM_ROOT_ELEMENT,null); 96 if (DOMName!=null && rootElement!=null) { 97 getLogger().debug("WriteSessionTransformer: "+WriteDOMSessionTransformer.DOM_NAME + "=" + 99 DOMName + "; " + WriteDOMSessionTransformer.DOM_ROOT_ELEMENT + "=" + 100 rootElement); 101 sessionAvailable = true; 102 storedPrefixMap = new HashMap (); 103 } else { 104 getLogger().error("WriteSessionTransformer: need " + WriteDOMSessionTransformer.DOM_NAME + 105 " and " + WriteDOMSessionTransformer.DOM_ROOT_ELEMENT + " parameters"); 106 } 107 } else { 108 getLogger().error("WriteSessionTransformer: no session object"); 109 } 110 } 111 112 113 114 115 116 public void startPrefixMapping(String prefix, String uri) 117 throws SAXException { 118 super.startPrefixMapping(prefix,uri); 119 if (buildDom) { 120 builder.startPrefixMapping(prefix,uri); 121 } else { 122 storePrefixMapping(prefix,uri); 123 } 124 } 125 126 public void startElement(String uri, String name, String raw, Attributes attributes) 127 throws SAXException { 128 if (name.equalsIgnoreCase(rootElement) && sessionAvailable) { 130 getLogger().debug("WriteSessionTransformer: start building DOM tree"); 131 buildDom = true; 132 builder = new DOMBuilder(); 133 builder.startDocument(); 134 launchStoredMappings(); 135 builder.startElement(uri,name,raw,attributes); 136 } else if (buildDom) { 137 builder.startElement(uri,name,raw,attributes); 138 } 139 super.contentHandler.startElement(uri,name,raw,attributes); 140 } 141 142 public void endElement(String uri, String name, String raw) 143 throws SAXException { 144 if (name.equalsIgnoreCase(rootElement) && sessionAvailable) { 145 buildDom = false; 146 builder.endElement(uri,name,raw); 147 builder.endDocument(); 148 getLogger().debug("WriteSessionTransformer: putting DOM tree in session object"); 149 session.setAttribute(DOMName,builder.getDocument().getFirstChild()); 150 getLogger().debug("WriteSessionTransformer: DOM tree is in session object"); 151 } else if (buildDom) { 152 builder.endElement(uri,name,raw); 153 } 154 super.contentHandler.endElement(uri,name,raw); 155 } 156 157 158 public void characters(char c[], int start, int len) 159 throws SAXException { 160 if (buildDom) { 161 builder.characters(c,start,len); 162 } 163 super.contentHandler.characters(c,start,len); 164 } 165 166 public void startCDATA() 167 throws SAXException { 168 if (buildDom) { 169 builder.startCDATA(); 170 } 171 super.lexicalHandler.startCDATA(); 172 } 173 174 public void endCDATA() 175 throws SAXException { 176 if (buildDom) { 177 builder.endCDATA(); 178 } 179 super.lexicalHandler.endCDATA(); 180 } 181 182 183 184 protected void storePrefixMapping(String prefix, String uri) { 185 storedPrefixMap.put(prefix,uri); 186 } 187 188 protected void launchStoredMappings() 189 throws SAXException { 190 Iterator it = storedPrefixMap.keySet().iterator(); 191 while(it.hasNext()) { 192 String pre = (String )it.next(); 193 String uri = (String )storedPrefixMap.get(pre); 194 getLogger().debug("WriteSessionTransformer: launching prefix mapping[ pre: "+pre+" uri: "+uri+" ]"); 195 builder.startPrefixMapping(pre,uri); 196 } 197 } 198 199 200 201 } 202 | Popular Tags |