001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.xml; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.xml.sax.Attributes; 024import org.xml.sax.SAXException; 025import org.xml.sax.helpers.AttributesImpl; 026import org.xml.sax.helpers.XMLFilterImpl; 027 028/** 029 * This is a description of what this class does - eldavid don't forget to fill this in. 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 * 033 */ 034public class TestGroupNamespaceURIFilter extends XMLFilterImpl { 035 036 // The URI of a Group 1.0.3 schema 037 public static final String GROUP_URI="http://rice.kuali.org/xsd/kim/group"; 038 039 // The Map containing element transformation values 040 private Map<String,String> elementTransformationMap; 041 042 // The List containing elements with attributes for transformation 043 private List<String> elementAttributeTransformationList; 044 045 // The list which helps keep track of where we are in the XML 046 // hierarchy as the stream is being processed 047 private List<String> groupXmlStack = new ArrayList<String>(); 048 049 public TestGroupNamespaceURIFilter(){ 050 super(); 051 // Initialize the element transformation map 052 setElementTransformationMap(); 053 // Initialize the element attribute transformation list 054 setElementAttributeTransformationList(); 055 } 056 057 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 058 // Push the element onto the stack 059 if (groupXmlStack.isEmpty()){ 060 // Push the root element without onto the stack without special formatting 061 groupXmlStack.add(localName); 062 } 063 else { 064 // Push a child element by appending localName to the value of the top element in the stack 065 groupXmlStack.add(groupXmlStack.get(groupXmlStack.size()-1) + "." + localName); 066 } 067 068 // Toss "data" and "groups" 069 if (localName.equals("data") || localName.equals("groups")){ 070 return; 071 } 072 073 // Transform elements of concern 074 if (elementTransformationMap.containsKey(groupXmlStack.get(groupXmlStack.size()-1))){ 075 String targetLocalName = elementTransformationMap.get(groupXmlStack.get(groupXmlStack.size()-1)); 076 String targetQualifiedName = targetLocalName; 077 super.startElement(GROUP_URI, targetLocalName, targetQualifiedName, getTransformedAttributes(groupXmlStack.get(groupXmlStack.size()-1), atts)); 078 } 079 // Pass other elements through as they are 080 else { 081 super.startElement(GROUP_URI, localName, qName, atts); 082 } 083 } 084 085 public void endElement(String uri, String localName, String qName) throws SAXException { 086 // Fetch the value of the element from the top of the stack 087 String topStackElement = groupXmlStack.get(groupXmlStack.size()-1); 088 089 // Toss "data" and "groups" 090 if (localName.equals("data") || localName.equals("groups")){ 091 // Pop the element from the stack if it's not empty 092 if (!groupXmlStack.isEmpty()){ 093 groupXmlStack.remove(topStackElement); 094 } 095 return; 096 } 097 098 // Transform elements of concern 099 if (elementTransformationMap.containsKey(topStackElement)){ 100 String targetLocalName = elementTransformationMap.get(topStackElement); 101 String targetQualifiedName = targetLocalName; 102 super.endElement(GROUP_URI, targetLocalName, targetQualifiedName); 103 } 104 // Pass other elements through as they are 105 else { 106 super.endElement(GROUP_URI, localName, qName); 107 } 108 109 // Pop the element from the stack if it's not empty 110 if (!groupXmlStack.isEmpty()){ 111 groupXmlStack.remove(topStackElement); 112 } 113 114 } 115 116 /* 117 * Build a Map that maps elements we intend to transform to their corresponding transformed value. 118 * The keys in this Map are "hierarchically-qualified" representations of the elements of concern. 119 * 120 * For example, if "group" is a child of "groups", which is in turn a child of the root 121 * element "data", then it is represented as "data.groups.group" in the Map. 122 */ 123 private void setElementTransformationMap(){ 124 Map<String,String> elementTransformationMap = new HashMap<String,String>(); 125 elementTransformationMap.put("data.groups.group", "group"); 126 elementTransformationMap.put("data.groups.group.name", "groupName"); 127 elementTransformationMap.put("data.groups.group.description", "groupDescription"); 128 elementTransformationMap.put("data.groups.group.namespace", "namespaceCode"); 129 //elementTransformationMap.put("data.groups.group.members.principalName", "memberId"); 130 this.elementTransformationMap = elementTransformationMap; 131 } 132 133 /* 134 * Placeholder method for defining which elements have attributes that we intend to transform 135 */ 136 private void setElementAttributeTransformationList(){ 137 List<String> elementAttributeTransformationList = new ArrayList<String>(); 138 this.elementAttributeTransformationList = elementAttributeTransformationList; 139 } 140 141 /* 142 * Placeholder method adding support for transforming an element's attributes 143 */ 144 private Attributes getTransformedAttributes(String stackRepresentedElement, Attributes attributes){ 145 // If the element is found in the Element Attribute Transformation List, transform its appropriate attributes 146 if (elementAttributeTransformationList.contains(stackRepresentedElement)){ 147 // Just a placeholder, remember? 148 return new AttributesImpl(); 149 } 150 else { 151 // Otherwise, return a "hollow" Attributes object 152 return new AttributesImpl(); 153 } 154 } 155}