1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.StringTokenizer ; 24 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.activity.Initializable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.parameters.Parameters; 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.components.modules.input.InputModuleHelper; 32 import org.apache.cocoon.environment.SourceResolver; 33 import org.apache.cocoon.transformation.helpers.VariableConfiguration; 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.helpers.AttributesImpl ; 37 38 139 public class VariableRewriterTransformer extends AbstractSAXTransformer 140 implements Initializable, Disposable { 141 142 private static final String NAMESPACE = ""; 143 144 145 private Set linkAttrs; 146 147 148 private Set inSchemes; 149 private Set outSchemes; 150 151 152 private Configuration origConf; 153 154 156 private Configuration conf; 157 158 private InputModuleHelper modHelper; 159 160 private String badLinkStr; 161 162 166 public void configure(Configuration conf) 167 throws ConfigurationException { 168 super.configure(conf); 169 this.origConf = conf; 170 } 171 172 175 public void initialize() throws Exception { 176 this.defaultNamespaceURI = NAMESPACE; 177 this.modHelper = new InputModuleHelper(); 178 this.modHelper.setup(this.manager); 179 } 180 181 184 public void setup(SourceResolver resolver, Map objectModel, 185 String src, Parameters parameters) 186 throws ProcessingException, SAXException , IOException 187 { 188 super.setup(resolver, objectModel, src, parameters); 189 this.badLinkStr = parameters.getParameter("bad-link-str", null); 190 this.linkAttrs = split(parameters.getParameter("link-attrs", "href"), " "); 191 this.inSchemes = split(parameters.getParameter("schemes", ""), " "); 192 this.outSchemes = split(parameters.getParameter("exclude-schemes", ""), " "); 193 194 VariableConfiguration varConf = new VariableConfiguration(this.origConf); 196 varConf.addVariable("src", src); 197 varConf.addVariables(parameters); 198 try { 199 this.conf = varConf.getConfiguration(); 200 } catch (ConfigurationException ce) { 201 throw new ProcessingException("Couldn't create dynamic config ", ce); 202 } 203 } 204 205 210 private Set split(String str, String delim) { 211 Set schemes = new HashSet (); 212 StringTokenizer st = new StringTokenizer (str, delim); 213 while (st.hasMoreTokens()) { 214 String pfx = st.nextToken(); 215 schemes.add(pfx); 216 } 217 return schemes; 218 } 219 220 221 229 public void startTransformingElement(String uri, 230 String name, 231 String raw, 232 Attributes attr) 233 throws ProcessingException, IOException , SAXException 234 { 235 Attributes newAttrs = null; 236 boolean matched = false; 237 238 Iterator iter = linkAttrs.iterator(); 239 while (iter.hasNext()) { 240 int attrIdx = attr.getIndex((String )iter.next()); 241 if (attrIdx != -1) { 242 String oldAttr = attr.getValue(attrIdx); 243 int i = oldAttr.indexOf(":"); 244 if (i != -1) { 245 String scheme = oldAttr.substring(0, i); 246 String addr = oldAttr.substring(i+1); 247 if (outSchemes.contains(scheme)) { 248 if (getLogger().isDebugEnabled()) { 249 getLogger().debug("Ignoring link '"+scheme+":"+addr+"'"); 250 } 251 } else if (inSchemes.contains(scheme)) { 252 matched = true; 253 newAttrs = getLinkAttr(attr, attrIdx, scheme, addr); 254 if (getLogger().isDebugEnabled()) { 255 getLogger().debug("Converted link '"+oldAttr+"' to '"+newAttrs.getValue(attrIdx)+"'"); 256 } 257 } else { 258 if (inSchemes.size() == 0) { 259 matched = true; 262 newAttrs = getLinkAttr(attr, attrIdx, scheme, addr); 263 getLogger().debug("Converted link '"+oldAttr+"' to '"+newAttrs.getValue(attrIdx)+"'"); 264 } 265 } 266 } 267 } 268 } 269 if (matched) { 270 super.startTransformingElement(uri, name, raw, newAttrs); 271 } else { 272 super.startTransformingElement(uri, name, raw, attr); 273 } 274 } 275 276 279 public void characters(char[] p0, int p1, int p2) 280 throws SAXException { 281 if (this.ignoreEventsCount == 0) { 282 if (this.ignoreEmptyCharacters == true) { 283 String value = new String (p0, p1, p2); 284 if (value.trim().length() > 0) { 285 super.characters(p0, p1, p2); 286 } 287 } else { 288 super.characters(p0, p1, p2); 289 } 290 } 291 } 292 293 302 private Attributes getLinkAttr(Attributes oldAttrs, int linkIndex, String scheme, String addr) { 303 AttributesImpl newAttrs = new AttributesImpl (oldAttrs); 304 try { 305 String modValue = (String )modHelper.getAttribute(this.objectModel, getConf(scheme), scheme, addr, (badLinkStr!=null?badLinkStr:scheme+":"+addr)); 306 newAttrs.setValue(linkIndex, modValue); 307 } catch (Exception e) { 308 getLogger().warn("## IM error: "+e, e); 311 } 312 return newAttrs; 313 } 314 315 320 private Configuration getConf(String scheme) { 321 Configuration[] schemeConfs = this.conf.getChildren(); 322 for (int i=0; i<schemeConfs.length; i++) { 323 if (scheme.equals(schemeConfs[i].getAttribute("name", null))) { 324 return schemeConfs[i]; 325 } 326 } 327 return null; 328 } 329 330 331 public void recycle() { 332 this.resolver = null; 333 this.linkAttrs = null; 334 this.inSchemes = null; 335 this.outSchemes = null; 336 this.conf = null; 337 super.recycle(); 340 } 341 342 345 public void dispose() { 346 if (this.modHelper != null) { 347 this.modHelper.releaseAll(); 348 this.modHelper = null; 349 } 350 super.dispose(); 351 } 352 } 353 | Popular Tags |