org.milyn.cdres.trans.RenameAttributeTU Maven / Gradle / Ivy
/*
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;
/**
* Renames an attribute from a DOM element during the processing phase.
* .cdrl Configuration
*
* <smooks-resource useragent="device/profile" selector="target-element-name"
* path="org.milyn.cdres.trans.RenameAttributeTU">
*
* <!-- The name of the element attribute to be renamed. -->
* <param name="attributeName">attribute-name</param>
*
* <!-- The new name of the element attribute. -->
* <param name="attributeNewName">attribute-new-name</param>
*
* <!-- (Optional) Overwrite existing attributes of the same name. Default false. -->
* <param name="overwrite">true/false</param>
*
* <!-- (Optional) Visit the target element before iterating over the elements
* child content. Default false. -->
* <param name="visitBefore">true/false</param>
* </smooks-resource>
* See {@link org.milyn.cdr.SmooksResourceConfiguration}.
* @author tfennelly
*/
public class RenameAttributeTU implements DOMElementVisitor {
@ConfigParam
private String attributeName;
@ConfigParam
private String attributeNewName;
@ConfigParam(use = ConfigParam.Use.OPTIONAL, defaultVal = "false")
private boolean overwrite;
@ConfigParam(use = ConfigParam.Use.OPTIONAL, defaultVal = "false")
private boolean visitBefore;
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) {
String attributeValue;
// If the element already has the new attribute and we're
// not overwriting, leave all as is and return.
if(!overwrite && element.hasAttribute(attributeNewName)) {
return;
}
attributeValue = element.getAttribute(attributeName);
if(attributeValue != null) {
element.removeAttribute(attributeName);
element.setAttribute(attributeNewName, attributeValue);
}
}
}