forked from ECF/gsoc2013salinda
/
ManifestEditor.java
164 lines (152 loc) · 5.68 KB
/
ManifestEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
* Copyright (c) 2013 Salinda Jayawardana. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
******************************************************************************/
package org.eclipse.ecf.tools.serviceGenerator.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.BundleException;
public class ManifestEditor {
public static void addPluginDependency(String newbundleHeader,String newbundleHeaderValue,String newbundleAttr,
String newAttrValue, boolean overwrite,String filepath) throws BundleException, IOException
{
FileInputStream in = new FileInputStream(filepath);
Manifest manifest = new Manifest(in);
boolean foundHeader = false;
boolean hasValuesForPlugin = false;
StringBuilder strBuilder = new StringBuilder();
Attributes mainAttrs = manifest.getMainAttributes();
for (Object entryName : mainAttrs.keySet()) {
String values;
String header;
if(entryName instanceof String) {
header = (String) entryName;
values = mainAttrs.getValue(header);
} else if(entryName instanceof Attributes.Name) {
header = (String) ((Attributes.Name) entryName).toString();
values = mainAttrs.getValue((Attributes.Name) entryName);
} else {
throw new BundleException("Bundle Confalsefiguration error: "
+ entryName.getClass() + " (" + entryName + ")");
}
if(!newbundleHeader.equals(header)) {
continue;
}
foundHeader = true;
if(values != null){
ManifestElement[] elements = ManifestElement.parseHeader(header, values);
for (int i = 0; i < elements.length; i++){
ManifestElement manifestElement = elements[i];
Enumeration<?> keys = manifestElement.getKeys();
Enumeration<?> directiveKeys = manifestElement.getDirectiveKeys();
StringBuilder valueComponents = new StringBuilder();
boolean lastElement = i >= elements.length-1;
boolean firstElement = i == 0;
boolean elementIsRequiredPlugin = false;
for (int j = 0; j < manifestElement.getValueComponents().length; j++){
String pureValue = manifestElement.getValueComponents()[j];
if(newbundleHeaderValue.equalsIgnoreCase(pureValue)){
hasValuesForPlugin = true;
elementIsRequiredPlugin = true;
if(!overwrite)
return;
}
valueComponents.append(pureValue + ";");
}
if(!elementIsRequiredPlugin){
if(!firstElement){
strBuilder.append(',');
strBuilder.append('\n');
strBuilder.append(' ');
}else{
strBuilder.append(' ');
}
strBuilder.append(manifestElement.toString());
continue;
}else{
strBuilder.append((firstElement?"":" ") + valueComponents);
}
boolean foundVersionAttr = false;
if(keys != null){
while (keys.hasMoreElements()){
String key = (String) keys.nextElement();
String value = manifestElement.getAttribute(key);
if(newAttrValue!=null && !newAttrValue.isEmpty()){
if(newbundleAttr.equalsIgnoreCase(key)){
strBuilder.append(key + "=\"" + newAttrValue+"\"");
foundVersionAttr = true;
}else{
strBuilder.append(key + "=\"" + value);
}
}
}
}
if(!foundVersionAttr){
if(newAttrValue!=null && !newAttrValue.isEmpty()){
strBuilder.append(newbundleAttr + "=\"" + newAttrValue+"\"");
}
}
if(directiveKeys != null){
while (directiveKeys.hasMoreElements()){
boolean lastDirective = !directiveKeys.hasMoreElements();
if(!lastDirective){
strBuilder.append(";");
}
}
}if(!lastElement){
strBuilder.append(",\n");
}
}
}
break;
}
in.close();
writeIntoBundleMF(newbundleHeaderValue, newAttrValue, overwrite, filepath, newbundleHeader,
newbundleAttr, foundHeader, hasValuesForPlugin, strBuilder,
manifest);
}
private static void writeIntoBundleMF(String plugin, String version, boolean overwrite,
String fileName, String importBundleHeader,
String bundleVersionAttr, boolean foundHeader,
boolean hasValuesForPlugin, StringBuilder strBuilder,
Manifest manifest) throws FileNotFoundException, IOException {
if(!foundHeader){
if(version!=null && !version.isEmpty()){
manifest.getMainAttributes().putValue(importBundleHeader, plugin + ";"
+ bundleVersionAttr + "=\"" + version+"\"");
}else{
manifest.getMainAttributes().putValue(importBundleHeader, plugin);
}
}else{
if(hasValuesForPlugin){
manifest.getMainAttributes().putValue(importBundleHeader, strBuilder.toString());
}else{
String existingValues = strBuilder.toString();
boolean areExistingValues = existingValues.trim().length() != 0;
String newValue = plugin;
if(version!=null && !version.isEmpty()){
newValue = plugin + ";" + bundleVersionAttr + "=\"" + version+"\"";
}
if(areExistingValues){
newValue =existingValues+",\n " + newValue;
}else{
newValue =" "+newValue;
}
manifest.getMainAttributes().putValue(importBundleHeader, newValue);
}
}
FileOutputStream outputStream = new FileOutputStream(fileName);
manifest.write(outputStream);
outputStream.flush();
outputStream.close();
}
}