KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > bundle > PDEManifestElement


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.text.bundle;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.TreeMap JavaDoc;
19
20 import org.eclipse.osgi.util.ManifestElement;
21 import org.eclipse.pde.internal.core.bundle.BundleObject;
22 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
23 import org.osgi.framework.BundleException;
24
25 public class PDEManifestElement extends BundleObject {
26
27     private static final long serialVersionUID = 1L;
28     
29     protected String JavaDoc[] fValueComponents;
30     protected TreeMap JavaDoc fAttributes;
31     protected TreeMap JavaDoc fDirectives;
32
33     protected transient ManifestHeader fHeader;
34     
35     public PDEManifestElement(ManifestHeader header, String JavaDoc value) {
36         setHeader(header);
37         setValue(value);
38         setModel(fHeader.getBundle().getModel());
39     }
40     
41     protected PDEManifestElement(ManifestHeader header, ManifestElement manifestElement) {
42         setHeader(header);
43         init(manifestElement);
44         setModel(fHeader.getBundle().getModel());
45     }
46     
47     public String JavaDoc[] getValueComponents() {
48         return fValueComponents;
49     }
50
51     protected void setValueComponents(String JavaDoc[] valueComponents) {
52         fValueComponents = valueComponents;
53     }
54     
55     public String JavaDoc[] getAttributes(String JavaDoc key) {
56         return getTableValues(fAttributes, key);
57     }
58     
59     public String JavaDoc getAttribute(String JavaDoc key) {
60         return getTableValue(fAttributes, key);
61     }
62     
63     public Set JavaDoc getKeys() {
64         return getTableKeys(fAttributes);
65     }
66
67     public void addAttribute(String JavaDoc key, String JavaDoc value) {
68         fAttributes = addTableValue(fAttributes, key, value);
69     }
70     
71     public void setAttribute(String JavaDoc key, String JavaDoc value) {
72         fAttributes = setTableValue(fAttributes, key, value);
73     }
74     
75     public String JavaDoc getDirective(String JavaDoc key) {
76         return getTableValue(fDirectives, key);
77     }
78     
79     public String JavaDoc[] getDirectives(String JavaDoc key) {
80         return getTableValues(fDirectives, key);
81     }
82
83     public Set JavaDoc getDirectiveKeys() {
84         return getTableKeys(fDirectives);
85     }
86
87     public void addDirective(String JavaDoc key, String JavaDoc value) {
88         fDirectives = addTableValue(fDirectives, key, value);
89     }
90     
91     public void setDirective(String JavaDoc key, String JavaDoc value) {
92         fDirectives = setTableValue(fDirectives, key, value);
93     }
94     
95     private String JavaDoc getTableValue(TreeMap JavaDoc table, String JavaDoc key) {
96         if (table == null)
97             return null;
98         Object JavaDoc result = table.get(key);
99         if (result == null)
100             return null;
101         if (result instanceof String JavaDoc)
102             return (String JavaDoc) result;
103
104         ArrayList JavaDoc valueList = (ArrayList JavaDoc) result;
105         //return the last value
106
return (String JavaDoc) valueList.get(valueList.size() - 1);
107     }
108     
109     private String JavaDoc[] getTableValues(TreeMap JavaDoc table, String JavaDoc key) {
110         if (table == null)
111             return null;
112         Object JavaDoc result = table.get(key);
113         if (result == null)
114             return null;
115         if (result instanceof String JavaDoc)
116             return new String JavaDoc[] {(String JavaDoc) result};
117         ArrayList JavaDoc valueList = (ArrayList JavaDoc) result;
118         return (String JavaDoc[]) valueList.toArray(new String JavaDoc[valueList.size()]);
119     }
120
121     private Set JavaDoc getTableKeys(TreeMap JavaDoc table) {
122         if (table == null)
123             return null;
124         return table.keySet();
125     }
126
127     private TreeMap JavaDoc addTableValue(TreeMap JavaDoc table, String JavaDoc key, String JavaDoc value) {
128         if (table == null) {
129             table = new TreeMap JavaDoc();
130         }
131         Object JavaDoc curValue = table.get(key);
132         if (curValue != null) {
133             ArrayList JavaDoc newList;
134             // create a list to contain multiple values
135
if (curValue instanceof ArrayList JavaDoc) {
136                 newList = (ArrayList JavaDoc) curValue;
137             } else {
138                 newList = new ArrayList JavaDoc(5);
139                 newList.add(curValue);
140             }
141             newList.add(value);
142             table.put(key, newList);
143         } else {
144             table.put(key, value);
145         }
146         return table;
147     }
148     
149     private TreeMap JavaDoc setTableValue(TreeMap JavaDoc table, String JavaDoc key, String JavaDoc value) {
150         if (table == null) {
151             table = new TreeMap JavaDoc();
152         }
153         if (value == null || value.trim().length() == 0)
154             table.remove(key);
155         else {
156             table.put(key, value);
157         }
158         return table;
159     }
160     
161     public void setValue(String JavaDoc value) {
162         if (value == null) {
163             setValueComponents(new String JavaDoc[0]);
164             return;
165         }
166         try {
167             ManifestElement[] elements = ManifestElement.parseHeader(fHeader.fName, value);
168             if (elements != null && elements.length > 0)
169                 init(elements[0]);
170         } catch (BundleException e) {
171         }
172     }
173     
174     private void init(ManifestElement manifestElement) {
175         setValueComponents(manifestElement.getValueComponents());
176         Enumeration JavaDoc attKeys = manifestElement.getKeys();
177         if (attKeys != null) {
178             while (attKeys.hasMoreElements()) {
179                 String JavaDoc attKey = (String JavaDoc)attKeys.nextElement();
180                 String JavaDoc[] values = ManifestElement.getArrayFromList(manifestElement.getAttribute(attKey));
181                 for (int i = 0; i < values.length; i++)
182                     addAttribute(attKey, values[i]);
183             }
184         }
185         Enumeration JavaDoc dirKeys = manifestElement.getDirectiveKeys();
186         if (dirKeys != null) {
187             while (dirKeys.hasMoreElements()) {
188                 String JavaDoc dirKey = (String JavaDoc)dirKeys.nextElement();
189                 String JavaDoc[] values = ManifestElement.getArrayFromList(manifestElement.getDirective(dirKey));
190                 for (int i = 0; i < values.length; i++)
191                     addDirective(dirKey, values[i]);
192             }
193         }
194     }
195     
196     public String JavaDoc write() {
197         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getValue());
198         appendValuesToBuffer(sb, fAttributes);
199         appendValuesToBuffer(sb, fDirectives);
200         return sb.toString();
201     }
202     
203     public String JavaDoc getValue() {
204         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
205         if (fValueComponents == null)
206             return ""; //$NON-NLS-1$
207
for (int i = 0; i < fValueComponents.length; i++) {
208             if (i != 0) sb.append("; "); //$NON-NLS-1$
209
sb.append(fValueComponents[i]);
210         }
211         return sb.toString();
212     }
213     
214     protected void appendValuesToBuffer(StringBuffer JavaDoc sb, TreeMap JavaDoc table) {
215         if (table == null)
216             return;
217         Iterator JavaDoc dkeys = table.keySet().iterator();
218         while (dkeys.hasNext()) {
219             String JavaDoc dkey = (String JavaDoc)dkeys.next();
220             Object JavaDoc value = table.get(dkey);
221             if (value == null)
222                 continue;
223             sb.append(";"); //$NON-NLS-1$
224
sb.append(dkey);
225             sb.append(table.equals(fDirectives) ? ":=" : "="); //$NON-NLS-1$ //$NON-NLS-2$
226

227             if (value instanceof String JavaDoc) {
228                 boolean wrap = shouldWrap(value.toString());
229                 if (wrap) sb.append("\""); //$NON-NLS-1$
230
sb.append(value);
231                 if (wrap) sb.append("\""); //$NON-NLS-1$
232
} else if (value instanceof ArrayList JavaDoc) {
233                 ArrayList JavaDoc values = (ArrayList JavaDoc)value;
234                 boolean wrap = (values.size() > 1
235                                     || (values.size() == 1 && shouldWrap(values.get(0).toString())));
236                 if (wrap) sb.append("\""); //$NON-NLS-1$
237
for (int i = 0; i < values.size(); i++) {
238                     if (i != 0) sb.append(","); //$NON-NLS-1$
239
sb.append(values.get(i));
240                 }
241                 if (wrap) sb.append("\""); //$NON-NLS-1$
242
}
243         }
244     }
245     
246     private boolean shouldWrap(String JavaDoc value) {
247         return value.indexOf(' ') != -1
248                 || value.indexOf(',') != -1
249                 || value.indexOf('.') != -1
250                 || value.indexOf('[') != -1
251                 || value.indexOf('(') != -1;
252     }
253     
254     public ManifestHeader getHeader() {
255         return fHeader;
256     }
257     
258     public void setHeader(ManifestHeader header) {
259         fHeader = header;
260     }
261    
262     /**
263      * @param model
264      * @param header
265      */

266     public void reconnect(IBundleModel model, ManifestHeader header) {
267         super.reconnect(model);
268         // Transient Field: Header
269
fHeader = header;
270     }
271     
272 }
273
Popular Tags