| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GroupNamespaceURIEliminationFilterPOC |
|
| 2.0;2 |
| 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.List; | |
| 20 | ||
| 21 | import org.xml.sax.Attributes; | |
| 22 | import org.xml.sax.SAXException; | |
| 23 | import org.xml.sax.helpers.XMLFilterImpl; | |
| 24 | ||
| 25 | /** | |
| 26 | * This XML Filter is used to eliminate unwanted elements from a pre-1.0.3 Group XML document. | |
| 27 | * As an example: | |
| 28 | * | |
| 29 | * -- Example from GroupXmlImportTest.xml as filtered by GroupNamespaceURIEliminationFilterPOC -- | |
| 30 | * <members> | |
| 31 | * <principalName>ewestfal</principalName> | |
| 32 | * <principalName>rkirkend</principalName> | |
| 33 | * <principalId>2015</principalId> | |
| 34 | * <groupName> | |
| 35 | * <name>TestWorkgroup</name> | |
| 36 | * <namespace>KR-WKFLW</namespace> | |
| 37 | * </groupName> | |
| 38 | * </members> | |
| 39 | * | |
| 40 | * -- Resulting transformation -- | |
| 41 | * <members> | |
| 42 | * <principalName>ewestfal</principalName> | |
| 43 | * <principalName>rkirkend</principalName> | |
| 44 | * <principalId>2015</principalId> | |
| 45 | * </members> | |
| 46 | * | |
| 47 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 48 | * | |
| 49 | */ | |
| 50 | public class GroupNamespaceURIEliminationFilterPOC extends XMLFilterImpl { | |
| 51 | ||
| 52 | // The URI of a Group 1.0.3 schema | |
| 53 | public static final String GROUP_URI="http://rice.kuali.org/xsd/kim/group"; | |
| 54 | ||
| 55 | // The List containing elements which will be eliminated | |
| 56 | private List<String> elementEliminationList; | |
| 57 | ||
| 58 | // A flag to determine if the current element is in the elimination list | |
| 59 | 0 | private boolean eliminatedElement = false; |
| 60 | ||
| 61 | // The list which helps keep track of where we are in the XML | |
| 62 | // hierarchy as the stream is being processed | |
| 63 | 0 | private List<String> groupXmlStack = new ArrayList<String>(); |
| 64 | ||
| 65 | public GroupNamespaceURIEliminationFilterPOC(){ | |
| 66 | 0 | super(); |
| 67 | ||
| 68 | // Initialize the element elimination list | |
| 69 | 0 | setElementEliminationList(); |
| 70 | 0 | } |
| 71 | ||
| 72 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { | |
| 73 | // Push the element onto the stack | |
| 74 | 0 | if (groupXmlStack.isEmpty()){ |
| 75 | // Push the root element without onto the stack without special formatting | |
| 76 | 0 | groupXmlStack.add(localName); |
| 77 | } | |
| 78 | else { | |
| 79 | // Push a child element by appending localName to the value of the top element in the stack | |
| 80 | 0 | groupXmlStack.add(groupXmlStack.get(groupXmlStack.size()-1) + "." + localName); |
| 81 | } | |
| 82 | ||
| 83 | // Fetch the current element from the top of the stack | |
| 84 | 0 | String currentElement = groupXmlStack.get(groupXmlStack.size()-1); |
| 85 | ||
| 86 | // Eliminate elements of concern: | |
| 87 | 0 | if (elementEliminationList.contains(currentElement)) { |
| 88 | // Flag the current element for elimination | |
| 89 | 0 | eliminatedElement = true; |
| 90 | } | |
| 91 | // Perform normal parsing | |
| 92 | else { | |
| 93 | 0 | eliminatedElement = false; |
| 94 | 0 | super.startElement(GROUP_URI, localName, qName, atts); |
| 95 | } | |
| 96 | 0 | } |
| 97 | ||
| 98 | public void endElement(String uri, String localName, String qName) throws SAXException { | |
| 99 | // Fetch the current element from the top of the stack | |
| 100 | 0 | String currentElement = groupXmlStack.get(groupXmlStack.size()-1); |
| 101 | ||
| 102 | // Eliminate elements of concern: | |
| 103 | 0 | if (elementEliminationList.contains(currentElement)) { |
| 104 | // Flag the current element for elimination | |
| 105 | 0 | eliminatedElement = true; |
| 106 | } | |
| 107 | // Perform normal parsing behavior | |
| 108 | else { | |
| 109 | 0 | eliminatedElement = false; |
| 110 | 0 | super.endElement(GROUP_URI, localName, qName); |
| 111 | } | |
| 112 | ||
| 113 | // Pop the element from the stack if it's not empty | |
| 114 | 0 | if (!groupXmlStack.isEmpty()){ |
| 115 | 0 | groupXmlStack.remove(currentElement); |
| 116 | } | |
| 117 | 0 | } |
| 118 | ||
| 119 | /* | |
| 120 | * Override the characters() method using an empty implementation so that the text | |
| 121 | * inside of each eliminated element is also eliminated. | |
| 122 | */ | |
| 123 | @Override | |
| 124 | public void characters (char[] ch, int start, int length) throws SAXException { | |
| 125 | 0 | if (!eliminatedElement) { |
| 126 | 0 | super.characters(ch, start, length); |
| 127 | } | |
| 128 | 0 | } |
| 129 | ||
| 130 | /* | |
| 131 | * Build a List defining which elements we intend to eliminate . | |
| 132 | * The values in this List are "hierarchically-qualified" representations of the elements of concern. | |
| 133 | * | |
| 134 | * For example, if "group" is a child of "groups", which is in turn a child of the root | |
| 135 | * element "data", then it is represented as "data.groups.group" in the Map. | |
| 136 | */ | |
| 137 | private void setElementEliminationList() { | |
| 138 | 0 | List<String> elementEliminationList = new ArrayList<String>(); |
| 139 | 0 | elementEliminationList.add("data"); |
| 140 | 0 | elementEliminationList.add("data.groups"); |
| 141 | 0 | elementEliminationList.add("data.groups.group.members.groupName"); |
| 142 | 0 | elementEliminationList.add("data.groups.group.members.groupName.name"); |
| 143 | 0 | elementEliminationList.add("data.groups.group.members.groupName.namespace"); |
| 144 | 0 | this.elementEliminationList = elementEliminationList; |
| 145 | 0 | } |
| 146 | } |