Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.rice.kew.xml;
 
 
 import java.util.List;

This abstract class handles the xml stack of elements and makes it easier to run a transformation on certain elements.

Author(s):
Kuali Rice Team (rice.collab@kuali.org)
 
 public abstract class AbstractTransformationFilter extends XMLFilterImpl {
 
 	
 	// The list which helps keep track of where we are in the XML 
 	// hierarchy as the stream is being processed
 	private List<StringgroupXmlStack = new ArrayList<String>();

This method allows you to modify the element passed in. The returned element will be pushed into a "super.startElement(uri, localName, qName, atts)" call.

Parameters:
currentElement
Returns:
 
 	public abstract CurrentElement transformStartElement(CurrentElement currentElementthrows SAXException;

This method allows you to modify the element passed in. The returned element will be pushed into a "super.endElement(uri, localName, qName" call.

Parameters:
currentElement
Returns:
 
 	public abstract CurrentElement transformEndElement(CurrentElement currentElementthrows SAXException;
 		
 	/*
 	 * Build a Map that maps elements we intend to transform to their corresponding transformed value.
 	 * The keys in this Map are "hierarchically-qualified" representations of the elements of concern.
 	 * 
 	 * For example, if "group" is a child of "groups", which is in turn a child of the root
 	 * element "data", then it is represented as "data.groups.group" in the Map.
 	 */
 	public abstract List<KeyValuegetElementTransformationList();	

This method returns the element that we should start transforming at. So, if we had: <data> <groups> <group> We might want to start transforming at the group level. In that case the startingElement = "group"

Returns:
 
 	public abstract String getStartingElementPath();
 	
 	public void startElement(String uriString localNameString qNameAttributes attsthrows SAXException {
 		// Push the element onto the stack
 				.add(localName);					
 		}
 		else {
 			// Push a child element by appending localName to the value of the top element in the stack
 			.add(.get(.size()-1) + "." + localName);
 		}
 		
 		// Fetch the current element from the top of the stack
 		String currentElementKey = .get(.size()-1);
 		CurrentElement currentElement = new CurrentElement(currentElementKey,urilocalNameqNameatts);
		// Transform elements of concern:
			CurrentElement transformedElement = this.transformStartElement(currentElement);			
			super.startElement(transformedElement.getUri(), transformedElement.getLocalName(), transformedElement.getqName(), transformedElement.getAttributes());
		}
		else {
			// Pass other elements through as they are
			super.startElement(urilocalNameqNameatts);
		}
    }
	protected String getTrimmedCurrentElementKey(String currentElementKey){
		return currentElementKey.replaceFirst(StringEscapeUtils.escapeJava(this.getStartingElementPath()+"."), "");
	}
    @Override
	public void endElement(String uriString localNameString qNamethrows SAXException {
		// Fetch the current element from the top of the stack
		String currentElementKey = .get(.size()-1);
		CurrentElement currentElement = new CurrentElement(currentElementKey,urilocalNameqName);
			CurrentElement transformedElement = this.transformEndElement(currentElement);			
			super.endElement(transformedElement.getUri(), transformedElement.getLocalName(), transformedElement.getqName());
		}		
		else {
			// Pass other elements through as they are
			super.endElement(urilocalNameqName);
		}
		// Pop the element from the stack if it's not empty
			.remove(currentElementKey);			
		}
    }
	public class CurrentElement {
		public CurrentElement(){}
		public CurrentElement(String nameKeyString uriString localName,
				String qName) {
			super();
			this. = nameKey;
			this. = uri;
			this. = localName;
			this. = qName;
		}
		public CurrentElement(String nameKey,String uriString localNameString qName,
				Attributes attributes) {
			super();
			this. = nameKey;
			this. = uri;
			this. = localName;
			this. = qName;
			this. = attributes;
		}
		public String getUri() {
			return this.;
		}
		public void setUri(String uri) {
			this. = uri;
		}
		public String getLocalName() {
			return this.;
		}
		public void setLocalName(String localName) {
			this. = localName;
		}
		public String getqName() {
			return this.;
		}
		public void setqName(String qName) {
			this. = qName;
		}
			return this.;
		}
		public void setAttributes(Attributes attributes) {
			this. = attributes;
		}
		public String getNameKey() {
			return this.;
		}
		public void setNameKey(String nameKey) {
			this. = nameKey;
		}		
	}
New to GrepCode? Check out our FAQ X