KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.*;
14 import java.util.jar.*;
15 import org.eclipse.jface.text.*;
16 import org.eclipse.pde.internal.core.*;
17 import org.eclipse.pde.internal.core.ibundle.*;
18 import org.eclipse.pde.internal.ui.model.*;
19 import org.osgi.framework.*;
20
21 public class Bundle implements IBundle {
22     
23     private BundleModel fModel;
24     private Hashtable fDocumentHeaders = new Hashtable();
25     
26     public Bundle(BundleModel model) {
27         fModel = model;
28     }
29     
30     public void clearHeaders() {
31         fDocumentHeaders.clear();
32     }
33     
34     public void load(Manifest manifest) {
35         Map attributes = manifest.getMainAttributes();
36         Iterator iter = attributes.keySet().iterator();
37         while (iter.hasNext()) {
38             Attributes.Name key = (Attributes.Name) iter.next();
39             String JavaDoc value = (String JavaDoc)attributes.get(key);
40             ManifestHeader header = createHeader(key.toString(), value);
41             fDocumentHeaders.put(key.toString(), header);
42         }
43         adjustOffsets(fModel.getDocument());
44     }
45     
46     public void clearOffsets() {
47         Iterator iter = fDocumentHeaders.values().iterator();
48         while (iter.hasNext()) {
49             ManifestHeader header = (ManifestHeader)iter.next();
50             header.setOffset(-1);
51             header.setLength(-1);
52         }
53     }
54     
55     public void trim() {
56         Iterator iter = fDocumentHeaders.keySet().iterator();
57         while (iter.hasNext()) {
58             String JavaDoc key = iter.next().toString();
59             ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key);
60             if (header.getOffset() == -1)
61                 fDocumentHeaders.remove(key);
62         }
63     }
64     
65     protected void adjustOffsets(IDocument document) {
66         int lines = document.getNumberOfLines();
67         try {
68             IDocumentKey currentKey = null;
69             for (int i = 0; i < lines; i++) {
70                 int offset = document.getLineOffset(i);
71                 int length = document.getLineLength(i);
72                 String JavaDoc line = document.get(offset, length);
73                 
74                 if (currentKey != null) {
75                     int lineNumber = line.startsWith(" ") ? i : i - 1; //$NON-NLS-1$
76
IRegion region = document.getLineInformation(lineNumber);
77                     String JavaDoc delimiter = document.getLineDelimiter(lineNumber);
78                     int keyLength = region.getOffset() + region.getLength() - currentKey.getOffset();
79                     currentKey.setLength(delimiter != null ? keyLength + delimiter.length() : keyLength);
80                     if (!line.startsWith(" ")) { //$NON-NLS-1$
81
currentKey = null;
82                     }
83                 }
84                 
85                 if (currentKey == null) {
86                     int index = line.indexOf(':');
87                     String JavaDoc name = (index != -1) ? line.substring(0, index) : line;
88                     currentKey = (IDocumentKey)fDocumentHeaders.get(name);
89                     if (currentKey != null) {
90                         IRegion region = document.getLineInformation(i);
91                         currentKey.setOffset(region.getOffset());
92                         String JavaDoc delimiter = document.getLineDelimiter(i);
93                         currentKey.setLength(delimiter != null ? region.getLength() + delimiter.length() : region.getLength());
94                     }
95                 }
96             }
97         } catch (BadLocationException e) {
98         }
99     }
100     /* (non-Javadoc)
101      * @see org.eclipse.pde.internal.core.ibundle.IBundle#setHeader(java.lang.String, java.lang.String)
102      */

103     public void setHeader(String JavaDoc key, String JavaDoc value) {
104         ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key);
105         String JavaDoc old = header == null ? null : header.getValue();
106         if (header == null) {
107             header = createHeader(key, value);
108         } else {
109             header.setValue(value);
110         }
111         fDocumentHeaders.put(key, header);
112         
113         fModel.fireModelObjectChanged(header, key, old, value);
114     }
115     
116     private ManifestHeader createHeader(String JavaDoc key, String JavaDoc value) {
117         ManifestHeader header = null;
118         if (key.equals(Constants.EXPORT_PACKAGE) || key.equals(ICoreConstants.PROVIDE_PACKAGE)) {
119             String JavaDoc newLine = TextUtilities.getDefaultLineDelimiter(fModel
120                     .getDocument());
121             header = new ExportPackageHeader(key, value, this, newLine);
122         } else if (key.equals(Constants.IMPORT_PACKAGE)){
123             String JavaDoc newLine = TextUtilities.getDefaultLineDelimiter(fModel
124                     .getDocument());
125             header = new ImportPackageHeader(key, value, this, newLine);
126         } else {
127             header = new ManifestHeader(key, value, this);
128         }
129         return header;
130     }
131     /* (non-Javadoc)
132      * @see org.eclipse.pde.internal.core.ibundle.IBundle#getHeader(java.lang.String)
133      */

134     public String JavaDoc getHeader(String JavaDoc key) {
135         ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key);
136         return (header != null) ? header.getValue() : null;
137     }
138     
139     public ManifestHeader getManifestHeader(String JavaDoc key) {
140         return (ManifestHeader)fDocumentHeaders.get(key);
141     }
142     
143     public Dictionary getHeaders() {
144         return fDocumentHeaders;
145     }
146
147     public IBundleModel getModel() {
148         return fModel;
149     }
150 }
151
Popular Tags