<commit_before>/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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 com.google.gwt.eclipse.core.properties;

import com.google.gdt.eclipse.core.PropertiesUtilities;
import com.google.gwt.eclipse.core.GWTPlugin;
import com.google.gwt.eclipse.core.compile.GWTCompileSettings;
import com.google.gwt.eclipse.core.modules.IModule;
import com.google.gwt.eclipse.core.modules.ModuleUtils;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.osgi.service.prefs.BackingStoreException;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Gets and sets GWT project properties.
 */
public final class GWTProjectProperties {

  private static final String ENTRY_POINT_MODULES = "entryPointModules";

  private static final String FILES_COPIED_TO_WEB_INF_LIB = "filesCopiedToWebInfLib";

  private static final String GWT_COMPILE_SETTINGS_XML = "gwtCompileSettings";

  private static final String SYNC_CODESERVER_RUNNING =  "gwtSyncCodeServer";

  /**
   * Returns the default set of entry point modules for a project. This set
   * contains all modules defined in source (.gwt.xml) in the project.
   */
  public static List<String> getDefaultEntryPointModules(IProject project) {
    List<String> moduleNames = new ArrayList<String>();
    IJavaProject javaProject = JavaCore.create(project);
    Map<String, Set<IModule>> inheritedModulesCache = new HashMap<String, Set<IModule>>();
    Map<String, List<String>> entryPointsCache = new HashMap<String, List<String>>();

    for (IModule module : ModuleUtils.findAllModules(javaProject,
        false)) {
      if (getEntryPoints(javaProject, module, entryPointsCache).size() > 0
          || getInheritedEntryPoints(javaProject, module,
              inheritedModulesCache, entryPointsCache).size() > 0) {
        moduleNames.add(module.getQualifiedName());
      }
    }

    return moduleNames;
  }

  /**
   * Returns the user-specified set of entry point modules.
   */
  public static List<String> getDefinedEntryPointModules(IProject project) {
    String rawPropVal = getProjectProperties(project).get(ENTRY_POINT_MODULES, null);
    return PropertiesUtilities.deserializeStrings(rawPropVal);
  }

  /**
   * Returns the list of entry point modules for a project (user defined, or if
   * not specified, a default list).
   */
  public static List<String> getEntryPointModules(IProject project) {
    List<String> moduleNames = getDefinedEntryPointModules(project);

    // If we don't have a user-specified set of entry point modules, return the
    // default entry point modules for this project
    if (moduleNames.isEmpty()) {
      moduleNames = getDefaultEntryPointModules(project);
    }
    return moduleNames;
  }

  public static List<String> getFileNamesCopiedToWebInfLib(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    String rawPropVal = prefs.get(FILES_COPIED_TO_WEB_INF_LIB, null);
    if (rawPropVal == null || rawPropVal.length() == 0) {
      return Collections.emptyList();
    }

    return Arrays.asList(rawPropVal.split("\\|"));
  }

  public static GWTCompileSettings getGwtCompileSettings(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    byte[] settingsBytes = prefs.getByteArray(GWT_COMPILE_SETTINGS_XML,
        new byte[0]);
    GWTCompileSettings settings = GWTCompileSettings.deserialize(settingsBytes, project);
    if (settings == null) {
      // Use default GWT compilation settings
      settings = new GWTCompileSettings(project);
    }

    return settings;
  }

  /**
   * GWT Facet sync running the server with the code server.
   *
   * @param project
   * @param syncCodeServer true to run code server with the web server
   * @throws BackingStoreException
   */
  public static void setFacetSyncCodeServer(IProject project, Boolean syncCodeServer) throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    prefs.put(SYNC_CODESERVER_RUNNING, syncCodeServer.toString());
    prefs.flush();
  }

  /**
   * GWT Facet sync running the server with the code server.
   *
   * @param project
   * @return true to sync with server
   */
  public static boolean getFacetSyncCodeServer(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    String valueStr = prefs.get(SYNC_CODESERVER_RUNNING, null);
    Boolean b = null;
    if (valueStr != null) {
      b = Boolean.valueOf(valueStr);
    }
    if (b == null) {
      b = false;
    }
    return b;
  }

  public static void setEntryPointModules(IProject project, List<String> modules)
      throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    String rawPropVal = PropertiesUtilities.serializeStrings(modules);
    prefs.put(ENTRY_POINT_MODULES, rawPropVal);
    prefs.flush();
  }

  public static void setFileNamesCopiedToWebInfLib(IProject project,
      List<String> fileNamesCopiedToWebInfLib) throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    StringBuilder sb = new StringBuilder();
    boolean addPipe = false;
    for (String fileNameCopiedToWebInfLib : fileNamesCopiedToWebInfLib) {
      if (addPipe) {
        sb.append("|");<commit_msg>Issue #332 - Sets the default to true code improvement. <commit_after>/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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 com.google.gwt.eclipse.core.properties;

import com.google.gdt.eclipse.core.PropertiesUtilities;
import com.google.gwt.eclipse.core.GWTPlugin;
import com.google.gwt.eclipse.core.compile.GWTCompileSettings;
import com.google.gwt.eclipse.core.modules.IModule;
import com.google.gwt.eclipse.core.modules.ModuleUtils;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.osgi.service.prefs.BackingStoreException;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Gets and sets GWT project properties.
 */
public final class GWTProjectProperties {

  private static final String ENTRY_POINT_MODULES = "entryPointModules";

  private static final String FILES_COPIED_TO_WEB_INF_LIB = "filesCopiedToWebInfLib";

  private static final String GWT_COMPILE_SETTINGS_XML = "gwtCompileSettings";

  private static final String SYNC_CODESERVER_RUNNING =  "gwtSyncCodeServer";

  /**
   * Returns the default set of entry point modules for a project. This set
   * contains all modules defined in source (.gwt.xml) in the project.
   */
  public static List<String> getDefaultEntryPointModules(IProject project) {
    List<String> moduleNames = new ArrayList<String>();
    IJavaProject javaProject = JavaCore.create(project);
    Map<String, Set<IModule>> inheritedModulesCache = new HashMap<String, Set<IModule>>();
    Map<String, List<String>> entryPointsCache = new HashMap<String, List<String>>();

    for (IModule module : ModuleUtils.findAllModules(javaProject,
        false)) {
      if (getEntryPoints(javaProject, module, entryPointsCache).size() > 0
          || getInheritedEntryPoints(javaProject, module,
              inheritedModulesCache, entryPointsCache).size() > 0) {
        moduleNames.add(module.getQualifiedName());
      }
    }

    return moduleNames;
  }

  /**
   * Returns the user-specified set of entry point modules.
   */
  public static List<String> getDefinedEntryPointModules(IProject project) {
    String rawPropVal = getProjectProperties(project).get(ENTRY_POINT_MODULES, null);
    return PropertiesUtilities.deserializeStrings(rawPropVal);
  }

  /**
   * Returns the list of entry point modules for a project (user defined, or if
   * not specified, a default list).
   */
  public static List<String> getEntryPointModules(IProject project) {
    List<String> moduleNames = getDefinedEntryPointModules(project);

    // If we don't have a user-specified set of entry point modules, return the
    // default entry point modules for this project
    if (moduleNames.isEmpty()) {
      moduleNames = getDefaultEntryPointModules(project);
    }
    return moduleNames;
  }

  public static List<String> getFileNamesCopiedToWebInfLib(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    String rawPropVal = prefs.get(FILES_COPIED_TO_WEB_INF_LIB, null);
    if (rawPropVal == null || rawPropVal.length() == 0) {
      return Collections.emptyList();
    }

    return Arrays.asList(rawPropVal.split("\\|"));
  }

  public static GWTCompileSettings getGwtCompileSettings(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    byte[] settingsBytes = prefs.getByteArray(GWT_COMPILE_SETTINGS_XML,
        new byte[0]);
    GWTCompileSettings settings = GWTCompileSettings.deserialize(settingsBytes, project);
    if (settings == null) {
      // Use default GWT compilation settings
      settings = new GWTCompileSettings(project);
    }

    return settings;
  }

  /**
   * GWT Facet sync running the server with the code server.
   *
   * @param project
   * @param syncCodeServer true to run code server with the web server
   * @throws BackingStoreException
   */
  public static void setFacetSyncCodeServer(IProject project, Boolean syncCodeServer) throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    prefs.put(SYNC_CODESERVER_RUNNING, syncCodeServer.toString());
    prefs.flush();
  }

  /**
   * GWT Facet sync running the server with the code server.
   *
   * @param project
   * @return true to sync with server
   */
  public static boolean getFacetSyncCodeServer(IProject project) {
    IEclipsePreferences prefs = getProjectProperties(project);
    String valueStr = prefs.get(SYNC_CODESERVER_RUNNING, "true");
    Boolean b = Boolean.valueOf(valueStr);
    return b;
  }

  public static void setEntryPointModules(IProject project, List<String> modules)
      throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    String rawPropVal = PropertiesUtilities.serializeStrings(modules);
    prefs.put(ENTRY_POINT_MODULES, rawPropVal);
    prefs.flush();
  }

  public static void setFileNamesCopiedToWebInfLib(IProject project,
      List<String> fileNamesCopiedToWebInfLib) throws BackingStoreException {
    IEclipsePreferences prefs = getProjectProperties(project);
    StringBuilder sb = new StringBuilder();
    boolean addPipe = false;
    for (String fileNameCopiedToWebInfLib : fileNamesCopiedToWebInfLib) {
      if (addPipe) {
        sb.append("|");
{'fraction_non_alphanumeric': 0.11079745942131264, 'fraction_numerical': 0.002117148906139732, 'mean_word_length': 4.401948326980093, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941768', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3539, 'n_words': 958}