1 16 package org.apache.cocoon.components.modules.input; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.SortedSet ; 22 import java.util.TreeSet ; 23 import java.util.Vector ; 24 25 import org.apache.avalon.framework.component.Component; 26 import org.apache.avalon.framework.component.ComponentException; 27 import org.apache.avalon.framework.component.ComponentManager; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.thread.ThreadSafe; 31 32 import org.apache.cocoon.environment.ObjectModelHelper; 33 import org.apache.cocoon.environment.Request; 34 import org.apache.cocoon.xml.dom.DOMUtil; 35 import org.apache.cocoon.xml.dom.DocumentWrapper; 36 import org.apache.excalibur.xml.xpath.XPathProcessor; 37 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 42 95 public class XMLMetaModule extends AbstractMetaModule implements ThreadSafe { 96 97 protected String rootName = "root"; 98 protected String ignore; 99 protected String use; 100 protected String strip; 101 protected Object config; 102 protected XPathProcessor xpathProcessor; 103 104 protected static final String CACHE_OBJECT_NAME = "org.apache.cocoon.component.modules.input.XMLMetaModule"; 105 106 final static Vector returnNames; 107 static { 108 Vector tmp = new Vector (); 109 tmp.add("XML"); 110 returnNames = tmp; 111 } 112 113 114 115 public void configure(Configuration config) throws ConfigurationException { 116 117 this.inputConf = config.getChild("input-module"); 118 this.defaultInput = this.inputConf.getAttribute("name", this.defaultInput); 119 this.rootName = this.inputConf.getAttribute("root",this.rootName); 120 this.ignore = this.inputConf.getAttribute("ignore",this.ignore); 121 this.use = this.inputConf.getAttribute("use",this.use); 122 this.strip = this.inputConf.getAttribute("strip",this.strip); 123 this.config = config; 124 125 this.rootName = config.getChild("root").getValue(this.rootName); 127 this.ignore = config.getChild("ignore").getValue(this.ignore); 128 this.use = config.getChild("use").getValue(this.use); 129 this.strip = config.getChild("strip").getValue(this.strip); 130 } 131 132 133 134 135 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 136 throws ConfigurationException { 137 138 if (!this.initialized) { 139 this.lazy_initialize(); 140 } 141 if (this.defaultInput == null) { 142 if (getLogger().isWarnEnabled()) 143 getLogger().warn("No input module given. FAILING"); 144 return null; 145 } 146 147 148 Configuration inputConfig = null; 151 String inputName=null; 152 String rootName = this.rootName; 153 String ignore = this.ignore; 154 String use = this.use; 155 String strip = this.strip; 156 if (modeConf!=null) { 157 inputName = modeConf.getChild("input-module").getAttribute("name",null); 158 rootName = modeConf.getAttribute("root",this.rootName); 159 ignore = modeConf.getAttribute("ignore" ,this.ignore ); 160 use = modeConf.getAttribute("use" ,this.use ); 161 strip = modeConf.getAttribute("strip" ,this.strip ); 162 163 rootName = modeConf.getChild("root").getValue(rootName); 165 ignore = modeConf.getChild("ignore").getValue(ignore ); 166 use = modeConf.getChild("use").getValue(use ); 167 strip = modeConf.getChild("strip").getValue(strip ); 168 if (inputName != null) { 169 inputConfig = modeConf.getChild("input-module"); 170 } 171 } 172 173 Request request = ObjectModelHelper.getRequest(objectModel); 176 Map cache = (Map ) request.getAttribute(CACHE_OBJECT_NAME); 177 Object key = (modeConf != null ? modeConf : this.config); 178 Document doc = null; 179 180 if (cache != null && cache.containsKey(key)) { 181 doc = (Document ) cache.get(key); 182 if (getLogger().isDebugEnabled()) 183 getLogger().debug("using cached copy "+doc); 184 return doc; 185 } 186 if (getLogger().isDebugEnabled()) 187 getLogger().debug("no cached copy "+cache+" / "+key); 188 189 190 InputModule input = null; 192 if (inputName != null) input = obtainModule(inputName); 193 194 Iterator names = getNames(objectModel, 195 this.input, this.defaultInput, this.inputConf, 196 input, inputName, inputConfig); 197 198 SortedSet set = new TreeSet (); 201 String aName = null; 202 while (names.hasNext()){ 203 aName = (String ) names.next(); 204 if ((use == null || aName.startsWith(use)) && 205 (ignore == null || !aName.startsWith(ignore))) { 206 set.add(aName); 207 } 208 } 209 210 try { 211 names = set.iterator(); 212 213 doc = DOMUtil.createDocument(); 215 Element elem = doc.createElement(rootName); 216 doc.appendChild(elem); 217 218 while (names.hasNext()){ 219 aName = (String ) names.next(); 220 Object [] value = getValues(aName, objectModel, 222 this.input, this.defaultInput, this.inputConf, 223 input, inputName, inputConfig); 224 225 if (strip != null && aName.startsWith(strip)) 227 aName = aName.substring(strip.length()); 228 229 if (value.length > 0) { 230 Node node = DOMUtil.selectSingleNode(doc.getDocumentElement(), aName, this.xpathProcessor); 233 node.appendChild( node.getOwnerDocument().createTextNode(value[0].toString())); 234 235 if (value.length > 1) { 236 239 int endPos = aName.length() - (aName.endsWith("/") ? 1 : 0); 242 int startPos = aName.lastIndexOf("/", endPos) +1; 243 String nodeName = aName.substring(startPos, endPos); 244 if (nodeName.indexOf("[") != -1) { 245 endPos = nodeName.lastIndexOf("]"); 246 startPos = nodeName.indexOf("[") +1; 247 nodeName = nodeName.substring(startPos, endPos); 248 } 249 250 Node parent = node.getParentNode(); 252 for (int i = 1; i < value.length; i++) { 253 Node newNode = parent.getOwnerDocument().createElementNS(null, nodeName); 254 parent.appendChild( newNode ); 255 newNode.appendChild( newNode.getOwnerDocument().createTextNode(value[i].toString())); 256 } 257 } 258 } 259 } 260 } catch (Exception e) { 261 throw new ConfigurationException(e.getMessage()); 262 } 263 264 if (input != null) releaseModule(input); 265 266 doc = new DocumentWrapper(doc); 268 269 if (cache == null) 271 cache = new HashMap (); 272 if (getLogger().isDebugEnabled()) 273 getLogger().debug("no cached copy "+cache+" / "+key); 274 cache.put(key, doc); 275 request.setAttribute(CACHE_OBJECT_NAME,cache); 276 277 278 if (getLogger().isDebugEnabled()) 279 getLogger().debug("returning "+doc.toString()); 280 return doc; 281 } 282 283 284 285 286 287 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 288 throws ConfigurationException { 289 290 if (!this.initialized) { 291 this.lazy_initialize(); 292 } 293 if (this.defaultInput == null) { 294 if (getLogger().isWarnEnabled()) 295 getLogger().warn("No input module given. FAILING"); 296 return null; 297 } 298 299 return XMLMetaModule.returnNames.iterator(); 300 } 301 302 303 304 305 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 306 throws ConfigurationException { 307 308 if (!this.initialized) { 309 this.lazy_initialize(); 310 } 311 if (this.defaultInput == null) { 312 if (getLogger().isWarnEnabled()) 313 getLogger().warn("No input module given. FAILING"); 314 return null; 315 } 316 317 Object [] values = new Object [1]; 318 values[0] = this.getAttribute(name, modeConf, objectModel); 319 return values; 320 } 321 322 325 public void compose(ComponentManager manager) throws ComponentException { 326 super.compose(manager); 327 this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); 328 } 329 330 333 public void dispose() { 334 if ( this.manager != null ) { 335 this.manager.release((Component)this.xpathProcessor); 336 this.xpathProcessor = null; 337 } 338 super.dispose(); 339 } 340 341 } 342 | Popular Tags |