How can StringTemplate import directory be used? - stringtemplate

I wrote a sample java file that is located in /src folder.
public class Main {
public static void main(String[] args){
STGroupFile StgFile=new STGroupFile("template.stg");
StgFile.delimiterStartChar = '$';
StgFile.delimiterStopChar = '$';
ST webtemp=StgFile.getInstanceOf("test");
webtemp.add("x","whyyy");
System.out.println(webtemp.render());
}
}
Also I have the file template.stg that is located in /src folder also
import "headers/header.stg"
test(x) ::= << $included(x)$ >>
and the file header.stg that is located in /src/headers folder.
included(x) ::= << headers[$x$] >>
The result obviously is
headers[whyyy]
but if I try to import whole headers directory making the template.stg like that:
import "headers"
test(x) ::= << $included(x)$ >>
the included subtemplate can't be used...
The version of StringTemplate I use is the latest 4.0.5 and according to the link http://www.antlr.org/wiki/display/ST4/Differences+between+v3+and+v4 importing a directory is allowed.. I have even used / before and/or after / - relative/absolute paths and it doesn't work :( Any Help please? Thanks :)

Have you tried $header/included(x)$?

Related

How can the Repast file sink filename be set programmatically?

Does anybody know if there is a simple way to make the filename element path of an file sink variable according to one field in the model?
So instead of using a fix path like :
fixed_path/filename.csv
{variable_path}/filename.csv
The attached ModelInitializer shows how to do this. I also copied the code below in case attachments doesn’t go through. To enable the ModelInitializer, you need to add the following to the scenario.xml file:
<model.initializer class="PredatorPrey.MyInitializer" />
I tested this in the Predator Prey demo so you should change the class package name. In the ModelInitializer example, you need to specify the root context ID which is the same as the context ID in the context.xml file. And you should specify the variable output folder name. This example requires a file name to be specified in the file sink as you would normally do and it inserts the variable path. On caveat is that the variable folder path will be saved in the scenario if the scenario is saved in the GUI, however this code will check for any existing path and simply replace the path with the outputFolder string. So you should put the entire path in the outputFolder string and not just part of it, or change the code behavior as needed.
package PredatorPrey;
import java.io.File;
import repast.simphony.data2.engine.FileSinkComponentControllerAction;
import repast.simphony.data2.engine.FileSinkDescriptor;
import repast.simphony.engine.controller.NullAbstractControllerAction;
import repast.simphony.engine.environment.ControllerAction;
import repast.simphony.engine.environment.RunEnvironmentBuilder;
import repast.simphony.engine.environment.RunState;
import repast.simphony.scenario.ModelInitializer;
import repast.simphony.scenario.Scenario;
import repast.simphony.util.collections.Tree;
public class MyInitializer implements ModelInitializer {
#Override
public void initialize(final Scenario scen, RunEnvironmentBuilder builder) {
scen.addMasterControllerAction(new NullAbstractControllerAction() {
String rootContextID = "Predator Prey";
String outputFolder = "testoutfolder";
#Override
public void batchInitialize(RunState runState, Object contextId) {
Tree<ControllerAction> scenarioTree = scen.getControllerRegistry().getActionTree(rootContextID);
findFileSinkTreeChildren(scenarioTree, scenarioTree.getRoot(), outputFolder);
// Reset the scenario dirty flag so the changes made to the file sink
// descriptors don't prompt a scenario save in the GUI
scen.setDirty(false);
}
});
}
public static void findFileSinkTreeChildren(Tree<ControllerAction> tree,
ControllerAction parent, String outputFolder){
// Check each ControllerAction in the scenario and if it is a FileSink,
// modify the output path to include the folder
for (ControllerAction act : tree.getChildren(parent)){
if (act instanceof FileSinkComponentControllerAction){
FileSinkDescriptor descriptor = ((FileSinkComponentControllerAction)act).getDescriptor();
String fileName = descriptor.getFileName();
// remove any prefix directories from the file name
int lastSeparatorIndex = fileName.lastIndexOf(File.separator);
// Check for backslash separator
if (fileName.lastIndexOf('\\') > lastSeparatorIndex)
lastSeparatorIndex = fileName.lastIndexOf('\\');
// Check for forward slash operator
if (fileName.lastIndexOf('/') > lastSeparatorIndex)
lastSeparatorIndex = fileName.lastIndexOf('/');
if (lastSeparatorIndex > 0){
fileName = fileName.substring(lastSeparatorIndex+1, fileName.length());
}
descriptor.setFileName(outputFolder + File.separator + fileName);
}
else findFileSinkTreeChildren(tree, act, outputFolder);
}
}
}

Minecraft modding 1.7.10 setTextureName not working

I am making a new mod and my block textures work fine but my item textures dont
i need assistance on this because i just started to code java
This Is My Script:
package com.HaydenMod.item;
import com.HaydenMod.lib.RefStrings;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class DiamondShard {
public static void MainRegistery(){
intializeItem();
registerItem();
}
public static Item Dshard;
public static void intializeItem(){
Dshard = new Item().setUnlocalizedName("Dshard").setCreativeTab(CreativeTabs.tabMaterials).setTextureName(RefStrings.MODID + ":Diamond_Shard").setMaxStackSize(16);
}
public static void registerItem(){
GameRegistry.registerItem(Dshard, Dshard.getUnlocalizedName());
}
}
Try
.setTextureName(RefStrings.MODID + ":" + "Diamond_Shard")
If it dosn't work can you attach a pastebin with the error log
YourItemName= new Item().setUnlocalizedName("YourItemName").setTextureName("yourModFile:YourtextureImageNAme").setCreativeTab(TheCreativeTabYouWantToPutItIn);
This is what is used and it works perfectly. The TextureName has to be exactly the same as the TextureName in the source folder.
For example, my TextureName is blah.png and in my source folder. When calling it, should write it as setTextureName("yourModFile:blah").

Reference a class' static field of the same internal module but in a different file?

I'm using TypeScript and require.js to resolve dependencies in my files. I'm in a situation where I want to reference a static field of a class in an other file, but in the same internal module (same folder) and I am not able to access it, even if the Visual Studio pre-compiler does not show any error in my code.
I have the following situation :
Game.ts
class Game {
// ...
static width: number = 1920;
// ...
}
export = Game;
Launcher.ts
/// <reference path='lib/require.d.ts'/>
import Game = require("Game");
var width: number = Game.width;
console.log(width); // Hoping to see "1920"
And the TypeScript compiler is ok with all of this. However, I keep getting "undefined" at execution when running the compiled Launcher.ts.
It's the only reference problem I'm having in my project, so I guess the rest is configured correctly.
I hope I provided all necessary information, if you need more, please ask
Any help is appreciated, thanks !
Your code seems sound, so check the following...
You are referencing require.js in a script tag on your page, pointing at Launcher (assuming Launcher.ts is in the root directory - adjust as needed:
<script src="Scripts/require.js" data-main="Launcher"></script>
Remove the reference comment from Launcher.ts:
import Game = require("Game");
var width: number = Game.width;
console.log(width); // Hoping to see "1920"
Check that you are compiling using --module amd to ensure it generates the correct module-loading code (your JavaScript output will look like this...)
define(["require", "exports", "Game"], function (require, exports, Game) {
var width = Game.width;
console.log(width); // Hoping to see "1920"
});
If you are using Visual Studio, you can set this in Project > Properties > TypeScript Build > Module Kind (AMD)
If you are using require.js to load the (external) modules, the Game class must be exported:
export class Game {}
If you import Game in Launcher.ts like
import MyGame = require('Game')
the class can be referenced with MyGame.Game and the static variable with MyGame.Game.width
You should compile the ts files with tsc using option --module amd or the equivalent option in Visual Studio

Creating new live-templates with import statements in IntelliJ IDEA

Here is the Eclipse template that I want to port:
${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);
My current version in IDEA is as follows:
private static final Logger LOG = Logger.getLogger($CLASS_NAME$.class);$END$
where $CLASS_NAME$ is configured to use className() as its expression.
Unfortunately, I don't find any documentation on adding the import statement. Is there somehing equivalent to Eclipse ${:import(...)}?
According to this post, it is intended to use only full-qualified expressions. I tried it out and this worked for me:
private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);$END$
IDEA automatically shortens it and adds the necessary import statements:
import org.apache.log4j.Logger;
// ...
private static final Logger LOG = Logger.getLogger(MyClass.class);
If you want to try yourself, note that you first have to define CLASS_NAME as className() via Edit variables. Also make sure that you allowed your Live Template for Java declarations via Change (at the bottom). Here is a screenshot with the final setup:
Just to save a little time for new visitors here: the accepted answer now needs some changes.
Go to Settings -> Editor -> Live Templates, select others, add a template:
private static final org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger($CLASS_NAME$.class);$END$
Then, press Edit Variables on the left and set expression for CLASS_NAME to className().
After all, set context on the bottom to Java -> Declaration (and Groovy -> Declaration if desired).
Imports will be magically generated on insert.
Now its possible to add live templates with static imports:
You have to check static import in Options
#org.junit.Test
public void should$EXPR$when$CONDITION$() {
org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue()));
}
For apache commons logging use:
private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog($CLASS_NAME$.class);$END$

JSLint4Java usage

JSLint4Java is a Java wrapper for JSLint. I need something like this for use in my GWT project, but the only way to use JSLint4Java seems to be from command line or with an ANT task. Does anyone know if there is any way to just import the JARs and use them in a project? When I try adding them to the GWT WAR folder, I get lots of errors like 'xxx cannot be resolved to a type'.
Thanks.
Yes you can import the jar and use it like this :
import com.googlecode.jslint4java.Issue;
import com.googlecode.jslint4java.JSLint;
import com.googlecode.jslint4java.JSLintBuilder;
import com.googlecode.jslint4java.JSLintResult;
public static void lint(final String filePath) throws FileNotFoundException, IOException {
String fileContent = Streams.read(new FileInputStream(new File(filePath))));
JSLintBuilder builder = new JSLintBuilder();
JSLint jsLint = builder.fromDefault();
JSLintResult result = jsLint.lint("test.js", fileContent);
for (Issue issue : result.getIssues()) {
System.out.println(issue.toString());
}
}