1 16 package org.apache.cocoon.ajax; 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.ObjectModelHelper; 24 import org.apache.cocoon.environment.Request; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.cocoon.transformation.AbstractTransformer; 27 import org.apache.cocoon.xml.AttributesImpl; 28 import org.apache.cocoon.xml.RedundantNamespacesFilter; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.Locator ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 34 38 39 public class BrowserUpdateTransformer extends AbstractTransformer { 40 41 public static final String AJAXMODE_PARAM = "cocoon-ajax"; 42 43 public static final String BU_NSURI = "http://apache.org/cocoon/browser-update/1.0"; 44 45 private boolean ajaxRequest = false; 46 47 private int replaceDepth = 0; 48 49 private boolean inUpdateTag = false; 50 private String updateTagId = null; 51 52 Locator locator; 53 54 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException , IOException { 55 56 Request request = ObjectModelHelper.getRequest(objectModel); 57 this.ajaxRequest = request.getParameter(AJAXMODE_PARAM) != null; 58 } 59 60 public void setDocumentLocator(Locator locator) { 61 super.setDocumentLocator(locator); 62 this.locator = locator; 63 } 64 65 public void startDocument() throws SAXException { 66 67 if (ajaxRequest) { 68 RedundantNamespacesFilter nsPipe = new RedundantNamespacesFilter(); 72 if (this.xmlConsumer != null) { 73 nsPipe.setConsumer(this.xmlConsumer); 74 } else { 75 nsPipe.setContentHandler(this.contentHandler); 76 } 77 setConsumer(nsPipe); 78 } 79 80 super.startDocument(); 81 if (ajaxRequest) { 82 super.startPrefixMapping("bu", BU_NSURI); 84 super.startElement(BU_NSURI, "document", "bu:document", new AttributesImpl()); 85 } 86 } 87 88 public void startPrefixMapping(String prefix, String uri) throws SAXException { 89 if (!this.ajaxRequest || this.replaceDepth > 0) { 91 super.startPrefixMapping(prefix, uri); 92 } 93 } 94 95 public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 96 if (BU_NSURI.equals(uri) && "replace".equals(loc)) { 97 this.updateTagId = attrs.getValue("id"); 100 this.inUpdateTag = true; 101 if (this.ajaxRequest && this.replaceDepth == 0) { 102 super.startElement(uri, loc, raw, attrs); 104 } 105 replaceDepth++; 106 } else { 107 109 if (this.inUpdateTag) { 111 this.inUpdateTag = false; 112 String localId = attrs.getValue("id"); 114 if (localId != null) { 115 if (this.updateTagId != null && !localId.equals(this.updateTagId)) { 117 throw new SAXParseException ("Id on bu:replace (" + this.updateTagId + ") and " + raw + " (" + 118 localId + ") don't match.", this.locator); 119 } 120 } else { 121 if (this.updateTagId == null) { 123 throw new SAXParseException ("Neither bu:replace nor " + raw + " have an id attribute.", this.locator); 124 } 125 AttributesImpl newAttrs = new AttributesImpl(attrs); 126 newAttrs.addCDATAAttribute("id", this.updateTagId); 127 attrs = newAttrs; 128 } 129 this.updateTagId = null; 130 } 131 if (!this.ajaxRequest || this.replaceDepth > 0) { 132 super.startElement(uri, loc, raw, attrs); 133 } 134 } 135 } 136 137 public void characters(char[] buffer, int offset, int len) throws SAXException { 138 if (this.inUpdateTag) { 139 for (int i = offset; i < len; i++) { 141 if (!Character.isWhitespace(buffer[i])) { 142 throw new SAXParseException ("bu:replace must include a single child element and no text.", this.locator); 143 } 144 } 145 } 146 if (!this.ajaxRequest || this.replaceDepth > 0) { 147 super.characters(buffer, offset, len); 148 } 149 } 150 151 public void endElement(String uri, String loc, String raw) throws SAXException { 152 if (BU_NSURI.equals(uri) && "replace".equals(loc)) { 153 replaceDepth--; 154 if (this.ajaxRequest && this.replaceDepth == 0) { 155 super.endElement(uri, loc, raw); 157 } 158 } else { 159 if (!this.ajaxRequest || this.replaceDepth > 0) { 161 super.endElement(uri, loc, raw); 162 } 163 } 164 } 165 166 public void endPrefixMapping(String prefix) throws SAXException { 167 if (!this.ajaxRequest || this.replaceDepth > 0) { 169 super.endPrefixMapping(prefix); 170 } 171 } 172 173 public void endDocument() throws SAXException { 174 if (ajaxRequest) { 175 super.endElement(BU_NSURI, "document", "bu:document"); 176 super.endPrefixMapping("bu"); 177 } 178 super.endDocument(); 179 } 180 } 181 | Popular Tags |