Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.parsing.api;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.mimelookup.MimePath;
import org.netbeans.api.lexer.InputAttributes;
import org.netbeans.api.lexer.Language;
import org.netbeans.api.lexer.LanguagePath;
import org.netbeans.api.queries.FileEncodingQuery;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.netbeans.modules.parsing.impl.ParserEventForward;
import org.netbeans.modules.parsing.impl.SchedulerAccessor;
import org.netbeans.modules.parsing.impl.SourceAccessor;
import org.netbeans.modules.parsing.impl.SourceCache;
import org.netbeans.modules.parsing.impl.SourceFlags;
import org.netbeans.modules.parsing.impl.TaskProcessor;
import org.netbeans.modules.parsing.impl.Utilities;
import org.netbeans.modules.parsing.implspi.SchedulerControl;
import org.netbeans.modules.parsing.implspi.SourceControl;
import org.netbeans.modules.parsing.implspi.SourceEnvironment;
import org.netbeans.modules.parsing.implspi.SourceFactory;
import org.netbeans.modules.parsing.spi.Parser;
import org.netbeans.modules.parsing.spi.Scheduler;
import org.netbeans.modules.parsing.spi.SchedulerEvent;
import org.netbeans.modules.parsing.spi.SourceModificationEvent;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;
import org.openide.util.Parameters;
/**
* The Source represents one file or document.
*
*
An instance of Source
* can either be obtained for a FileObject or Document. If
* a particular FileObject and a Document are tied together
* in the way that the Document was loaded from the FileObject
* using either of them will get the same Source instance.
*
*
Please note that the infrastructure does not keep
* Source instances forever and they can and will be garbage collected
* if nobody references them. This also means that two successive Source.create
* calls for the same file or document will return two different Source
* instances if the first instance is garbage collected prior the second call.
*
*
* The Source can be created with a {@link Lookup} instance. This Lookup should be used
* by parsing system, or callback tasks to locate services, that depend on the execution
* context (ie which user is executing the task). Check the specific service's documentation
* on details of the service's context dependencies.
*
* @author Jan Jancura
* @author Tomas Zezula
* @since 9.2 implements Lookup.Provider
*/
public final class Source implements Lookup.Provider {
/**
* Gets a Source instance for a file. The FileObject
* passed to this method has to be a valid data file. There is no Source
* representation for a folder.
*
* @param fileObject The file to get Source for.
*
* @return The Source for the given file or null
* if the file doesn't exist.
*/
// XXX: this should really be called 'get'
public static Source create (
FileObject fileObject
) {
Parameters.notNull("fileObject", fileObject); //NOI18N
if (!fileObject.isValid() || !fileObject.isData()) {
return null;
}
return _get(fileObject.getMIMEType(), fileObject, Lookup.getDefault());
}
/**
* Gets a Source instance for a file. The FileObject
* passed to this method has to be a valid data file. There is no Source
* representation for a folder.
*
* This form allows to specify Lookup that provides access to context-dependent
* services for the parsing system and the user tasks called from parsing API.
*
* @param fileObject The file to get Source for.
* @param lkp The Lookup that provides the context
* @return The Source for the given file or null
* if the file doesn't exist.
* @since 9.2
*/
public static Source create (
FileObject fileObject, Lookup lkp
) {
Parameters.notNull("fileObject", fileObject); //NOI18N
Parameters.notNull("lkp", lkp); //NOI18N
if (!fileObject.isValid() || !fileObject.isData()) {
return null;
}
return _get(fileObject.getMIMEType(), fileObject, lkp);
}
/**
* Gets a Source instance for a Document. This method
* is consistent with {@link #create(org.openide.filesystems.FileObject)} in the way
* that they both will return the same Source instance for
* documents loaded from files. For example the following asserts will never fail
* (providing that relevant method calls return non-null).
*
*
Please note that you can get Source instance for any arbitrary
* document no matter if it was loaded from a file or not. However, the editor
* infrastructure generally does not support creation of fileless documents that
* are later saved and re-bound to a FileObject. If you wish to do
* something like that you will have to create a new Document instance
* loaded from the FileObject and use it instead of the original one.
*
* @param document The Document to get Source for.
*
* @return The Source for the given document; never null.
*/
// XXX: this should really be called 'get'
public static Source create (
Document document
) {
return create(document, Lookup.getDefault());
}
/**
* Gets a Source instance for a Document. For details, please
* see {@link #create(javax.swing.text.Document)}; this form allows to specify
* a Lookup that provides access to user or context-dependent services.
*
* @param document the document to get Source for
* @param lkp the context for the source
* @return the Source instance
* @see #create(javax.swing.text.Document)
* @since 9.2
*/
public static Source create (
Document document, Lookup lkp
) {
Parameters.notNull("document", document); //NOI18N
String mimeType = DocumentUtilities.getMimeType(document);
if (mimeType == null) {
throw new NullPointerException("Netbeans documents must have 'mimeType' property: " //NOI18N
+ document.getClass() + "@" + Integer.toHexString(System.identityHashCode(document))); //NOI18N
}
synchronized (Source.class) {
@SuppressWarnings("unchecked") //NOI18N
Reference