]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/lib/linux/rpmfactory/RpmSpecFile.java
899603a7b93707f3861abde1a3ee5e6e39cf5c51
[gpl/argeo-slc.git] / org.argeo.slc.support / src / org / argeo / slc / lib / linux / rpmfactory / RpmSpecFile.java
1 package org.argeo.slc.lib.linux.rpmfactory;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.commons.io.IOUtils;
9 import org.springframework.core.io.Resource;
10
11 public class RpmSpecFile {
12 private Resource specFile;
13
14 private String name;
15 private String version;
16 private String release;
17 private Map<String, String> sources = new HashMap<String, String>();
18 private Map<String, String> patches = new HashMap<String, String>();
19
20 public RpmSpecFile(Resource specFile) {
21 this.specFile = specFile;
22 parseSpecFile();
23 }
24
25 public void init() {
26 parseSpecFile();
27 }
28
29 @SuppressWarnings("unchecked")
30 protected void parseSpecFile() {
31 try {
32 List<String> lines = (List<String>) IOUtils.readLines(specFile
33 .getInputStream());
34
35 lines: for (String line : lines) {
36 int indexSemiColon = line.indexOf(':');
37 if (indexSemiColon <= 0)
38 continue lines;
39 String directive = line.substring(0, indexSemiColon).trim();
40 String value = line.substring(indexSemiColon + 1).trim();
41 if ("name".equals(directive.toLowerCase()))
42 name = value;
43 else if ("version".equals(directive.toLowerCase()))
44 version = value;
45 else if ("release".equals(directive.toLowerCase()))
46 release = value;
47 else if (directive.toLowerCase().startsWith("source"))
48 sources.put(directive, interpret(value));
49 else if (directive.toLowerCase().startsWith("patch"))
50 patches.put(directive, interpret(value));
51 }
52
53 } catch (IOException e) {
54 throw new RuntimeException("Cannot parse spec file " + specFile, e);
55 }
56 }
57
58 protected String interpret(String value) {
59 StringBuffer buf = new StringBuffer(value.length());
60 StringBuffer currKey = null;
61 boolean mayBeKey = false;
62 chars: for (char c : value.toCharArray()) {
63 if (c == '%')
64 mayBeKey = true;
65 else if (c == '{') {
66 if (mayBeKey)
67 currKey = new StringBuffer();
68 } else if (c == '}') {
69 if (currKey == null)
70 continue chars;
71 String key = currKey.toString();
72 if ("name".equals(key.toLowerCase()))
73 buf.append(name);
74 else if ("version".equals(key.toLowerCase()))
75 buf.append(version);
76 else
77 buf.append("%{").append(key).append('}');
78 currKey = null;
79 } else {
80 if (currKey != null)
81 currKey.append(c);
82 else
83 buf.append(c);
84 }
85 }
86 return buf.toString();
87 }
88
89 public Resource getSpecFile() {
90 return specFile;
91 }
92
93 public String getName() {
94 return name;
95 }
96
97 public String getVersion() {
98 return version;
99 }
100
101 public String getRelease() {
102 return release;
103 }
104
105 public Map<String, String> getSources() {
106 return sources;
107 }
108
109 public Map<String, String> getPatches() {
110 return patches;
111 }
112
113 }