1 package org.apache.maven.index.reader;
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.index.reader.Record.EntryKey;
23 import org.apache.maven.index.reader.Record.Type;
24 import org.apache.maven.index.reader.ResourceHandler.Resource;
25 import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Properties;
35 import java.util.TimeZone;
36 import java.util.regex.Pattern;
37
38
39
40
41
42
43 public final class Utils
44 {
45 private Utils()
46 {
47
48 }
49
50 public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
51
52 public static final DateFormat INDEX_DATE_FORMAT;
53
54 static
55 {
56 INDEX_DATE_FORMAT = new SimpleDateFormat( "yyyyMMddHHmmss.SSS Z" );
57 INDEX_DATE_FORMAT.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
58 }
59
60 public static final String FIELD_SEPARATOR = "|";
61
62 public static final String NOT_AVAILABLE = "NA";
63
64 public static final String UINFO = "u";
65
66 public static final String INFO = "i";
67
68 public static final Pattern FS_PATTERN = Pattern.compile( Pattern.quote( FIELD_SEPARATOR ) );
69
70
71
72
73 public static Properties loadProperties( final InputStream inputStream )
74 throws IOException
75 {
76 try
77 {
78 final Properties properties = new Properties();
79 properties.load( inputStream );
80 return properties;
81 }
82 finally
83 {
84 inputStream.close();
85 }
86 }
87
88
89
90
91
92 public static Properties loadProperties( final Resource resource )
93 throws IOException
94 {
95 final InputStream inputStream = resource.read();
96 if ( inputStream == null )
97 {
98 return null;
99 }
100 return loadProperties( inputStream );
101 }
102
103
104
105
106 public static void storeProperties( final OutputStream outputStream, final Properties properties )
107 throws IOException
108 {
109 try
110 {
111 properties.store( outputStream, "Maven Indexer Writer" );
112 }
113 finally
114 {
115 outputStream.close();
116 }
117 }
118
119
120
121
122
123 public static void storeProperties( final WritableResource writableResource, final Properties properties )
124 throws IOException
125 {
126 try
127 {
128 storeProperties( writableResource.write(), properties );
129 }
130 finally
131 {
132 writableResource.close();
133 }
134 }
135
136
137
138
139 public static Record descriptor( final String repoId )
140 {
141 HashMap<EntryKey, Object> entries = new HashMap<>();
142 entries.put( Record.REPOSITORY_ID, repoId );
143 return new Record( Type.DESCRIPTOR, entries );
144 }
145
146
147
148
149 public static Record allGroups( final Collection<String> allGroups )
150 {
151 HashMap<EntryKey, Object> entries = new HashMap<>();
152 entries.put( Record.ALL_GROUPS, allGroups.toArray( new String[allGroups.size()] ) );
153 return new Record( Type.ALL_GROUPS, entries );
154 }
155
156
157
158
159 public static Record rootGroups( final Collection<String> rootGroups )
160 {
161 HashMap<EntryKey, Object> entries = new HashMap<>();
162 entries.put( Record.ROOT_GROUPS, rootGroups.toArray( new String[rootGroups.size()] ) );
163 return new Record( Type.ROOT_GROUPS, entries );
164 }
165
166
167
168
169 public static String renvl( final String v )
170 {
171 return NOT_AVAILABLE.equals( v ) ? null : v;
172 }
173
174
175
176
177 public static String nvl( final String v )
178 {
179 return v == null ? NOT_AVAILABLE : v;
180 }
181
182
183
184
185 public static String rootGroup( final String groupId )
186 {
187 int n = groupId.indexOf( '.' );
188 if ( n > -1 )
189 {
190 return groupId.substring( 0, n );
191 }
192 return groupId;
193 }
194 }