Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.
  */
 
 package org.wicketstuff.console.templates;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 
A utility class for handling ScriptTemplates.

Author(s):
cretzel
 
 public class ScriptTemplateUtils
 {

Read a source file from class path into a ScriptTemplate.

The file has to have an appropriate extension such as .groovy or .clj.

Parameters:
cl ClassLoader
path path on the class path
name name of the script (without file extension)
lang source language
Returns:
a ScriptTemplate
 
 	static ScriptTemplate readTemplateFromClasspath(final ClassLoader clfinal String path,
 		final String namefinal Lang lang)
 	{
 
 		final String resourceName = path + name + lang.getFileExtension();
 		final URL url = cl.getResource(resourceName);
 		if (url == null)
 		{
 			throw new RuntimeException("Classpath URL for " + resourceName + " not found");
 		}
 
 		StringBuilder content;
 		try
 		{
 			content = readUrl(url);
 		}
 		catch (final IOException e)
 		{
 			throw new RuntimeException(String.format("Could not read class path file %s %s %s",
 				pathnamelang));
 		}
 
 		final ScriptTemplate template = new ScriptTemplate(camelCaseSpace(name),
 			content.toString(), lang);
 		return template;
 	}
 
 	private static StringBuilder readUrl(final URL urlthrows IOException
 	{
 
 		final BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));
 		try
 		{
 			final StringBuilder content = new StringBuilder();
 			String line = null;
 			while ((line = r.readLine()) != null)
 			{
 				content.append(line).append("\n");
 			}
 
 			return content;
 		}
		finally
		{
			r.close();
		}
	}

Utility method to read ScriptTemplates from a source directory.

Parameters:
lang source language
dir directory to scan for source files
Returns:
list of ScriptTemplates
	public static List<ScriptTemplatereadTemplatesFromDir(final Lang langfinal File dir)
	{
		final List<ScriptTemplatetemplates = new ArrayList<ScriptTemplate>();
		final File[] files = dir.listFiles(LangFileFilter.create(lang));
		for (final File file : files)
		{
			final ScriptTemplate template = readTemplateFromFile(langfile);
			templates.add(template);
		}
		return templates;
	}

Utility method to read a ScriptTemplate from a source file.

Parameters:
lang source language
file source file
Returns:
a ScriptTemplate
	public static ScriptTemplate readTemplateFromFile(final Lang langfinal File file)
	{
		final String title = camelCaseSpaceFilename(file);
		final String script = readFile(file);
		final ScriptTemplate template = new ScriptTemplate(titlescriptlang);
		return template;
	}

Utility method to convert a camel cased file name using camelCaseSpace(java.lang.String).
	public static String camelCaseSpaceFilename(final File file)
	{
		final String filename = file.getName();
		return camelCaseSpaceFilename(filename);
	}

Utility method to convert a camel cased file name using camelCaseSpace(java.lang.String).
	public static String camelCaseSpaceFilename(final String filename)
	{
		final int indexOfExt = filename.lastIndexOf('.');
		if (indexOfExt == 0)
		{
			throw new RuntimeException("Invalid filename " + filename);
		}
		final String baseFilename = filename.substring(0, indexOfExt);
		final String scriptName = camelCaseSpace(baseFilename);
		return scriptName;
	}

Converts a camel cased string into a string where each upper case letter is prefixed with a space.

Parameters:
str a camel cased string
Returns:
a string where each upper case letter is prefixed with a space
	public static String camelCaseSpace(final String str)
	{
		if (str.length() == 0)
		{
			throw new RuntimeException("camelCaseSpace not allowed for string length 0");
		}
		final char[] charArray = str.toCharArray();
		final StringBuilder result = new StringBuilder(charArray[0] + "");
		for (int i = 1; i < charArray.lengthi++)
		{
			final char c = charArray[i];
			if (Character.isUpperCase(c))
			{
				result.append(' ');
			}
			result.append(c);
		}
		return result.toString();
	}
	static String readFile(final File file)
	{
		try
		{
			try
			{
				return IOUtils.toString(bin);
			}
			finally
			{
				bin.close();
			}
		}
		catch (final IOException e)
		{
			throw new RuntimeException("Could not read file " + filee);
		}
	}
New to GrepCode? Check out our FAQ X