1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.index.reader;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Properties;
29  import java.util.TimeZone;
30  import java.util.regex.Pattern;
31  
32  import org.apache.maven.index.reader.Record.EntryKey;
33  import org.apache.maven.index.reader.Record.Type;
34  import org.apache.maven.index.reader.ResourceHandler.Resource;
35  import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
36  
37  
38  
39  
40  
41  
42  public final class Utils {
43      private Utils() {
44          
45      }
46  
47      public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
48  
49      public static final DateFormat INDEX_DATE_FORMAT;
50  
51      static {
52          INDEX_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z");
53          INDEX_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
54      }
55  
56      public static final String FIELD_SEPARATOR = "|";
57  
58      public static final String NOT_AVAILABLE = "NA";
59  
60      public static final String UINFO = "u";
61  
62      public static final String INFO = "i";
63  
64      public static final Pattern FS_PATTERN = Pattern.compile(Pattern.quote(FIELD_SEPARATOR));
65  
66      
67  
68  
69  
70      public static Properties loadProperties(final Resource resource) throws IOException {
71          try (InputStream inputStream = resource.read()) {
72              if (inputStream == null) {
73                  return null;
74              }
75              final Properties properties = new Properties();
76              properties.load(inputStream);
77              return properties;
78          }
79      }
80  
81      
82  
83  
84      public static void storeProperties(final WritableResource writableResource, final Properties properties)
85              throws IOException {
86          try (writableResource) {
87              try (OutputStream outputStream = writableResource.write()) {
88                  properties.store(outputStream, "Maven Indexer Writer");
89              }
90          }
91      }
92  
93      
94  
95  
96      public static Record descriptor(final String repoId) {
97          HashMap<EntryKey, Object> entries = new HashMap<>();
98          entries.put(Record.REPOSITORY_ID, repoId);
99          return new Record(Type.DESCRIPTOR, entries);
100     }
101 
102     
103 
104 
105     public static Record allGroups(final Collection<String> allGroups) {
106         HashMap<EntryKey, Object> entries = new HashMap<>();
107         entries.put(Record.ALL_GROUPS, allGroups.toArray(new String[0]));
108         return new Record(Type.ALL_GROUPS, entries);
109     }
110 
111     
112 
113 
114     public static Record rootGroups(final Collection<String> rootGroups) {
115         HashMap<EntryKey, Object> entries = new HashMap<>();
116         entries.put(Record.ROOT_GROUPS, rootGroups.toArray(new String[0]));
117         return new Record(Type.ROOT_GROUPS, entries);
118     }
119 
120     
121 
122 
123     public static String renvl(final String v) {
124         return NOT_AVAILABLE.equals(v) ? null : v;
125     }
126 
127     
128 
129 
130     public static String nvl(final String v) {
131         return v == null ? NOT_AVAILABLE : v;
132     }
133 
134     
135 
136 
137     public static String rootGroup(final String groupId) {
138         int n = groupId.indexOf('.');
139         if (n > -1) {
140             return groupId.substring(0, n);
141         }
142         return groupId;
143     }
144 }