View Javadoc
1   /*
2    * Copyright 2005 Ralf Joachim
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.castor.mapping;
17  
18  import org.exolab.castor.mapping.xml.ClassMapping;
19  import org.exolab.castor.mapping.xml.MappingRoot;
20  import org.exolab.castor.xml.IDResolver;
21  
22  /**
23   * An IDResolver to allow us to resolve ClassMappings from included Mapping files.
24   * 
25   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
26   * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
27   */
28  public final class MappingUnmarshallIDResolver implements IDResolver {
29      private MappingRoot _mapping = null;
30  
31      public MappingUnmarshallIDResolver() { super(); }
32  
33      public void setMapping(final MappingRoot mapping) { _mapping = mapping; }
34  
35      /**
36       * Returns the Object whose id matches the given IDREF, or null if no Object was
37       * found.
38       * 
39       * @param idref the IDREF to resolve.
40       * @return the Object whose id matches the given IDREF.
41       * @see org.exolab.castor.xml.IDResolver#resolve(java.lang.String)
42       */
43      public Object resolve(final String idref) {
44          if (_mapping == null) { return null; }
45          for (int i = 0; i < _mapping.getClassMappingCount(); i++) {
46              ClassMapping clsMap = _mapping.getClassMapping(i);
47              if (idref.equals(clsMap.getName())) { return clsMap; }
48          }
49          return null;
50      }
51  }