KickJava   Java API By Example, From Geeks To Geeks.

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


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

110     public void setHeader(String JavaDoc key, String JavaDoc value) {
111         IManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key);
112         String JavaDoc old = null;
113         if (header == null) {
114             header = getModel().getFactory().createHeader(key, value);
115             fDocumentHeaders.put(key, header);
116             fModel.fireModelObjectChanged(header, key, old, value);
117         } else {
118             old = header.getValue();
119             header.setValue(value);
120         }
121     }
122     
123     /* (non-Javadoc)
124      * @see org.eclipse.pde.internal.core.ibundle.IBundle#getHeader(java.lang.String)
125      */

126     public String JavaDoc getHeader(String JavaDoc key) {
127         ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key);
128         return (header != null) ? header.getValue() : null;
129     }
130     
131     public IManifestHeader getManifestHeader(String JavaDoc key) {
132         return (ManifestHeader)fDocumentHeaders.get(key);
133     }
134     
135     public Map JavaDoc getHeaders() {
136         return fDocumentHeaders;
137     }
138
139     public IBundleModel getModel() {
140         return fModel;
141     }
142
143     public String JavaDoc getLocalization() {
144         return getHeader(Constants.BUNDLE_LOCALIZATION);
145     }
146
147     public void setLocalization(String JavaDoc localization) {
148         setHeader(Constants.BUNDLE_LOCALIZATION, localization);
149     }
150
151     public void renameHeader(String JavaDoc key, String JavaDoc newKey) {
152         ManifestHeader header = (ManifestHeader)getManifestHeader(key);
153         if (header != null) {
154             header.setName(newKey);
155             fDocumentHeaders.put(newKey, fDocumentHeaders.remove(key));
156         }
157         fModel.fireModelObjectChanged(header, newKey, key, newKey);
158     }
159 }
160
Popular Tags