KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > ExportBreakpointsOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.debug.ui.actions;
13
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18
19 import org.eclipse.core.resources.IMarker;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.internal.ui.importexport.breakpoints.IImportExportConstants;
25 import org.eclipse.debug.internal.ui.importexport.breakpoints.ImportExportMessages;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.XMLMemento;
29
30 /**
31  * Exports breakpoints to a file.
32  * <p>
33  * This class may be instantiated; not intended to be subclassed.
34  * <p>
35  * @since 3.2
36  */

37 public class ExportBreakpointsOperation implements IRunnableWithProgress {
38
39     private IBreakpoint[] fBreakpoints = null;
40     private String JavaDoc fFileName = null;
41     
42     /**
43      * Constructs an operation to export breakpoints to a file.
44      *
45      * @param breakpoints the breakpoints to export
46      * @param fileName absolute path of file to export breakpoints to - the file
47      * will be overwritten if it already exists
48      */

49     public ExportBreakpointsOperation(IBreakpoint[] breakpoints, String JavaDoc fileName) {
50         fBreakpoints = breakpoints;
51         fFileName = fileName;
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
56      */

57     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
58         XMLMemento memento = XMLMemento.createWriteRoot(IImportExportConstants.IE_NODE_BREAKPOINTS);
59         monitor.beginTask(ImportExportMessages.ExportOperation_0, fBreakpoints.length);
60         try {
61             for (int i = 0; i < fBreakpoints.length; i++) {
62                 IBreakpoint breakpoint = fBreakpoints[i];
63                 //in the event we are in working set view, we can have multiple selection of the same breakpoint
64
//so do a simple check for it
65
IMarker marker = breakpoint.getMarker();
66                 IMemento root = memento.createChild(IImportExportConstants.IE_NODE_BREAKPOINT);
67                 root.putString(IImportExportConstants.IE_BP_ENABLED, Boolean.toString(breakpoint.isEnabled()));
68                 root.putString(IImportExportConstants.IE_BP_REGISTERED, Boolean.toString(breakpoint.isRegistered()));
69                 root.putString(IImportExportConstants.IE_BP_PERSISTANT, Boolean.toString(breakpoint.isPersisted()));
70                 //write out the resource information
71
IResource resource = marker.getResource();
72                 IMemento child = root.createChild(IImportExportConstants.IE_NODE_RESOURCE);
73                 child.putString(IImportExportConstants.IE_NODE_PATH, resource.getFullPath().toPortableString());
74                 child.putInteger(IImportExportConstants.IE_NODE_TYPE, resource.getType());
75                 //a generalized (name, value) pairing for attributes each stored as an ATTRIB element
76
root = root.createChild(IImportExportConstants.IE_NODE_MARKER);
77                 root.putString(IImportExportConstants.IE_NODE_TYPE, marker.getType());
78                 Object JavaDoc val = marker.getAttribute(IMarker.LINE_NUMBER);
79                 root.putString(IMarker.LINE_NUMBER, (val != null) ? val.toString() : null);
80                 val = marker.getAttribute(IImportExportConstants.CHARSTART);
81                 root.putString(IImportExportConstants.CHARSTART, (val != null) ? val.toString() : null);
82                 for(java.util.Iterator JavaDoc iter = marker.getAttributes().keySet().iterator(); iter.hasNext();) {
83                     String JavaDoc iterval = iter.next().toString();
84                     if(!iterval.equals(IMarker.LINE_NUMBER)) {
85                         child = root.createChild(IImportExportConstants.IE_NODE_ATTRIB);
86                         child.putString(IImportExportConstants.IE_NODE_NAME, iterval);
87                         child.putString(IImportExportConstants.IE_NODE_VALUE, marker.getAttribute(iterval).toString());
88                     }
89                 }
90             }
91             OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(fFileName), "UTF-8"); //$NON-NLS-1$
92
memento.save(osw);
93             osw.close();
94         } catch (CoreException e) {
95             throw new InvocationTargetException JavaDoc(e);
96         } catch (IOException JavaDoc e) {
97             throw new InvocationTargetException JavaDoc(e);
98         }
99     }
100     
101     
102 }
103
Popular Tags