Coverage Report - org.kuali.rice.kew.xml.GroupNamespaceURIAllInOneFilterPOC
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupNamespaceURIAllInOneFilterPOC
0%
0/68
0%
0/24
2.625
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.xml;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.xml.sax.Attributes;
 24  
 import org.xml.sax.SAXException;
 25  
 import org.xml.sax.helpers.AttributesImpl;
 26  
 import org.xml.sax.helpers.XMLFilterImpl;
 27  
 
 28  
 /**
 29  
  * This XML Filter is used to upgrade pre-1.0.3 Group XML document to 1.0.3 document.  
 30  
  * 
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  *
 33  
  */
 34  
 public class GroupNamespaceURIAllInOneFilterPOC extends XMLFilterImpl {
 35  
 
 36  
         // The URI of a Group 1.0.3 schema 
 37  
     public static final String GROUP_URI="http://rice.kuali.org/xsd/kim/group";
 38  
     
 39  
         // The Map containing element transformation values
 40  
         private Map<String,String> elementTransformationMap;
 41  
 
 42  
         // The List containing elements which will be eliminated
 43  
         private List<String> elementEliminationList;
 44  
 
 45  
         // A flag to determine if the current element is in the elimination list
 46  0
         private boolean eliminatedElement = false;
 47  
         
 48  
         // The List containing elements with attributes for transformation
 49  
         private List<String> elementAttributeTransformationList;
 50  
         
 51  
         // The list which helps keep track of where we are in the XML 
 52  
         // hierarchy as the stream is being processed
 53  0
         private List<String> groupXmlStack = new ArrayList<String>();
 54  
     
 55  
         public GroupNamespaceURIAllInOneFilterPOC(){
 56  0
                 super();
 57  
                 // Initialize the element transformation map
 58  0
                 setElementTransformationMap();
 59  
                 // Initialize the element elimination list
 60  0
                 setElementEliminationList();
 61  
                 // Initialize the element attribute transformation list
 62  0
                 setElementAttributeTransformationList();
 63  0
         }
 64  
         
 65  
     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
 66  
                 // Push the element onto the stack
 67  0
                 if (groupXmlStack.isEmpty()){
 68  
                         // Push the root element without onto the stack without special formatting
 69  0
                         groupXmlStack.add(localName);
 70  
                 }
 71  
                 else {
 72  
                         // Push a child element by appending localName to the value of the top element in the stack
 73  0
                         groupXmlStack.add(groupXmlStack.get(groupXmlStack.size()-1) + "." + localName);
 74  
                 }
 75  
                 
 76  
                 // Fetch the current element from the top of the stack
 77  0
                 String currentElement = groupXmlStack.get(groupXmlStack.size()-1);
 78  
                 
 79  
                 // Eliminate elements of concern:
 80  0
                 if (elementEliminationList.contains(currentElement)) {
 81  
                         // Flag the current element for elimination
 82  0
                         eliminatedElement = true;
 83  
                 }
 84  
                 // Perform normal parsing
 85  
                 else {
 86  0
                         eliminatedElement = false;
 87  
                         // Transform <principalName> and <principalId> sections
 88  0
                         if (currentElement.equals("data.groups.group.members.principalName") || currentElement.equals("data.groups.group.members.principalId")){
 89  0
                                 super.startElement(GROUP_URI, "member", "member", new AttributesImpl());
 90  
                         }
 91  
                                                                                                                   
 92  
                         // Transform elements of concern
 93  0
                         if (elementTransformationMap.containsKey(currentElement)){
 94  0
                                 String transformedLocalName = elementTransformationMap.get(currentElement);
 95  0
                                 String transformedQualifiedName = transformedLocalName;
 96  0
                                 super.startElement(GROUP_URI, transformedLocalName, transformedQualifiedName, getTransformedAttributes(currentElement, atts));
 97  0
                         }
 98  
                         else {
 99  
                                 // Pass other elements through as they are
 100  0
                                 super.startElement(GROUP_URI, localName, qName, atts);
 101  
                         }
 102  
                 }
 103  0
     }
 104  
 
 105  
     public void endElement(String uri, String localName, String qName) throws SAXException {
 106  
                 // Fetch the current element from the top of the stack
 107  0
                 String currentElement = groupXmlStack.get(groupXmlStack.size()-1);
 108  
         
 109  
                 // Eliminate elements of concern:
 110  0
                 if (elementEliminationList.contains(currentElement)) {
 111  
                         // Flag the current element for elimination
 112  0
                         eliminatedElement = true;
 113  
                 }
 114  
                 // Perform normal parsing behavior
 115  
                 else {
 116  0
                         eliminatedElement = false;
 117  
                         // Transform elements of concern
 118  0
                         if (elementTransformationMap.containsKey(currentElement)){
 119  0
                                 String transformedLocalName = elementTransformationMap.get(currentElement);
 120  0
                                 String transformedQualifiedName = transformedLocalName;
 121  0
                                 super.endElement(GROUP_URI, transformedLocalName, transformedQualifiedName);
 122  0
                         }
 123  
                         else {
 124  
                                 // Pass other elements through as they are
 125  0
                                 super.endElement(GROUP_URI, localName, qName);
 126  
                         }
 127  
                 
 128  
                         // Transform <principalName> and <principalId> sections
 129  0
                         if (currentElement.equals("data.groups.group.members.principalName") || currentElement.equals("data.groups.group.members.principalId")){
 130  0
                             super.startElement(GROUP_URI, "memberTypeCode", "memberTypeCode", new AttributesImpl());
 131  0
                             String memberTypeCode = "P";
 132  0
                             characters(memberTypeCode.toCharArray(), 0, 1);
 133  0
                             super.endElement(GROUP_URI, "memberTypeCode", "memberTypeCode");
 134  0
                                 super.endElement(GROUP_URI, "member", "member");
 135  
                         }
 136  
                 }
 137  
                 // Pop the element from the stack if it's not empty
 138  0
                 if (!groupXmlStack.isEmpty()){
 139  0
                         groupXmlStack.remove(currentElement);                        
 140  
                 }
 141  0
     }
 142  
     
 143  
     /*
 144  
      * Override the characters() method using an empty implementation so that the text
 145  
      * inside of each eliminated element is also eliminated. 
 146  
      */
 147  
     @Override
 148  
     public void characters (char[] ch, int start, int length) throws SAXException {        
 149  0
             if (!eliminatedElement) {
 150  0
                     super.characters(ch, start, length);
 151  
             }
 152  0
     }
 153  
 
 154  
         /*
 155  
          * Build a Map that maps elements we intend to transform to their corresponding transformed value.
 156  
          * The keys in this Map are "hierarchically-qualified" representations of the elements of concern.
 157  
          * 
 158  
          * For example, if "group" is a child of "groups", which is in turn a child of the root
 159  
          * element "data", then it is represented as "data.groups.group" in the Map.
 160  
          */
 161  
         private void setElementTransformationMap(){
 162  0
                 Map<String,String> elementTransformationMap = new HashMap<String,String>();
 163  0
                 elementTransformationMap.put("data.groups.group", "group");
 164  0
                 elementTransformationMap.put("data.groups.group.name", "groupName");
 165  0
                 elementTransformationMap.put("data.groups.group.description", "groupDescription");
 166  0
                 elementTransformationMap.put("data.groups.group.namespace", "namespaceCode");
 167  0
                 elementTransformationMap.put("data.groups.group.members.principalName", "memberId");
 168  0
                 elementTransformationMap.put("data.groups.group.members.principalId", "memberId");
 169  0
                 this.elementTransformationMap = elementTransformationMap;
 170  0
         }
 171  
         
 172  
         /*
 173  
          * Build a List defining which elements we intend to eliminate .
 174  
          * The values in this List are "hierarchically-qualified" representations of the elements of concern.
 175  
          * 
 176  
          * For example, if "group" is a child of "groups", which is in turn a child of the root
 177  
          * element "data", then it is represented as "data.groups.group" in the Map.
 178  
          */
 179  
         private void setElementEliminationList() {
 180  0
                 List<String> elementEliminationList = new ArrayList<String>();
 181  0
                 elementEliminationList.add("data");
 182  0
                 elementEliminationList.add("data.groups");
 183  0
                 elementEliminationList.add("data.groups.group.members.groupName");
 184  0
                 elementEliminationList.add("data.groups.group.members.groupName.name");
 185  0
                 elementEliminationList.add("data.groups.group.members.groupName.namespace");
 186  0
                 this.elementEliminationList = elementEliminationList;
 187  0
         }
 188  
         
 189  
         /*
 190  
          * Placeholder: Build a List defining which elements have attributes that we intend to transform
 191  
          */
 192  
         private void setElementAttributeTransformationList() {
 193  0
                 List<String> elementAttributeTransformationList = new ArrayList<String>();
 194  0
                 this.elementAttributeTransformationList = elementAttributeTransformationList;
 195  0
         }
 196  
         
 197  
         /*
 198  
          * Placeholder method adding support for transforming an element's attributes
 199  
          */
 200  
         private Attributes getTransformedAttributes(String stackRepresentedElement, Attributes attributes){
 201  
                 // If the element is found in the Element Attribute Transformation List, transform its appropriate attributes
 202  0
                 if (elementAttributeTransformationList.contains(stackRepresentedElement)){
 203  
                         // Just a placeholder, remember?
 204  0
                         return new AttributesImpl();
 205  
                 }
 206  
                 else {
 207  
                         // Otherwise, return a "hollow" Attributes object
 208  0
                         return new AttributesImpl();
 209  
                 }                
 210  
         }
 211  
 }