KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > instr > InstrMarker


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.instr;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Date JavaDoc;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IFolder;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22 /**
23  * Static utilities to mark instrumented output folders. The mark created as a
24  * special file in the output directory, which will be deleted during a clean
25  * build.
26  *
27  * @author Marc R. Hoffmann
28  * @version $Revision: $
29  */

30 public class InstrMarker {
31
32   private static final String JavaDoc MARKERFILE = ".emma_instrumented"; //$NON-NLS-1$
33

34   /**
35    * Sets a mark on the given output folder.
36    *
37    * @param path
38    * workspace relative path of the output folder
39    * @throws CoreException
40    * Thrown when creating the marker file fails
41    */

42   public static void mark(IPath path) throws CoreException {
43     IFolder folder = getFolder(path);
44     if (folder != null) {
45       IFile marker = folder.getFile(MARKERFILE);
46       if (!marker.exists()) {
47         marker.create(getMarkerContent(), true, null);
48         marker.setDerived(true);
49       }
50     }
51   }
52
53   /**
54    * Checks whether the given output folder has an instrumentation mark.
55    *
56    * @param path
57    * workspace relative path of the output folder
58    * @return <code>true</true> if the mark exists
59    */

60   public static boolean isMarked(IPath path) {
61     IFolder folder = getFolder(path);
62     return folder == null ? false : folder.getFile(MARKERFILE).exists();
63   }
64
65   private static IFolder getFolder(IPath path) {
66     IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
67     IResource res = root.findMember(path);
68     return res instanceof IFolder ? (IFolder) res : null;
69   }
70
71   private static InputStream JavaDoc getMarkerContent() {
72     String JavaDoc text = "Class files instrumented at " + new Date JavaDoc(); //$NON-NLS-1$
73
return new ByteArrayInputStream JavaDoc(text.getBytes());
74   }
75
76 }
77
Popular Tags