Search in sources :

Example 1 with CustomStaxWriter

use of org.kie.dmn.backend.marshalling.CustomStaxWriter 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) {
            DMNModelInstrumentedBase base = (DMNModelInstrumentedBase) o;
            String dmnPrefix = base.getNsContext().entrySet().stream().filter(kv -> DMNModelInstrumentedBase.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) KnowledgeSource(org.kie.dmn.model.v1_1.KnowledgeSource) Transformer(javax.xml.transform.Transformer) InformationItem(org.kie.dmn.model.v1_1.InformationItem) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) OutputClause(org.kie.dmn.model.v1_1.OutputClause) StreamResult(javax.xml.transform.stream.StreamResult) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) LoggerFactory(org.slf4j.LoggerFactory) TextAnnotation(org.kie.dmn.model.v1_1.TextAnnotation) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) InformationRequirement(org.kie.dmn.model.v1_1.InformationRequirement) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) Map(java.util.Map) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Binding(org.kie.dmn.model.v1_1.Binding) DMNModelInstrumentedBase(org.kie.dmn.model.v1_1.DMNModelInstrumentedBase) DecisionTable(org.kie.dmn.model.v1_1.DecisionTable) ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) Reader(java.io.Reader) AbstractPullReader(com.thoughtworks.xstream.io.xml.AbstractPullReader) Context(org.kie.dmn.model.v1_1.Context) List(java.util.List) Definitions(org.kie.dmn.model.v1_1.Definitions) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) BusinessContextElement(org.kie.dmn.model.v1_1.BusinessContextElement) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) DecisionService(org.kie.dmn.model.v1_1.DecisionService) Invocation(org.kie.dmn.model.v1_1.Invocation) Writer(java.io.Writer) DMNExtensionRegister(org.kie.dmn.api.marshalling.v1_1.DMNExtensionRegister) QName(javax.xml.namespace.QName) BusinessKnowledgeModel(org.kie.dmn.model.v1_1.BusinessKnowledgeModel) DMNElement(org.kie.dmn.model.v1_1.DMNElement) CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Association(org.kie.dmn.model.v1_1.Association) DMNMarshaller(org.kie.dmn.api.marshalling.v1_1.DMNMarshaller) Source(javax.xml.transform.Source) InputData(org.kie.dmn.model.v1_1.InputData) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ArrayList(java.util.ArrayList) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) Relation(org.kie.dmn.model.v1_1.Relation) DecisionRule(org.kie.dmn.model.v1_1.DecisionRule) UnaryTests(org.kie.dmn.model.v1_1.UnaryTests) OrganizationUnit(org.kie.dmn.model.v1_1.OrganizationUnit) Expression(org.kie.dmn.model.v1_1.Expression) OutputStreamWriter(java.io.OutputStreamWriter) KnowledgeRequirement(org.kie.dmn.model.v1_1.KnowledgeRequirement) OutputStream(java.io.OutputStream) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) OutputKeys(javax.xml.transform.OutputKeys) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) Artifact(org.kie.dmn.model.v1_1.Artifact) AuthorityRequirement(org.kie.dmn.model.v1_1.AuthorityRequirement) DMNElementReference(org.kie.dmn.model.v1_1.DMNElementReference) ImportedValues(org.kie.dmn.model.v1_1.ImportedValues) Decision(org.kie.dmn.model.v1_1.Decision) ElementCollection(org.kie.dmn.model.v1_1.ElementCollection) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) NamedElement(org.kie.dmn.model.v1_1.NamedElement) Import(org.kie.dmn.model.v1_1.Import) InputClause(org.kie.dmn.model.v1_1.InputClause) PerformanceIndicator(org.kie.dmn.model.v1_1.PerformanceIndicator) StringWriter(java.io.StringWriter) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) DMNModelInstrumentedBase(org.kie.dmn.model.v1_1.DMNModelInstrumentedBase) 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 CustomStaxWriter

use of org.kie.dmn.backend.marshalling.CustomStaxWriter in project drools by kiegroup.

the class QNameConverter method marshal.

@Override
public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
    QName qname = (QName) object;
    if (!XMLConstants.NULL_NS_URI.equals(qname.getNamespaceURI()) && !XMLConstants.DEFAULT_NS_PREFIX.equals(qname.getPrefix())) {
        CustomStaxWriter staxWriter = ((CustomStaxWriter) writer.underlyingWriter());
        try {
            staxWriter.writeNamespace(qname.getPrefix(), qname.getNamespaceURI());
        } catch (XMLStreamException e) {
            // TODO what to do?
            e.printStackTrace();
        }
    }
    writer.setValue(MarshallingUtils.formatQName(qname));
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) QName(javax.xml.namespace.QName)

Example 3 with CustomStaxWriter

use of org.kie.dmn.backend.marshalling.CustomStaxWriter in project drools by kiegroup.

the class DMNModelInstrumentedBaseConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    DMNModelInstrumentedBase mib = (DMNModelInstrumentedBase) parent;
    CustomStaxWriter staxWriter = ((CustomStaxWriter) writer.underlyingWriter());
    for (Entry<String, String> kv : mib.getNsContext().entrySet()) {
        try {
            if (DMNModelInstrumentedBase.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) {
            // TODO what to do?
            e.printStackTrace();
        }
    }
    for (Entry<QName, String> kv : mib.getAdditionalAttributes().entrySet()) {
        staxWriter.addAttribute(kv.getKey().getPrefix() + ":" + kv.getKey().getLocalPart(), kv.getValue());
    }
}
Also used : CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) QName(javax.xml.namespace.QName) DMNModelInstrumentedBase(org.kie.dmn.model.v1_1.DMNModelInstrumentedBase)

Aggregations

QName (javax.xml.namespace.QName)3 CustomStaxWriter (org.kie.dmn.backend.marshalling.CustomStaxWriter)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 XStream (com.thoughtworks.xstream.XStream)1 AbstractPullReader (com.thoughtworks.xstream.io.xml.AbstractPullReader)1 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)1 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)1 StaxWriter (com.thoughtworks.xstream.io.xml.StaxWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1