From 5d54369378e72a7fa19634bece1c7afb26315783 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 20 Mar 2012 20:43:40 +0100 Subject: [PATCH] util-rt module introduced --- .idea/modules.xml | 1 + .../org/jetbrains/util/rt/FileUtilRt.java} | 36 ++-- .../src/org/jetbrains/util/rt/Logger.java | 159 ++++++++++++++++++ .../org/jetbrains/util/rt/StringUtilRt.java | 124 ++++++++++++++ .../org/jetbrains/util/rt/SystemInfoRt.java | 37 ++++ platform/util-rt/util-rt.iml | 13 ++ .../com/intellij/openapi/util/SystemInfo.java | 22 +-- .../intellij/openapi/util/io/FileUtil.java | 2 +- .../openapi/util/text/StringUtil.java | 104 +----------- platform/util/util.iml | 1 + .../jetbrains/git4idea/ssh/GitSSHService.java | 7 +- .../org/jetbrains/git4idea/ssh/SSHMain.java | 6 +- .../git4idea/util/ScriptGenerator.java | 10 +- 13 files changed, 381 insertions(+), 141 deletions(-) rename platform/{util/src/com/intellij/openapi/util/io/FileUtilLight.java => util-rt/src/org/jetbrains/util/rt/FileUtilRt.java} (84%) create mode 100644 platform/util-rt/src/org/jetbrains/util/rt/Logger.java create mode 100644 platform/util-rt/src/org/jetbrains/util/rt/StringUtilRt.java create mode 100644 platform/util-rt/src/org/jetbrains/util/rt/SystemInfoRt.java create mode 100644 platform/util-rt/util-rt.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 52a0244f3249..b4b07b103f8d 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -107,6 +107,7 @@ + diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtilLight.java b/platform/util-rt/src/org/jetbrains/util/rt/FileUtilRt.java similarity index 84% rename from platform/util/src/com/intellij/openapi/util/io/FileUtilLight.java rename to platform/util-rt/src/org/jetbrains/util/rt/FileUtilRt.java index ae174b7901d7..b91b1dc48490 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtilLight.java +++ b/platform/util-rt/src/org/jetbrains/util/rt/FileUtilRt.java @@ -13,11 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.openapi.util.io; +package org.jetbrains.util.rt; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,14 +24,16 @@ import java.io.*; import java.util.UUID; /** - * This is a light version of {@link FileUtil}. - * It is used by scripts loaded externally and almost independent of IDEA, so add here as few dependencies as possible. + * Stripped-down version of {@code com.intellij.openapi.util.io.FileUtil}. + * Intended to use by external (out-of-IDE-process) runners and helpers so it should not contain any library dependencies. + * + * @since 12.0 */ -public class FileUtilLight { - - private static String ourCanonicalTempPathCache = null; +@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) +public class FileUtilRt { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.io.FileUtilLight"); + private static String ourCanonicalTempPathCache = null; @NotNull public static File createTempDirectory(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { @@ -55,8 +54,7 @@ public class FileUtilLight { } @NotNull - public static File createTempFile(@NonNls final File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, final boolean create) - throws IOException { + public static File createTempFile(@NonNls File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, boolean create) throws IOException { return createTempFile(dir, prefix, suffix, create, true); } @@ -122,7 +120,7 @@ public class FileUtilLight { private static File normalizeFile(File temp) throws IOException { final File canonical = temp.getCanonicalFile(); - return SystemInfo.isWindows && canonical.getAbsolutePath().contains(" ") ? temp.getAbsoluteFile() : canonical; + return SystemInfoRt.isWindows && canonical.getAbsolutePath().contains(" ") ? temp.getAbsoluteFile() : canonical; } public static String getTempDirectory() { @@ -136,7 +134,7 @@ public class FileUtilLight { final File file = new File(System.getProperty("java.io.tmpdir")); try { final String canonical = file.getCanonicalPath(); - if (!SystemInfo.isWindows || !canonical.contains(" ")) { + if (!SystemInfoRt.isWindows || !canonical.contains(" ")) { return canonical; } } @@ -169,7 +167,7 @@ public class FileUtilLight { * * @param path the path to use * @param executableFlag new value of executable attribute - * @throws IOException if there is a problem with setting the flag + * @throws java.io.IOException if there is a problem with setting the flag */ public static void setExecutableAttribute(@NotNull String path, boolean executableFlag) throws IOException { final File file = new File(path); @@ -189,14 +187,14 @@ public class FileUtilLight { } @NotNull - public static String loadFile(@NotNull File file, String encoding) throws IOException { + public static String loadFile(@NotNull File file, @Nullable @NonNls String encoding) throws IOException { return loadFile(file, encoding, false); } @NotNull - public static String loadFile(@NotNull File file, String encoding, boolean convertLineSeparators) throws IOException { + public static String loadFile(@NotNull File file, @Nullable @NonNls String encoding, boolean convertLineSeparators) throws IOException { final String s = new String(loadFileText(file, encoding)); - return convertLineSeparators ? StringUtil.convertLineSeparators(s) : s; + return convertLineSeparators ? StringUtilRt.convertLineSeparators(s) : s; } @NotNull @@ -205,8 +203,9 @@ public class FileUtilLight { } @NotNull - public static char[] loadFileText(@NotNull File file, @NonNls String encoding) throws IOException { + public static char[] loadFileText(@NotNull File file, @Nullable @NonNls String encoding) throws IOException { InputStream stream = new FileInputStream(file); + @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") Reader reader = encoding == null ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding); try { return loadText(reader, (int)file.length()); @@ -234,5 +233,4 @@ public class FileUtilLight { return newChars; } } - } diff --git a/platform/util-rt/src/org/jetbrains/util/rt/Logger.java b/platform/util-rt/src/org/jetbrains/util/rt/Logger.java new file mode 100644 index 000000000000..0ccd5c85d579 --- /dev/null +++ b/platform/util-rt/src/org/jetbrains/util/rt/Logger.java @@ -0,0 +1,159 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.util.rt; + +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Method; +import java.util.logging.Level; +import java.util.logging.LogManager; + +/** + * A wrapper which uses either IDE logging subsystem (if available) or java.util.logging. + * + * @since 12.0 + */ +public abstract class Logger { + private interface Factory { + Logger getInstance(@NotNull @NonNls final String category); + } + + private static final Factory ourFactory; + static { + Factory factory; + try { + factory = new IdeaFactory(); + } + catch (Exception e) { + factory = new JavaFactory(); + } + ourFactory = factory; + } + + @NotNull + public static Logger getInstance(@NotNull @NonNls final String category) { + return ourFactory.getInstance(category); + } + + public void info(@Nullable @NonNls final String message) { + info(message, null); + } + + public void info(@NotNull final Throwable t) { + info(t.getMessage(), t); + } + + public void warn(@Nullable @NonNls final String message) { + warn(message, null); + } + + public void warn(@NotNull final Throwable t) { + warn(t.getMessage(), t); + } + + public void error(@Nullable @NonNls final String message) { + error(message, null); + } + + public void error(@NotNull final Throwable t) { + error(t.getMessage(), t); + } + + public abstract void info(@Nullable @NonNls final String message, @Nullable final Throwable t); + public abstract void warn(@Nullable @NonNls final String message, @Nullable final Throwable t); + public abstract void error(@Nullable @NonNls final String message, @Nullable final Throwable t); + + private static class JavaFactory implements Factory { + private final LogManager myManager = LogManager.getLogManager(); + + @Override + public Logger getInstance(@NotNull @NonNls final String category) { + final java.util.logging.Logger logger = myManager.getLogger(category); + return new Logger() { + @Override + public void info(@Nullable @NonNls final String message, @Nullable final Throwable t) { + logger.log(Level.INFO, message, t); + } + + @Override + public void warn(@Nullable @NonNls final String message, @Nullable final Throwable t) { + logger.log(Level.WARNING, message, t); + } + + @Override + public void error(@Nullable @NonNls final String message, @Nullable final Throwable t) { + logger.log(Level.SEVERE, message, t); + } + }; + } + } + + private static class IdeaFactory implements Factory { + private final Method myGetInstance; + private final Method myInfo; + private final Method myWarn; + private final Method myError; + + private IdeaFactory() throws Exception { + final Class loggerClass = Class.forName("com.intellij.openapi.diagnostic.Logger"); + myGetInstance = loggerClass.getMethod("getInstance", String.class); + myGetInstance.setAccessible(true); + myInfo = loggerClass.getMethod("info", String.class, Throwable.class); + myInfo.setAccessible(true); + myWarn = loggerClass.getMethod("warn", String.class, Throwable.class); + myInfo.setAccessible(true); + myError = loggerClass.getMethod("error", String.class, Throwable.class); + myError.setAccessible(true); + } + + @Override + public Logger getInstance(@NotNull @NonNls final String category) { + try { + final Object logger = myGetInstance.invoke(null, category); + return new Logger() { + @Override + public void info(@Nullable @NonNls final String message, @Nullable final Throwable t) { + try { + myInfo.invoke(logger, message, t); + } + catch (Exception ignored) { } + } + + @Override + public void warn(@Nullable @NonNls final String message, @Nullable final Throwable t) { + try { + myWarn.invoke(logger, message, t); + } + catch (Exception ignored) { } + } + + @Override + public void error(@Nullable @NonNls final String message, @Nullable final Throwable t) { + try { + myError.invoke(logger, message, t); + } + catch (Exception ignored) { } + } + }; + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/platform/util-rt/src/org/jetbrains/util/rt/StringUtilRt.java b/platform/util-rt/src/org/jetbrains/util/rt/StringUtilRt.java new file mode 100644 index 000000000000..cc915bc08591 --- /dev/null +++ b/platform/util-rt/src/org/jetbrains/util/rt/StringUtilRt.java @@ -0,0 +1,124 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.util.rt; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Stripped-down version of {@code com.intellij.openapi.util.text.StringUtil}. + * Intended to use by external (out-of-IDE-process) runners and helpers so it should not contain any library dependencies. + * + * @since 12.0 + */ +@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) +public class StringUtilRt { + /** + * Converts line separators to "\n" + */ + @NotNull + public static String convertLineSeparators(@NotNull String text) { + return convertLineSeparators(text, false); + } + + @NotNull + public static String convertLineSeparators(@NotNull String text, boolean keepCarriageReturn) { + return convertLineSeparators(text, "\n", null, keepCarriageReturn); + } + + @NotNull + public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator) { + return convertLineSeparators(text, newSeparator, null); + } + + @NotNull + public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator, @Nullable int[] offsetsToKeep) { + return convertLineSeparators(text, newSeparator, offsetsToKeep, false); + } + + @NotNull + public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator, @Nullable int[] offsetsToKeep, + boolean keepCarriageReturn) { + StringBuilder buffer = null; + int intactLength = 0; + final boolean newSeparatorIsSlashN = "\n".equals(newSeparator); + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); + if (c == '\n') { + if (!newSeparatorIsSlashN) { + if (buffer == null) { + buffer = new StringBuilder(text.length()); + buffer.append(text, 0, intactLength); + } + buffer.append(newSeparator); + shiftOffsets(offsetsToKeep, buffer.length(), 1, newSeparator.length()); + } + else if (buffer == null) { + intactLength++; + } + else { + buffer.append(c); + } + } + else if (c == '\r') { + boolean followedByLineFeed = i < text.length() - 1 && text.charAt(i + 1) == '\n'; + if (!followedByLineFeed && keepCarriageReturn) { + if (buffer == null) { + intactLength++; + } + else { + buffer.append(c); + } + continue; + } + if (buffer == null) { + buffer = new StringBuilder(text.length()); + buffer.append(text, 0, intactLength); + } + buffer.append(newSeparator); + if (followedByLineFeed) { + //noinspection AssignmentToForLoopParameter + i++; + shiftOffsets(offsetsToKeep, buffer.length(), 2, newSeparator.length()); + } + else { + shiftOffsets(offsetsToKeep, buffer.length(), 1, newSeparator.length()); + } + } + else { + if (buffer == null) { + intactLength++; + } + else { + buffer.append(c); + } + } + } + return buffer == null ? text : buffer.toString(); + } + + private static void shiftOffsets(int[] offsets, int changeOffset, int oldLength, int newLength) { + if (offsets == null) return; + int shift = newLength - oldLength; + if (shift == 0) return; + for (int i = 0; i < offsets.length; i++) { + int offset = offsets[i]; + if (offset >= changeOffset + oldLength) { + offsets[i] += shift; + } + } + } +} diff --git a/platform/util-rt/src/org/jetbrains/util/rt/SystemInfoRt.java b/platform/util-rt/src/org/jetbrains/util/rt/SystemInfoRt.java new file mode 100644 index 000000000000..6b3dd54260af --- /dev/null +++ b/platform/util-rt/src/org/jetbrains/util/rt/SystemInfoRt.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.util.rt; + +/** + * Stripped-down version of {@code com.intellij.openapi.util.SystemInfo}. + * Intended to use by external (out-of-IDE-process) runners and helpers so it should not contain any library dependencies. + * + * @since 12.0 + */ +@SuppressWarnings({"HardCodedStringLiteral", "UtilityClassWithoutPrivateConstructor"}) +public class SystemInfoRt { + public static final String OS_NAME = System.getProperty("os.name"); + public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(); + + protected static final String _OS_NAME = OS_NAME.toLowerCase(); + public static final boolean isWindows = _OS_NAME.startsWith("windows"); + public static final boolean isOS2 = _OS_NAME.startsWith("os/2") || _OS_NAME.startsWith("os2"); + public static final boolean isMac = _OS_NAME.startsWith("mac"); + public static final boolean isLinux = _OS_NAME.startsWith("linux"); + public static final boolean isUnix = !isWindows && !isOS2; + + public static final boolean isFileSystemCaseSensitive = !isWindows && !isOS2 && !isMac; +} diff --git a/platform/util-rt/util-rt.iml b/platform/util-rt/util-rt.iml new file mode 100644 index 000000000000..4d3f06e41375 --- /dev/null +++ b/platform/util-rt/util-rt.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/platform/util/src/com/intellij/openapi/util/SystemInfo.java b/platform/util/src/com/intellij/openapi/util/SystemInfo.java index a72b268f74cb..6e13a9f1cb71 100644 --- a/platform/util/src/com/intellij/openapi/util/SystemInfo.java +++ b/platform/util/src/com/intellij/openapi/util/SystemInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -17,22 +17,22 @@ package com.intellij.openapi.util; import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.util.rt.SystemInfoRt; import java.io.File; import java.util.List; @SuppressWarnings({"HardCodedStringLiteral", "UtilityClassWithoutPrivateConstructor", "UnusedDeclaration"}) -public class SystemInfo { - public static final String OS_NAME = System.getProperty("os.name"); - public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(); +public class SystemInfo extends SystemInfoRt { + public static final String OS_NAME = SystemInfoRt.OS_NAME; + public static final String OS_VERSION = SystemInfoRt.OS_VERSION; public static final String OS_ARCH = System.getProperty("os.arch"); public static final String JAVA_VERSION = System.getProperty("java.version"); public static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version"); public static final String ARCH_DATA_MODEL = System.getProperty("sun.arch.data.model"); public static final String SUN_DESKTOP = System.getProperty("sun.desktop", ""); - private static final String _OS_NAME = OS_NAME.toLowerCase(); - public static final boolean isWindows = _OS_NAME.startsWith("windows"); + public static final boolean isWindows = SystemInfoRt.isWindows; public static final boolean isWindowsNT = _OS_NAME.startsWith("windows nt"); public static final boolean isWindows2000 = _OS_NAME.startsWith("windows 2000"); public static final boolean isWindows2003 = _OS_NAME.startsWith("windows 2003"); @@ -40,12 +40,12 @@ public class SystemInfo { public static final boolean isWindowsVista = _OS_NAME.startsWith("windows vista"); public static final boolean isWindows7 = _OS_NAME.startsWith("windows 7"); public static final boolean isWindows9x = _OS_NAME.startsWith("windows 9") || _OS_NAME.startsWith("windows me"); - public static final boolean isOS2 = _OS_NAME.startsWith("os/2") || _OS_NAME.startsWith("os2"); - public static final boolean isMac = _OS_NAME.startsWith("mac"); + public static final boolean isOS2 = SystemInfoRt.isOS2; + public static final boolean isMac = SystemInfoRt.isMac; + public static final boolean isLinux = SystemInfoRt.isLinux; public static final boolean isFreeBSD = _OS_NAME.startsWith("freebsd"); - public static final boolean isLinux = _OS_NAME.startsWith("linux"); public static final boolean isSolaris = _OS_NAME.startsWith("sunos"); - public static final boolean isUnix = !isWindows && !isOS2; + public static final boolean isUnix = SystemInfoRt.isUnix; private static final String _SUN_DESKTOP = SUN_DESKTOP.toLowerCase(); public static final boolean isKDE = _SUN_DESKTOP.contains("kde"); @@ -55,7 +55,7 @@ public class SystemInfo { public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar")); - public static final boolean isFileSystemCaseSensitive = !isWindows && !isOS2 && !isMac; + public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive; public static final boolean areSymLinksSupported = isUnix || isWindows && OS_VERSION.compareTo("6.0") >= 0 && isJavaVersionAtLeast("1.7"); diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java index 149dbea1ac0f..1125485bf795 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java @@ -43,7 +43,7 @@ import java.util.*; import java.util.regex.Pattern; @SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) -public class FileUtil extends FileUtilLight { +public class FileUtil extends FileUtilRt { public static final int MEGABYTE = 1024 * 1024; @NonNls public static final String ASYNC_DELETE_EXTENSION = ".__del__"; diff --git a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java index d4c5412e294e..4f7453801e2c 100644 --- a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java +++ b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -27,6 +27,7 @@ import com.intellij.util.text.LineReader; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.util.rt.StringUtilRt; import java.beans.Introspector; import java.io.ByteArrayInputStream; @@ -37,10 +38,12 @@ import java.util.regex.Pattern; //TeamCity inherits StringUtil: do not add private constructors!!! @SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) -public class StringUtil { +public class StringUtil extends StringUtilRt { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.text.StringUtil"); + @NonNls private static final String VOWELS = "aeiouy"; - private static final Pattern EOL_SPLIT_PATTERN = Pattern.compile(" *(\r|\n|\r\n)+ *"); + @NonNls private static final Pattern EOL_SPLIT_PATTERN = Pattern.compile(" *(\r|\n|\r\n)+ *"); + public static final NotNullFunction QUOTER = new NotNullFunction() { @NotNull public String fun(String s) { @@ -284,101 +287,6 @@ public class StringUtil { return ""; } - /** - * Converts line separators to "\n" - */ - @NotNull - public static String convertLineSeparators(@NotNull String text) { - return convertLineSeparators(text, false); - } - - @NotNull - public static String convertLineSeparators(@NotNull String text, boolean keepCarriageReturn) { - return convertLineSeparators(text, "\n", null, keepCarriageReturn); - } - - @NotNull - public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator) { - return convertLineSeparators(text, newSeparator, null); - } - - @NotNull - public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator, @Nullable int[] offsetsToKeep) { - return convertLineSeparators(text, newSeparator, offsetsToKeep, false); - } - - @NotNull - public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator, @Nullable int[] offsetsToKeep, - boolean keepCarriageReturn) { - StringBuilder buffer = null; - int intactLength = 0; - final boolean newSeparatorIsSlashN = "\n".equals(newSeparator); - for (int i = 0; i < text.length(); i++) { - char c = text.charAt(i); - if (c == '\n') { - if (!newSeparatorIsSlashN) { - if (buffer == null) { - buffer = new StringBuilder(text.length()); - buffer.append(text, 0, intactLength); - } - buffer.append(newSeparator); - shiftOffsets(offsetsToKeep, buffer.length(), 1, newSeparator.length()); - } - else if (buffer == null) { - intactLength++; - } - else { - buffer.append(c); - } - } - else if (c == '\r') { - boolean followedByLineFeed = i < text.length() - 1 && text.charAt(i + 1) == '\n'; - if (!followedByLineFeed && keepCarriageReturn) { - if (buffer == null) { - intactLength++; - } - else { - buffer.append(c); - } - continue; - } - if (buffer == null) { - buffer = new StringBuilder(text.length()); - buffer.append(text, 0, intactLength); - } - buffer.append(newSeparator); - if (followedByLineFeed) { - i++; - shiftOffsets(offsetsToKeep, buffer.length(), 2, newSeparator.length()); - } - else { - shiftOffsets(offsetsToKeep, buffer.length(), 1, newSeparator.length()); - } - } - else { - if (buffer == null) { - intactLength++; - } - else { - buffer.append(c); - } - } - } - return buffer == null ? text : buffer.toString(); - } - - private static void shiftOffsets(int[] offsets, int changeOffset, int oldLength, int newLength) { - if (offsets == null) return; - int shift = newLength - oldLength; - if (shift == 0) return; - for (int i = 0; i < offsets.length; i++) { - int offset = offsets[i]; - if (offset >= changeOffset + oldLength) { - offsets[i] += shift; - } - } - } - public static int getLineBreakCount(@NotNull CharSequence text) { int count = 0; for (int i = 0; i < text.length(); i++) { diff --git a/platform/util/util.iml b/platform/util/util.iml index 160382d542e7..378b380da974 100644 --- a/platform/util/util.iml +++ b/platform/util/util.iml @@ -15,6 +15,7 @@ + diff --git a/plugins/git4idea/src/org/jetbrains/git4idea/ssh/GitSSHService.java b/plugins/git4idea/src/org/jetbrains/git4idea/ssh/GitSSHService.java index 389254f3ab8a..430ea6f0b8ba 100644 --- a/plugins/git4idea/src/org/jetbrains/git4idea/ssh/GitSSHService.java +++ b/plugins/git4idea/src/org/jetbrains/git4idea/ssh/GitSSHService.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -15,7 +15,6 @@ */ package org.jetbrains.git4idea.ssh; -import com.intellij.openapi.util.io.FileUtilLight; import com.trilead.ssh2.KnownHosts; import gnu.trove.THashMap; import org.apache.commons.codec.DecoderException; @@ -23,6 +22,7 @@ import org.apache.xmlrpc.XmlRpcClientLite; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.git4idea.util.ScriptGenerator; +import org.jetbrains.util.rt.FileUtilRt; import java.io.File; import java.io.IOException; @@ -66,8 +66,7 @@ public abstract class GitSSHService { public synchronized File getScriptPath() throws IOException { if (myScriptPath == null || !myScriptPath.exists()) { ScriptGenerator generator = new ScriptGenerator(GitSSHHandler.GIT_SSH_PREFIX, SSHMain.class, getTempDir()); - generator.addClasses(XmlRpcClientLite.class, DecoderException.class); - generator.addClasses(KnownHosts.class, FileUtilLight.class); + generator.addClasses(XmlRpcClientLite.class, DecoderException.class, KnownHosts.class, FileUtilRt.class); generator.addResource(SSHMainBundle.class, "/org/jetbrains/git4idea/ssh/SSHMainBundle.properties"); myScriptPath = generator.generate(); } diff --git a/plugins/git4idea/src/org/jetbrains/git4idea/ssh/SSHMain.java b/plugins/git4idea/src/org/jetbrains/git4idea/ssh/SSHMain.java index 85b67ce944ca..bc3737565480 100644 --- a/plugins/git4idea/src/org/jetbrains/git4idea/ssh/SSHMain.java +++ b/plugins/git4idea/src/org/jetbrains/git4idea/ssh/SSHMain.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -15,12 +15,12 @@ */ package org.jetbrains.git4idea.ssh; -import com.intellij.openapi.util.io.FileUtilLight; import com.intellij.util.ArrayUtil; import com.trilead.ssh2.*; import com.trilead.ssh2.crypto.PEMDecoder; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; +import org.jetbrains.util.rt.FileUtilRt; import java.io.*; import java.util.*; @@ -302,7 +302,7 @@ public class SSHMain { if (file.exists()) { // if encrypted ask user for passphrase String passphrase = null; - char[] text = FileUtilLight.loadFileText(file); + char[] text = FileUtilRt.loadFileText(file); if (isEncryptedKey(text)) { // need to ask passphrase from user int i; diff --git a/plugins/git4idea/src/org/jetbrains/git4idea/util/ScriptGenerator.java b/plugins/git4idea/src/org/jetbrains/git4idea/util/ScriptGenerator.java index b754a56493e6..a2903ae32095 100644 --- a/plugins/git4idea/src/org/jetbrains/git4idea/util/ScriptGenerator.java +++ b/plugins/git4idea/src/org/jetbrains/git4idea/util/ScriptGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -17,11 +17,11 @@ package org.jetbrains.git4idea.util; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.io.FileUtilLight; import com.intellij.util.PathUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; +import org.jetbrains.util.rt.FileUtilRt; import java.io.File; import java.io.FileWriter; @@ -149,8 +149,8 @@ public class ScriptGenerator { */ @SuppressWarnings({"HardCodedStringLiteral"}) public File generate() throws IOException { - File scriptPath = myTempDir != null ? FileUtilLight.createTempFile(myTempDir, myPrefix, SCRIPT_EXT, true) - : FileUtilLight.createTempFile(myPrefix, SCRIPT_EXT); + File scriptPath = myTempDir != null ? FileUtilRt.createTempFile(myTempDir, myPrefix, SCRIPT_EXT, true) + : FileUtilRt.createTempFile(myPrefix, SCRIPT_EXT); scriptPath.deleteOnExit(); PrintWriter out = new PrintWriter(new FileWriter(scriptPath)); try { @@ -172,7 +172,7 @@ public class ScriptGenerator { finally { out.close(); } - FileUtilLight.setExecutableAttribute(scriptPath.getPath(), true); + FileUtilRt.setExecutableAttribute(scriptPath.getPath(), true); return scriptPath; }