/* Milyn - Copyright (C) 2006 - 2010 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (version 2.1) as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details: http://www.gnu.org/licenses/lgpl.txt */ package org.milyn.cdres.trans; import org.milyn.cdr.SmooksResourceConfiguration; import org.milyn.cdr.SmooksConfigurationException; import org.milyn.cdr.annotation.ConfigParam; import org.milyn.container.ExecutionContext; import org.milyn.delivery.dom.DOMElementVisitor; import org.w3c.dom.Element; /** * Set a DOM element attribute <u>during the processing phase</u>. * <h3>.cdrl Configuration</h3> * <pre> * <smooks-resource useragent="<i>device/profile</i>" selector="<i>target-element-name</i>" * path="org.milyn.cdres.trans.SetAttributeTU"> * * <!-- The name of the element attribute to be set. --> * <param name="<b>attributeName</b>"><i>attribute-name</i></param> * * <!-- The value of the element attribute to be set. --> * <param name="<b>attributeValue</b>"><i>attribute-value</i></param> * * <!-- (Optional) Overwrite existing attributes of the same name. Default false. --> * <param name="<b>overwrite</b>"><i>true/false</i></param> * * <!-- (Optional) Visit the target element before iterating over the elements * child content. Default false. --> * <param name="<b>visitBefore</b>"><i>true/false</i></param> * </smooks-resource></pre> * See {@link org.milyn.cdr.SmooksResourceConfiguration}. * @author tfennelly */ public class SetAttributeTU implements DOMElementVisitor { @ConfigParam private String attributeName; @ConfigParam private String attributeValue; @ConfigParam(use = ConfigParam.Use.OPTIONAL, defaultVal = "false") private boolean visitBefore; @ConfigParam(use = ConfigParam.Use.OPTIONAL, defaultVal = "false") private boolean overwrite; public void visitBefore(Element element, ExecutionContext executionContext) { if(visitBefore) { visit(element, executionContext); } } public void visitAfter(Element element, ExecutionContext executionContext) { if(!visitBefore) { visit(element, executionContext); } } private void visit(Element element, ExecutionContext executionContext) { // If the element already has the new attribute and we're // not overwriting, leave all as is and return. if(!overwrite && element.hasAttribute(attributeName)) { return; } element.setAttribute(attributeName, attributeValue); } }