1 package org.apache.maven.plugin.coreit;
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.plugin.MojoExecutionException;
23 import org.codehaus.plexus.configuration.PlexusConfiguration;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.lang.reflect.Array;
32 import java.text.SimpleDateFormat;
33 import java.util.Collection;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Properties;
39
40
41
42
43
44
45 class PropertiesUtil
46 {
47
48 public static Properties read( File inputFile )
49 throws MojoExecutionException
50 {
51 Properties props = new Properties();
52
53 if ( inputFile.exists() )
54 {
55 InputStream is = null;
56 try
57 {
58 is = new FileInputStream( inputFile );
59 props.load( is );
60 }
61 catch ( IOException e )
62 {
63 throw new MojoExecutionException( "Input file " + inputFile + " could not be read: " + e.getMessage(),
64 e );
65 }
66 finally
67 {
68 if ( is != null )
69 {
70 try
71 {
72 is.close();
73 }
74 catch ( IOException e )
75 {
76
77 }
78 }
79 }
80 }
81
82 return props;
83 }
84
85 public static void write( File outputFile, Properties props )
86 throws MojoExecutionException
87 {
88 OutputStream os = null;
89 try
90 {
91 outputFile.getParentFile().mkdirs();
92 os = new FileOutputStream( outputFile );
93 props.store( os, "MAVEN-CORE-IT-LOG" );
94 }
95 catch ( IOException e )
96 {
97 throw new MojoExecutionException( "Output file " + outputFile + " could not be created: " + e.getMessage(),
98 e );
99 }
100 finally
101 {
102 if ( os != null )
103 {
104 try
105 {
106 os.close();
107 }
108 catch ( IOException e )
109 {
110
111 }
112 }
113 }
114 }
115
116 public static void serialize( Properties props, String key, Object value )
117 {
118 if ( value != null && value.getClass().isArray() )
119 {
120 props.setProperty( key, Integer.toString( Array.getLength( value ) ) );
121 for ( int i = Array.getLength( value ) - 1; i >= 0; i-- )
122 {
123 serialize( props, key + "." + i, Array.get( value, i ) );
124 }
125 }
126 else if ( value instanceof Collection )
127 {
128 Collection collection = (Collection) value;
129 props.setProperty( key, Integer.toString( collection.size() ) );
130 int i = 0;
131 for ( Iterator it = collection.iterator(); it.hasNext(); i++ )
132 {
133 serialize( props, key + "." + i, it.next() );
134 }
135 }
136 else if ( value instanceof Map )
137 {
138 Map map = (Map) value;
139 props.setProperty( key, Integer.toString( map.size() ) );
140 int i = 0;
141 for ( Iterator it = map.keySet().iterator(); it.hasNext(); i++ )
142 {
143 Object k = it.next();
144 Object v = map.get( k );
145 serialize( props, key + "." + k, v );
146 }
147 }
148 else if ( value instanceof PlexusConfiguration )
149 {
150 PlexusConfiguration config = (PlexusConfiguration) value;
151
152 String val = config.getValue( null );
153 if ( val != null )
154 {
155 props.setProperty( key + ".value", val );
156 }
157
158 String[] attributes = config.getAttributeNames();
159 props.setProperty( key + ".attributes", Integer.toString( attributes.length ) );
160 for ( int i = attributes.length - 1; i >= 0; i-- )
161 {
162 props.setProperty( key + ".attributes." + attributes[i], config.getAttribute( attributes[i], "" ) );
163 }
164
165 PlexusConfiguration children[] = config.getChildren();
166 props.setProperty( key + ".children", Integer.toString( children.length ) );
167 Map<String, Integer> indices = new HashMap<>();
168 for ( PlexusConfiguration child : children )
169 {
170 String name = child.getName();
171 Integer index = indices.get( name );
172 if ( index == null )
173 {
174 index = 0;
175 }
176 serialize( props, key + ".children." + name + "." + index, child );
177 indices.put( name, index + 1 );
178 }
179 }
180 else if ( value instanceof Date )
181 {
182 props.setProperty( key, new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( (Date) value ) );
183 }
184 else if ( value != null )
185 {
186 props.setProperty( key, value.toString() );
187 }
188 }
189
190 }