View Javadoc

1   // $Id: UnusedTypeFinderWorker.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.HashSet;
22  import java.util.Set;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.xerces.xs.XSObject;
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 UnusedTypeFinderWorker extends NamespaceFilteredWorker implements XsComponentWorker {
38  
39      private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
40  
41      /** List of unused types */
42      private final Set<XSTypeDefinition> unusedTypes;
43  
44      public UnusedTypeFinderWorker(Set<? extends XSTypeDefinition> definedTypes) {
45          super();
46          unusedTypes = new HashSet<XSTypeDefinition>();
47          /* threat all defined types as potentially unsed types. */
48          unusedTypes.addAll(definedTypes);
49      }
50  
51      /**
52       * Returns the list of unused types in given model.
53       * 
54       * @return the unusedTypes
55       */
56      public Set<XSTypeDefinition> getUnusedTypes() {
57          return Collections.unmodifiableSet(unusedTypes);
58      }
59  
60      @Override
61      public boolean isSupported(XSObject object) {
62          return super.isSupported(object);
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see de.mindcrimeilab.xsanalyzer.XsComponentWorker#execute(org.apache.xerces .xs.XSObject)
69       */
70      @Override
71      public void execute(XSObject object, XSObject parent) {
72          /*
73           * LOOK FORWARD
74           * 
75           * get base type of given object and check if the type is in the list of global defined types.
76           * 
77           * There are is one special case, if it is a simple type of varity list, there is no real base type.
78           */
79          final XSTypeDefinition type = XSModelHelper.getBaseType(object);
80  
81          if (null != type) {
82              boolean result = unusedTypes.remove(type);
83              UnusedTypeFinderWorker.logger.debug("Remove result is [" + result + "]");
84          }
85  
86          // checkUsage(definedTypes, object, (XSTypeDefinition) parent);
87      }
88  }