org.netbeans.modules.javascript2.requirejs.RequireJsDataProvider Maven / Gradle / Ivy
/*
* 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.javascript2.requirejs;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.openide.modules.Places;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
/**
*
* @author Petr Pisl
*/
@NbBundle.Messages({
"doc.building=Loading RequireJS Documentation",
"# {0} - the documentation URL",
"doc.cannotGet=Cannot load RequireJS documentation from \"{0}\".",
"doc.notFound=Documentation not found."
})
public class RequireJsDataProvider {
private static final Logger LOG = Logger.getLogger(RequireJsDataProvider.class.getSimpleName()); //NOI18N
private static RequestProcessor RP = new RequestProcessor(RequireJsDataProvider.class);
private static RequireJsDataProvider INSTANCE;
private boolean loadingStarted;
private ProgressHandle progress;
private static final String CACHE_FOLDER_NAME = "requirejs-doc"; //NOI18N
private static final String API_FILE = "api.html"; //NOI18N
public static final String API_URL = "http://requirejs.org/docs/api.html";
private static final int URL_CONNECTION_TIMEOUT = 1000; //ms
private static final int URL_READ_TIMEOUT = URL_CONNECTION_TIMEOUT * 3; //ms
private static final String SEARCH_TEXT = " TRANSLATE_NAME = new HashMap<>();
static {
TRANSLATE_NAME.put("moduleconfig", "config");//NOI18N
// remove this option
TRANSLATE_NAME.put("map-notes", ""); //NOI18N
}
private RequireJsDataProvider() {
loadingStarted = false;
}
public static synchronized RequireJsDataProvider getDefault() {
if (INSTANCE == null) {
INSTANCE = new RequireJsDataProvider();
}
return INSTANCE;
}
public Collection getConfigurationOptions() {
Collection options = getConfigurationOptionsFromDoc();
if (options.isEmpty()) {
options = getConfigurationOptionsStatic();
}
return options;
}
public String getDocForHtmlTagAttribute(final String attributeName) {
String api = getContentApiFile();
if (api != null) {
String searchText = "href=\"#" + attributeName + "\" name=\"" + attributeName + "\""; //NOI18N
int start = api.indexOf(searchText);
if (start > 0) {
String tmpText = api.substring(0, start);
start = tmpText.lastIndexOf(""); // NOI18N
int end = api.indexOf("", start + 10); //NOI18N
if (start < end) {
return api.substring(start, end);
}
}
}
return "";
}
public String getDocFocOption(String name) {
String api = getContentApiFile();
if (api != null) {
int index = api.indexOf(SEARCH_TEXT + name);
if (index == -1 && TRANSLATE_NAME.containsValue(name)) {
for (Map.Entry entry : TRANSLATE_NAME.entrySet()) {
if (entry.getValue().equals(name)) {
index = api.indexOf(SEARCH_TEXT + entry.getKey());
break;
}
}
}
if (index > 0) {
int start = index;
index = api.indexOf(SEARCH_TEXT, start + SEARCH_TEXT.length());
if (index == -1) {
index = api.indexOf("