Search in sources :

Example 61 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class FormEditingUtil method collectUsedLocales.

public static Locale[] collectUsedLocales(final Module module, final IRootContainer rootContainer) {
    final Set<Locale> locales = new HashSet<>();
    final PropertiesReferenceManager propManager = PropertiesReferenceManager.getInstance(module.getProject());
    for (String bundleName : collectUsedBundleNames(rootContainer)) {
        List<PropertiesFile> propFiles = propManager.findPropertiesFiles(module, bundleName.replace('/', '.'));
        for (PropertiesFile propFile : propFiles) {
            locales.add(propFile.getLocale());
        }
    }
    return locales.toArray(new Locale[locales.size()]);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) HashSet(com.intellij.util.containers.HashSet)

Example 62 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class StringDescriptorManager method resolveToProperty.

public IProperty resolveToProperty(@NotNull StringDescriptor descriptor, @Nullable Locale locale) {
    String propFileName = descriptor.getDottedBundleName();
    Pair<Locale, String> cacheKey = Pair.create(locale, propFileName);
    PropertiesFile propertiesFile;
    synchronized (myPropertiesFileCache) {
        propertiesFile = myPropertiesFileCache.get(cacheKey);
    }
    if (propertiesFile == null || !propertiesFile.getContainingFile().isValid()) {
        propertiesFile = PropertiesUtilBase.getPropertiesFile(propFileName, myModule, locale);
        synchronized (myPropertiesFileCache) {
            myPropertiesFileCache.put(cacheKey, propertiesFile);
        }
    }
    if (propertiesFile != null) {
        final IProperty propertyByKey = propertiesFile.findPropertyByKey(descriptor.getKey());
        if (propertyByKey != null) {
            return propertyByKey;
        }
    }
    return null;
}
Also used : Locale(java.util.Locale) IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 63 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class AntDomProperty method buildProperties.

private Map<String, String> buildProperties() {
    Map<String, String> result = myCachedProperties;
    if (result != null) {
        return result;
    }
    result = Collections.emptyMap();
    final String propertyName = getName().getRawText();
    if (propertyName != null) {
        final String propertyValue = getValue().getRawText();
        if (propertyValue != null) {
            result = Collections.singletonMap(propertyName, propertyValue);
        } else {
            String locValue = getLocation().getStringValue();
            if (locValue != null) {
                final File file = new File(locValue);
                if (!file.isAbsolute()) {
                    final String baseDir = getContextAntProject().getProjectBasedirPath();
                    if (baseDir != null) {
                        locValue = PathUtil.getCanonicalPath(new File(baseDir, locValue).getPath());
                    }
                }
                result = Collections.singletonMap(propertyName, FileUtil.toSystemDependentName(locValue));
            } else {
                // todo: process refid attrib if specified for the value
                final String tagText = getXmlTag().getText();
                result = Collections.singletonMap(propertyName, tagText);
            }
        }
    } else {
        // name attrib is not specified
        final PsiFileSystemItem psiFile = getFile().getValue();
        if (psiFile != null) {
            final PropertiesFile file = toPropertiesFile(psiFile);
            if (file != null) {
                result = new HashMap<>();
                for (final IProperty property : file.getProperties()) {
                    result.put(property.getUnescapedKey(), property.getUnescapedValue());
                }
            }
        } else if (getEnvironment().getRawText() != null) {
            String prefix = getEnvironment().getRawText();
            if (!prefix.endsWith(".")) {
                prefix += ".";
            }
            result = new HashMap<>();
            for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
                result.put(prefix + entry.getKey(), entry.getValue());
            }
        } else {
            // todo: consider Url attribute?
            final String resource = getResource().getStringValue();
            if (resource != null) {
                final ClassLoader loader = getClassLoader();
                if (loader != null) {
                    final InputStream stream = loader.getResourceAsStream(resource);
                    if (stream != null) {
                        try {
                            // todo: Remote file can be XmlPropertiesFile
                            final PropertiesFile propFile = (PropertiesFile) CustomAntElementsRegistry.loadContentAsFile(getXmlTag().getProject(), stream, StdFileTypes.PROPERTIES);
                            result = new HashMap<>();
                            for (final IProperty property : propFile.getProperties()) {
                                result.put(property.getUnescapedKey(), property.getUnescapedValue());
                            }
                        } catch (IOException ignored) {
                        }
                    }
                }
            }
        }
    }
    return (myCachedProperties = result);
}
Also used : IProperty(com.intellij.lang.properties.IProperty) HashMap(com.intellij.util.containers.HashMap) InputStream(java.io.InputStream) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IOException(java.io.IOException) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 64 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class AntResolveInspection method getPropertyFiles.

@NotNull
private static Collection<PropertiesFile> getPropertyFiles(@Nullable AntDomProject antDomProject, @NotNull XmlElement stopElement) {
    if (antDomProject == null) {
        return Collections.emptyList();
    }
    final Set<PropertiesFile> files = new java.util.HashSet<>();
    final int stopOffset = stopElement.getTextOffset();
    for (Iterator<AntDomElement> iterator = antDomProject.getAntChildrenIterator(); iterator.hasNext(); ) {
        AntDomElement child = iterator.next();
        final XmlElement xmlElement = child.getXmlElement();
        if (xmlElement != null && xmlElement.getTextOffset() >= stopOffset) {
            // no need to offer to add properties to files that are imported after the property reference
            break;
        }
        if (child instanceof AntDomProperty) {
            final AntDomProperty property = (AntDomProperty) child;
            final PropertiesFile file = property.getPropertiesFile();
            if (file != null) {
                files.add(file);
            }
        }
    }
    return files;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project android by JetBrains.

the class AndroidPropertyFilesUpdater method updateProjectPropertiesIfNecessary.

@Nullable
private static Pair<VirtualFile, List<Runnable>> updateProjectPropertiesIfNecessary(@NotNull AndroidFacet facet) {
    if (facet.isDisposed()) {
        return null;
    }
    final Module module = facet.getModule();
    final Pair<PropertiesFile, VirtualFile> pair = AndroidRootUtil.findPropertyFile(module, SdkConstants.FN_PROJECT_PROPERTIES);
    if (pair == null) {
        return null;
    }
    final PropertiesFile projectProperties = pair.getFirst();
    final VirtualFile projectPropertiesVFile = pair.getSecond();
    final Pair<Properties, VirtualFile> localProperties = AndroidRootUtil.readPropertyFile(module, SdkConstants.FN_LOCAL_PROPERTIES);
    final List<Runnable> changes = new ArrayList<Runnable>();
    final IAndroidTarget androidTarget = facet.getConfiguration().getAndroidTarget();
    final String androidTargetHashString = androidTarget != null ? androidTarget.hashString() : null;
    final VirtualFile[] dependencies = collectDependencies(module);
    final String[] dependencyPaths = toSortedPaths(dependencies);
    final List<Object> newState = Arrays.asList(androidTargetHashString, facet.getProjectType(), Arrays.asList(dependencyPaths), facet.getProperties().ENABLE_MANIFEST_MERGING, facet.getProperties().ENABLE_PRE_DEXING);
    final List<Object> state = facet.getUserData(ANDROID_PROPERTIES_STATE_KEY);
    if (state == null || !Comparing.equal(state, newState)) {
        updateTargetProperty(facet, projectProperties, changes);
        updateProjectTypeProperty(facet, projectProperties, changes);
        updateManifestMergerProperty(facet, projectProperties, changes);
        updateDependenciesInPropertyFile(projectProperties, localProperties, dependencies, changes);
        facet.putUserData(ANDROID_PROPERTIES_STATE_KEY, newState);
    }
    return changes.size() > 0 ? Pair.create(projectPropertiesVFile, changes) : null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IAndroidTarget(com.android.sdklib.IAndroidTarget) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)143 IProperty (com.intellij.lang.properties.IProperty)44 PsiFile (com.intellij.psi.PsiFile)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 ResourceBundle (com.intellij.lang.properties.ResourceBundle)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)19 Nullable (org.jetbrains.annotations.Nullable)18 Property (com.intellij.lang.properties.psi.Property)15 Project (com.intellij.openapi.project.Project)10 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 THashSet (gnu.trove.THashSet)8 ArrayList (java.util.ArrayList)7 Module (com.intellij.openapi.module.Module)6 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (com.intellij.util.containers.HashSet)6 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5