1 // $Id: TypeUsageWorker.java 165 2009-05-28 21:46:38Z agony $
2 /*
3 * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7 * version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 */
15 /**
16 *
17 */
18 package de.mindcrimeilab.xsanalyzer;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
25 import org.apache.xerces.xs.XSObject;
26 import org.apache.xerces.xs.XSObjectList;
27 import org.apache.xerces.xs.XSTypeDefinition;
28
29 import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
30
31 /**
32 * @author Michael Engelhardt<me@mindcrime-ilab.de>
33 * @author $Author: agony $
34 * @version $Revision: 165 $
35 *
36 */
37 public class TypeUsageWorker extends AbstractXsComponentWorker implements XsComponentWorker {
38
39 /** XRef Map of types and respective users */
40 private final Map<XSObject, XSObjectListImpl> usedBy;
41
42 public TypeUsageWorker() {
43 super();
44 usedBy = new HashMap<XSObject, XSObjectListImpl>();
45 }
46
47 /**
48 * Returns the xref map of types and users
49 *
50 * @return the usedBy
51 */
52 public Map<XSObject, ? extends XSObjectList> getUsedBy() {
53 return Collections.unmodifiableMap(usedBy);
54 }
55
56 /*
57 * (non-Javadoc)
58 *
59 * @see de.mindcrimeilab.xsanalyzer.XsComponentWorker#execute(org.apache.xerces .xs.XSObject,
60 * org.apache.xerces.xs.XSObject)
61 */
62 @Override
63 public void execute(XSObject object, XSObject parent) {
64 final XSTypeDefinition type = XSModelHelper.getBaseType(object);
65 if (null == type) { return; }
66
67 addUsedByType(object, type);
68
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see de.mindcrimeilab.xsanalyzer.XsComponentWorker#isSupported(org.apache. xerces.xs.XSObject)
75 */
76 @Override
77 public boolean isSupported(XSObject object) {
78 // TODO Auto-generated method stub
79 return true;
80 }
81
82 /**
83 * Add xref to usedBy map.
84 *
85 * @param element
86 * element which is used by <code>type<code>
87 * @param type
88 * type definition
89 */
90 private void addUsedByType(XSObject element, XSTypeDefinition type) {
91 // do not add null ;)
92 if (null == element) { return; }
93
94 // check for self references
95 if (XSModelHelper.isSameTypeDefinition(element, type)) { return; }
96
97 if (usedBy.containsKey(type)) {
98 final XSObjectListImpl objectList = usedBy.get(type);
99 for (int i = 0; i < objectList.getLength(); ++i) {
100 XSObject object = objectList.item(i);
101 // check for duplicate types
102 if (XSModelHelper.isSameTypeDefinition(object, element)) { return; }
103 }
104 objectList.add(element);
105 }
106 else {
107 final XSObjectListImpl xsoList = new XSObjectListImpl();
108 xsoList.add(element);
109 usedBy.put(type, xsoList);
110 }
111 }
112
113 }