com.intellij.lang.properties.psi.PropertiesElementFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of properties-psi-api Show documentation
Show all versions of properties-psi-api Show documentation
A packaging of the IntelliJ Community Edition properties-psi-api library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* 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 com.intellij.lang.properties.psi;
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesFileType;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataCache;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFileFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author cdr
*/
public class PropertiesElementFactory {
private static final UserDataCache PROPERTIES = new UserDataCache("system.properties.file") {
protected PropertiesFile compute(Project project, Void p) {
return createPropertiesFile(project, System.getProperties(), "system");
}
};
@NotNull
public static IProperty createProperty(@NotNull Project project, @NonNls @NotNull String name, @NonNls @NotNull String value) {
String text = getPropertyText(name, value, null, project);
final PropertiesFile dummyFile = createPropertiesFile(project, text);
return dummyFile.getProperties().get(0);
}
@NotNull
public static String getPropertyText(@NonNls @NotNull String name,
@NonNls @NotNull String value,
@NonNls @Nullable Character delimiter,
@Nullable Project project) {
if (delimiter == null) {
delimiter = project == null ? PropertiesCodeStyleSettings.DEFAULT_KEY_VALUE_DELIMITER : PropertiesCodeStyleSettings.getInstance(project).KEY_VALUE_DELIMITER;
}
return escape(name) + String.valueOf(delimiter) + escapeValue(value);
}
@NotNull
public static PropertiesFile createPropertiesFile(@NotNull Project project, @NonNls @NotNull String text) {
@NonNls String filename = "dummy." + PropertiesFileType.INSTANCE.getDefaultExtension();
return (PropertiesFile)PsiFileFactory.getInstance(project)
.createFileFromText(filename, PropertiesFileType.INSTANCE, text);
}
@NotNull
public static PropertiesFile createPropertiesFile(@NotNull Project project, Properties properties, String fileName) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
properties.store(stream, "");
}
catch (IOException e) {
throw new RuntimeException(e);
}
@NonNls String filename = fileName + "." + PropertiesFileType.INSTANCE.getDefaultExtension();
return (PropertiesFile)PsiFileFactory.getInstance(project)
.createFileFromText(filename, PropertiesFileType.INSTANCE, stream.toString());
}
@NotNull
public static PropertiesFile getSystemProperties(@NotNull Project project) {
return PROPERTIES.get(project, null);
}
@NotNull
private static String escape(@NotNull String name) {
if (StringUtil.startsWithChar(name, '#')) {
name = escapeChar(name, '#');
}
if (StringUtil.startsWithChar(name, '!')) {
name = escapeChar(name, '!');
}
name = escapeChar(name, '=');
name = escapeChar(name, ':');
name = escapeChar(name, ' ');
name = escapeChar(name, '\t');
return name;
}
@NotNull
private static String escapeChar(@NotNull String name, char c) {
int offset = 0;
while (true) {
int i = name.indexOf(c, offset);
if (i == -1) return name;
if (i == 0 || name.charAt(i - 1) != '\\') {
name = name.substring(0, i) + '\\' + name.substring(i);
}
offset = i + 2;
}
}
public static String escapeValue(String value) {
return PropertiesResourceBundleUtil.fromValueEditorToPropertyValue(value);
}
}