1 11 package org.eclipse.pde.internal.core.text.build; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.PrintWriter ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.Properties ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.pde.core.IModelChangedEvent; 24 import org.eclipse.pde.core.ModelChangedEvent; 25 import org.eclipse.pde.core.build.IBuild; 26 import org.eclipse.pde.core.build.IBuildEntry; 27 import org.eclipse.pde.internal.core.text.IDocumentKey; 28 import org.eclipse.pde.internal.core.util.PropertiesUtil; 29 30 public class Build implements IBuild { 31 32 private BuildModel fModel; 33 private HashMap fEntries = new HashMap (); 34 35 public Build(BuildModel model) { 36 fModel = model; 37 } 38 39 42 public void add(IBuildEntry entry) throws CoreException { 43 fEntries.put(entry.getName(), entry); 44 fModel.fireModelChanged(new ModelChangedEvent(fModel, 45 IModelChangedEvent.INSERT, new Object []{entry}, null)); 46 } 47 50 public IBuildEntry[] getBuildEntries() { 51 return (IBuildEntry[])fEntries.values().toArray(new IBuildEntry[fEntries.size()]); 52 } 53 56 public IBuildEntry getEntry(String name) { 57 return (IBuildEntry)fEntries.get(name); 58 } 59 62 public void remove(IBuildEntry entry) throws CoreException { 63 if (fEntries.remove(entry.getName()) != null) 64 fModel.fireModelChanged(new ModelChangedEvent(fModel, 65 IModelChangedEvent.REMOVE, new Object []{entry}, null)); 66 } 67 73 public void write(String indent, PrintWriter writer) { 74 } 75 76 public void load(InputStream source) throws IOException { 77 fEntries.clear(); 78 Properties properties = new Properties (); 79 properties.load(source); 80 Enumeration keys = properties.keys(); 81 while (keys.hasMoreElements()) { 82 String name = keys.nextElement().toString(); 83 BuildEntry entry = (BuildEntry)fModel.getFactory().createEntry(name); 84 entry.processEntry(properties.get(name).toString()); 85 fEntries.put(name, entry); 86 } 87 adjustOffsets(fModel.getDocument()); 88 } 89 90 public void adjustOffsets(IDocument document) { 91 int lines = document.getNumberOfLines(); 92 try { 93 IDocumentKey currentKey = null; 94 for (int i = 0; i < lines; i++) { 95 int offset = document.getLineOffset(i); 96 int length = document.getLineLength(i); 97 String line = document.get(offset, length); 98 if (line.startsWith("#") | line.startsWith("!")) { if (currentKey != null) { 100 currentKey.setLength(offset - 1 - currentKey.getOffset()); 101 currentKey = null; 102 } 103 continue; 104 } 105 106 line = line.trim(); 107 if (line.length() == 0) { 108 if (currentKey != null && i == lines - 1) { 111 currentKey.setLength(offset - 1 - currentKey.getOffset()); 112 currentKey = null; 113 } 114 continue; 115 } 116 117 if (currentKey != null) { 118 if (!line.endsWith("\\")) { currentKey.setLength(offset + document.getLineLength(i) - currentKey.getOffset()); 120 currentKey = null; 121 } 122 } else { 123 int index = line.indexOf('='); 124 if (index == -1) 125 index = line.indexOf(':'); 126 if (index == -1) 127 index = line.indexOf(' '); 128 if (index == -1) 129 index = line.indexOf('\t'); 130 String name = (index != -1) ? line.substring(0, index).trim() : line; 131 String propertyKey; 132 try{ 133 propertyKey=PropertiesUtil.windEscapeChars(name); 134 }catch(IllegalArgumentException iae){ 135 propertyKey = name; 136 } 137 currentKey = (IDocumentKey)getEntry(propertyKey); 138 if (currentKey != null) { 139 while (Character.isSpaceChar(document.getChar(offset))) { 140 offset += 1; 141 } 142 currentKey.setOffset(offset); 143 if (!line.endsWith("\\")) { currentKey.setLength(document.getLineOffset(i) + document.getLineLength(i) - currentKey.getOffset()); 145 currentKey = null; 146 } 147 } 148 } 149 } 150 } catch (BadLocationException e) { 151 } 152 } 153 154 public BuildModel getModel() { 155 return fModel; 156 } 157 } 158 | Popular Tags |