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 | 0 | { |
42 | |
|
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static void initializeBuiltIns() |
49 | |
{ |
50 | 0 | if ( implementations == null ) |
51 | |
{ |
52 | 0 | Properties props = new Properties(); |
53 | |
|
54 | 0 | ClassLoader cloader = Thread.currentThread().getContextClassLoader(); |
55 | |
|
56 | 0 | InputStream stream = null; |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 0 | stream = cloader.getResourceAsStream( MAPPER_PROPERTIES ); |
61 | |
|
62 | 0 | if ( stream == null ) |
63 | |
{ |
64 | 0 | throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES ); |
65 | |
} |
66 | |
|
67 | |
try |
68 | |
{ |
69 | 0 | props.load( stream ); |
70 | |
} |
71 | 0 | catch ( IOException e ) |
72 | |
{ |
73 | 0 | throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES ); |
74 | 0 | } |
75 | |
} |
76 | |
finally |
77 | |
{ |
78 | 0 | IOUtil.close( stream ); |
79 | 0 | } |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public static FileNameMapper getFileNameMapper( Mapper mapper ) |
91 | |
throws MapperException |
92 | |
{ |
93 | 0 | if ( mapper == null ) |
94 | |
{ |
95 | 0 | return null; |
96 | |
} |
97 | |
|
98 | 0 | initializeBuiltIns(); |
99 | |
|
100 | 0 | String type = mapper.getType(); |
101 | 0 | String classname = mapper.getClassname(); |
102 | |
|
103 | 0 | if ( type == null && classname == null ) |
104 | |
{ |
105 | 0 | throw new MapperException( "nested mapper or " + "one of the attributes type or classname is required" ); |
106 | |
} |
107 | |
|
108 | 0 | if ( type != null && classname != null ) |
109 | |
{ |
110 | 0 | throw new MapperException( "must not specify both type and classname attribute" ); |
111 | |
} |
112 | 0 | if ( type != null ) |
113 | |
{ |
114 | 0 | classname = implementations.getProperty( type ); |
115 | |
} |
116 | |
|
117 | |
try |
118 | |
{ |
119 | 0 | FileNameMapper m = (FileNameMapper) Class.forName( classname ).newInstance(); |
120 | |
|
121 | 0 | m.setFrom( mapper.getFrom() ); |
122 | 0 | m.setTo( mapper.getTo() ); |
123 | |
|
124 | 0 | return m; |
125 | |
} |
126 | 0 | catch ( ClassNotFoundException e ) |
127 | |
{ |
128 | 0 | throw new MapperException( "Cannot find mapper implementation: " + classname, e ); |
129 | |
} |
130 | 0 | catch ( InstantiationException e ) |
131 | |
{ |
132 | 0 | throw new MapperException( "Cannot load mapper implementation: " + classname, e ); |
133 | |
} |
134 | 0 | catch ( IllegalAccessException e ) |
135 | |
{ |
136 | 0 | throw new MapperException( "Cannot load mapper implementation: " + classname, e ); |
137 | |
} |
138 | |
} |
139 | |
} |