Search in sources :

Example 1 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase in project drools by kiegroup.

the class XStreamMarshaller method marshal.

@Override
public String marshal(Object o) {
    try (Writer writer = new StringWriter();
        CustomStaxWriter hsWriter = (CustomStaxWriter) staxDriver.createWriter(writer)) {
        XStream xStream = newXStream();
        if (o instanceof DMNModelInstrumentedBase) {
            KieDMNModelInstrumentedBase base = (KieDMNModelInstrumentedBase) o;
            String dmnPrefix = base.getNsContext().entrySet().stream().filter(kv -> KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())).findFirst().map(Map.Entry::getKey).orElse("");
            hsWriter.getQNameMap().setDefaultPrefix(dmnPrefix);
        }
        extensionRegisters.forEach(r -> r.beforeMarshal(o, hsWriter.getQNameMap()));
        xStream.marshal(o, hsWriter);
        hsWriter.flush();
        return writer.toString();
    } catch (Exception e) {
        logger.error("Error marshalling DMN model to XML.", e);
    }
    return null;
}
Also used : Transformer(javax.xml.transform.Transformer) XStream(com.thoughtworks.xstream.XStream) TImportedValues(org.kie.dmn.model.v1_1.TImportedValues) TInputClause(org.kie.dmn.model.v1_1.TInputClause) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) StreamResult(javax.xml.transform.stream.StreamResult) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) TFunctionDefinition(org.kie.dmn.model.v1_1.TFunctionDefinition) LoggerFactory(org.slf4j.LoggerFactory) TypeHierarchyPermission(com.thoughtworks.xstream.security.TypeHierarchyPermission) TKnowledgeSource(org.kie.dmn.model.v1_1.TKnowledgeSource) TImport(org.kie.dmn.model.v1_1.TImport) TInformationItem(org.kie.dmn.model.v1_1.TInformationItem) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) XStreamUtils(org.kie.utll.xml.XStreamUtils) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TElementCollection(org.kie.dmn.model.v1_1.TElementCollection) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) TDMNElementReference(org.kie.dmn.model.v1_1.TDMNElementReference) TDecisionTable(org.kie.dmn.model.v1_1.TDecisionTable) TDecisionRule(org.kie.dmn.model.v1_1.TDecisionRule) TTextAnnotation(org.kie.dmn.model.v1_1.TTextAnnotation) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) TDecisionService(org.kie.dmn.model.v1_1.TDecisionService) TNamedElement(org.kie.dmn.model.v1_1.TNamedElement) Reader(java.io.Reader) AbstractPullReader(com.thoughtworks.xstream.io.xml.AbstractPullReader) TOrganizationUnit(org.kie.dmn.model.v1_1.TOrganizationUnit) TInformationRequirement(org.kie.dmn.model.v1_1.TInformationRequirement) List(java.util.List) TExpression(org.kie.dmn.model.v1_1.TExpression) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) TBinding(org.kie.dmn.model.v1_1.TBinding) TBusinessContextElement(org.kie.dmn.model.v1_1.TBusinessContextElement) TOutputClause(org.kie.dmn.model.v1_1.TOutputClause) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) Writer(java.io.Writer) TRelation(org.kie.dmn.model.v1_1.TRelation) QName(javax.xml.namespace.QName) TBusinessKnowledgeModel(org.kie.dmn.model.v1_1.TBusinessKnowledgeModel) TAssociation(org.kie.dmn.model.v1_1.TAssociation) CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TKnowledgeRequirement(org.kie.dmn.model.v1_1.TKnowledgeRequirement) TLiteralExpression(org.kie.dmn.model.v1_1.TLiteralExpression) DMNMarshaller(org.kie.dmn.api.marshalling.v1_1.DMNMarshaller) Source(javax.xml.transform.Source) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TItemDefinition(org.kie.dmn.model.v1_1.TItemDefinition) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) TContextEntry(org.kie.dmn.model.v1_1.TContextEntry) TDecision(org.kie.dmn.model.v1_1.TDecision) OutputStreamWriter(java.io.OutputStreamWriter) XMLConstants(javax.xml.XMLConstants) OutputStream(java.io.OutputStream) TArtifact(org.kie.dmn.model.v1_1.TArtifact) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) TInvocation(org.kie.dmn.model.v1_1.TInvocation) TPerformanceIndicator(org.kie.dmn.model.v1_1.TPerformanceIndicator) TDMNElement(org.kie.dmn.model.v1_1.TDMNElement) OutputKeys(javax.xml.transform.OutputKeys) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) DMNExtensionRegister(org.kie.dmn.api.marshalling.DMNExtensionRegister) TContext(org.kie.dmn.model.v1_1.TContext) TAuthorityRequirement(org.kie.dmn.model.v1_1.TAuthorityRequirement) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) TInputData(org.kie.dmn.model.v1_1.TInputData) TUnaryTests(org.kie.dmn.model.v1_1.TUnaryTests) TransformerFactory(javax.xml.transform.TransformerFactory) StringWriter(java.io.StringWriter) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) XStream(com.thoughtworks.xstream.XStream) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) Map(java.util.Map) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) Writer(java.io.Writer) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase in project drools by kiegroup.

the class ExtensionElementsConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    DMNModelInstrumentedBase obj = createModelObject();
    assignAttributes(reader, obj);
    if (extensionRegisters.size() == 0) {
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            // skipping nodeName
            reader.moveUp();
        }
    } else {
        // do as default behavior, but in case cannot unmarshall an extension element child, just skip it.
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            try {
                Object object = readItem(reader, context, null);
                if (object instanceof DMNModelInstrumentedBase) {
                    ((KieDMNModelInstrumentedBase) object).setParent(obj);
                    ((KieDMNModelInstrumentedBase) obj).addChildren((KieDMNModelInstrumentedBase) object);
                }
                assignChildElement(obj, nodeName, object);
            } catch (CannotResolveClassException e) {
                // do nothing; I tried to convert the extension element child with the converters, but no converter is registered for this child.
                LOG.debug("Tried to convert the extension element child {}, but no converter is registered for this child.", nodeName);
            }
            reader.moveUp();
        }
    }
    return obj;
}
Also used : DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Example 3 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase in project drools by kiegroup.

the class DMNModelInstrumentedBaseConverter method assignAttributes.

@Override
protected void assignAttributes(HierarchicalStreamReader reader, Object parent) {
    KieDMNModelInstrumentedBase mib = (KieDMNModelInstrumentedBase) parent;
    CustomStaxReader customStaxReader = (CustomStaxReader) reader.underlyingReader();
    Map<String, String> currentNSCtx = customStaxReader.getNsContext();
    mib.getNsContext().putAll(currentNSCtx);
    mib.setLocation(customStaxReader.getLocation());
    mib.setAdditionalAttributes(customStaxReader.getAdditionalAttributes());
}
Also used : CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase)

Example 4 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase in project drools by kiegroup.

the class ExtensionElementsConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    DMNModelInstrumentedBase obj = createModelObject();
    assignAttributes(reader, obj);
    if (extensionRegisters.size() == 0) {
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            // skipping nodeName
            reader.moveUp();
        }
    } else {
        // do as default behavior, but in case cannot unmarshall an extension element child, just skip it.
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            try {
                Object object = readItem(reader, context, null);
                if (object instanceof DMNModelInstrumentedBase) {
                    ((KieDMNModelInstrumentedBase) object).setParent(obj);
                    ((KieDMNModelInstrumentedBase) obj).addChildren((KieDMNModelInstrumentedBase) object);
                }
                assignChildElement(obj, nodeName, object);
            } catch (CannotResolveClassException e) {
                // do nothing; I tried to convert the extension element child with the converters, but no converter is registered for this child.
                LOG.debug("Tried to convert the extension element child {}, but no converter is registered for this child.", nodeName);
            }
            reader.moveUp();
        }
    }
    return obj;
}
Also used : DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Example 5 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase in project drools by kiegroup.

the class XStreamMarshaller method marshal.

@Override
public String marshal(Object o) {
    try (Writer writer = new StringWriter();
        CustomStaxWriter hsWriter = (CustomStaxWriter) staxDriver.createWriter(writer)) {
        XStream xStream = newXStream();
        if (o instanceof DMNModelInstrumentedBase) {
            KieDMNModelInstrumentedBase base = (KieDMNModelInstrumentedBase) o;
            String dmnPrefix = base.getNsContext().entrySet().stream().filter(kv -> KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())).findFirst().map(Map.Entry::getKey).orElse("");
            hsWriter.getQNameMap().setDefaultPrefix(dmnPrefix);
        }
        extensionRegisters.forEach(r -> r.beforeMarshal(o, hsWriter.getQNameMap()));
        xStream.marshal(o, hsWriter);
        hsWriter.flush();
        return writer.toString();
    } catch (Exception e) {
        logger.error("Error marshalling DMN model to XML.", e);
    }
    return null;
}
Also used : TLiteralExpression(org.kie.dmn.model.v1_2.TLiteralExpression) XStream(com.thoughtworks.xstream.XStream) TKnowledgeSource(org.kie.dmn.model.v1_2.TKnowledgeSource) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) DMNEdge(org.kie.dmn.model.v1_2.dmndi.DMNEdge) TDMNElementReference(org.kie.dmn.model.v1_2.TDMNElementReference) TAuthorityRequirement(org.kie.dmn.model.v1_2.TAuthorityRequirement) TInputData(org.kie.dmn.model.v1_2.TInputData) LoggerFactory(org.slf4j.LoggerFactory) DMNStyle(org.kie.dmn.model.v1_2.dmndi.DMNStyle) TDecisionRule(org.kie.dmn.model.v1_2.TDecisionRule) TypeHierarchyPermission(com.thoughtworks.xstream.security.TypeHierarchyPermission) TArtifact(org.kie.dmn.model.v1_2.TArtifact) DMNShape(org.kie.dmn.model.v1_2.dmndi.DMNShape) XMLStreamReader(javax.xml.stream.XMLStreamReader) TRelation(org.kie.dmn.model.v1_2.TRelation) XStreamUtils(org.kie.utll.xml.XStreamUtils) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) TDecisionService(org.kie.dmn.model.v1_2.TDecisionService) DMNDiagram(org.kie.dmn.model.v1_2.dmndi.DMNDiagram) Dimension(org.kie.dmn.model.v1_2.dmndi.Dimension) TItemDefinition(org.kie.dmn.model.v1_2.TItemDefinition) TNamedElement(org.kie.dmn.model.v1_2.TNamedElement) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TOrganizationUnit(org.kie.dmn.model.v1_2.TOrganizationUnit) TInformationRequirement(org.kie.dmn.model.v1_2.TInformationRequirement) TInformationItem(org.kie.dmn.model.v1_2.TInformationItem) TTextAnnotation(org.kie.dmn.model.v1_2.TTextAnnotation) DiagramElement(org.kie.dmn.model.v1_2.dmndi.DiagramElement) TAssociation(org.kie.dmn.model.v1_2.TAssociation) Point(org.kie.dmn.model.v1_2.dmndi.Point) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) Reader(java.io.Reader) Color(org.kie.dmn.model.v1_2.dmndi.Color) AbstractPullReader(com.thoughtworks.xstream.io.xml.AbstractPullReader) TDecisionTable(org.kie.dmn.model.v1_2.TDecisionTable) List(java.util.List) DMNDI(org.kie.dmn.model.v1_2.dmndi.DMNDI) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) Writer(java.io.Writer) QName(javax.xml.namespace.QName) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) TInputClause(org.kie.dmn.model.v1_2.TInputClause) TUnaryTests(org.kie.dmn.model.v1_2.TUnaryTests) TContext(org.kie.dmn.model.v1_2.TContext) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions) TExpression(org.kie.dmn.model.v1_2.TExpression) TContextEntry(org.kie.dmn.model.v1_2.TContextEntry) Definitions(org.kie.dmn.model.api.Definitions) TRuleAnnotationClause(org.kie.dmn.model.v1_2.TRuleAnnotationClause) ArrayList(java.util.ArrayList) TBusinessKnowledgeModel(org.kie.dmn.model.v1_2.TBusinessKnowledgeModel) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) TImport(org.kie.dmn.model.v1_2.TImport) TElementCollection(org.kie.dmn.model.v1_2.TElementCollection) TOutputClause(org.kie.dmn.model.v1_2.TOutputClause) TRuleAnnotation(org.kie.dmn.model.v1_2.TRuleAnnotation) Bounds(org.kie.dmn.model.v1_2.dmndi.Bounds) TKnowledgeRequirement(org.kie.dmn.model.v1_2.TKnowledgeRequirement) TImportedValues(org.kie.dmn.model.v1_2.TImportedValues) TBinding(org.kie.dmn.model.v1_2.TBinding) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) TDMNElement(org.kie.dmn.model.v1_2.TDMNElement) TInvocation(org.kie.dmn.model.v1_2.TInvocation) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) DMNExtensionRegister(org.kie.dmn.api.marshalling.DMNExtensionRegister) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) DMNDecisionServiceDividerLine(org.kie.dmn.model.v1_2.dmndi.DMNDecisionServiceDividerLine) TFunctionDefinition(org.kie.dmn.model.v1_2.TFunctionDefinition) TDecision(org.kie.dmn.model.v1_2.TDecision) StringReader(java.io.StringReader) TBusinessContextElement(org.kie.dmn.model.v1_2.TBusinessContextElement) TPerformanceIndicator(org.kie.dmn.model.v1_2.TPerformanceIndicator) DMNLabel(org.kie.dmn.model.v1_2.dmndi.DMNLabel) StringWriter(java.io.StringWriter) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) XStream(com.thoughtworks.xstream.XStream) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) Map(java.util.Map) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) Writer(java.io.Writer) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

DMNModelInstrumentedBase (org.kie.dmn.model.api.DMNModelInstrumentedBase)14 QName (javax.xml.namespace.QName)8 CustomStaxReader (org.kie.dmn.backend.marshalling.CustomStaxReader)8 CustomStaxWriter (org.kie.dmn.backend.marshalling.CustomStaxWriter)8 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)6 KieDMNModelInstrumentedBase (org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase)6 KieDMNModelInstrumentedBase (org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase)6 KieDMNModelInstrumentedBase (org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase)5 KieDMNModelInstrumentedBase (org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase)5 XStream (com.thoughtworks.xstream.XStream)4 AbstractPullReader (com.thoughtworks.xstream.io.xml.AbstractPullReader)4 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)4 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)4 StaxWriter (com.thoughtworks.xstream.io.xml.StaxWriter)4 TypeHierarchyPermission (com.thoughtworks.xstream.security.TypeHierarchyPermission)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 ArrayList (java.util.ArrayList)4