KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > ModelObjectWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.internal.resources;
12
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.net.URI JavaDoc;
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 //
26
public class ModelObjectWriter implements IModelObjectConstants {
27
28     /**
29      * Returns the string representing the serialized set of build triggers for
30      * the given command
31      */

32     private static String JavaDoc triggerString(BuildCommand command) {
33         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc[] getReferencedProjects(ProjectDescription description) {
50         IProject[] projects = description.getReferencedProjects();
51         String JavaDoc[] result = new String JavaDoc[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     /**
69      * Returns whether the build triggers for this command should be written.
70      */

71     private boolean shouldWriteTriggers(BuildCommand command) {
72         //only write triggers if command is configurable and there exists a trigger
73
//that the builder does NOT respond to. I.e., don't write out on the default
74
//cases to avoid dirtying .project files unnecessarily.
75
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             //use ASCII string of URI to ensure spaces are encoded
89
writeLocation(description.getLocationURI(), writer);
90         }
91         writer.endTag(LINK);
92     }
93
94     /**
95      * Writes a location to the XML writer. For backwards compatibility,
96      * local file system locations are written and read using a different tag
97      * from non-local file systems.
98      * @param location
99      * @param writer
100      */

101     private void writeLocation(URI JavaDoc 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     /**
110      * The parameter tempLocation is a location to place our temp file (copy of the target one)
111      * to be used in case we could not successfully write the new file.
112      */

113     public void write(Object JavaDoc object, IPath location, IPath tempLocation) throws IOException JavaDoc {
114         SafeFileOutputStream file = null;
115         String JavaDoc 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     /**
126      * The OutputStream is closed in this method.
127      */

128     public void write(Object JavaDoc object, OutputStream JavaDoc output) throws IOException JavaDoc {
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 JavaDoc obj, XMLWriter writer) throws IOException JavaDoc {
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 JavaDoc {
161         writer.startTag(PROJECT_DESCRIPTION, null);
162         if (description != null) {
163             writer.printSimpleTag(NAME, description.getName());
164             String JavaDoc comment = description.getComment();
165             writer.printSimpleTag(COMMENT, comment == null ? "" : comment); //$NON-NLS-1$
166
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 JavaDoc name, Collection collection, XMLWriter writer) throws IOException JavaDoc {
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     /**
184      * Write maps of (String, String).
185      */

186     protected void write(String JavaDoc 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 JavaDoc key = (String JavaDoc) entry.getKey();
191             Object JavaDoc 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 JavaDoc name, String JavaDoc elementTagName, String JavaDoc[] 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"); //$NON-NLS-1$ //$NON-NLS-2$
214
writer.printSimpleTag(SNAPSHOT_INTERVAL, new Long JavaDoc(description.getSnapshotInterval()));
215             writer.printSimpleTag(FILE_STATE_LONGEVITY, new Long JavaDoc(description.getFileStateLongevity()));
216             writer.printSimpleTag(MAX_FILE_STATE_SIZE, new Long JavaDoc(description.getMaxFileStateSize()));
217             writer.printSimpleTag(MAX_FILE_STATES, new Integer JavaDoc(description.getMaxFileStates()));
218             String JavaDoc[] 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