Search in sources :

Example 6 with ModelTemplate

use of org.whole.lang.templates.ModelTemplate in project whole by wholeplatform.

the class XsdModelBuilderTest method testBuilderAPI.

@Category(KnownFailingTests.class)
@Test
public void testBuilderAPI() throws Exception {
    Schema xsdFromInputStream = (Schema) XsdPersistenceKit.instance().readModel(new StreamPersistenceProvider(getInputStream("datatypes.xsd")));
    Document document = (Document) XmlSourcePersistenceKit.instance().readModel(new StreamPersistenceProvider(getInputStream("datatypes.xsd")));
    IBindingManager bm = BindingManagerFactory.instance.createBindingManager();
    bm.wDefValue("mergeCDataSect", true);
    XsdDeployStrategies.setDeployStrategy(bm, XsdDeployStrategies.ALL);
    ModelBuilderOperation mop = new ModelBuilderOperation();
    IBuilderOperation xsdBuilderOperation = new XmlSpecific2XsdBuilderAdapterOperation(mop, bm);
    XmlNormalizerBuilderOperation normalizerOp = new XmlNormalizerBuilderOperation(xsdBuilderOperation, bm);
    ModelTemplate template = new ModelTemplate(document);
    template.apply(new XmlGenericBuilderAdapter((IXmlBuilder) normalizerOp.wGetBuilder(XmlLanguageKit.URI)));
    Schema xsdFromXmlModel = (Schema) mop.wGetResult();
    Assert.assertTrue(Matcher.match(xsdFromInputStream, xsdFromXmlModel));
}
Also used : ModelBuilderOperation(org.whole.lang.builders.ModelBuilderOperation) ModelTemplate(org.whole.lang.templates.ModelTemplate) IBuilderOperation(org.whole.lang.builders.IBuilderOperation) XmlNormalizerBuilderOperation(org.whole.lang.xml.codebase.XmlNormalizerBuilderOperation) XmlGenericBuilderAdapter(org.whole.lang.xml.builders.XmlGenericBuilderAdapter) Schema(org.whole.lang.xsd.model.Schema) IBindingManager(org.whole.lang.bindings.IBindingManager) IXmlBuilder(org.whole.lang.xml.builders.IXmlBuilder) StreamPersistenceProvider(org.whole.lang.codebase.StreamPersistenceProvider) Document(org.whole.lang.xml.model.Document) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 7 with ModelTemplate

use of org.whole.lang.templates.ModelTemplate in project whole by wholeplatform.

the class XmlBuilderPersistenceKit method doWriteModel.

protected void doWriteModel(IEntity model, IPersistenceProvider pp) throws Exception {
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(pp.getOutputStream(), pp.getEncoding()));
    XmlPrettyPrinterBuilderOperation op = new XmlPrettyPrinterBuilderOperation(printWriter);
    XmlStoreProducerBuilderOperation xmlOp = new XmlStoreProducerBuilderOperation(op);
    ((XmlStoreProducerBuilder) xmlOp.wGetBuilder()).buildStartDocument();
    new ModelTemplate(model).apply(xmlOp);
    ((XmlStoreProducerBuilder) xmlOp.wGetBuilder()).buildEndDocument();
    printWriter.flush();
}
Also used : ModelTemplate(org.whole.lang.templates.ModelTemplate) XmlStoreProducerBuilder(org.whole.lang.xml.util.XmlStoreProducerBuilder) OutputStreamWriter(java.io.OutputStreamWriter) XmlStoreProducerBuilderOperation(org.whole.lang.xml.util.XmlStoreProducerBuilderOperation) PrintWriter(java.io.PrintWriter)

Example 8 with ModelTemplate

use of org.whole.lang.templates.ModelTemplate in project whole by wholeplatform.

the class JavaBuilderPersistenceKit method doWriteModel.

protected void doWriteModel(IEntity model, IPersistenceProvider pp) throws Exception {
    IBindingManager bm = pp.getBindings();
    if (bm.wIsSet("file")) {
        Object file = bm.wGetValue("file");
        if (file instanceof File)
            bm.wDefValue("className", StringUtils.stripFileExtension(((File) file).getName()));
    }
    if (// TODO test was !bm.wIsSet("className") &&
    bm.wIsSet("fileName"))
        bm.wDefValue("className", bm.wStringValue("fileName"));
    IEntity packageNameEntity = bm.wGet("packageName");
    String packageName = packageNameEntity != null ? packageNameEntity.wStringValue() : null;
    IEntity classNameEntity = bm.wGet("className");
    String className = classNameEntity != null ? bm.wStringValue("className") : "AnonymousClass";
    ModelBuilderOperation op = new ModelBuilderOperation();
    JavaStoreProducerBuilderOperation javaOp = new JavaStoreProducerBuilderOperation(op);
    ((JavaStoreProducerBuilder) javaOp.wGetBuilder()).buildStartCompilationUnit(packageName, className, model.wGetEntityDescriptor().getEntityType().getName());
    new ModelTemplate(model).apply(javaOp);
    ((JavaStoreProducerBuilder) javaOp.wGetBuilder()).buildEndCompilationUnit();
    IEntity javaModel = op.wGetResult();
    PrettyPrinterOperation.prettyPrint(javaModel, pp.getOutputStream(), pp.getEncoding());
}
Also used : ModelBuilderOperation(org.whole.lang.builders.ModelBuilderOperation) ModelTemplate(org.whole.lang.templates.ModelTemplate) JavaStoreProducerBuilder(org.whole.lang.java.util.JavaStoreProducerBuilder) IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) JavaStoreProducerBuilderOperation(org.whole.lang.java.util.JavaStoreProducerBuilderOperation) File(java.io.File)

Example 9 with ModelTemplate

use of org.whole.lang.templates.ModelTemplate in project whole by wholeplatform.

the class HtmlPrettyPrinterVisitor method visit.

public void visit(IEntity entity) {
    IEntity xhtmlDocument = BehaviorUtils.apply("whole:org.whole.lang.html:HTML5Semantics#toXHtml", entity);
    HtmlSerializer serializer = new HtmlSerializer(out.asWriter());
    SaxHandlerBuilderOperation sbop = new SaxHandlerBuilderOperation(serializer, serializer, "http://www.w3.org/1999/xhtml");
    if (xhtmlDocument != null)
        new ModelTemplate(xhtmlDocument).apply(sbop);
}
Also used : ModelTemplate(org.whole.lang.templates.ModelTemplate) IEntity(org.whole.lang.model.IEntity) HtmlSerializer(nu.validator.htmlparser.sax.HtmlSerializer) SaxHandlerBuilderOperation(org.whole.lang.xml.util.SaxHandlerBuilderOperation)

Example 10 with ModelTemplate

use of org.whole.lang.templates.ModelTemplate in project whole by wholeplatform.

the class XsiModelTemplate method visit.

public void visit(IEntity entity) {
    IEntity adaptee = entity.wGetAdaptee(false);
    EntityDescriptor<?> adapteeEd = adaptee.wGetEntityDescriptor();
    if (adapteeEd.getLanguageKit().getURI().equals(CommonsLanguageKit.URI)) {
        switch(adapteeEd.getOrdinal()) {
            case CommonsEntityDescriptorEnum.Resolver_ord:
                return;
            case CommonsEntityDescriptorEnum.SameStageFragment_ord:
            case CommonsEntityDescriptorEnum.RootFragment_ord:
            case CommonsEntityDescriptorEnum.StageDownFragment_ord:
            case CommonsEntityDescriptorEnum.StageUpFragment_ord:
                // String lang = entity.wGetLanguageKit().getURI();
                IEntity root = entity.wGetRoot();
                if (XmlLanguageKit.URI.equals(root.wGetLanguageKit().getURI())) {
                    ModelTemplate template = new ModelTemplate(root);
                    template.apply(new XmlGenericBuilderAdapter(builder));
                } else
                    visit(root);
                return;
        }
    }
    EntityDescriptor<?> ed = entity.wGetEntityDescriptor();
    EntityDescriptor<?> context;
    FeatureDescriptor fd;
    String languageURI;
    IEntity parent = entity.wGetParent();
    if (EntityUtils.isNull(parent) || CommonsEntityDescriptorEnum.RootFragment.equals(parent.wGetEntityDescriptor()) || CommonsEntityDescriptorEnum.StageUpFragment.equals(parent.wGetEntityDescriptor())) {
        context = CommonsEntityDescriptorEnum.RootFragment;
        fd = CommonsFeatureDescriptorEnum.rootEntity;
        // get languageURI from parent entity descriptor but from RootFragment
        languageURI = ed.getLanguageKit().getURI();
    } else {
        context = parent.wGetEntityDescriptor();
        // always skip SameStageFragment parent
        if (CommonsEntityDescriptorEnum.SameStageFragment.equals(context)) {
            IEntity parentParent = parent.wGetParent();
            context = parentParent.wGetEntityDescriptor();
            fd = parentParent.wGetFeatureDescriptor(parent);
            languageURI = context.getLanguageKit().getURI();
        } else {
            fd = parent.wGetFeatureDescriptor(entity);
            languageURI = parent.wGetEntityDescriptor().getLanguageKit().getURI();
        }
    }
    IMappingStrategy strategy = getXsiMappingStrategy(languageURI);
    // if (!hasNCName(strategy, context, ed, fd)) {
    if (getElementNCName(entity) == null) {
        if (isMixedType(strategy, context, ed))
            builder.CharData(toContentValue(entity, strategy));
        else {
            final int size = entity.wSize();
            for (int i = 0; i < size; i++) visit(entity.wGet(i));
        }
    } else {
        nsPrefixes.wEnterScope();
        boolean needNamespacePrefix = (strategy.isElementsFormQualified()) && !NamespaceUtils.isInternalNamespace(languageURI);
        boolean needNamespaceDeclaration = needNamespacePrefix && !hasNamespacePrefix(entity, strategy.getNamespace());
        builder.Element_();
        String tagName = getElementNCName(entity);
        if (needNamespacePrefix) {
            String prefix = getNamespacePrefix(entity, languageURI);
            builder.QualifiedName_();
            builder.NameSpace(prefix);
            builder.Name(tagName);
            builder._QualifiedName();
        } else
            builder.Name(tagName);
        builder.Attributes_();
        if (needNamespaceDeclaration)
            addNamespaceDeclaration(entity, languageURI);
        if (needSchemaLocationDeclaration(strategy, context, ed, fd))
            addSchemaLocationDeclaration(entity, languageURI);
        if (entity.wGetEntityKind().isData()) {
            builder._Attributes();
            builder.CharData(toContentValue(entity, strategy));
        } else {
            applyAttributes(entity, 0);
            builder._Attributes();
            builder.Content_();
            final int size = entity.wSize();
            for (int i = 0; i < size; i++) {
                context = entity.wGetEntityDescriptor();
                fd = context.getEntityFeatureDescriptor(i);
                IEntity child = entity.wGet(i);
                ed = child.wGetEntityDescriptor();
                if (!isAttributeMapping(strategy, context, ed, fd)) {
                    if (isContentMapping(strategy, context, ed, fd))
                        builder.CharData(toContentValue(child, strategy));
                    else
                        visit(child);
                }
            }
            builder._Content();
        }
        builder._Element();
        nsPrefixes.wExitScope();
    }
}
Also used : ModelTemplate(org.whole.lang.templates.ModelTemplate) IMappingStrategy(org.whole.lang.xsd.codebase.IMappingStrategy) IEntity(org.whole.lang.model.IEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) XmlGenericBuilderAdapter(org.whole.lang.xml.builders.XmlGenericBuilderAdapter)

Aggregations

ModelTemplate (org.whole.lang.templates.ModelTemplate)15 IEntity (org.whole.lang.model.IEntity)11 Test (org.junit.Test)7 ModelBuilderOperation (org.whole.lang.builders.ModelBuilderOperation)6 StreamPersistenceProvider (org.whole.lang.codebase.StreamPersistenceProvider)4 XmlStoreProducerBuilderOperation (org.whole.lang.xml.util.XmlStoreProducerBuilderOperation)4 PrintWriter (java.io.PrintWriter)3 XmlStoreProducerBuilder (org.whole.lang.xml.util.XmlStoreProducerBuilder)3 IOException (java.io.IOException)2 IBindingManager (org.whole.lang.bindings.IBindingManager)2 JavaStoreProducerBuilder (org.whole.lang.java.util.JavaStoreProducerBuilder)2 JavaStoreProducerBuilderOperation (org.whole.lang.java.util.JavaStoreProducerBuilderOperation)2 ModelsModel (org.whole.lang.models.codebase.ModelsModel)2 XmlGenericBuilderAdapter (org.whole.lang.xml.builders.XmlGenericBuilderAdapter)2 XmlPrettyPrinterBuilderOperation (org.whole.lang.xml.codebase.XmlPrettyPrinterBuilderOperation)2 Document (org.whole.lang.xml.model.Document)2 SaxHandlerBuilderOperation (org.whole.lang.xml.util.SaxHandlerBuilderOperation)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 File (java.io.File)1