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;
 
Provides a set of sample ScriptTemplates.

Use dataProvider() and feed a ScriptTemplateSelectionTablePanel with it.

Author(s):
cretzel
 
 public class PackagedScriptTemplates {
 
 	private static final String SCRIPT_DIR_BASE = "org/wicketstuff/console/templates/";
 
 	private static final String[] PACKAGED_GROOVY_TEMPLATES = new String[] {
 			"HibernateCriteria""HibernateHqlQuery""HibernateSave",
 			"HibernateShowSql""HibernateStatistics""Log4j",
 			"MethodsAndFields""ReadClasspathResource""SystemProperties",
 			"WicketClearMarkupCache""WicketClearPropertiesCache",
 			"WicketClientInfo""WicketComponentHierarchy",
 			"WicketInvalidateSession""WicketSize" };
 
 	private static final String[] PACKAGED_CLOJURE_TEMPLATES = new String[] { "MethodsAndFields" };

Creates a data provider that returns all packaged templates for a given source language.

Parameters:
lang language
Returns:
data provider
 
 			final Lang lang) {
 	}

Returns all packaged script templates for a given source language.

Parameters:
lang language
Returns:
List of ScriptTemplates
 
 			final Lang lang) {
 
 		final List<ScriptTemplatetemplates = new ArrayList<ScriptTemplate>();
 
 		String[] templateNames = new String[0];
 
 		switch (lang) {
 		case :
 			templateNames = ;
 			break;
 		case :
 			templateNames = ;
 			break;
 		default:
 			break;
 		}
 
 		for (final String name : templateNames) {
 			final String scriptBase = 
 					+ lang.name().toLowerCase() + "/";
					scriptBasenamelang);
			templates.add(template);
		}
		return templates;
	}

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
			final String pathfinal 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 {
				is));
		final StringBuilder content = new StringBuilder();
		String line = null;
		while ((line = r.readLine()) != null) {
			content.append(line).append("\n");
		}
		return content;
	}

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 lang,
			final 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 lang,
			final 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 {
			return new String(Files.readBytes(file));
catch (final IOException e) {
			throw new RuntimeException("Could not read file " + filee);
		}
	}
New to GrepCode? Check out our FAQ X