1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.IOException ; 14 import java.io.OutputStream ; 15 import java.net.URI ; 16 import java.util.*; 17 import org.eclipse.core.filesystem.EFS; 18 import org.eclipse.core.internal.events.BuildCommand; 19 import org.eclipse.core.internal.localstore.SafeFileOutputStream; 20 import org.eclipse.core.internal.utils.FileUtil; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IncrementalProjectBuilder; 23 import org.eclipse.core.runtime.IPath; 24 25 public class ModelObjectWriter implements IModelObjectConstants { 27 28 32 private static String triggerString(BuildCommand command) { 33 StringBuffer buf = new StringBuffer (); 34 if (command.isBuilding(IncrementalProjectBuilder.AUTO_BUILD)) 35 buf.append(TRIGGER_AUTO).append(','); 36 if (command.isBuilding(IncrementalProjectBuilder.CLEAN_BUILD)) 37 buf.append(TRIGGER_CLEAN).append(','); 38 if (command.isBuilding(IncrementalProjectBuilder.FULL_BUILD)) 39 buf.append(TRIGGER_FULL).append(','); 40 if (command.isBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD)) 41 buf.append(TRIGGER_INCREMENTAL).append(','); 42 return buf.toString(); 43 } 44 45 public ModelObjectWriter() { 46 super(); 47 } 48 49 protected String [] getReferencedProjects(ProjectDescription description) { 50 IProject[] projects = description.getReferencedProjects(); 51 String [] result = new String [projects.length]; 52 for (int i = 0; i < projects.length; i++) 53 result[i] = projects[i].getName(); 54 return result; 55 } 56 57 protected void write(BuildCommand command, XMLWriter writer) { 58 writer.startTag(BUILD_COMMAND, null); 59 if (command != null) { 60 writer.printSimpleTag(NAME, command.getName()); 61 if (shouldWriteTriggers(command)) 62 writer.printSimpleTag(BUILD_TRIGGERS, triggerString(command)); 63 write(ARGUMENTS, command.getArguments(false), writer); 64 } 65 writer.endTag(BUILD_COMMAND); 66 } 67 68 71 private boolean shouldWriteTriggers(BuildCommand command) { 72 if (!command.isConfigurable()) 76 return false; 77 return !command.isBuilding(IncrementalProjectBuilder.AUTO_BUILD) || 78 !command.isBuilding(IncrementalProjectBuilder.CLEAN_BUILD) || 79 !command.isBuilding(IncrementalProjectBuilder.FULL_BUILD) || 80 !command.isBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD); 81 } 82 83 protected void write(LinkDescription description, XMLWriter writer) { 84 writer.startTag(LINK, null); 85 if (description != null) { 86 writer.printSimpleTag(NAME, description.getProjectRelativePath()); 87 writer.printSimpleTag(TYPE, Integer.toString(description.getType())); 88 writeLocation(description.getLocationURI(), writer); 90 } 91 writer.endTag(LINK); 92 } 93 94 101 private void writeLocation(URI location, XMLWriter writer) { 102 if (EFS.SCHEME_FILE.equals(location.getScheme())) { 103 writer.printSimpleTag(LOCATION, FileUtil.toPath(location).toPortableString()); 104 } else { 105 writer.printSimpleTag(LOCATION_URI, location.toASCIIString()); 106 } 107 } 108 109 113 public void write(Object object, IPath location, IPath tempLocation) throws IOException { 114 SafeFileOutputStream file = null; 115 String tempPath = tempLocation == null ? null : tempLocation.toOSString(); 116 try { 117 file = new SafeFileOutputStream(location.toOSString(), tempPath); 118 write(object, file); 119 } finally { 120 if (file != null) 121 file.close(); 122 } 123 } 124 125 128 public void write(Object object, OutputStream output) throws IOException { 129 try { 130 XMLWriter writer = new XMLWriter(output); 131 write(object, writer); 132 writer.flush(); 133 writer.close(); 134 } finally { 135 output.close(); 136 } 137 } 138 139 protected void write(Object obj, XMLWriter writer) throws IOException { 140 if (obj instanceof BuildCommand) { 141 write((BuildCommand) obj, writer); 142 return; 143 } 144 if (obj instanceof ProjectDescription) { 145 write((ProjectDescription) obj, writer); 146 return; 147 } 148 if (obj instanceof WorkspaceDescription) { 149 write((WorkspaceDescription) obj, writer); 150 return; 151 } 152 if (obj instanceof LinkDescription) { 153 write((LinkDescription) obj, writer); 154 return; 155 } 156 writer.printTabulation(); 157 writer.println(obj.toString()); 158 } 159 160 protected void write(ProjectDescription description, XMLWriter writer) throws IOException { 161 writer.startTag(PROJECT_DESCRIPTION, null); 162 if (description != null) { 163 writer.printSimpleTag(NAME, description.getName()); 164 String comment = description.getComment(); 165 writer.printSimpleTag(COMMENT, comment == null ? "" : comment); write(PROJECTS, PROJECT, getReferencedProjects(description), writer); 167 write(BUILD_SPEC, Arrays.asList(description.getBuildSpec(false)), writer); 168 write(NATURES, NATURE, description.getNatureIds(false), writer); 169 HashMap links = description.getLinks(); 170 if (links != null) 171 write(LINKED_RESOURCES, links.values(), writer); 172 } 173 writer.endTag(PROJECT_DESCRIPTION); 174 } 175 176 protected void write(String name, Collection collection, XMLWriter writer) throws IOException { 177 writer.startTag(name, null); 178 for (Iterator it = collection.iterator(); it.hasNext();) 179 write(it.next(), writer); 180 writer.endTag(name); 181 } 182 183 186 protected void write(String name, Map table, XMLWriter writer) { 187 writer.startTag(name, null); 188 for (Iterator it = table.entrySet().iterator(); it.hasNext();) { 189 Map.Entry entry = (Map.Entry) it.next(); 190 String key = (String ) entry.getKey(); 191 Object value = entry.getValue(); 192 writer.startTag(DICTIONARY, null); 193 { 194 writer.printSimpleTag(KEY, key); 195 writer.printSimpleTag(VALUE, value); 196 } 197 writer.endTag(DICTIONARY); 198 } 199 writer.endTag(name); 200 } 201 202 protected void write(String name, String elementTagName, String [] array, XMLWriter writer) { 203 writer.startTag(name, null); 204 for (int i = 0; i < array.length; i++) 205 writer.printSimpleTag(elementTagName, array[i]); 206 writer.endTag(name); 207 } 208 209 protected void write(WorkspaceDescription description, XMLWriter writer) { 210 writer.startTag(WORKSPACE_DESCRIPTION, null); 211 if (description != null) { 212 writer.printSimpleTag(NAME, description.getName()); 213 writer.printSimpleTag(AUTOBUILD, description.isAutoBuilding() ? "1" : "0"); writer.printSimpleTag(SNAPSHOT_INTERVAL, new Long (description.getSnapshotInterval())); 215 writer.printSimpleTag(FILE_STATE_LONGEVITY, new Long (description.getFileStateLongevity())); 216 writer.printSimpleTag(MAX_FILE_STATE_SIZE, new Long (description.getMaxFileStateSize())); 217 writer.printSimpleTag(MAX_FILE_STATES, new Integer (description.getMaxFileStates())); 218 String [] order = description.getBuildOrder(false); 219 if (order != null) 220 write(BUILD_ORDER, PROJECT, order, writer); 221 } 222 writer.endTag(WORKSPACE_DESCRIPTION); 223 } 224 } 225 | Popular Tags |