01 /*
02 * FeatureMapFactoryBean.java
03 *
04 * Copyright (c) 1995-2010, The University of Sheffield. See the file
05 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06 *
07 * This file is part of GATE (see http://gate.ac.uk/), and is free
08 * software, licenced under the GNU Library General Public License,
09 * Version 2, June 1991 (in the distribution as file licence.html,
10 * and also available at http://gate.ac.uk/gate/licence.html).
11 *
12 * Ian Roberts, 22/Jan/2008
13 *
14 * $Id: FeatureMapFactoryBean.java 12940 2010-08-08 17:41:22Z ian_roberts $
15 */
16
17 package gate.util.spring;
18
19 import gate.Factory;
20 import gate.FeatureMap;
21
22 import java.io.IOException;
23 import java.util.Map;
24
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.core.io.Resource;
27
28 /**
29 * Spring FactoryBean to create a FeatureMap from a source Map
30 * (typically one created with a <map> element in a spring config
31 * file). Values in the source map whose type is Spring "Resource" are
32 * converted to URLs in the feature map (file: URLs if possible). If you
33 * are defining the source map inline (as opposed to ref-ing another
34 * bean) the following shorthand is available.
35 *
36 * <pre>
37 * <gate:feature-map>
38 * <entry key="kind" value="word" />
39 * </gate:feature-map>
40 * </pre>
41 *
42 * The <code>entry</code> elements follow the same pattern as those in
43 * a <code><map></code> element in normal Spring configuration.
44 * See {@link Init} for an example of how to include the
45 * <code>gate</code> namespace.
46 */
47 public class FeatureMapFactoryBean extends GateAwareObject implements
48 FactoryBean {
49
50 private Map<Object, Object> sourceMap;
51
52 public void setSourceMap(Map<Object, Object> sourceMap) {
53 this.sourceMap = sourceMap;
54 }
55
56 public Object getObject() throws IOException {
57 ensureGateInit();
58 FeatureMap fm = Factory.newFeatureMap();
59 if(sourceMap != null) {
60 for(Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
61 Object key = entry.getKey();
62 Object value = entry.getValue();
63
64 // convert Spring resources to URLs
65 if(value instanceof Resource) {
66 value = SpringFactory.resourceToUrl((Resource)value);
67 }
68
69 fm.put(key, value);
70 }
71 }
72
73 return fm;
74 }
75
76 public Class getObjectType() {
77 return gate.FeatureMap.class;
78 }
79
80 public boolean isSingleton() {
81 return false;
82 }
83
84 }
|