<commit_before>package org.jetbrains.android.uipreview;

import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
import com.android.ide.common.resources.*;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.VersionQualifier;
import com.android.io.FolderWrapper;
import com.android.io.IAbstractFile;
import com.android.io.IAbstractFolder;
import com.android.io.IAbstractResource;
import com.android.sdklib.IAndroidTarget;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlTag;
import org.jetbrains.android.dom.manifest.Application;
import org.jetbrains.android.dom.manifest.Manifest;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.android.sdk.AndroidPlatform;
import org.jetbrains.android.sdk.AndroidSdkAdditionalData;
import org.jetbrains.android.sdk.AndroidSdkType;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidSdkNotConfiguredException;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.xmlpull.v1.XmlPullParserException;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * @author Eugene.Kudelevsky
 */
class RenderUtil {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.uipreview.RenderUtil");

  private static final String DEFAULT_APP_LABEL = "Android application";

  private RenderUtil() {
  }

  public static boolean renderLayout(@NotNull Project project,
                                     @NotNull String layoutXmlText,
                                     @NotNull String imgPath,
                                     @NotNull IAndroidTarget target,
                                     @NotNull AndroidFacet facet,
                                     @NotNull FolderConfiguration config,
                                     float xdpi,
                                     float ydpi,
                                     @NotNull ThemeData theme,
                                     StringBuilder warningBuilder)
    throws RenderingException, IOException, AndroidSdkNotConfiguredException {
    final Sdk sdk = ModuleRootManager.getInstance(facet.getModule()).getSdk();
    if (sdk == null || !(sdk.getSdkType() instanceof AndroidSdkType)) {
      throw new AndroidSdkNotConfiguredException();
    }

    final AndroidSdkAdditionalData data = (AndroidSdkAdditionalData)sdk.getSdkAdditionalData();
    if (data == null) {
      throw new RenderingException(AndroidBundle.message("sdk.broken.error"));
    }

    final AndroidPlatform platform = data.getAndroidPlatform();
    if (platform == null) {
      throw new RenderingException(AndroidBundle.message("sdk.broken.error"));
    }

    config.setVersionQualifier(new VersionQualifier(target.getVersion().getApiLevel()));

    final RenderServiceFactory factory = platform.getSdk().getTargetData(target).getRenderServiceFactory(project);
    if (factory == null) {
      throw new RenderingException(AndroidBundle.message("android.layout.preview.cannot.load.library.error"));
    }

    final ProjectResources projectResources = new ProjectResources();

    final VirtualFile[] resourceDirs = facet.getLocalResourceManager().getAllResourceDirs();
    final IAbstractFolder[] resFolders = toAbstractFolders(resourceDirs);

    loadResources(projectResources, resFolders);

    final ResourceResolver resolver = factory.createResourceResolver(config, projectResources, theme.getName(), theme.isProjectTheme());
    final int minSdkVersion = getMinSdkVersion(facet);
    
    final ProjectCallback callback = new ProjectCallback(factory.getLibrary(), facet.getModule(), projectResources);
    try {
      callback.loadAndParseRClass();
    }
    catch (ClassNotFoundException e) {
      LOG.debug(e);
      final String message = e.getMessage();
      warningBuilder.append(message != null && message.length() > 0
                            ? ("Class not found error: " + message + ".")
                            : "R class not found.")
        .append(" Try to build project\n");
    }

    final RenderService renderService = factory.createService(resolver, config, xdpi, ydpi, callback, minSdkVersion);

    final RenderSession session;
    try {
      session = renderService.createRenderSession(layoutXmlText, getAppLabelToShow(facet));
    }
    catch (XmlPullParserException e) {
      throw new RenderingException(e);
    }
    if (session == null) {
      return false;<commit_msg>create resource resolver after parsing R.class, because ids can be added during this<commit_after>package org.jetbrains.android.uipreview;

import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
import com.android.ide.common.resources.*;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.VersionQualifier;
import com.android.io.FolderWrapper;
import com.android.io.IAbstractFile;
import com.android.io.IAbstractFolder;
import com.android.io.IAbstractResource;
import com.android.sdklib.IAndroidTarget;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlTag;
import org.jetbrains.android.dom.manifest.Application;
import org.jetbrains.android.dom.manifest.Manifest;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.android.sdk.AndroidPlatform;
import org.jetbrains.android.sdk.AndroidSdkAdditionalData;
import org.jetbrains.android.sdk.AndroidSdkType;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidSdkNotConfiguredException;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.xmlpull.v1.XmlPullParserException;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * @author Eugene.Kudelevsky
 */
class RenderUtil {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.uipreview.RenderUtil");

  private static final String DEFAULT_APP_LABEL = "Android application";

  private RenderUtil() {
  }

  public static boolean renderLayout(@NotNull Project project,
                                     @NotNull String layoutXmlText,
                                     @NotNull String imgPath,
                                     @NotNull IAndroidTarget target,
                                     @NotNull AndroidFacet facet,
                                     @NotNull FolderConfiguration config,
                                     float xdpi,
                                     float ydpi,
                                     @NotNull ThemeData theme,
                                     StringBuilder warningBuilder)
    throws RenderingException, IOException, AndroidSdkNotConfiguredException {
    final Sdk sdk = ModuleRootManager.getInstance(facet.getModule()).getSdk();
    if (sdk == null || !(sdk.getSdkType() instanceof AndroidSdkType)) {
      throw new AndroidSdkNotConfiguredException();
    }

    final AndroidSdkAdditionalData data = (AndroidSdkAdditionalData)sdk.getSdkAdditionalData();
    if (data == null) {
      throw new RenderingException(AndroidBundle.message("sdk.broken.error"));
    }

    final AndroidPlatform platform = data.getAndroidPlatform();
    if (platform == null) {
      throw new RenderingException(AndroidBundle.message("sdk.broken.error"));
    }

    config.setVersionQualifier(new VersionQualifier(target.getVersion().getApiLevel()));

    final RenderServiceFactory factory = platform.getSdk().getTargetData(target).getRenderServiceFactory(project);
    if (factory == null) {
      throw new RenderingException(AndroidBundle.message("android.layout.preview.cannot.load.library.error"));
    }

    final ProjectResources projectResources = new ProjectResources();

    final VirtualFile[] resourceDirs = facet.getLocalResourceManager().getAllResourceDirs();
    final IAbstractFolder[] resFolders = toAbstractFolders(resourceDirs);

    loadResources(projectResources, resFolders);
    final int minSdkVersion = getMinSdkVersion(facet);
    
    final ProjectCallback callback = new ProjectCallback(factory.getLibrary(), facet.getModule(), projectResources);
    try {
      callback.loadAndParseRClass();
    }
    catch (ClassNotFoundException e) {
      LOG.debug(e);
      final String message = e.getMessage();
      warningBuilder.append(message != null && message.length() > 0
                            ? ("Class not found error: " + message + ".")
                            : "R class not found.")
        .append(" Try to build project\n");
    }

    final ResourceResolver resolver =
      factory.createResourceResolver(facet, config, projectResources, theme.getName(), theme.isProjectTheme());
    final RenderService renderService = factory.createService(resolver, config, xdpi, ydpi, callback, minSdkVersion);

    final RenderSession session;
    try {
      session = renderService.createRenderSession(layoutXmlText, getAppLabelToShow(facet));
    }
    catch (XmlPullParserException e) {
      throw new RenderingException(e);
    }
    if (session == null) {
      return false;
{'fraction_non_alphanumeric': 0.09605985037406484, 'fraction_numerical': 0.00039900249376558606, 'mean_word_length': 3.436283185840708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7488009', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2640, 'n_words': 524}