use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class TestsMatcherFactory method same.
public IVisitor same(final IEntity asEntity) {
return new GenericIdentityVisitor() {
@Override
public void visit(IEntity entity) {
if (entity != asEntity)
throw new VisitException();
}
public void toString(StringBuilder sb) {
sb.append("same(");
sb.append(asEntity);
sb.append(")");
}
};
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class ScriptsInterpreterVisitor method visit.
@Override
public void visit(Script entity) {
Writer outputWriter = op().getWriter();
IBindingManager bm = getBindings();
BindingsAdapter bindingsAdapter = new BindingsAdapter(bm, true);
LanguageName languageName = entity.getLanguageName();
/*
* TODO
* if contains an upper level fragment fail
*/
Source source = entity.getSource();
IEntity sourceRoot = EntityUtils.isFragment(source) ? source.wGetRoot() : source;
IEntity result0 = bm.getResult();
IVisitor ov = op().setVisitor(sourceRoot, 0, op().getVisitor(sourceRoot, 1));
op().stagedVisit(source);
IEntity sourceModel = bm.getResult();
// workaround restore previous value (also for self and queryIterator?)
bm.setResult(result0);
op().setVisitor(sourceRoot, 0, ov);
String sourceString = toPrettyPrintString(sourceModel);
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(languageName.getValue());
if (scriptEngine == null)
throw new VisitException("Cannot find an interpreter for the language: " + languageName);
scriptEngine.setBindings(bindingsAdapter, ScriptContext.ENGINE_SCOPE);
PrintWriter printWriter = new PrintWriter(outputWriter);
scriptEngine.getContext().setWriter(printWriter);
scriptEngine.getContext().setErrorWriter(printWriter);
Object result = null;
bm.wEnterScope();
do {
try {
result = scriptEngine.eval(sourceString);
break;
} catch (ScriptException e) {
bm.wExitScope();
if (!bindingsAdapter.isAutoboxing())
throw new VisitException("Script Interpreter failure.", e);
bindingsAdapter.setAutoboxing(false);
bm.wEnterScope();
}
} while (true);
bm.wExitScope(true);
if (result instanceof IEntity)
bm.setResult((IEntity) result);
else if (result != null)
bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class ResourceArtifactsGeneratorVisitor method visit.
public void visit(FolderArtifact entity) {
env().wEnterScope();
if (!Matcher.matchImplAndBind(ArtifactsEntityDescriptorEnum.Name, entity.getName(), env(), "folderName"))
throw new VisitException("No Folder name");
entity.getMetadata().accept(this);
File parentFolder = getParentFolder();
String path = parentFolder.getAbsolutePath();
String folderPath = path + File.separatorChar + env().wStringValue("folderName").replace('/', File.separatorChar);
File folder = new File(folderPath);
if (!folder.exists())
folder.mkdirs();
env().wDefValue("folder", folder);
if (env().wIsSet("derived"))
// not implemented yet
;
if (env().wIsSet("readonly") && folder.canWrite()) {
folder.setReadOnly();
}
if (env().wLocalNames().contains("source") && env().wIsSet("javaProject"))
// not implemented yet
;
entity.getArtifacts().accept(this);
env().wExitScope();
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class ResourceArtifactsGeneratorVisitor method visit.
public void visit(Project entity) {
LocationURI locationURI = entity.getLocationURI();
if (DataTypeUtils.getDataKind(locationURI).isString() || env().wIsSet("folder") || env().wIsSet("folderLocation")) {
env().wEnterScope();
if (!Matcher.matchImplAndBind(ArtifactsEntityDescriptorEnum.Name, entity.getName(), env(), "projectName"))
throw new VisitException("No project name");
entity.getMetadata().accept(this);
File folder;
if (DataTypeUtils.getDataKind(locationURI).isString()) {
try {
folder = new File(new URI(locationURI.getValue()));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid project location URI specified", e);
}
} else {
File parentFolder = getParentFolder();
String path = parentFolder.getAbsolutePath();
String projectPath = path + File.separatorChar + env().wStringValue("projectName");
folder = new File(projectPath);
}
if (!folder.exists())
folder.mkdirs();
env().wDefValue("folder", folder);
entity.getNatures().accept(this);
entity.getArtifacts().accept(this);
env().wExitScope();
} else
throw new IllegalStateException("Project generation not supported yet");
}
use of org.whole.lang.visitors.VisitException in project whole by wholeplatform.
the class ResourceArtifactsGeneratorVisitor method visit.
public void visit(FileArtifact entity) {
env().wEnterScope();
entity.getName().accept(this);
if (!env().wIsSet("name"))
throw new VisitException("No File name");
String fileNameWithExtension = env().wStringValue("name");
env().wDefValue("fileNameWithExtension", fileNameWithExtension);
env().wDefValue("fileName", StringUtils.stripFileExtension(fileNameWithExtension));
env().wDefValue("fileExtension", StringUtils.getFileExtension(fileNameWithExtension));
if (fileNameWithExtension.indexOf(File.separatorChar) != -1 || fileNameWithExtension.indexOf('/') != -1)
throw new VisitException("Invalid File name");
entity.getMetadata().accept(this);
File parentFolder = getParentFolder();
File file = new File(parentFolder.getAbsolutePath() + File.separatorChar + fileNameWithExtension);
try {
if (!file.exists())
file.createNewFile();
env().wDefValue("persistenceProvider", new FilePersistenceProvider(file, env()));
IEntity result = InterpreterOperation.interpret(entity.getContent(), env()).getResult();
if (result != null)
writeContents(result);
} catch (IOException e) {
throw new VisitException(e);
}
if (env().wIsSet("derived"))
// not implemented yet
;
if (env().wIsSet("readonly") && file.canWrite())
file.setReadOnly();
env().wExitScope();
}
Aggregations