KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > compare > ManifestStructureCreator


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.compare;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17
18 import org.eclipse.compare.CompareUI;
19 import org.eclipse.compare.IEditableContent;
20 import org.eclipse.compare.IEncodedStreamContentAccessor;
21 import org.eclipse.compare.ISharedDocumentAdapter;
22 import org.eclipse.compare.IStreamContentAccessor;
23 import org.eclipse.compare.ITypedElement;
24 import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
25 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
26 import org.eclipse.compare.structuremergeviewer.StructureCreator;
27 import org.eclipse.compare.structuremergeviewer.StructureRootNode;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.NullProgressMonitor;
33 import org.eclipse.core.runtime.OperationCanceledException;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.core.runtime.SubProgressMonitor;
36 import org.eclipse.jface.text.BadLocationException;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IDocumentPartitioner;
39 import org.eclipse.jface.text.rules.FastPartitioner;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42 import org.eclipse.pde.internal.ui.editor.text.ManifestPartitionScanner;
43 import org.eclipse.swt.graphics.Image;
44
45 public class ManifestStructureCreator extends StructureCreator {
46
47     static class ManifestNode extends DocumentRangeNode implements ITypedElement {
48         
49         public ManifestNode(DocumentRangeNode parent, int type, String JavaDoc id , IDocument doc, int start, int length) {
50             super(parent, type, id, doc, start, length);
51             if (parent != null) {
52                 parent.addChild(ManifestNode.this);
53             }
54         }
55         
56         public String JavaDoc getName() {
57             return this.getId();
58         }
59
60         public String JavaDoc getType() {
61             return "MF2"; //$NON-NLS-1$
62
}
63         
64         public Image getImage() {
65             return CompareUI.getImage(getType());
66         }
67     }
68     
69     public String JavaDoc getName() {
70         return PDEUIMessages.ManifestStructureCreator_name;
71     }
72
73     public IStructureComparator locate(Object JavaDoc path, Object JavaDoc input) {
74         return null;
75     }
76
77     public String JavaDoc getContents(Object JavaDoc node, boolean ignoreWhitespace) {
78         if (node instanceof IStreamContentAccessor) {
79             IStreamContentAccessor sca = (IStreamContentAccessor) node;
80             try {
81                 return readString(sca);
82             } catch (CoreException ex) {
83             }
84         }
85         return null;
86     }
87
88     private void parseManifest(DocumentRangeNode root, IDocument doc, IProgressMonitor monitor) throws IOException JavaDoc {
89         int lineStart = 0;
90         int[] args = new int[2];
91         args[0] = 0; // here we return the line number
92
args[1] = 0; // and here the offset of the first character of the line
93

94         try {
95             monitor = beginWork(monitor);
96             StringBuffer JavaDoc headerBuffer = new StringBuffer JavaDoc();
97             int headerStart = 0;
98             while (true) {
99                 lineStart = args[1]; // start of current line
100
String JavaDoc line = readLine(args, doc);
101                 if (line == null)
102                     return;
103                     
104                 if (line.length() <= 0) {
105                     saveNode(root, doc, headerBuffer.toString(), headerStart); // empty line, save buffer to node
106
continue;
107                 }
108                 if (line.charAt(0) == ' ') {
109                     if (headerBuffer.length() > 0)
110                         headerBuffer.append(line);
111                     continue;
112                 }
113                 
114                 // save old buffer and start loading again
115
saveNode(root, doc, headerBuffer.toString(), headerStart);
116                 
117                 headerStart = lineStart;
118                 headerBuffer.replace(0, headerBuffer.length(), line);
119                 worked(monitor);
120             }
121         } finally {
122             monitor.done();
123         }
124     }
125
126     private void worked(IProgressMonitor monitor) {
127         if (monitor.isCanceled())
128             throw new OperationCanceledException();
129         monitor.worked(1);
130     }
131
132     private IProgressMonitor beginWork(IProgressMonitor monitor) {
133         if (monitor == null)
134             return new NullProgressMonitor();
135         return new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN);
136     }
137
138     private void saveNode(DocumentRangeNode root, IDocument doc, String JavaDoc header, int start) {
139         if (header.length() > 0)
140             new ManifestNode(
141                 root, 0, extractKey(header),
142                 doc, start, header.length());
143     }
144
145     private String JavaDoc extractKey(String JavaDoc headerBuffer) {
146         int assign = headerBuffer.indexOf(":"); //$NON-NLS-1$
147
if (assign != -1)
148             return headerBuffer.substring(0, assign);
149         return headerBuffer;
150     }
151
152     private String JavaDoc readLine(int[] args, IDocument doc) {
153         int line = args[0]++;
154         try {
155             if (line >= doc.getNumberOfLines())
156                 return null;
157             int start = doc.getLineOffset(line);
158             int length = doc.getLineLength(line);
159             try {
160                 args[1] = doc.getLineOffset(line+1);
161             } catch (BadLocationException ex) {
162                 args[1] = doc.getLength();
163             }
164             
165             return doc.get(start, length);
166         } catch (BadLocationException ex) {
167         }
168         return null;
169     }
170     
171     private static String JavaDoc readString(InputStream JavaDoc is, String JavaDoc encoding) {
172         if (is == null)
173             return null;
174         BufferedReader JavaDoc reader= null;
175         try {
176             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
177             char[] part= new char[2048];
178             int read= 0;
179             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, encoding));
180
181             while ((read= reader.read(part)) != -1)
182                 buffer.append(part, 0, read);
183             
184             return buffer.toString();
185             
186         } catch (IOException JavaDoc ex) {
187             // NeedWork
188
} finally {
189             if (reader != null) {
190                 try {
191                     reader.close();
192                 } catch (IOException JavaDoc ex) {
193                     // silently ignored
194
}
195             }
196         }
197         return null;
198     }
199     
200     private static String JavaDoc readString(IStreamContentAccessor sa) throws CoreException {
201         InputStream JavaDoc is= sa.getContents();
202         if (is != null) {
203             String JavaDoc encoding= null;
204             if (sa instanceof IEncodedStreamContentAccessor) {
205                 try {
206                     encoding= ((IEncodedStreamContentAccessor) sa).getCharset();
207                 } catch (Exception JavaDoc e) {
208                 }
209             }
210             if (encoding == null)
211                 encoding= ResourcesPlugin.getEncoding();
212             return readString(is, encoding);
213         }
214         return null;
215     }
216     
217     protected IDocumentPartitioner getDocumentPartitioner() {
218         return new FastPartitioner(new ManifestPartitionScanner(), ManifestPartitionScanner.PARTITIONS);
219     }
220     
221     protected String JavaDoc getDocumentPartitioning() {
222         return ManifestPartitionScanner.MANIFEST_FILE_PARTITIONING;
223     }
224     
225     protected IStructureComparator createStructureComparator(Object JavaDoc input,
226             IDocument document, ISharedDocumentAdapter adapter, IProgressMonitor monitor) throws CoreException {
227         
228         final boolean isEditable;
229         if (input instanceof IEditableContent)
230             isEditable= ((IEditableContent) input).isEditable();
231         else
232             isEditable= false;
233
234         DocumentRangeNode rootNode = new StructureRootNode(document, input, this, adapter) {
235             public boolean isEditable() {
236                 return isEditable;
237             }
238         };
239         try {
240             parseManifest(rootNode, document, monitor);
241         } catch (IOException JavaDoc ex) {
242             if (adapter != null)
243                 adapter.disconnect(input);
244             throw new CoreException(new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 0, PDEUIMessages.ManifestStructureCreator_errorMessage, ex));
245         }
246         
247         return rootNode;
248     }
249     
250 }
251
Popular Tags