]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/RpmSpecFile.java
Refactor RPM Factory
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / RpmSpecFile.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.rpmfactory.core;
17
18 import java.io.IOException;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.io.IOUtils;
24 import org.springframework.core.io.Resource;
25
26 public class RpmSpecFile {
27 private Resource specFile;
28
29 private String name;
30 private String version;
31 private String release;
32 private Map<String, String> sources = new HashMap<String, String>();
33 private Map<String, String> patches = new HashMap<String, String>();
34
35 public RpmSpecFile(Resource specFile) {
36 this.specFile = specFile;
37 parseSpecFile();
38 }
39
40 public void init() {
41 parseSpecFile();
42 }
43
44 @SuppressWarnings("unchecked")
45 protected void parseSpecFile() {
46 try {
47 List<String> lines = (List<String>) IOUtils.readLines(specFile
48 .getInputStream());
49
50 lines: for (String line : lines) {
51 int indexSemiColon = line.indexOf(':');
52 if (indexSemiColon <= 0)
53 continue lines;
54 String directive = line.substring(0, indexSemiColon).trim();
55 String value = line.substring(indexSemiColon + 1).trim();
56 if ("name".equals(directive.toLowerCase()))
57 name = value;
58 else if ("version".equals(directive.toLowerCase()))
59 version = value;
60 else if ("release".equals(directive.toLowerCase()))
61 release = value;
62 else if (directive.toLowerCase().startsWith("source"))
63 sources.put(directive, interpret(value));
64 else if (directive.toLowerCase().startsWith("patch"))
65 patches.put(directive, interpret(value));
66 }
67
68 } catch (IOException e) {
69 throw new RuntimeException("Cannot parse spec file " + specFile, e);
70 }
71 }
72
73 protected String interpret(String value) {
74 StringBuffer buf = new StringBuffer(value.length());
75 StringBuffer currKey = null;
76 boolean mayBeKey = false;
77 chars: for (char c : value.toCharArray()) {
78 if (c == '%')
79 mayBeKey = true;
80 else if (c == '{') {
81 if (mayBeKey)
82 currKey = new StringBuffer();
83 } else if (c == '}') {
84 if (currKey == null)
85 continue chars;
86 String key = currKey.toString();
87 if ("name".equals(key.toLowerCase()))
88 buf.append(name);
89 else if ("version".equals(key.toLowerCase()))
90 buf.append(version);
91 else
92 buf.append("%{").append(key).append('}');
93 currKey = null;
94 } else {
95 if (currKey != null)
96 currKey.append(c);
97 else
98 buf.append(c);
99 }
100 }
101 return buf.toString();
102 }
103
104 public Resource getSpecFile() {
105 return specFile;
106 }
107
108 public String getName() {
109 return name;
110 }
111
112 public String getVersion() {
113 return version;
114 }
115
116 public String getRelease() {
117 return release;
118 }
119
120 public Map<String, String> getSources() {
121 return sources;
122 }
123
124 public Map<String, String> getPatches() {
125 return patches;
126 }
127
128 }