KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > model > bundle > PackageObject


1 /*******************************************************************************
2  * Copyright (c) 2005 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 package org.eclipse.pde.internal.ui.model.bundle;
12
13 import java.util.Enumeration JavaDoc;
14
15 import org.eclipse.osgi.util.ManifestElement;
16 import org.eclipse.pde.internal.core.bundle.BundleObject;
17
18 public class PackageObject extends BundleObject {
19
20     private static final long serialVersionUID = 1L;
21
22     private String JavaDoc fVersionAttribute;
23     private String JavaDoc fName;
24     private String JavaDoc fVersion;
25     private transient ManifestElement fElement;
26
27     private ManifestHeader fHeader;
28     
29     public PackageObject(ManifestHeader header, ManifestElement element, String JavaDoc versionAttribute) {
30         fHeader = header;
31         fVersionAttribute = versionAttribute;
32         fName = element.getValue();
33         fVersion = element.getAttribute(fVersionAttribute);
34         fElement = element;
35         setModel(fHeader.getBundle().getModel());
36     }
37     
38     public PackageObject(ManifestHeader header, String JavaDoc name, String JavaDoc version, String JavaDoc versionAttribute) {
39         fHeader = header;
40         fVersion = version;
41         fVersionAttribute = versionAttribute;
42         fName = name;
43         setModel(fHeader.getBundle().getModel());
44     }
45     
46      public String JavaDoc toString() {
47         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(fName);
48         if (fVersion != null && fVersion.length() > 0) {
49             buffer.append(" "); //$NON-NLS-1$
50
boolean wrap = Character.isDigit(fVersion.charAt(0));
51             if (wrap)
52                 buffer.append("("); //$NON-NLS-1$
53
buffer.append(fVersion);
54             if (wrap)
55                 buffer.append(")"); //$NON-NLS-1$
56
}
57         return buffer.toString();
58     }
59     
60     public String JavaDoc write() {
61         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
62         buffer.append(fName);
63         
64         appendSupportedAttributes(buffer);
65
66         if (fElement == null)
67             return buffer.toString();
68         
69         Enumeration JavaDoc attrs = fElement.getKeys();
70         if (attrs != null) {
71             while (attrs.hasMoreElements()) {
72                 String JavaDoc attr = attrs.nextElement().toString();
73                 if (attr.equals(fVersionAttribute))
74                     continue;
75                 buffer.append(";"); //$NON-NLS-1$
76
buffer.append(attr);
77                 buffer.append("=\""); //$NON-NLS-1$
78
buffer.append(fElement.getAttribute(attr));
79                 buffer.append("\""); //$NON-NLS-1$
80
}
81         }
82         
83         Enumeration JavaDoc directives = fElement.getDirectiveKeys();
84         if (directives != null) {
85             while (directives.hasMoreElements()) {
86                 String JavaDoc directive = directives.nextElement().toString();
87                 if (skipDirective(directive))
88                     continue;
89                 buffer.append(";"); //$NON-NLS-1$
90
buffer.append(directive);
91                 buffer.append(":="); //$NON-NLS-1$
92
buffer.append("\""); //$NON-NLS-1$
93
buffer.append(fElement.getDirective(directive));
94                 buffer.append("\""); //$NON-NLS-1$
95
}
96         }
97         return buffer.toString();
98     }
99     
100     protected void appendSupportedAttributes(StringBuffer JavaDoc buffer) {
101         if (fVersion != null && fVersion.length() > 0) {
102             buffer.append(";"); //$NON-NLS-1$
103
buffer.append(fVersionAttribute);
104             buffer.append("=\""); //$NON-NLS-1$
105
buffer.append(fVersion.trim());
106             buffer.append("\""); //$NON-NLS-1$
107
}
108     }
109     
110     protected boolean skipDirective(String JavaDoc directive) {
111         return false;
112     }
113
114     public String JavaDoc getVersion() {
115         return fVersion;
116     }
117
118     public String JavaDoc getName() {
119         return fName;
120     }
121
122     public void setVersion(String JavaDoc version) {
123         String JavaDoc old = fVersion;
124         fVersion = version;
125         firePropertyChanged(this, fVersionAttribute, old, version);
126     }
127
128     public ManifestHeader getHeader() {
129         return fHeader;
130     }
131
132 }
133
Popular Tags