Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Project Persistant Make Targets

Folks,

  This patch address the problem described in PR 53486 where the 
make build targets defined for a standard make project or any project
using the IMakeTargetManager, are persisted in a workspace specific
location rather than a project specific location.  The patch is backward
compatible in that if it doesn't find any information, it reverts to
the previous method of loading the initial targets and then saves the
targets to the .cdtproject

For the ChangeLog
- Persist the make build target information in a per-project location
  rather than in a workspace specific location.  Fall back to looking
  in the workspace location if no project information is found on load.

Index: src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java
===================================================================
retrieving revision 1.15
diff -u -r1.15 MakeTargetManager.java
--- src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java	24 Feb 2004 02:03:08 -0000	1.15
+++ src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java	12 Mar 2004 13:17:40 -0000
@@ -11,9 +11,6 @@
 package org.eclipse.cdt.make.internal.core;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -243,13 +240,8 @@
 	}
 
 	protected void writeTargets(ProjectTargets projectTargets) throws CoreException {
-		IPath targetFilePath =
-			MakeCorePlugin.getDefault().getStateLocation().append(projectTargets.getProject().getName()).addFileExtension(
-				TARGETS_EXT);
-		File targetFile = targetFilePath.toFile();
 		try {
-			FileOutputStream file = new FileOutputStream(targetFile);
-			projectTargets.saveTargets(file);
+			projectTargets.saveTargets();
 		} catch (IOException e) {
 			throw new CoreException(
 				new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.error_writing_file"), e)); //$NON-NLS-1$
@@ -257,24 +249,13 @@
 	}
 
 	protected ProjectTargets readTargets(IProject project) throws CoreException {
-		IPath targetFilePath =
-			MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT);
-		File targetFile = targetFilePath.toFile();
-		ProjectTargets projectTargets = null;
-		if (targetFile.exists()) {
-			try {
-				projectTargets = new ProjectTargets(this, project, new FileInputStream(targetFile));
-			} catch (FileNotFoundException e) {
-			}
-		}
-		if (projectTargets == null) {
-			projectTargets = new ProjectTargets(project);
-		}
+		ProjectTargets projectTargets = new ProjectTargets(this, project);
 		projectMap.put(project, projectTargets);
 		return projectTargets;
 	}
 
 	protected void deleteTargets(IProject project) {
+		//Historical: We clean up after all other parts.
 		IPath targetFilePath =
 			MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT);
 		File targetFile = targetFilePath.toFile();
Index: src/org/eclipse/cdt/make/internal/core/ProjectTargets.java
===================================================================
retrieving revision 1.11
diff -u -r1.11 ProjectTargets.java
--- src/org/eclipse/cdt/make/internal/core/ProjectTargets.java	2 Mar 2004 17:13:52 -0000	1.11
+++ src/org/eclipse/cdt/make/internal/core/ProjectTargets.java	12 Mar 2004 13:17:40 -0000
@@ -1,28 +1,27 @@
 package org.eclipse.cdt.make.internal.core;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.xerces.dom.DocumentImpl;
-import org.apache.xml.serialize.Method;
-import org.apache.xml.serialize.OutputFormat;
-import org.apache.xml.serialize.Serializer;
-import org.apache.xml.serialize.SerializerFactory;
+import org.eclipse.cdt.core.CCorePlugin;
+import org.eclipse.cdt.core.ICDescriptor;
 import org.eclipse.cdt.make.core.IMakeTarget;
 import org.eclipse.cdt.make.core.MakeCorePlugin;
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
@@ -33,6 +32,9 @@
 import org.w3c.dom.NodeList;
 
 public class ProjectTargets {
+	private static final String MAKE_TARGET_KEY = "buildtargets";
+	private static final String TARGETS_EXT = "targets"; //$NON-NLS-1$
+
 
 	private static final String BUILD_TARGET_ELEMENT = "buildTargets"; //$NON-NLS-1$
 	private static final String TARGET_ELEMENT = "target"; //$NON-NLS-1$
@@ -53,66 +55,27 @@
 		this.project = project;
 	}
 
-	public ProjectTargets(MakeTargetManager manager, IProject project, InputStream input) throws CoreException {
+	public ProjectTargets(MakeTargetManager manager, IProject project) throws CoreException {
 		this(project);
 
-		Document document = null;
-		try {
-			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-			document = parser.parse(input);
-		} catch (Exception e) {
-			throw new CoreException(
-				new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("ProjectTargets.error_reading_project_targets"), e)); //$NON-NLS-1$
-		}
-		Node node = document.getFirstChild();
-		if (node.getNodeName().equals(BUILD_TARGET_ELEMENT)) {
-			NodeList list = node.getChildNodes();
-			for (int i = 0; i < list.getLength(); i++) {
-				node = list.item(i);
-				if (node.getNodeName().equals(TARGET_ELEMENT)) {
-					IContainer container = null;
-					NamedNodeMap attr = node.getAttributes();
-					String path = attr.getNamedItem(TARGET_ATTR_PATH).getNodeValue();
-					if (path != null && !path.equals("")) { //$NON-NLS-1$
-						container = project.getFolder(path);
-					} else {
-						container = project;
-					}
-					try {
-						MakeTarget target =
-							new MakeTarget(
-								manager,
-								project,
-								attr.getNamedItem(TARGET_ATTR_ID).getNodeValue(),
-								attr.getNamedItem(TARGET_ATTR_NAME).getNodeValue());
-						target.setContainer(container);
-						String option = getString(node, TARGET_STOP_ON_ERROR);
-						if (option != null) {
-							target.setStopOnError(Boolean.valueOf(option).booleanValue());
-						}
-						option = getString(node, TARGET_USE_DEFAULT_CMD);
-						if (option != null) {
-							target.setUseDefaultBuildCmd(Boolean.valueOf(option).booleanValue());
-						}
-						option = getString(node, TARGET_COMMAND);
-						if (option != null) {
-							target.setBuildCommand(new Path(option));
-						}
-						option = getString(node, TARGET_ARGUMENTS);
-						if (option != null) {
-							target.setBuildArguments(option);
-						}
-						option = getString(node, TARGET);
-						if (option != null) {
-							target.setBuildTarget(option);
-						}
-						add(target);
-					} catch (CoreException e) {
-						MakeCorePlugin.log(e);
-					}
-				}
+		Document document = translateCDTProjectToDocument();
+
+		//Historical ... fall back to the workspace and look in previous location
+		if(document == null) {
+			IPath targetFilePath = 
+				MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT);
+			File targetFile = targetFilePath.toFile();
+			try {
+				InputStream input = new FileInputStream(targetFile);
+				document = translateInputStreamToDocument(input);
+			} catch(FileNotFoundException ex) {
+				/* Ignore */
 			}
 		}
+
+		if(document != null) {		
+			extractMakeTargetsFromDocument(document, manager);
+		}
 	}
 
 	protected String getString(Node target, String tagName) {
@@ -187,8 +150,14 @@
 		return project;
 	}
 
-	protected Document getAsXML() {
-		Document doc = new DocumentImpl();
+	protected Document getAsXML() throws IOException {
+		Document doc;
+		try {
+			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+		} catch(ParserConfigurationException ex) {
+			//This should never happen.
+			throw new IOException("Error creating new XML storage document"); //$NON-NLS-1$
+		}
 		Element targetsRootElement = doc.createElement(BUILD_TARGET_ELEMENT);
 		doc.appendChild(targetsRootElement);
 		Iterator container = targetMap.entrySet().iterator();
@@ -229,14 +198,137 @@
 		return targetElem;
 	}
 
-	public void saveTargets(OutputStream output) throws IOException {
-		Document doc = getAsXML();
-		OutputFormat format = new OutputFormat();
-		format.setIndenting(true);
-		format.setPreserveSpace(true);
-		format.setLineSeparator(System.getProperty("line.separator")); //$NON-NLS-1$
-		Serializer serializer =
-			SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(new OutputStreamWriter(output, "UTF8"), format); //$NON-NLS-1$
-		serializer.asDOMSerializer().serialize(doc);
+	public void saveTargets() throws IOException {
+		try {
+			Document doc = getAsXML();
+			//Historical method would save the output to the stream specified
+			//translateDocumentToOutputStream(doc, output);
+			translateDocumentToCDTProject(doc);
+		} catch(CoreException ex) {
+			throw new IOException(ex.getMessage());
+		}
+	}
+
+	/**
+	 * This output method saves the information into the .cdtproject metadata file.
+	 * @param doc
+	 * @throws IOException
+	 */
+	protected void translateDocumentToCDTProject(Document doc) throws CoreException, IOException {
+		ICDescriptor descriptor;
+		descriptor = CCorePlugin.getDefault().getCProjectDescription(getProject());
+		
+		Element rootElement = descriptor.getProjectData(MAKE_TARGET_KEY);
+		
+		//Nuke the children since we are going to write out new ones
+		NodeList kids = rootElement.getChildNodes();
+		for(int i = 0; i < kids.getLength(); i++) {
+			rootElement.removeChild(kids.item(i));
+		}
+
+		//Extract the root of our temporary document
+		Node node = doc.getFirstChild();
+		//Create a copy which is a part of the new document	
+		Node appendNode = rootElement.getOwnerDocument().importNode(node, true);
+		//Put the copy into the document in the appropriate location
+		rootElement.appendChild(appendNode);
+
+		
+		//Save the results
+		descriptor.saveProjectData();
+	}
+	
+	/**
+	 * This method parses the .cdtproject file for the XML document describing the build targets.
+	 * @param input
+	 * @return
+	 */
+	protected Document translateCDTProjectToDocument() {
+		try {
+			ICDescriptor descriptor;
+			descriptor = CCorePlugin.getDefault().getCProjectDescription(getProject());
+
+			Element rootElement = descriptor.getProjectData(MAKE_TARGET_KEY);
+			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+			
+			Element element = rootElement.getOwnerDocument().getDocumentElement();
+			Node appendNode = document.importNode(element, true);
+			document.appendChild(appendNode);
+			
+			return document;
+		} catch(Exception ex) {
+			return null;
+		}
+	}
+
+	/**
+	 * This method parses the input stream for the XML document describing the build targets.
+	 * @param input
+	 * @return
+	 */
+	protected Document translateInputStreamToDocument(InputStream input) {
+		Document document = null;
+		try {
+			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
+		} catch (Exception e) {
+			MakeCorePlugin.log(e);
+		}
+		return document;
+	}
+
+	/**
+	 * Extract the make target information which is contained in the XML Document
+	 * @param document
+	 */
+	protected void extractMakeTargetsFromDocument(Document document, MakeTargetManager manager) {
+		Node node = document.getFirstChild();
+		if (node.getNodeName().equals(BUILD_TARGET_ELEMENT)) {
+			NodeList list = node.getChildNodes();
+			for (int i = 0; i < list.getLength(); i++) {
+				node = list.item(i);
+				if (node.getNodeName().equals(TARGET_ELEMENT)) {
+					IContainer container = null;
+					NamedNodeMap attr = node.getAttributes();
+					String path = attr.getNamedItem(TARGET_ATTR_PATH).getNodeValue();
+					if (path != null && !path.equals("")) { //$NON-NLS-1$
+						container = project.getFolder(path);
+					} else {
+						container = project;
+					}
+					try {
+						MakeTarget target =
+							new MakeTarget(
+								manager,
+								project,
+								attr.getNamedItem(TARGET_ATTR_ID).getNodeValue(),
+								attr.getNamedItem(TARGET_ATTR_NAME).getNodeValue());
+						target.setContainer(container);
+						String option = getString(node, TARGET_STOP_ON_ERROR);
+						if (option != null) {
+							target.setStopOnError(Boolean.valueOf(option).booleanValue());
+						}
+						option = getString(node, TARGET_USE_DEFAULT_CMD);
+						if (option != null) {
+							target.setUseDefaultBuildCmd(Boolean.valueOf(option).booleanValue());
+						}
+						option = getString(node, TARGET_COMMAND);
+						if (option != null) {
+							target.setBuildCommand(new Path(option));
+						}
+						option = getString(node, TARGET_ARGUMENTS);
+						if (option != null) {
+							target.setBuildArguments(option);
+						}
+						option = getString(node, TARGET);
+						if (option != null) {
+							target.setBuildTarget(option);
+						}
+						add(target);
+					} catch (CoreException e) {
+						MakeCorePlugin.log(e);
+					}
+				}
+			}
+		}		
 	}
 }

Back to the top