Revert "IDEA-201136 Update velocity library to 2.0"

it's too troublesome and has no clear benefits for now
we can return to this when we have more convincing reasons to upgrade than just "it's too old"
This commit is contained in:
peter
2019-01-14 22:16:43 +01:00
parent 9528bff51a
commit 506f7989e0
16 changed files with 85 additions and 740 deletions

View File

@@ -1,18 +1,18 @@
<component name="libraryTable">
<library name="Velocity" type="repository">
<properties maven-id="org.apache.velocity:velocity-engine-core:2.0">
<properties maven-id="org.apache.velocity:velocity:1.7">
<exclude>
<dependency maven-id="org.apache.commons:commons-lang3" />
<dependency maven-id="commons-collections:commons-collections" />
</exclude>
</properties>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/velocity/velocity-engine-core/2.0/velocity-engine-core-2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/velocity/velocity/1.7/velocity-1.7.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.4/commons-lang-2.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/velocity/velocity-engine-core/2.0/velocity-engine-core-2.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/velocity/velocity/1.7/velocity-1.7-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.4/commons-lang-2.4-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -1,12 +0,0 @@
<component name="libraryTable">
<library name="commons-lang3" type="repository">
<properties maven-id="org.apache.commons:commons-lang3:3.5" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -1,18 +1,16 @@
<component name="libraryTable">
<library name="maven-model" type="repository">
<properties maven-id="org.apache.maven:maven-model:3.3.9">
<exclude>
<dependency maven-id="org.apache:commons-lang3" />
</exclude>
</properties>
<properties maven-id="org.apache.maven:maven-model:3.3.9" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-model/3.3.9/maven-model-3.3.9.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-model/3.3.9/maven-model-3.3.9-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -119,19 +119,17 @@
<orderEntry type="library" name="maven-model" level="project" />
<orderEntry type="module-library">
<library name="org.apache.maven:maven-artifact" type="repository">
<properties maven-id="org.apache.maven:maven-artifact:3.3.9">
<exclude>
<dependency maven-id="org.apache:commons-lang3" />
</exclude>
</properties>
<properties maven-id="org.apache.maven:maven-artifact:3.3.9" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
@@ -151,19 +149,17 @@
</orderEntry>
<orderEntry type="module-library">
<library name="org.apache.maven:maven-builder-support" type="repository">
<properties maven-id="org.apache.maven:maven-builder-support:3.3.9">
<exclude>
<dependency maven-id="org.apache:commons-lang3" />
</exclude>
</properties>
<properties maven-id="org.apache.maven:maven-builder-support:3.3.9" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-builder-support/3.3.9/maven-builder-support-3.3.9.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/maven/maven-builder-support/3.3.9/maven-builder-support-3.3.9-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
@@ -191,6 +187,5 @@
</SOURCES>
</library>
</orderEntry>
<orderEntry type="library" name="commons-lang3" level="project" />
</component>
</module>

View File

@@ -18,11 +18,11 @@ package com.intellij.codeInsight.generation;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import java.io.Reader;
import java.io.InputStream;
public class VelocityIncludesClassLoader extends ClasspathResourceLoader {
@Override
public Reader getResourceReader(String name, String encoding) throws ResourceNotFoundException {
return super.getResourceReader("com/intellij/codeInsight/generation/" + name, encoding);
public InputStream getResourceStream(String name) throws ResourceNotFoundException {
return super.getResourceStream("com/intellij/codeInsight/generation/" + name);
}
}

View File

@@ -14,8 +14,8 @@
* limitations under the License.
*/
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ClassUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@@ -23,7 +23,7 @@ public class BytecodeAnalysisResultsHighlightingTest extends DataFlowInspectionT
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
super.configureModule(module, model, contentEntry);
PsiTestUtil.addProjectLibrary(model, "lang", IntelliJProjectConfiguration.getProjectLibraryClassesRootPaths("commons-lang3"));
PsiTestUtil.addProjectLibrary(model, "velocity", IntelliJProjectConfiguration.getProjectLibraryClassesRootPaths("Velocity"));
}
};
}

View File

@@ -96,13 +96,8 @@ class CommunityLibraryLicenses {
license: "Apache 2.0", url: "https://github.com/JetBrains/intellij-deps-commons-imaging"),
new LibraryLicense(name: "Apache Commons IO", libraryName: "commons-io", version: "2.6", license: "Apache 2.0",
url: "http://commons.apache.org/io/"),
new LibraryLicense(name: "Apache Commons Lang", libraryName: "commons-lang2", version: "2.6",
attachedTo: "intellij.platform.lang.impl", url: "http://commons.apache.org/proper/commons-lang/",
license: "Apache 2.0", licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.txt"),
new LibraryLicense(name: "Apache Commons Lang 3", libraryName: "commons-lang3-3.2.1.jar", version: "3.2.1", license: "Apache 2.0",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.txt", url: "http://commons.apache.org/proper/commons-lang/"),
new LibraryLicense(name: "Apache Commons Lang 3", libraryName: "commons-lang3", version: "3.5", license: "Apache 2.0",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.txt", url: "http://commons.apache.org/proper/commons-lang/"),
new LibraryLicense(name: "Apache Commons Logging", libraryName: "commons-logging", version: "1.1.1", license: "Apache 2.0",
url: "http://commons.apache.org/logging/"),
new LibraryLicense(name: "Apache Commons Net", libraryName: "commons-net", version: "3.3", license: "Apache 2.0",
@@ -454,7 +449,7 @@ class CommunityLibraryLicenses {
license: "BSD style (see LICENSE.txt in trilead-ssh2-build213.jar)", url: "http://www.trilead.com/SSH_Library/"),
new LibraryLicense(name: "Trove4j", version: "1.1 (with patches by JetBrains)", license: "LGPL", url: "http://trove4j.sourceforge.net/",
licenseUrl: "http://trove4j.sourceforge.net/html/license.html"),
new LibraryLicense(name: "Velocity", version: "2.0", license: "Apache 2.0", url: "http://velocity.apache.org/",
new LibraryLicense(name: "Velocity", version: "1.7", license: "Apache 2.0", url: "http://velocity.apache.org/",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.txt"),
new LibraryLicense(name: "weberknecht", libraryName: "weberknecht-0.1.5.jar", version: "0.1.5", license: "Apache 2.0",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0", url: "https://github.com/pelotoncycle/weberknecht"),

View File

@@ -31,6 +31,5 @@
<orderEntry type="library" name="maven-model" level="project" />
<orderEntry type="library" name="gson" level="project" />
<orderEntry type="library" name="http-client" level="project" />
<orderEntry type="library" name="commons-lang3" level="project" />
</component>
</module>

View File

@@ -35,18 +35,5 @@
<orderEntry type="library" name="miglayout-swing" level="project" />
<orderEntry type="library" name="jna" level="project" />
<orderEntry type="library" name="lz4-java" level="project" />
<orderEntry type="library" exported="" name="commons-lang3" level="project" />
<orderEntry type="module-library" scope="RUNTIME">
<library name="commons-lang2" type="repository">
<properties maven-id="commons-lang:commons-lang:2.6" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.6/commons-lang-2.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.6/commons-lang-2.6-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>

View File

@@ -30,6 +30,7 @@ import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.runtime.parser.Token;
import org.apache.velocity.runtime.parser.node.*;
import org.apache.velocity.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -64,7 +65,7 @@ public class FileTemplateUtil {
private static String[] calculateAttributes(@NotNull String templateContent, @NotNull Set<String> propertiesNames, boolean includeDummies, @NotNull Project project) throws ParseException {
final Set<String> unsetAttributes = new LinkedHashSet<>();
final Set<String> definedAttributes = new HashSet<>();
SimpleNode template = VelocityWrapper.parse(new StringReader(templateContent));
SimpleNode template = VelocityWrapper.parse(new StringReader(templateContent), "MyTemplate");
collectAttributes(unsetAttributes, definedAttributes, template, propertiesNames, includeDummies, new HashSet<>(), project);
for (String definedAttribute : definedAttributes) {
unsetAttributes.remove(definedAttribute);
@@ -110,7 +111,7 @@ public class FileTemplateUtil {
includedTemplate = templateManager.getPattern(s);
}
if (includedTemplate != null && visitedIncludes.add(s)) {
SimpleNode template = VelocityWrapper.parse(new StringReader(includedTemplate.getText()));
SimpleNode template = VelocityWrapper.parse(new StringReader(includedTemplate.getText()), "MyTemplate");
collectAttributes(referenced, defined, template, propertiesNames, includeDummies, visitedIncludes, project);
}
}
@@ -185,7 +186,7 @@ public class FileTemplateUtil {
@NotNull
private static VelocityContext createVelocityContext() {
VelocityContext context = new VelocityContext();
context.put("StringUtils", Velocity17StringUtils.class);
context.put("StringUtils", StringUtils.class);
return context;
}

View File

@@ -1,652 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.fileTemplates;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
// JB: Copied from Velocity 1.7 for user template backward compatibility (https://youtrack.jetbrains.com/issue/IDEA-107709)
/**
* This class provides some methods for dynamically
* invoking methods in objects, and some string
* manipulation methods used by torque. The string
* methods will soon be moved into the turbine
* string utilities class.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @version $Id: StringUtils.java 685685 2008-08-13 21:43:27Z nbubna $
*/
@SuppressWarnings({"unused", "StringBufferMayBeStringBuilder", "ForLoopReplaceableByForEach", "MissingDeprecatedAnnotation",
"UnusedAssignment", "StringToUpperCaseOrToLowerCaseWithoutLocale", "unchecked", "SSBasedInspection", "UseOfObsoleteCollectionType",
"SwitchStatementWithTooFewBranches", "AssignmentToForLoopParameter", "IOResourceOpenedButNotSafelyClosed", "CallToPrintStackTrace",
"UnnecessaryFullyQualifiedName", "ToArrayCallWithZeroLengthArrayArgument", "FinalStaticMethod"})
public class Velocity17StringUtils
{
/**
* Line separator for the OS we are operating on.
*/
private static final String EOL = System.getProperty("line.separator");
/**
* Concatenates a list of objects as a String.
*
* @param list The list of objects to concatenate.
* @return A text representation of the concatenated objects.
*/
public String concat(List list)
{
StringBuffer sb = new StringBuffer();
int size = list.size();
for (int i = 0; i < size; i++)
{
sb.append(list.get(i).toString());
}
return sb.toString();
}
/**
* Return a package name as a relative path name
*
* @param pckge package name to convert to a directory.
* @return String directory path.
*/
static public String getPackageAsPath(String pckge)
{
return pckge.replace( '.', File.separator.charAt(0) ) + File.separator;
}
/**
* <p>
* Remove underscores from a string and replaces first
* letters with capitals. Other letters are changed to lower case.
* </p>
*
* <p>
* For example <code>foo_bar</code> becomes <code>FooBar</code>
* but <code>foo_barBar</code> becomes <code>FooBarbar</code>.
* </p>
*
* @param data string to remove underscores from.
* @return String
* @deprecated Use the org.apache.commons.util.StringUtils class
* instead. Using its firstLetterCaps() method in conjunction
* with a StringTokenizer will achieve the same result.
*/
static public String removeUnderScores (String data)
{
String temp = null;
StringBuffer out = new StringBuffer();
temp = data;
StringTokenizer st = new StringTokenizer(temp, "_");
while (st.hasMoreTokens())
{
String element = (String) st.nextElement();
out.append ( firstLetterCaps(element));
}
return out.toString();
}
/**
* <p>
* 'Camels Hump' replacement of underscores.
* </p>
*
* <p>
* Remove underscores from a string but leave the capitalization of the
* other letters unchanged.
* </p>
*
* <p>
* For example <code>foo_barBar</code> becomes <code>FooBarBar</code>.
* </p>
*
* @param data string to hump
* @return String
*/
static public String removeAndHump (String data)
{
return removeAndHump(data,"_");
}
/**
* <p>
* 'Camels Hump' replacement.
* </p>
*
* <p>
* Remove one string from another string but leave the capitalization of the
* other letters unchanged.
* </p>
*
* <p>
* For example, removing "_" from <code>foo_barBar</code> becomes <code>FooBarBar</code>.
* </p>
*
* @param data string to hump
* @param replaceThis string to be replaced
* @return String
*/
static public String removeAndHump (String data,String replaceThis)
{
String temp = null;
StringBuffer out = new StringBuffer();
temp = data;
StringTokenizer st = new StringTokenizer(temp, replaceThis);
while (st.hasMoreTokens())
{
String element = (String) st.nextElement();
out.append ( capitalizeFirstLetter(element));
}//while
return out.toString();
}
/**
* <p>
* Makes the first letter caps and the rest lowercase.
* </p>
*
* <p>
* For example <code>fooBar</code> becomes <code>Foobar</code>.
* </p>
*
* @param data capitalize this
* @return String
*/
static public String firstLetterCaps ( String data )
{
String firstLetter = data.substring(0,1).toUpperCase();
String restLetters = data.substring(1).toLowerCase();
return firstLetter + restLetters;
}
/**
* <p>
* Capitalize the first letter but leave the rest as they are.
* </p>
*
* <p>
* For example <code>fooBar</code> becomes <code>FooBar</code>.
* </p>
*
* @param data capitalize this
* @return String
*/
static public String capitalizeFirstLetter ( String data )
{
String firstLetter = data.substring(0,1).toUpperCase();
String restLetters = data.substring(1);
return firstLetter + restLetters;
}
/**
* Create a string array from a string separated by delim
*
* @param line the line to split
* @param delim the delimter to split by
* @return a string array of the split fields
*/
public static String [] split(String line, String delim)
{
List list = new ArrayList();
StringTokenizer t = new StringTokenizer(line, delim);
while (t.hasMoreTokens())
{
list.add(t.nextToken());
}
return (String []) list.toArray(new String[list.size()]);
}
/**
* Chop i characters off the end of a string.
* This method assumes that any EOL characters in String s
* and the platform EOL will be the same.
* A 2 character EOL will count as 1 character.
*
* @param s String to chop.
* @param i Number of characters to chop.
* @return String with processed answer.
*/
public static String chop(String s, int i)
{
return chop(s, i, EOL);
}
/**
* Chop i characters off the end of a string.
* A 2 character EOL will count as 1 character.
*
* @param s String to chop.
* @param i Number of characters to chop.
* @param eol A String representing the EOL (end of line).
* @return String with processed answer.
*/
public static String chop(String s, int i, String eol)
{
if ( i == 0 || s == null || eol == null )
{
return s;
}
int length = s.length();
/*
* if it is a 2 char EOL and the string ends with
* it, nip it off. The EOL in this case is treated like 1 character
*/
if ( eol.length() == 2 && s.endsWith(eol ))
{
length -= 2;
i -= 1;
}
if ( i > 0)
{
length -= i;
}
if ( length < 0)
{
length = 0;
}
return s.substring( 0, length);
}
/**
* @param argStr
* @param vars
* @return Substituted String.
*/
public static StringBuffer stringSubstitution( String argStr,
Hashtable vars )
{
return stringSubstitution( argStr, (Map) vars );
}
/**
* Perform a series of substitutions. The substitions
* are performed by replacing $variable in the target
* string with the value of provided by the key "variable"
* in the provided hashtable.
*
* @param argStr target string
* @param vars name/value pairs used for substitution
* @return String target string with replacements.
*/
public static StringBuffer stringSubstitution(String argStr,
Map vars)
{
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length();)
{
char ch = argStr.charAt(cIdx);
switch (ch)
{
case '$':
StringBuffer nameBuf = new StringBuffer();
for (++cIdx ; cIdx < argStr.length(); ++cIdx)
{
ch = argStr.charAt(cIdx);
if (ch == '_' || Character.isLetterOrDigit(ch))
nameBuf.append(ch);
else
break;
}
if (nameBuf.length() > 0)
{
String value =
(String) vars.get(nameBuf.toString());
if (value != null)
{
argBuf.append(value);
}
}
break;
default:
argBuf.append(ch);
++cIdx;
break;
}
}
return argBuf;
}
/**
* Read the contents of a file and place them in
* a string object.
*
* @param file path to file.
* @return String contents of the file.
*/
public static String fileContentsToString(String file)
{
String contents = "";
File f = null;
try
{
f = new File(file);
if (f.exists())
{
FileReader fr = null;
try
{
fr = new FileReader(f);
char[] template = new char[(int) f.length()];
fr.read(template);
contents = new String(template);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (fr != null)
{
fr.close();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return contents;
}
/**
* Remove/collapse multiple newline characters.
*
* @param argStr string to collapse newlines in.
* @return String
*/
public static String collapseNewlines(String argStr)
{
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++)
{
char ch = argStr.charAt(cIdx);
if (ch != '\n' || last != '\n')
{
argBuf.append(ch);
last = ch;
}
}
return argBuf.toString();
}
/**
* Remove/collapse multiple spaces.
*
* @param argStr string to remove multiple spaces from.
* @return String
*/
public static String collapseSpaces(String argStr)
{
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++)
{
char ch = argStr.charAt(cIdx);
if (ch != ' ' || last != ' ')
{
argBuf.append(ch);
last = ch;
}
}
return argBuf.toString();
}
/**
* Replaces all instances of oldString with newString in line.
* Taken from the Jive forum package.
*
* @param line original string.
* @param oldString string in line to replace.
* @param newString replace oldString with this.
* @return String string with replacements.
*/
public static final String sub(String line, String oldString,
String newString)
{
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0)
{
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0)
{
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* Returns the output of printStackTrace as a String.
*
* @param e A Throwable.
* @return A String.
*/
public static final String stackTrace(Throwable e)
{
String foo = null;
try
{
// And show the Error Screen.
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
e.printStackTrace( new PrintWriter(ostr,true) );
foo = ostr.toString();
}
catch (Exception f)
{
// Do nothing.
}
return foo;
}
/**
* Return a context-relative path, beginning with a "/", that represents
* the canonical version of the specified path after ".." and "." elements
* are resolved out. If the specified path attempts to go outside the
* boundaries of the current context (i.e. too many ".." path elements
* are present), return <code>null</code> instead.
*
* @param path Path to be normalized
* @return String normalized path
*/
public static final String normalizePath(String path)
{
// Normalize the slashes and add leading slash if necessary
String normalized = path;
if (normalized.indexOf('\\') >= 0)
{
normalized = normalized.replace('\\', '/');
}
if (!normalized.startsWith("/"))
{
normalized = "/" + normalized;
}
// Resolve occurrences of "//" in the normalized path
while (true)
{
int index = normalized.indexOf("//");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 1);
}
// Resolve occurrences of "%20" in the normalized path
while (true)
{
int index = normalized.indexOf("%20");
if (index < 0)
break;
normalized = normalized.substring(0, index) + " " +
normalized.substring(index + 3);
}
// Resolve occurrences of "/./" in the normalized path
while (true)
{
int index = normalized.indexOf("/./");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 2);
}
// Resolve occurrences of "/../" in the normalized path
while (true)
{
int index = normalized.indexOf("/../");
if (index < 0)
break;
if (index == 0)
return (null); // Trying to go outside our context
int index2 = normalized.lastIndexOf('/', index - 1);
normalized = normalized.substring(0, index2) +
normalized.substring(index + 3);
}
// Return the normalized path that we have completed
return (normalized);
}
/**
* If state is true then return the trueString, else
* return the falseString.
*
* @param state
* @param trueString
* @param falseString
* @return Selected result.
*/
public String select(boolean state, String trueString, String falseString)
{
if (state)
{
return trueString;
}
else
{
return falseString;
}
}
/**
* Check to see if all the string objects passed
* in are empty.
*
* @param list A list of {@link java.lang.String} objects.
* @return Whether all strings are empty.
*/
public boolean allEmpty(List list)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (list.get(i) != null && list.get(i).toString().length() > 0)
{
return false;
}
}
return true;
}
/**
* Trim all strings in a List. Changes the strings in the existing list.
* @param list
* @return List of trimmed strings.
* @since 1.5
*/
public static List trimStrings(List list)
{
if (list == null)
return null;
int sz = list.size();
for (int i = 0; i < sz; i++)
list.set(i,nullTrim((String) list.get(i)));
return list;
}
/**
* Trim the string, but pass a null through.
* @param s
* @return List of trimmed Strings.
* @since 1.5
*/
public static String nullTrim(String s)
{
if (s == null)
{
return null;
}
else
{
return s.trim();
}
}
}

View File

@@ -4,27 +4,26 @@ package com.intellij.ide.fileTemplates;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import org.apache.velocity.Template;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeSingleton;
import org.apache.velocity.runtime.log.LogSystem;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.ResourceManager;
import org.apache.velocity.runtime.resource.ResourceManagerImpl;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.util.ExtProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.io.*;
/**
* Initializes Velocity when it's actually needed. All interaction with Velocity should go through this class.
@@ -44,23 +43,40 @@ class VelocityWrapper {
", ResourceManagerImpl in " + PathManager.getJarPathForClass(ResourceManagerImpl.class));
}
LogSystem emptyLogSystem = new LogSystem() {
@Override
public void init(RuntimeServices runtimeServices) throws Exception {
}
@Override
public void logVelocityMessage(int i, String s) {
//todo[myakovlev] log somethere?
}
};
Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, emptyLogSystem);
Velocity.setProperty(RuntimeConstants.INPUT_ENCODING, FileTemplate.ourEncoding);
Velocity.setProperty(RuntimeConstants.PARSER_POOL_SIZE, 3);
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "includes");
Velocity.setProperty("includes.resource.loader.instance", new ResourceLoader() {
@Override
public void init(ExtProperties configuration) {
public void init(ExtendedProperties configuration) {
}
@Override
public Reader getResourceReader(String resourceName, String encoding) throws ResourceNotFoundException {
public InputStream getResourceStream(String resourceName) throws ResourceNotFoundException {
FileTemplateManager templateManager = ourTemplateManager.get();
if (templateManager == null) templateManager = FileTemplateManager.getDefaultInstance();
final FileTemplate include = templateManager.getPattern(resourceName);
if (include == null) {
throw new ResourceNotFoundException("Template not found: " + resourceName);
}
return new StringReader(include.getText());
final String text = include.getText();
try {
return new ByteArrayInputStream(text.getBytes(FileTemplate.ourEncoding));
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
@@ -82,15 +98,15 @@ class VelocityWrapper {
}
@NotNull
static SimpleNode parse(@NotNull Reader reader) throws ParseException {
return RuntimeSingleton.parse(reader, new Template());
static SimpleNode parse(@NotNull Reader reader, @NotNull String templateName) throws ParseException {
return RuntimeSingleton.parse(reader, templateName);
}
static void evaluate(@Nullable Project project, Context context, @NotNull Writer writer, String templateContent)
static boolean evaluate(@Nullable Project project, Context context, @NotNull Writer writer, String templateContent)
throws ParseErrorException, MethodInvocationException, ResourceNotFoundException {
try {
ourTemplateManager.set(project == null ? FileTemplateManager.getDefaultInstance() : FileTemplateManager.getInstance(project));
Velocity.evaluate(context, writer, "", templateContent);
return Velocity.evaluate(context, writer, "", templateContent);
}
finally {
ourTemplateManager.set(null);

View File

@@ -24,9 +24,11 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilCore;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.SimpleLog4JLogSystem;
import java.io.StringWriter;
import java.util.HashMap;
@@ -82,7 +84,8 @@ public class VelocityHelper
return "\n";
}
public static void verify(String text) {
public static void verify(String text) throws Exception
{
VelocityEngine engine = getEngine();
VelocityContext vc = new VelocityContext();
@@ -101,15 +104,20 @@ public class VelocityHelper
try
{
VelocityEngine engine = new VelocityEngine();
ExtendedProperties extendedProperties = new ExtendedProperties();
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
engine.setProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");
extendedProperties.addProperty(RuntimeConstants.RESOURCE_LOADER, "file");
extendedProperties.addProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");
engine.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
engine.setProperty("file.resource.loader.path", PathManager.getPluginsPath() + "/Copyright/resources");
extendedProperties.addProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
extendedProperties.addProperty("file.resource.loader.path", PathManager.getPluginsPath() + "/Copyright/resources");
engine.setProperty(RuntimeConstants.RUNTIME_LOG_NAME, CopyrightManager.class.getName());
extendedProperties.addProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
SimpleLog4JLogSystem.class.getName());
extendedProperties
.addProperty("runtime.log.logsystem.log4j.category", CopyrightManager.class.getName());
engine.setExtendedProperties(extendedProperties);
engine.init();
instance = engine;

View File

@@ -16,8 +16,10 @@
package org.jetbrains.java.generate.velocity;
import com.intellij.codeInsight.generation.VelocityIncludesClassLoader;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.SimpleLog4JLogSystem;
/**
* Velocity factory.
@@ -29,6 +31,12 @@ public class VelocityFactory {
private static final VelocityEngine engine = newVelocityEngine();
}
/**
* Privte constructor.
*/
private VelocityFactory() {
}
/**
* Returns a new instance of the VelocityEngine.
* <p/>
@@ -37,11 +45,14 @@ public class VelocityFactory {
* @return a new velocity engine that is initialized.
*/
private static VelocityEngine newVelocityEngine() {
ExtendedProperties prop = new ExtendedProperties();
prop.addProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, SimpleLog4JLogSystem.class.getName());
prop.addProperty("runtime.log.logsystem.log4j.category", "GenerateToString");
prop.addProperty(RuntimeConstants.RESOURCE_LOADER, "includes");
prop.addProperty("includes.resource.loader.class", VelocityIncludesClassLoader.class.getName());
prop.addProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
VelocityEngine velocity = new VelocityEngine();
velocity.setProperty(RuntimeConstants.RUNTIME_LOG_NAME, "GenerateToString");
velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "includes");
velocity.setProperty("includes.resource.loader.class", VelocityIncludesClassLoader.class.getName());
velocity.setProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
velocity.setExtendedProperties(prop);
velocity.init();
return velocity;
}

View File

@@ -1,4 +1,3 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.ipnb.format;
import com.google.common.collect.Lists;
@@ -21,7 +20,7 @@ import com.intellij.util.text.VersionComparatorUtil;
import com.jetbrains.python.packaging.PyPackage;
import com.jetbrains.python.packaging.PyPackageUtil;
import com.jetbrains.python.sdk.PythonSdkType;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
@@ -575,7 +574,7 @@ public class IpnbParser {
if (codemirrorModeMap.containsKey("version")) {
String version = (String)codemirrorModeMap.get("version");
if (NumberUtils.isCreatable(version)) {
if (NumberUtils.isNumber(version)) {
try {
codemirrorModeMap.put("version", Integer.parseInt(version));
}