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
74 public static Properties loadProperties( final Resource resource )
75 throws IOException
76 {
77 try ( InputStream inputStream = resource.read() )
78 {
79 if ( inputStream == null )
80 {
81 return null;
82 }
83 final Properties properties = new Properties();
84 properties.load( inputStream );
85 return properties;
86 }
87 }
88
89
90
91
92 public static void storeProperties( final WritableResource writableResource, final Properties properties )
93 throws IOException
94 {
95 try ( writableResource )
96 {
97 try ( OutputStream outputStream = writableResource.write() )
98 {
99 properties.store( outputStream, "Maven Indexer Writer" );
100 }
101 }
102 }
103
104
105
106
107 public static Record descriptor( final String repoId )
108 {
109 HashMap<EntryKey, Object> entries = new HashMap<>();
110 entries.put( Record.REPOSITORY_ID, repoId );
111 return new Record( Type.DESCRIPTOR, entries );
112 }
113
114
115
116
117 public static Record allGroups( final Collection<String> allGroups )
118 {
119 HashMap<EntryKey, Object> entries = new HashMap<>();
120 entries.put( Record.ALL_GROUPS, allGroups.toArray( new String[0] ) );
121 return new Record( Type.ALL_GROUPS, entries );
122 }
123
124
125
126
127 public static Record rootGroups( final Collection<String> rootGroups )
128 {
129 HashMap<EntryKey, Object> entries = new HashMap<>();
130 entries.put( Record.ROOT_GROUPS, rootGroups.toArray( new String[0] ) );
131 return new Record( Type.ROOT_GROUPS, entries );
132 }
133
134
135
136
137 public static String renvl( final String v )
138 {
139 return NOT_AVAILABLE.equals( v ) ? null : v;
140 }
141
142
143
144
145 public static String nvl( final String v )
146 {
147 return v == null ? NOT_AVAILABLE : v;
148 }
149
150
151
152
153 public static String rootGroup( final String groupId )
154 {
155 int n = groupId.indexOf( '.' );
156 if ( n > -1 )
157 {
158 return groupId.substring( 0, n );
159 }
160 return groupId;
161 }
162 }