/*
* Copyright 2015 Sudipto Chandra.
*
* Licensed 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.sandsoft.cymric.util;
import org.sandsoft.cymric.Launcher;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.scene.image.Image;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/**
* Resources to use in this project.
*
* @author Sudipto Chandra
*/
public final class Resources {
//Tutorial file names
public static final String TUTORIAL_INTRO = "intro";
// path where the resources are stored
private static final String IMAGE_RESOURCE = "/img";
private static final String THEME_RESOURCE = "/themes.list";
private static final String VALUE_RESOURCE = "/values/data.xml";
private static final String TUTORIAL_PATH = "/tutorial";
private static final String DEFAULT_THEME = "/default.css";
// default class to get resource
private static final Class mDefaultClass = Launcher.class;
/**
* Gets the default CSS style-sheet.
*
* @return Path to default CSS file.
*/
public static String getDefaultTheme() {
return DEFAULT_THEME;
}
/**
* Gets a list of all available styles.
*
* @return List of available styles.
*/
public static ArrayList<String> getAllThemes() {
ArrayList<String> list = new ArrayList<>();
try (Scanner sc = new Scanner(
mDefaultClass.getResourceAsStream(THEME_RESOURCE))) {
while (sc.hasNext()) {
list.add(sc.next());
}
}
return list;
}
/**
* Finds the resource with a given name, and converts it into an image
* object.
*
* @param name Name of the resource image.
* @return Image object from resource.
*/
public static Image getImage(String name)
throws NullPointerException, IllegalArgumentException {
return new Image(
mDefaultClass.getResourceAsStream(IMAGE_RESOURCE + "/" + name));
}
/**
* Extract main icon image resource.
*
* @param dimension Dimension of image to get. like- 16, 24, 32, 48, 64, 96,
* 128 or 256.
* @return PNG image of the main icon.
*/
public static Image getMainIcon(int dimension) {
return new Image(mDefaultClass.getResourceAsStream(
String.format("/main/main%d.png", dimension)));
}
/**
* Gets a tutorial file from tutorial folder in the resource.
*
* @param type The name of the file in tutorial folder to get.
* @return Content of the tutorial file.
*/
public static String getTutorial(String type) {
String htmlContent = null;
String file = TUTORIAL_PATH + "/" + type + ".html";
try (InputStream is = mDefaultClass.getResourceAsStream(file)) {
/*
Replace all relative path links in this HTML document to the absolute
path link.
*/
String absPath = mDefaultClass.getResource(file).toExternalForm();
Document doc = Jsoup.parse(is, "utf-8", absPath);
for (String tag : new String[]{"src", "href"}) {
doc.getElementsByAttribute(tag).stream().forEach((elem) -> {
elem.attr(tag, elem.absUrl(tag));
});
}
htmlContent = doc.outerHtml();
} catch (Exception ex) {
Logs.showStackTrace(ex);
}
return htmlContent;
}
/**
* Gets a string data by value name. <br>
* Note: All string data are stored in
* <code>resources/values/data.xml</code> file.
*
* @param valueName Name of the value (ID of the resource element).
* @return String data if found, null otherwise.
*/
public static String getString(String valueName) {
String value = null;
try (InputStream is = mDefaultClass.getResourceAsStream(VALUE_RESOURCE)) {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document d = db.parse(is);
value = d.getElementsByTagName("string").item(0).getTextContent();
} catch (Exception ex) {
Logs.showStackTrace(ex);
}
return value;
}
/**
* Gets a string data by value name. <br>
* Note: All string data are stored in
* <code>resources/values/data.xml</code> file.
*
* @param valueName Name of the value (ID of the resource element).
* @return Number if found, null otherwise.
*/
public static Number getNumber(String valueName) {
Number value = null;
try (InputStream is = mDefaultClass.getResourceAsStream(VALUE_RESOURCE)) {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document d = db.parse(is);
String data = d.getElementsByTagName("number").item(0).getTextContent();
value = new Double(data);
} catch (Exception ex) {
Logs.showStackTrace(ex);
}
return value;
}
}