1 16 package org.apache.cocoon.webapps.session.transformation; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.cocoon.ProcessingException; 23 import org.apache.cocoon.environment.SourceResolver; 24 import org.apache.cocoon.webapps.session.SessionConstants; 25 import org.apache.excalibur.source.SourceParameters; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.SAXException ; 28 29 37 public class SessionPreTransformer 38 extends AbstractSessionTransformer { 39 40 43 public static final String CREATECONTEXT_ELEMENT = "createcontext"; 44 public static final String CREATECONTEXT_NAME_ATTRIBUTE = "name"; 45 public static final String CREATECONTEXT_SAVE_ATTRIBUTE = "save"; public static final String CREATECONTEXT_LOAD_ATTRIBUTE = "load"; 48 public static final String GETXML_ELEMENT = "getxml"; 49 public static final String GETXML_CONTEXT_ATTRIBUTE = "context"; 50 public static final String GETXML_PATH_ATTRIBUTE = "path"; 51 52 public static final String LOADCONTEXT_ELEMENT = "loadxml"; 53 public static final String LOADCONTEXT_CONTEXT_ATTRIBUTE = "context"; 54 public static final String LOADCONTEXT_PATH_ATTRIBUTE = "path"; 56 58 public static final String CONTEXT_URL_ELEMENT = "contexturl"; 59 60 61 protected int processingGetXML; 62 63 public SessionPreTransformer() { 64 this.defaultNamespaceURI = SessionConstants.SESSION_NAMESPACE_URI; 65 } 66 67 75 public void setup(SourceResolver resolver, 76 Map objectModel, 77 String src, 78 Parameters par) 79 throws ProcessingException, 80 SAXException , 81 IOException { 82 super.setup(resolver, objectModel, src, par); 83 this.processingGetXML = 0; 84 } 85 86 91 public void endElement(String uri, String name, String raw) throws SAXException { 92 super.endElement(uri, name, raw); 93 if (namespaceURI.equals(uri) 94 && this.processingGetXML > 0 95 && name.equals(GETXML_ELEMENT)) { 96 this.processingGetXML--; 97 this.ignoreEventsCount--; 98 this.ignoreHooksCount--; 99 } 100 } 101 102 107 public void startElement(String uri, 108 String name, 109 String raw, 110 Attributes attr) 111 throws SAXException { 112 if (namespaceURI.equals(uri) 113 && this.processingGetXML > 0 114 && name.equals(GETXML_ELEMENT)) { 115 this.processingGetXML++; 116 this.ignoreEventsCount++; 117 this.ignoreHooksCount++; 118 } 119 super.startElement(uri, name, raw, attr); 120 } 121 122 127 public void startTransformingElement(String uri, 128 String name, 129 String raw, 130 Attributes attr) 131 throws ProcessingException, IOException , SAXException { 132 133 if (this.getLogger().isDebugEnabled() == true) { 134 this.getLogger().debug("BEGIN startTransformingElement uri=" + uri + 135 ", name=" + name + 136 ", raw=" + raw + 137 ", attr=" + attr); 138 } 139 if (name.equals(CREATECONTEXT_ELEMENT) == true) { 140 this.getContextManager().createContext(attr.getValue(CREATECONTEXT_NAME_ATTRIBUTE), 141 attr.getValue(CREATECONTEXT_LOAD_ATTRIBUTE), 142 attr.getValue(CREATECONTEXT_SAVE_ATTRIBUTE)); 143 144 } else if (name.equals(GETXML_ELEMENT) == true) { 145 final String path = attr.getValue(GETXML_PATH_ATTRIBUTE); 146 final String contextName = attr.getValue(GETXML_CONTEXT_ATTRIBUTE); 147 148 if (this.getSessionManager().streamContextFragment(contextName, 149 path, 150 this) == true) { 151 this.ignoreEventsCount++; 152 this.ignoreHooksCount++; 153 this.processingGetXML++; 154 } 155 156 } else if (name.equals(LOADCONTEXT_ELEMENT) == true) { 157 this.startParametersRecording(); 158 stack.push(attr.getValue(LOADCONTEXT_CONTEXT_ATTRIBUTE)); 159 if (attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE) != null) { 160 stack.push(attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE)); 161 } else { 162 stack.push("/"); 163 } 164 165 } else if (name.equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) { 167 this.ignoreEventsCount++; 168 169 } else { 171 super.startTransformingElement(uri, name, raw, attr); 172 } 173 174 if (this.getLogger().isDebugEnabled() == true) { 175 this.getLogger().debug("END startTransformingElement"); 176 } 177 } 178 179 public void endTransformingElement(String uri, 180 String name, 181 String raw) 182 throws ProcessingException ,IOException , SAXException { 183 184 if (this.getLogger().isDebugEnabled() == true) { 185 this.getLogger().debug("BEGIN endTransformingElement uri=" + uri + ", name=" + name + ", raw=" + raw); 186 } 187 if (name.equals(CREATECONTEXT_ELEMENT) == true) { 188 190 } else if (name.equals(GETXML_ELEMENT) == true) { 191 193 } else if (name.equals(LOADCONTEXT_ELEMENT) == true) { 195 String path = (String )stack.pop(); 196 String contextName = (String )stack.pop(); 197 SourceParameters pars = this.endParametersRecording((SourceParameters)null); 198 pars.setSingleParameterValue("contextname", contextName); 199 pars.setSingleParameterValue("path", path); 200 201 this.getContextManager().getContext(contextName).loadXML(path, 202 pars); 203 } else if (name.equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) { 205 this.ignoreEventsCount--; 206 String contextUrl = this.request.getScheme() + "://" + 207 this.request.getServerName() + ":" + 208 this.request.getServerPort() + 209 this.request.getContextPath(); 210 this.sendTextEvent(contextUrl); 211 212 213 } else { 215 super.endTransformingElement(uri, name, raw); 216 } 217 if (this.getLogger().isDebugEnabled() == true) { 218 this.getLogger().debug("END endTransformingElement"); 219 } 220 } 221 } 222 | Popular Tags |