/** * Loads our CSS profile. * * @throws IOException if profile loading fails */ private static void loadAptanaCSSProfile() { InputStream configStream = CSSValidator.class.getResourceAsStream(CONFIG_FILE); InputStream profilesStream = CSSValidator.class.getResourceAsStream(PROFILES_CONFIG_FILE); try { // loads our config PropertiesLoader.config.load(configStream); // loads our profile Utf8Properties profiles = new Utf8Properties(); profiles.load(profilesStream); // a hack, but no other way since PropertiesLoader provides no public access to stored // profiles Field field = PropertiesLoader.class.getDeclaredField("profiles"); // $NON-NLS-1$ field.setAccessible(true); field.set(null, profiles); } catch (Exception e) { IdeLog.logError(CSSPlugin.getDefault(), Messages.CSSValidator_ERR_FailToLoadProfile, e); } finally { try { configStream.close(); } catch (IOException e) { } try { profilesStream.close(); } catch (IOException e) { } } }
/** * @param profile the profile needed * @return an Utf8Properties containing all the properties for the specified profile */ public static Utf8Properties getProfile(String profile) { Utf8Properties result = (Utf8Properties) allProps.get(profile); // the profile has not been loaded yet if (result == null) { result = new Utf8Properties(); String profilePath = (String) profiles.get(profile); if (profilePath != null && !profilePath.equals("")) { try { return loadProfile(profile, profilePath); } catch (IOException e) { if (Util.onDebug) { System.out.println( PropertiesLoader.class + ": Error while loading " + profile + " profile"); } e.printStackTrace(); } } // if the wanted profile is unknown, or there has been an error // while loading it, we return the default profile return DEFAULT_PROFILE; } else { return result; } }
private static Utf8Properties loadProfile(String profile, String profilePath) throws IOException { Utf8Properties result = new Utf8Properties(); InputStream f = null; URL url = null; // the url of the properties file of the selected profile if (profilePath != null) { url = PropertiesLoader.class.getResource(profilePath); } f = url.openStream(); // we load the properties result.load(f); // we add the profile to the profiles Hashtable allProps.put(new String(profile), result); if (Util.onDebug) { System.out.println(profile + " profile loaded"); } return result; }
/** * This method returns the profile to check against when no special profile is specified This * function is used in CssPropertyFactory * * @return the sorted list of profiles as a string array */ public static String[] getProfiles() { // initializations Iterator it = profiles.keySet().iterator(); ArrayList<String> res = new ArrayList<String>(); String profil; while (it.hasNext()) { // we filtered only the profiles that we're interessted in profil = it.next().toString(); if (profil.startsWith("css")) res.add(profil); } // we sort them Collections.sort(res); // return them as an array return (String[]) res.toArray(new String[res.size()]); }
static { config = new Utf8Properties(); mediaProperties = new Utf8Properties(); profiles = new Utf8Properties(); allProps = new Hashtable<String, Utf8Properties>(); InputStream f = null; try { // first, we load the general Config URL url = PropertiesLoader.class.getResource("Config.properties"); f = url.openStream(); config.load(f); // the media associated to each property url = PropertiesLoader.class.getResource(config.getProperty("media")); f = url.openStream(); mediaProperties.load(f); // profiles url = PropertiesLoader.class.getResource(config.getProperty("profilesProperties")); f = url.openStream(); profiles.load(f); // Load the default profile String defaultProfile = config.getProperty("defaultProfile"); String defaultPath = (String) profiles.get(defaultProfile); DEFAULT_PROFILE = loadProfile(defaultProfile, defaultPath); if (Util.onDebug) { System.out.println("Default profile (" + defaultProfile + ") loaded"); } } catch (Exception e) { System.err.println(PropertiesLoader.class + ": Error while loading default config"); e.printStackTrace(); } finally { try { if (f != null) { f.close(); f = null; } } catch (Exception e) { e.printStackTrace(); } } }