Search in sources :

Example 1 with XmlGenericBuilderAdapter

use of org.whole.lang.xml.builders.XmlGenericBuilderAdapter 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 2 with XmlGenericBuilderAdapter

use of org.whole.lang.xml.builders.XmlGenericBuilderAdapter in project whole by wholeplatform.

the class XmlLanguageDeployer method deploy.

public void deploy(ReflectionFactory platform) {
    platform.addLanguageKit(new XmlLanguageKit());
    platform.addOperationFactory(XmlLanguageKit.URI, GenericBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new XmlGenericBuilderAdapter((IXmlBuilder) strategy, entityContext);
        }
    });
    platform.addOperationFactory(XmlLanguageKit.URI, SpecificBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new XmlSpecificBuilderAdapter(strategy, entityContext);
        }
    });
    platform.addOperationFactory(XmlLanguageKit.URI, PrettyPrinterOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new XmlPrettyPrinterVisitor((PrettyPrinterOperation) operation);
        }
    });
    platform.addOperationFactory(XmlLanguageKit.URI, NormalizerOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new XmlNormalizerVisitor();
        }
    });
    platform.addPersistenceKit("org.whole.lang.xml.XmlBuilderEditor", XmlBuilderPersistenceKit.instance());
    platform.addPersistenceKit("org.whole.lang.xml.XmlSourceEditor", XmlSourcePersistenceKit.instance());
}
Also used : IBuilder(org.whole.lang.builders.IBuilder) IOperation(org.whole.lang.operations.IOperation) PrettyPrinterOperation(org.whole.lang.operations.PrettyPrinterOperation) XmlNormalizerVisitor(org.whole.lang.xml.visitors.XmlNormalizerVisitor) IVisitor(org.whole.lang.visitors.IVisitor) IEntityContext(org.whole.lang.contexts.IEntityContext) XmlGenericBuilderAdapter(org.whole.lang.xml.builders.XmlGenericBuilderAdapter) IBuilderFactory(org.whole.lang.builders.IBuilderFactory) XmlPrettyPrinterVisitor(org.whole.lang.xml.visitors.XmlPrettyPrinterVisitor) IVisitorFactory(org.whole.lang.visitors.IVisitorFactory) XmlSpecificBuilderAdapter(org.whole.lang.xml.builders.XmlSpecificBuilderAdapter) IXmlBuilder(org.whole.lang.xml.builders.IXmlBuilder)

Example 3 with XmlGenericBuilderAdapter

use of org.whole.lang.xml.builders.XmlGenericBuilderAdapter 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

XmlGenericBuilderAdapter (org.whole.lang.xml.builders.XmlGenericBuilderAdapter)3 ModelTemplate (org.whole.lang.templates.ModelTemplate)2 IXmlBuilder (org.whole.lang.xml.builders.IXmlBuilder)2 Test (org.junit.Test)1 Category (org.junit.experimental.categories.Category)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 IBuilder (org.whole.lang.builders.IBuilder)1 IBuilderFactory (org.whole.lang.builders.IBuilderFactory)1 IBuilderOperation (org.whole.lang.builders.IBuilderOperation)1 ModelBuilderOperation (org.whole.lang.builders.ModelBuilderOperation)1 StreamPersistenceProvider (org.whole.lang.codebase.StreamPersistenceProvider)1 IEntityContext (org.whole.lang.contexts.IEntityContext)1 IEntity (org.whole.lang.model.IEntity)1 IOperation (org.whole.lang.operations.IOperation)1 PrettyPrinterOperation (org.whole.lang.operations.PrettyPrinterOperation)1 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)1 IVisitor (org.whole.lang.visitors.IVisitor)1 IVisitorFactory (org.whole.lang.visitors.IVisitorFactory)1 XmlSpecificBuilderAdapter (org.whole.lang.xml.builders.XmlSpecificBuilderAdapter)1 XmlNormalizerBuilderOperation (org.whole.lang.xml.codebase.XmlNormalizerBuilderOperation)1