1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer;
19
20 import java.lang.reflect.Method;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.xml.namespace.QName;
28
29 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
30 import org.apache.xerces.xs.XSConstants;
31 import org.apache.xerces.xs.XSObject;
32 import org.apache.xerces.xs.XSObjectList;
33 import org.apache.xerces.xs.XSTypeDefinition;
34
35 import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
36
37
38
39
40
41
42
43
44 public class SameNameDifferentTypeFinderWorker extends NamespaceFilteredWorker implements XsComponentWorker {
45
46 private final Map<QName, XSObjectListImpl> typesWithSameName;
47
48
49
50
51 public SameNameDifferentTypeFinderWorker() {
52 super();
53 typesWithSameName = new HashMap<QName, XSObjectListImpl>();
54 }
55
56
57
58
59 public synchronized Map<QName, ? extends XSObjectList> getTypesWithSameName() {
60 removeSingularTypes();
61 return Collections.unmodifiableMap(typesWithSameName);
62 }
63
64
65
66
67
68
69
70 @Override
71 public void execute(XSObject object, XSObject parent) {
72 try {
73 Method m = object.getClass().getMethod("getTypeDefinition", new Class[] {});
74 XSTypeDefinition typedef = (XSTypeDefinition) m.invoke(object, new Object[] {});
75 this.addTypeWithSameName(new QName(object.getNamespace(), object.getName()), typedef);
76 }
77 catch (Exception e) {
78 logger.error("Cannot invoke method getTypeDefinition() on " + object + ".", e);
79 }
80 }
81
82
83
84
85
86
87 @Override
88 public boolean isSupported(XSObject object) {
89 return object.getType() == XSConstants.ELEMENT_DECLARATION || object.getType() == XSConstants.ATTRIBUTE_DECLARATION;
90 }
91
92 private void addTypeWithSameName(QName qname, XSTypeDefinition type) {
93
94 final XSObjectListImpl xsoList;
95 if (typesWithSameName.containsKey(qname)) {
96 xsoList = typesWithSameName.get(qname);
97 for (int i = 0; i < xsoList.getLength(); ++i) {
98 XSObject object = xsoList.item(i);
99
100 if (XSModelHelper.isSameTypeDefinition(object, type)) { return; }
101 }
102 xsoList.add(type);
103 }
104 else {
105 xsoList = new XSObjectListImpl();
106 xsoList.add(type);
107 typesWithSameName.put(qname, xsoList);
108 }
109 }
110
111 private void removeSingularTypes() {
112 Set<QName> keys = typesWithSameName.keySet();
113 for (Iterator<QName> it = keys.iterator(); it.hasNext();) {
114 QName key = it.next();
115 if (typesWithSameName.get(key).getLength() < 2) {
116 logger.debug("Removing key [" + key + "] having count < 2");
117 it.remove();
118 }
119 }
120 }
121
122 }