Search in sources :

Example 6 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_2.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 : XStream(com.thoughtworks.xstream.XStream) DMNDecisionServiceDividerLine(org.kie.dmn.model.v1_3.dmndi.DMNDecisionServiceDividerLine) TContextEntry(org.kie.dmn.model.v1_4.TContextEntry) TImportedValues(org.kie.dmn.model.v1_4.TImportedValues) DMNDIConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNDIConverter) TBusinessKnowledgeModel(org.kie.dmn.model.v1_4.TBusinessKnowledgeModel) Bounds(org.kie.dmn.model.v1_3.dmndi.Bounds) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) TBusinessContextElement(org.kie.dmn.model.v1_4.TBusinessContextElement) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DMNStyleConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNStyleConverter) Point(org.kie.dmn.model.v1_3.dmndi.Point) TGroup(org.kie.dmn.model.v1_4.TGroup) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) Reader(java.io.Reader) TDMNElementReference(org.kie.dmn.model.v1_4.TDMNElementReference) TKnowledgeSource(org.kie.dmn.model.v1_4.TKnowledgeSource) TEvery(org.kie.dmn.model.v1_4.TEvery) TRelation(org.kie.dmn.model.v1_4.TRelation) DiagramElementExtensionConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DiagramElementExtensionConverter) TFunctionItem(org.kie.dmn.model.v1_4.TFunctionItem) TTextAnnotation(org.kie.dmn.model.v1_4.TTextAnnotation) DMNLabelConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNLabelConverter) QName(javax.xml.namespace.QName) TInputClause(org.kie.dmn.model.v1_4.TInputClause) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) TExpression(org.kie.dmn.model.v1_4.TExpression) TDefinitions(org.kie.dmn.model.v1_4.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) TItemDefinition(org.kie.dmn.model.v1_4.TItemDefinition) ArrayList(java.util.ArrayList) TInputData(org.kie.dmn.model.v1_4.TInputData) DMNEdgeConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNEdgeConverter) TElementCollection(org.kie.dmn.model.v1_4.TElementCollection) PointConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.PointConverter) DMNDecisionServiceDividerLineConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNDecisionServiceDividerLineConverter) StringWriter(java.io.StringWriter) TOrganizationUnit(org.kie.dmn.model.v1_4.TOrganizationUnit) DMNExtensionRegister(org.kie.dmn.api.marshalling.DMNExtensionRegister) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) TDecisionRule(org.kie.dmn.model.v1_4.TDecisionRule) StringReader(java.io.StringReader) TInformationItem(org.kie.dmn.model.v1_4.TInformationItem) TRuleAnnotation(org.kie.dmn.model.v1_4.TRuleAnnotation) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) TOutputClause(org.kie.dmn.model.v1_4.TOutputClause) LoggerFactory(org.slf4j.LoggerFactory) TLiteralExpression(org.kie.dmn.model.v1_4.TLiteralExpression) TypeHierarchyPermission(com.thoughtworks.xstream.security.TypeHierarchyPermission) BoundsConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.BoundsConverter) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase) TSome(org.kie.dmn.model.v1_4.TSome) XMLStreamReader(javax.xml.stream.XMLStreamReader) XStreamUtils(org.kie.utll.xml.XStreamUtils) TFilter(org.kie.dmn.model.v1_4.TFilter) TPerformanceIndicator(org.kie.dmn.model.v1_4.TPerformanceIndicator) TAuthorityRequirement(org.kie.dmn.model.v1_4.TAuthorityRequirement) TAssociation(org.kie.dmn.model.v1_4.TAssociation) Color(org.kie.dmn.model.v1_3.dmndi.Color) TRuleAnnotationClause(org.kie.dmn.model.v1_4.TRuleAnnotationClause) DimensionConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DimensionConverter) DMNEdge(org.kie.dmn.model.v1_3.dmndi.DMNEdge) AbstractPullReader(com.thoughtworks.xstream.io.xml.AbstractPullReader) TDecisionService(org.kie.dmn.model.v1_4.TDecisionService) List(java.util.List) DMNDI(org.kie.dmn.model.v1_3.dmndi.DMNDI) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) TContext(org.kie.dmn.model.v1_4.TContext) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) Writer(java.io.Writer) DMNDiagramConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNDiagramConverter) DiagramElement(org.kie.dmn.model.v1_3.dmndi.DiagramElement) TInformationRequirement(org.kie.dmn.model.v1_4.TInformationRequirement) ColorConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.ColorConverter) TKnowledgeRequirement(org.kie.dmn.model.v1_4.TKnowledgeRequirement) TFunctionDefinition(org.kie.dmn.model.v1_4.TFunctionDefinition) TUnaryTests(org.kie.dmn.model.v1_4.TUnaryTests) TTypedChildExpression(org.kie.dmn.model.v1_4.TTypedChildExpression) CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) Dimension(org.kie.dmn.model.v1_3.dmndi.Dimension) DMNShapeConverter(org.kie.dmn.backend.marshalling.v1_3.xstream.DMNShapeConverter) DMNShape(org.kie.dmn.model.v1_3.dmndi.DMNShape) TImport(org.kie.dmn.model.v1_4.TImport) TDecisionTable(org.kie.dmn.model.v1_4.TDecisionTable) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) TArtifact(org.kie.dmn.model.v1_4.TArtifact) TDecision(org.kie.dmn.model.v1_4.TDecision) DMNStyle(org.kie.dmn.model.v1_3.dmndi.DMNStyle) TNamedElement(org.kie.dmn.model.v1_4.TNamedElement) Logger(org.slf4j.Logger) DMNDiagram(org.kie.dmn.model.v1_3.dmndi.DMNDiagram) TDMNElement(org.kie.dmn.model.v1_4.TDMNElement) TChildExpression(org.kie.dmn.model.v1_4.TChildExpression) TInvocation(org.kie.dmn.model.v1_4.TInvocation) DMNLabel(org.kie.dmn.model.v1_3.dmndi.DMNLabel) TBinding(org.kie.dmn.model.v1_4.TBinding) TConditional(org.kie.dmn.model.v1_4.TConditional) TFor(org.kie.dmn.model.v1_4.TFor) 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_4.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase) Map(java.util.Map) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringWriter(java.io.StringWriter) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) Writer(java.io.Writer) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 7 with KieDMNModelInstrumentedBase

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

the class DMNModelInstrumentedBaseConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    KieDMNModelInstrumentedBase mib = (KieDMNModelInstrumentedBase) parent;
    CustomStaxWriter staxWriter = ((CustomStaxWriter) writer.underlyingWriter());
    for (Entry<String, String> kv : mib.getNsContext().entrySet()) {
        try {
            if (KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())) {
            // skip as that is the default namespace xmlns<:prefix>=DMN is handled by the stax driver.
            } else {
                staxWriter.writeNamespace(kv.getKey(), kv.getValue());
            }
        } catch (Exception e) {
            LOG.warn("The XML driver writer failed to manage writing namespace, namespaces prefixes could be wrong in the resulting file.", e);
        }
    }
    for (Entry<QName, String> kv : mib.getAdditionalAttributes().entrySet()) {
        staxWriter.addAttribute(kv.getKey().getPrefix() + ":" + kv.getKey().getLocalPart(), kv.getValue());
    }
    if (parent instanceof TDefinitions) {
        TDefinitions tDefinitions = (TDefinitions) parent;
        String dmndiPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DMNDI).orElse("dmndi");
        String diPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DI).orElse("di");
        String dcPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DC).orElse("dc");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDI", dmndiPrefix), "DMNDI");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDiagram", dmndiPrefix), "DMNDiagram");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "style");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "DMNStyle");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNShape", dmndiPrefix), "DMNShape");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNEdge", dmndiPrefix), "DMNEdge");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDecisionServiceDividerLine", dmndiPrefix), "DMNDecisionServiceDividerLine");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNLabel", dmndiPrefix), "DMNLabel");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, DMNLabelConverter.TEXT, dmndiPrefix), DMNLabelConverter.TEXT);
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "Size", dmndiPrefix), "Size");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FillColor", dmndiPrefix), "FillColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "StrokeColor", dmndiPrefix), "StrokeColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FontColor", dmndiPrefix), "FontColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "waypoint", diPrefix), "waypoint");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "extension", diPrefix), "extension");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DC, "Bounds", dcPrefix), "Bounds");
    }
}
Also used : CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) QName(javax.xml.namespace.QName) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase) TDefinitions(org.kie.dmn.model.v1_4.TDefinitions)

Example 8 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_2.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_4.KieDMNModelInstrumentedBase)

Example 9 with KieDMNModelInstrumentedBase

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

the class DecisionServiceConverter method parseElements.

@Override
protected void parseElements(HierarchicalStreamReader reader, UnmarshallingContext context, Object parent) {
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        Object object = null;
        String nodeName = reader.getNodeName();
        if (nodeName.equals(INPUT_DATA)) {
            // Patch because the tag name inputData is used in both decision services and as a DRG Element
            DMNElementReference ref = new TDMNElementReference();
            ref.setHref(reader.getAttribute("href"));
            object = ref;
        } else {
            // Default behaviour
            object = readItem(reader, context, null);
        }
        if (object instanceof DMNModelInstrumentedBase) {
            ((KieDMNModelInstrumentedBase) object).setParent((KieDMNModelInstrumentedBase) parent);
            ((KieDMNModelInstrumentedBase) parent).addChildren((KieDMNModelInstrumentedBase) object);
        }
        reader.moveUp();
        assignChildElement(parent, nodeName, object);
    }
}
Also used : DMNElementReference(org.kie.dmn.model.api.DMNElementReference) TDMNElementReference(org.kie.dmn.model.v1_4.TDMNElementReference) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase) TDMNElementReference(org.kie.dmn.model.v1_4.TDMNElementReference) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase)

Example 10 with KieDMNModelInstrumentedBase

use of org.kie.dmn.model.v1_2.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_4.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Aggregations

DMNModelInstrumentedBase (org.kie.dmn.model.api.DMNModelInstrumentedBase)14 QName (javax.xml.namespace.QName)9 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 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 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