Search in sources :

Example 16 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class SQLInterpreterVisitor method visit.

@Override
public void visit(SQLStatement entity) {
    try {
        IVisitor ov = getOperation().setVisitor(entity, 0, getOperation().getVisitor(entity, 1));
        getOperation().stagedVisit(entity);
        getOperation().setVisitor(entity, 0, ov);
        entity = (SQLStatement) getResult();
        String sql = toPrettyPrintString(entity);
        if (batchMode)
            statement.addBatch(sql);
        else {
            String[] names;
            if (entity.wGetEntityDescriptor().equals(SQLEntityDescriptorEnum.Select)) {
                IEntity columnExprs = entity.wGet(SQLFeatureDescriptorEnum.columnExprs);
                int size = columnExprs.wSize();
                names = new String[size];
                for (int i = 0; i < size; i++) {
                    IEntity columnExpr = columnExprs.wGet(i);
                    if (Matcher.matchImpl(SQLEntityDescriptorEnum.ColumnExpression, columnExpr)) {
                        IEntity alias = columnExpr.wGet(SQLFeatureDescriptorEnum.alias);
                        if (DataTypeUtils.getDataKind(alias).isString())
                            names[i] = alias.wStringValue();
                        else {
                            IEntity expr = columnExpr.wGet(SQLFeatureDescriptorEnum.expression);
                            if (Matcher.matchImpl(SQLEntityDescriptorEnum.ColumnName, expr))
                                names[i] = expr.wStringValue();
                        }
                    }
                }
            } else
                names = new String[0];
            if (statement.execute(sql))
                setResultIterator(new ResultSetIterator(statement.getResultSet(), names));
            else {
                setResult(null);
                statement.close();
            }
        }
    } catch (Exception e) {
        try {
            statement.close();
        } catch (Exception e1) {
        }
        throw new VisitException(SQL_INTERPRETER_ERROR_MESSAGE, e);
    } finally {
        if (!batchMode)
            statement = null;
    }
}
Also used : ResultSetIterator(org.whole.lang.sql.iterators.ResultSetIterator) IVisitor(org.whole.lang.visitors.IVisitor) IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) VisitException(org.whole.lang.visitors.VisitException)

Example 17 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class LegacyOperationsDeployer method deploy.

public void deploy(ReflectionFactory platform) {
    // TODO move to MappingStrategyRegistry?
    MappingStrategyRegistry.instance().putMappingStrategy(new CommonsMappingStrategy());
    InterpreterOperation.interpret(new SchemaActions().create());
    platform.addOperationFactory(XsdLanguageKit.URI, PrettyPrinterOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new XsdPrettyPrinterVisitor();
        }
    });
    // TODO rewrite using queries/actions in SchemaActions
    // platform.addOperationFactory(XsdLanguageKit.ID, ArtifactsGeneratorOperation.ID,
    // new IVisitorFactory() {
    // public IVisitor create(IOperation operation, int stage) {
    // return new Xsd2ModelsArtifactsGeneratorVisitor((ArtifactsGeneratorOperation) operation);
    // }
    // });
    InterpreterOperation.interpret(new MappingActions().create());
    // FIXME requires WorkflowsInterpreter see WorkflowsInterpreterDeployer
    XsdRegistry.initialize();
    platform.addPersistenceKit("org.whole.lang.xsd.XsdSourceEditor", XsdPersistenceKit.instance());
    platform.addPersistenceKit("org.whole.lang.xsd.XsiSourceEditor", XsiPersistenceKit.instance());
    InterpreterOperation.interpret(new SchemaModelsActions().create());
}
Also used : IOperation(org.whole.lang.operations.IOperation) SchemaActions(org.whole.lang.xsd.codebase.SchemaActions) CommonsMappingStrategy(org.whole.lang.xsd.codebase.CommonsMappingStrategy) XsdPrettyPrinterVisitor(org.whole.lang.xsd.visitors.XsdPrettyPrinterVisitor) IVisitor(org.whole.lang.visitors.IVisitor) MappingActions(org.whole.lang.xsd.codebase.MappingActions) SchemaModelsActions(org.whole.lang.xsd.codebase.SchemaModelsActions) IVisitorFactory(org.whole.lang.visitors.IVisitorFactory)

Example 18 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class TextInterpreterVisitor method visit.

@Override
public void visit(ITextEntity entity) {
    IVisitor ov = op().setVisitor(entity, 0, op().getVisitor(entity, 1));
    op().stagedVisit(entity);
    op().setVisitor(entity, 0, ov);
    IBindingManager bm = getBindings();
    bm.setResult(TextEntityFactory.instance.createText(toPrettyPrintString(bm.getResult())));
}
Also used : IVisitor(org.whole.lang.visitors.IVisitor) IBindingManager(org.whole.lang.bindings.IBindingManager)

Example 19 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class IteratorFactoryTest method testTopDownIterator.

@Test
public void testTopDownIterator() throws Exception {
    Grammar g = new TestXmlGrammar().create();
    final Productions productions = g.getPhraseStructure();
    final IEntityIterator<IEntity> ci = IteratorFactory.<IEntity>descendantOrSelfIterator();
    ci.reset(productions);
    IVisitor v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (ci.hasNext())
                assertSame(entity, ci.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    final IEntityIterator<IEntity> ci2 = IteratorFactory.<IEntity>descendantOrSelfScannerIterator();
    ci2.reset(productions);
    v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (EntityUtils.isResolver(entity))
                return;
            if (ci2.hasNext())
                assertSame(entity, ci2.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    final IEntityIterator<IEntity> ci3 = IteratorFactory.<IEntity>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci3.reset(productions);
    v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (!Matcher.match(GrammarsEntityDescriptorEnum.Production, entity))
                return;
            if (ci3.hasNext())
                assertSame(entity, ci3.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    IEntity artifactsModel = XmlBuilderPersistenceKit.instance().readModel(new ClasspathPersistenceProvider("org/whole/lang/artifacts/ArtifactsModel.xwl"));
    Set<Type> typeSet = new HashSet<Type>();
    IEntityIterator<Type> ci4 = IteratorFactory.<Type>descendantOrSelfMatcherIterator().withPattern(ModelsEntityFactory.instance.createSimpleName("Atifacts"));
    ci4.reset(artifactsModel);
    while (ci4.hasNext()) assertTrue(typeSet.add(ci4.next()));
}
Also used : Productions(org.whole.lang.grammars.model.Productions) Type(org.whole.lang.models.model.Type) IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) ClasspathPersistenceProvider(org.whole.lang.codebase.ClasspathPersistenceProvider) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) GenericIdentityVisitor(org.whole.lang.visitors.GenericIdentityVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class EntityMatchers method defined.

@Factory
public static <T> Matcher<IEntity> defined(IBindingManager bindings) {
    IVisitor v = TestsMatcherFactory.instance.defined();
    v.setBindings(bindings);
    return new VisitorMatcher(v);
}
Also used : IVisitor(org.whole.lang.visitors.IVisitor) TestsMatcherFactory(org.whole.lang.tests.matchers.TestsMatcherFactory) Factory(org.hamcrest.Factory) GenericMatcherFactory(org.whole.lang.matchers.GenericMatcherFactory)

Aggregations

IVisitor (org.whole.lang.visitors.IVisitor)63 IOperation (org.whole.lang.operations.IOperation)34 IVisitorFactory (org.whole.lang.visitors.IVisitorFactory)34 IBuilder (org.whole.lang.builders.IBuilder)20 IBuilderFactory (org.whole.lang.builders.IBuilderFactory)20 IEntityContext (org.whole.lang.contexts.IEntityContext)20 PrettyPrinterOperation (org.whole.lang.operations.PrettyPrinterOperation)13 IEntity (org.whole.lang.model.IEntity)9 ILanguageKit (org.whole.lang.reflect.ILanguageKit)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 IBindingManager (org.whole.lang.bindings.IBindingManager)3 Test (org.junit.Test)2 ITransactionScope (org.whole.lang.bindings.ITransactionScope)2 GenericMatcherFactory (org.whole.lang.matchers.GenericMatcherFactory)2 AbstractOperation (org.whole.lang.operations.AbstractOperation)2 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)2 VisitException (org.whole.lang.visitors.VisitException)2 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1