Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TestGroupNamespaceURIFilter |
|
| 3.1666666666666665;3.167 |
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 is a description of what this class does - eldavid don't forget to fill this in. | |
30 | * | |
31 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
32 | * | |
33 | */ | |
34 | public class TestGroupNamespaceURIFilter 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 with attributes for transformation | |
43 | private List<String> elementAttributeTransformationList; | |
44 | ||
45 | // The list which helps keep track of where we are in the XML | |
46 | // hierarchy as the stream is being processed | |
47 | 0 | private List<String> groupXmlStack = new ArrayList<String>(); |
48 | ||
49 | public TestGroupNamespaceURIFilter(){ | |
50 | 0 | super(); |
51 | // Initialize the element transformation map | |
52 | 0 | setElementTransformationMap(); |
53 | // Initialize the element attribute transformation list | |
54 | 0 | setElementAttributeTransformationList(); |
55 | 0 | } |
56 | ||
57 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { | |
58 | // Push the element onto the stack | |
59 | 0 | if (groupXmlStack.isEmpty()){ |
60 | // Push the root element without onto the stack without special formatting | |
61 | 0 | groupXmlStack.add(localName); |
62 | } | |
63 | else { | |
64 | // Push a child element by appending localName to the value of the top element in the stack | |
65 | 0 | groupXmlStack.add(groupXmlStack.get(groupXmlStack.size()-1) + "." + localName); |
66 | } | |
67 | ||
68 | // Toss "data" and "groups" | |
69 | 0 | if (localName.equals("data") || localName.equals("groups")){ |
70 | 0 | return; |
71 | } | |
72 | ||
73 | // Transform elements of concern | |
74 | 0 | if (elementTransformationMap.containsKey(groupXmlStack.get(groupXmlStack.size()-1))){ |
75 | 0 | String targetLocalName = elementTransformationMap.get(groupXmlStack.get(groupXmlStack.size()-1)); |
76 | 0 | String targetQualifiedName = targetLocalName; |
77 | 0 | super.startElement(GROUP_URI, targetLocalName, targetQualifiedName, getTransformedAttributes(groupXmlStack.get(groupXmlStack.size()-1), atts)); |
78 | 0 | } |
79 | // Pass other elements through as they are | |
80 | else { | |
81 | 0 | super.startElement(GROUP_URI, localName, qName, atts); |
82 | } | |
83 | 0 | } |
84 | ||
85 | public void endElement(String uri, String localName, String qName) throws SAXException { | |
86 | // Fetch the value of the element from the top of the stack | |
87 | 0 | String topStackElement = groupXmlStack.get(groupXmlStack.size()-1); |
88 | ||
89 | // Toss "data" and "groups" | |
90 | 0 | if (localName.equals("data") || localName.equals("groups")){ |
91 | // Pop the element from the stack if it's not empty | |
92 | 0 | if (!groupXmlStack.isEmpty()){ |
93 | 0 | groupXmlStack.remove(topStackElement); |
94 | } | |
95 | 0 | return; |
96 | } | |
97 | ||
98 | // Transform elements of concern | |
99 | 0 | if (elementTransformationMap.containsKey(topStackElement)){ |
100 | 0 | String targetLocalName = elementTransformationMap.get(topStackElement); |
101 | 0 | String targetQualifiedName = targetLocalName; |
102 | 0 | super.endElement(GROUP_URI, targetLocalName, targetQualifiedName); |
103 | 0 | } |
104 | // Pass other elements through as they are | |
105 | else { | |
106 | 0 | super.endElement(GROUP_URI, localName, qName); |
107 | } | |
108 | ||
109 | // Pop the element from the stack if it's not empty | |
110 | 0 | if (!groupXmlStack.isEmpty()){ |
111 | 0 | groupXmlStack.remove(topStackElement); |
112 | } | |
113 | ||
114 | 0 | } |
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 | 0 | Map<String,String> elementTransformationMap = new HashMap<String,String>(); |
125 | 0 | elementTransformationMap.put("data.groups.group", "group"); |
126 | 0 | elementTransformationMap.put("data.groups.group.name", "groupName"); |
127 | 0 | elementTransformationMap.put("data.groups.group.description", "groupDescription"); |
128 | 0 | elementTransformationMap.put("data.groups.group.namespace", "namespaceCode"); |
129 | //elementTransformationMap.put("data.groups.group.members.principalName", "memberId"); | |
130 | 0 | this.elementTransformationMap = elementTransformationMap; |
131 | 0 | } |
132 | ||
133 | /* | |
134 | * Placeholder method for defining which elements have attributes that we intend to transform | |
135 | */ | |
136 | private void setElementAttributeTransformationList(){ | |
137 | 0 | List<String> elementAttributeTransformationList = new ArrayList<String>(); |
138 | 0 | this.elementAttributeTransformationList = elementAttributeTransformationList; |
139 | 0 | } |
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 | 0 | if (elementAttributeTransformationList.contains(stackRepresentedElement)){ |
147 | // Just a placeholder, remember? | |
148 | 0 | return new AttributesImpl(); |
149 | } | |
150 | else { | |
151 | // Otherwise, return a "hollow" Attributes object | |
152 | 0 | return new AttributesImpl(); |
153 | } | |
154 | } | |
155 | } |