1 11 package org.eclipse.pde.internal.ui.compare; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 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 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 getName() { 57 return this.getId(); 58 } 59 60 public String getType() { 61 return "MF2"; } 63 64 public Image getImage() { 65 return CompareUI.getImage(getType()); 66 } 67 } 68 69 public String getName() { 70 return PDEUIMessages.ManifestStructureCreator_name; 71 } 72 73 public IStructureComparator locate(Object path, Object input) { 74 return null; 75 } 76 77 public String getContents(Object 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 { 89 int lineStart = 0; 90 int[] args = new int[2]; 91 args[0] = 0; args[1] = 0; 94 try { 95 monitor = beginWork(monitor); 96 StringBuffer headerBuffer = new StringBuffer (); 97 int headerStart = 0; 98 while (true) { 99 lineStart = args[1]; String line = readLine(args, doc); 101 if (line == null) 102 return; 103 104 if (line.length() <= 0) { 105 saveNode(root, doc, headerBuffer.toString(), headerStart); continue; 107 } 108 if (line.charAt(0) == ' ') { 109 if (headerBuffer.length() > 0) 110 headerBuffer.append(line); 111 continue; 112 } 113 114 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 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 extractKey(String headerBuffer) { 146 int assign = headerBuffer.indexOf(":"); if (assign != -1) 148 return headerBuffer.substring(0, assign); 149 return headerBuffer; 150 } 151 152 private String 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 readString(InputStream is, String encoding) { 172 if (is == null) 173 return null; 174 BufferedReader reader= null; 175 try { 176 StringBuffer buffer= new StringBuffer (); 177 char[] part= new char[2048]; 178 int read= 0; 179 reader= new BufferedReader (new InputStreamReader (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 ex) { 187 } finally { 189 if (reader != null) { 190 try { 191 reader.close(); 192 } catch (IOException ex) { 193 } 195 } 196 } 197 return null; 198 } 199 200 private static String readString(IStreamContentAccessor sa) throws CoreException { 201 InputStream is= sa.getContents(); 202 if (is != null) { 203 String encoding= null; 204 if (sa instanceof IEncodedStreamContentAccessor) { 205 try { 206 encoding= ((IEncodedStreamContentAccessor) sa).getCharset(); 207 } catch (Exception 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 getDocumentPartitioning() { 222 return ManifestPartitionScanner.MANIFEST_FILE_PARTITIONING; 223 } 224 225 protected IStructureComparator createStructureComparator(Object 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 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 |