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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.ruby;
029:
030: import java.io.IOException;
031: import java.util.ArrayList;
032: import java.util.Collection;
033: import java.util.Collections;
034: import java.util.HashMap;
035: import java.util.List;
036: import java.util.Map;
037: import java.util.Map;
038: import javax.swing.text.Document;
039: import org.netbeans.modules.gsf.api.CompilationInfo;
040: import org.netbeans.modules.gsf.api.Error;
041: import org.netbeans.modules.gsf.api.Index;
042: import org.netbeans.modules.gsf.api.ParseEvent;
043: import org.netbeans.modules.gsf.api.ParseListener;
044: import org.netbeans.modules.gsf.api.ParserFile;
045: import org.netbeans.modules.gsf.api.ParserResult;
046: import org.netbeans.modules.gsf.api.TranslatedSource;
047: import org.netbeans.napi.gsfret.source.ClasspathInfo;
048: import org.netbeans.napi.gsfret.source.Source;
049: import org.netbeans.editor.BaseDocument;
050: import org.netbeans.modules.gsf.Language;
051: import org.netbeans.modules.gsf.LanguageRegistry;
052: import org.netbeans.modules.gsf.spi.DefaultParserFile;
053: import org.openide.filesystems.FileObject;
054: import org.openide.util.Exceptions;
055:
056: /**
057: *
058: * @author tor
059: */
060: class TestCompilationInfo extends CompilationInfo {
061: private final String text;
062: private Document doc;
063: private Source source;
064: private int caretOffset = -1;
065: private RubyTestBase test;
066:
067: public TestCompilationInfo(RubyTestBase test,
068: FileObject fileObject, BaseDocument doc, String text)
069: throws IOException {
070: super (fileObject);
071: this .test = test;
072: this .text = text;
073: assert text != null;
074: this .doc = doc;
075: if (fileObject != null) {
076: //source = Source.forFileObject(fileObject);
077: ClasspathInfo cpInfo = ClasspathInfo.create(fileObject);
078: source = Source.create(cpInfo, Collections
079: .singletonList(fileObject));
080: }
081: }
082:
083: public void setCaretOffset(int caretOffset) {
084: this .caretOffset = caretOffset;
085: }
086:
087: public String getText() {
088: return text;
089: }
090:
091: public Source getSource() {
092: return source;
093: }
094:
095: public Index getIndex(String mimeType) {
096: ClasspathInfo cpi = source.getClasspathInfo();
097: if (cpi != null) {
098: return cpi.getClassIndex(mimeType);
099: }
100:
101: return null;
102: }
103:
104: @Override
105: public Document getDocument() throws IOException {
106: return this .doc;
107: }
108:
109: private Map<String, ParserResult> embeddedResults = new HashMap<String, ParserResult>();
110:
111: @Override
112: public Collection<? extends ParserResult> getEmbeddedResults(
113: String mimeType) {
114: ParserResult result = getEmbeddedResult(mimeType, 0);
115: if (result != null) {
116: return Collections.singletonList(result);
117: } else {
118: return Collections.emptyList();
119: }
120: }
121:
122: @Override
123: public ParserResult getEmbeddedResult(String embeddedMimeType,
124: int offset) {
125: assert embeddedMimeType.equals(RubyMimeResolver.RUBY_MIME_TYPE);
126:
127: if (embeddedResults.size() == 0) {
128: final List<Error> errors = new ArrayList<Error>();
129: ParseListener listener = new ParseListener() {
130:
131: public void started(ParseEvent e) {
132: errors.clear();
133: }
134:
135: public void error(Error error) {
136: errors.add(error);
137: }
138:
139: public void exception(Exception exception) {
140: Exceptions.printStackTrace(exception);
141: }
142:
143: public void finished(ParseEvent e) {
144: }
145: };
146:
147: List<ParserFile> sourceFiles = new ArrayList<ParserFile>(1);
148: ParserFile file = new DefaultParserFile(getFileObject(),
149: null, false);
150: sourceFiles.add(file);
151:
152: TranslatedSource translatedSource = null; // TODO
153: RubyParser.Context context = new RubyParser.Context(file,
154: listener, text, caretOffset, translatedSource);
155: RubyParser parser = new RubyParser();
156: ParserResult parserResult = ((RubyParser) parser)
157: .parseBuffer(context, RubyParser.Sanitize.NONE);
158: for (Error error : errors) {
159: parserResult.addError(error);
160: }
161: embeddedResults.put(RubyMimeResolver.RUBY_MIME_TYPE,
162: parserResult);
163: parserResult.setInfo(this );
164: }
165:
166: return embeddedResults.get(embeddedMimeType);
167: }
168:
169: @Override
170: public List<Error> getErrors() {
171: // Force initialization
172: getEmbeddedResult(RubyMimeResolver.RUBY_MIME_TYPE, 0);
173:
174: List<Error> errors = new ArrayList<Error>();
175: for (ParserResult result : embeddedResults.values()) {
176: errors.addAll(result.getDiagnostics());
177: }
178:
179: return errors;
180: }
181: //
182: // @Override
183: // public void setParserResult(final ParserResult parserResult) {
184: // parserResult = parserResult;
185: //
186: // embeddedResults.put(getFileObject().getMIMEType(), parserResult);
187: // }
188: }
|