1 package org.apache.maven.shared.model.fileset.mappers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.model.fileset.Mapper;
23 import org.codehaus.plexus.util.IOUtil;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Properties;
28
29
30
31
32
33
34 public final class MapperUtil
35 {
36 private static final String MAPPER_PROPERTIES = "mapper.properties";
37
38 private static Properties implementations;
39
40 private MapperUtil()
41 {
42
43 }
44
45
46
47
48 private static void initializeBuiltIns()
49 {
50 if ( implementations == null )
51 {
52 Properties props = new Properties();
53
54 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
55
56 InputStream stream = null;
57
58 try
59 {
60 stream = cloader.getResourceAsStream( MAPPER_PROPERTIES );
61
62 if ( stream == null )
63 {
64 throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
65 }
66
67 try
68 {
69 props.load( stream );
70 }
71 catch ( IOException e )
72 {
73 throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
74 }
75 }
76 finally
77 {
78 IOUtil.close( stream );
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90 public static FileNameMapper getFileNameMapper( Mapper mapper )
91 throws MapperException
92 {
93 if ( mapper == null )
94 {
95 return null;
96 }
97
98 initializeBuiltIns();
99
100 String type = mapper.getType();
101 String classname = mapper.getClassname();
102
103 if ( type == null && classname == null )
104 {
105 throw new MapperException( "nested mapper or " + "one of the attributes type or classname is required" );
106 }
107
108 if ( type != null && classname != null )
109 {
110 throw new MapperException( "must not specify both type and classname attribute" );
111 }
112 if ( type != null )
113 {
114 classname = implementations.getProperty( type );
115 }
116
117 try
118 {
119 FileNameMapper m = (FileNameMapper) Class.forName( classname ).newInstance();
120
121 m.setFrom( mapper.getFrom() );
122 m.setTo( mapper.getTo() );
123
124 return m;
125 }
126 catch ( ClassNotFoundException e )
127 {
128 throw new MapperException( "Cannot find mapper implementation: " + classname, e );
129 }
130 catch ( InstantiationException e )
131 {
132 throw new MapperException( "Cannot load mapper implementation: " + classname, e );
133 }
134 catch ( IllegalAccessException e )
135 {
136 throw new MapperException( "Cannot load mapper implementation: " + classname, e );
137 }
138 }
139 }