1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.configuration.Configurable; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.service.ServiceSelector; 28 import org.apache.avalon.framework.service.Serviceable; 29 30 import org.apache.avalon.excalibur.pool.Recyclable; 31 32 import org.apache.cocoon.ProcessingException; 33 import org.apache.cocoon.components.modules.output.OutputModule; 34 import org.apache.cocoon.environment.SourceResolver; 35 import org.apache.cocoon.xml.dom.DocumentWrapper; 36 37 import org.w3c.dom.Document ; 38 import org.xml.sax.Attributes ; 39 import org.xml.sax.SAXException ; 40 41 60 public class SimpleFormInstanceExtractionTransformer extends AbstractExtractionTransformer 61 implements Configurable, Serviceable, Recyclable { 62 63 protected static class ElementData { 64 public String uri = null; 65 public String loc = null; 66 public String raw = null; 67 68 public ElementData() { 69 } 70 71 public ElementData(String uri, String loc, String raw) { 72 this.uri = uri; 73 this.loc = loc; 74 this.raw =raw; 75 } 76 77 public boolean equals(String uri, String loc, String raw) { 78 79 if (!this.uri.equals(uri)) 80 return false; 81 if (!this.loc.equals(loc)) 82 return false; 83 if (!this.raw.equals(raw)) 84 return false; 85 return true; 86 } 87 88 } 89 90 protected final static String OUTPUT_MODULE_SELECTOR = OutputModule.ROLE+"Selector"; 91 92 ElementData startElement = null; 93 ElementData nameElement = null; 94 String qname = "name"; 95 96 String instanceName = null; 97 boolean nameAsRoot = true; 98 99 String outputModuleName = "request-attr"; 100 Configuration outputConf = null; 101 102 ServiceManager manager = null; 103 Map objectModel = null; 104 105 public void configure(Configuration config) throws ConfigurationException { 106 this.startElement = new ElementData(); 107 this.startElement.uri = config.getChild("start").getAttribute("uri", ""); 108 this.startElement.loc = config.getChild("start").getAttribute("local-name", "form-instance"); 109 this.startElement.raw = config.getChild("start").getAttribute("raw-name", "form-instance"); 110 111 this.nameElement = new ElementData(); 112 this.nameElement.uri = config.getChild("name").getAttribute("uri", ""); 113 this.nameElement.loc = config.getChild("name").getAttribute("local-name", "form"); 114 this.nameElement.raw = config.getChild("name").getAttribute("raw-name", "form"); 115 this.qname = config.getChild("name").getAttribute("name-attribute", "name"); 116 117 this.nameAsRoot = config.getChild("name-as-root").getValueAsBoolean(this.nameAsRoot); 118 119 this.outputConf = config.getChild("output"); 120 this.outputModuleName = this.outputConf.getAttribute("name",this.outputModuleName); 121 } 122 123 public void service(ServiceManager manager) throws ServiceException { 124 this.manager = manager; 125 } 126 127 128 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 129 throws ProcessingException, SAXException , IOException { 130 super.setup(resolver, objectModel, src, parameters); 131 this.objectModel = objectModel; 132 } 133 134 public void recycle() { 135 super.recycle(); 136 this.instanceName = null; 137 } 138 139 153 public boolean startExtracting(String uri, String loc, String raw, Attributes a) { 154 if (this.nameElement.equals(uri,loc,raw)) { 155 this.instanceName = a.getValue(this.qname); 156 } 157 boolean res = this.startElement.equals(uri,loc,raw); 158 return res; 159 } 160 161 172 public boolean endExtracting(String uri, String loc, String raw) { 173 boolean res = this.startElement.equals(uri,loc,raw); 174 return res; 175 } 176 177 178 182 public void startExtractingDocument(String uri, String loc, String raw, Attributes a) throws SAXException { 183 if (this.nameAsRoot) { 184 loc = this.instanceName; 185 if (uri != null && !uri.equals("")) { 186 int pos = raw.indexOf(':'); 187 raw = raw.substring(0, pos+1) + this.instanceName; 188 } else { 189 raw = loc; 190 } 191 } 192 this.currentBuilder.startElement(uri,loc,raw,a); 193 } 194 195 199 public void endExtractingDocument(String uri, String loc, String raw) throws SAXException { 200 if(this.nameAsRoot){ 201 loc = this.instanceName; 202 if (uri != null && !uri.equals("")) { 203 int pos = raw.indexOf(':'); 204 raw = raw.substring(0, pos+1) + this.instanceName; 205 } else { 206 raw = loc; 207 } 208 } 209 this.currentBuilder.endElement(uri, loc, raw); 210 } 211 212 213 218 public void handleExtractedDocument(Document doc) { 219 220 ServiceSelector outputSelector = null; 221 OutputModule output = null; 222 223 try { 224 if (getLogger().isDebugEnabled()) 225 getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" using "+outputConf); 226 outputSelector = (ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR); 227 if (outputSelector.isSelectable(this.outputModuleName)) { 228 output = (OutputModule) outputSelector.select(this.outputModuleName); 229 } 230 output.setAttribute(outputConf, this.objectModel, this.instanceName, new DocumentWrapper(doc)); 231 output.commit(outputConf, this.objectModel); 232 if (getLogger().isDebugEnabled()) 233 getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" using "+outputConf); 234 235 } catch (Exception e) { 236 if (getLogger().isWarnEnabled()) 237 getLogger().warn("Problem writing document data: "+e.getMessage()); 238 } finally { 239 if (outputSelector != null) { 240 if (output != null) { 241 outputSelector.release(output); 242 output = null; 243 } 244 this.manager.release(outputSelector); 245 } 246 } 247 this.instanceName = null; 248 } 249 250 } 251 | Popular Tags |