001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.php.dbgp.breakpoints;
043:
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: import org.netbeans.api.debugger.Properties;
050: import org.netbeans.modules.php.dbgp.breakpoints.FunctionBreakpoint.Type;
051: import org.openide.cookies.LineCookie;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileStateInvalidException;
054: import org.openide.filesystems.URLMapper;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.text.Line;
058:
059: /**
060: *
061: * @author ads
062: */
063: public class BreakpointsReader implements Properties.Reader {
064:
065: private static final String LINE_NUMBER = "lineNumber"; // NOI18N
066:
067: private static final String URL = "url"; // NOI18N
068:
069: private static final String ENABED = "enabled"; // NOI18N
070:
071: private static final String FUNC_NAME = "functionName"; // NOI18N
072:
073: private static final String TYPE = "type"; // NOI18N
074:
075: public String[] getSupportedClassNames() {
076: return new String[] { LineBreakpoint.class.getName(),
077: FunctionBreakpoint.class.getName() };
078: }
079:
080: public Object read(String typeID, Properties properties) {
081: if (typeID.equals(LineBreakpoint.class.getName())) {
082: Line line = getLine(properties.getString(URL, null),
083: properties.getInt(LINE_NUMBER, 1));
084:
085: if (line == null) {
086: return null;
087: }
088: LineBreakpoint breakpoint = new LineBreakpoint(line);
089: if (!properties.getBoolean(ENABED, true)) {
090: breakpoint.disable();
091: }
092: return new LineBreakpoint(line);
093: } else if (typeID.equals(FunctionBreakpoint.class.getName())) {
094: String func = properties.getString(FUNC_NAME, null);
095: Type type = Type
096: .forString(properties.getString(TYPE, null));
097: if (func == null || type == null) {
098: return null;
099: }
100: return new FunctionBreakpoint(type, func);
101: } else {
102: return null;
103: }
104: }
105:
106: public void write(Object object, Properties properties) {
107: if (object instanceof LineBreakpoint) {
108: LineBreakpoint breakpoint = (LineBreakpoint) object;
109: FileObject fileObject = breakpoint.getLine().getLookup()
110: .lookup(FileObject.class);
111:
112: try {
113: properties.setString(URL, fileObject.getURL()
114: .toString());
115: properties.setInt(LINE_NUMBER, breakpoint.getLine()
116: .getLineNumber());
117: properties.setBoolean(ENABED, breakpoint.isEnabled());
118: } catch (FileStateInvalidException ex) {
119: log(ex);
120: }
121: } else if (object instanceof FunctionBreakpoint) {
122: FunctionBreakpoint breakpoint = (FunctionBreakpoint) object;
123: String func = breakpoint.getFunction();
124: properties.setString(FUNC_NAME, func);
125: properties.setString(TYPE, breakpoint.getType().toString());
126: properties.setBoolean(ENABED, breakpoint.isEnabled());
127: }
128: }
129:
130: private void log(FileStateInvalidException ex) {
131: Logger.getLogger(BreakpointsReader.class.getName()).log(
132: Level.SEVERE, null, ex);
133: }
134:
135: private Line getLine(String url, int lineNumber) {
136: FileObject file = getFileObject(url);
137: if (file == null) {
138: return null;
139: }
140:
141: DataObject dataObject = null;
142: try {
143: dataObject = DataObject.find(file);
144: } catch (DataObjectNotFoundException ex) {
145: return null;
146: }
147:
148: if (dataObject == null) {
149: return null;
150: }
151:
152: LineCookie lineCookie = dataObject.getCookie(LineCookie.class);
153: if (lineCookie == null) {
154: return null;
155: }
156:
157: Line.Set ls = lineCookie.getLineSet();
158: if (ls == null) {
159: return null;
160: }
161:
162: try {
163: return ls.getCurrent(lineNumber);
164: } catch (IndexOutOfBoundsException e) {
165: return null;
166: }
167: }
168:
169: private FileObject getFileObject(String url) {
170: FileObject file;
171: try {
172: file = URLMapper.findFileObject(new URL(url));
173: } catch (MalformedURLException e) {
174: return null;
175: }
176:
177: return file;
178: }
179: }
|