Search in sources :

Example 1 with VisitException

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(")");
        }
    };
}
Also used : IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) GenericIdentityVisitor(org.whole.lang.visitors.GenericIdentityVisitor)

Example 2 with VisitException

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));
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) VisitException(org.whole.lang.visitors.VisitException) ScriptEngineManager(javax.script.ScriptEngineManager) BindingsAdapter(org.whole.lang.scripts.bindings.BindingsAdapter) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) Source(org.whole.lang.scripts.model.Source) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IBindingManager(org.whole.lang.bindings.IBindingManager) LanguageName(org.whole.lang.scripts.model.LanguageName) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 3 with VisitException

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();
}
Also used : VisitException(org.whole.lang.visitors.VisitException) File(java.io.File)

Example 4 with VisitException

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");
}
Also used : LocationURI(org.whole.lang.artifacts.model.LocationURI) VisitException(org.whole.lang.visitors.VisitException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) LocationURI(org.whole.lang.artifacts.model.LocationURI) URI(java.net.URI)

Example 5 with VisitException

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();
}
Also used : FilePersistenceProvider(org.whole.lang.codebase.FilePersistenceProvider) IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) IOException(java.io.IOException) File(java.io.File)

Aggregations

VisitException (org.whole.lang.visitors.VisitException)35 IEntity (org.whole.lang.model.IEntity)25 IBindingManager (org.whole.lang.bindings.IBindingManager)6 File (java.io.File)4 AbstractVisitor (org.whole.lang.visitors.AbstractVisitor)4 HashSet (java.util.HashSet)3 GenericIdentityVisitor (org.whole.lang.visitors.GenericIdentityVisitor)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)2 GenericTraversalFactory (org.whole.lang.visitors.GenericTraversalFactory)2 IVisitor (org.whole.lang.visitors.IVisitor)2 EvalError (bsh.EvalError)1 Interpreter (bsh.Interpreter)1 BufferedReader (java.io.BufferedReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1