View Javadoc

1   /*
2    * Copyright 2007 Hippo
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" 
12   * BASIS, 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 nl.hippo.portal.cms.source;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  /***
24   * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
25   *
26   * @version $Id: SourceResolverImpl.java 10568 2008-02-19 21:20:00Z ddam $
27   */
28  public class SourceResolverImpl implements SourceResolver {
29  
30      private Map sourceFactories=new HashMap(0);
31      
32      private String getSourceFactoryName(String url){
33          return StringUtils.substringBefore(url,":");
34      }
35      
36      public Source resolve(String url) throws SourceException { 
37          String sourceFactoryName = getSourceFactoryName(url);
38          SourceFactory sourceFactory = getSourceFactory(getSourceFactoryName(url));
39          if (sourceFactory != null){
40              return sourceFactory.getSource(url);
41          }
42          
43          throw new SourceException("Could not resolve '"+sourceFactoryName+"' source with url "+url);
44      }
45      
46      public void setSourceFactories(Map factories) {
47          this.sourceFactories=factories;
48      }
49      
50      protected SourceFactory getSourceFactory(String name){
51          Object obj = sourceFactories.get(name);
52          if (obj!=null){
53              return (SourceFactory)obj;
54          }
55          return null;
56      }
57  
58  
59  }