Search in sources :

Example 6 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testSimpleParser.

@Test
public void testSimpleParser() throws Exception {
    MyParser parser = new MyParser();
    String xml = readResource("simple-subsystem.xml");
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);
    ModelNode resource = subsystem.get("resource", "foo");
    ModelNode complexMap = MyParser.COMPLEX_MAP.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, resource);
    Assert.assertEquals("Model type should be map", ModelType.OBJECT, complexMap.getType());
    Assert.assertEquals("Map should have 3 elements", 3, complexMap.asList().size());
    Assert.assertEquals("some.class1", complexMap.get("key1", "name").asString());
    Assert.assertEquals("some.class2", complexMap.get("key2", "name").asString());
    Assert.assertEquals("some.module3", complexMap.get("key3", "module").asString());
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 7 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project keycloak by keycloak.

the class KeycloakAdapterConfigDeploymentProcessor method getXML.

private String getXML(DeploymentUnit deploymentUnit) throws XMLStreamException {
    ModelNode node = Configuration.INSTANCE.getSecureDeployment(deploymentUnit);
    if (node != null) {
        KeycloakSubsystemParser writer = new KeycloakSubsystemParser();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        XMLExtendedStreamWriter streamWriter = new FormattingXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(output));
        try {
            streamWriter.writeStartElement("keycloak-saml-adapter");
            writer.writeSps(streamWriter, node);
            streamWriter.writeEndElement();
        } finally {
            streamWriter.close();
        }
        return new String(output.toByteArray(), Charset.forName("utf-8"));
    }
    return null;
}
Also used : FormattingXMLStreamWriter(org.jboss.staxmapper.FormattingXMLStreamWriter) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ModelNode(org.jboss.dmr.ModelNode)

Example 8 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project keycloak by keycloak.

the class KeycloakAdapterConfigDeploymentProcessor method getXML.

private String getXML(DeploymentUnit deploymentUnit) throws XMLStreamException {
    ModelNode node = Configuration.INSTANCE.getSecureDeployment(deploymentUnit);
    if (node != null) {
        KeycloakSubsystemParser writer = new KeycloakSubsystemParser();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        XMLExtendedStreamWriter streamWriter = new FormattingXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(output));
        try {
            streamWriter.writeStartElement("keycloak-saml-adapter");
            writer.writeSps(streamWriter, node);
            streamWriter.writeEndElement();
        } finally {
            streamWriter.close();
        }
        return new String(output.toByteArray(), Charset.forName("utf-8"));
    }
    return null;
}
Also used : FormattingXMLStreamWriter(org.keycloak.subsystem.saml.as7.xml.FormattingXMLStreamWriter) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ModelNode(org.jboss.dmr.ModelNode)

Example 9 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testServerParser.

@Test
public void testServerParser() throws Exception {
    ServerParser parser = new ServerParser();
    String xml = readResource("server-subsystem.xml");
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);
    ModelNode server = subsystem.get("server", "default");
    ModelNode interceptors = MyParser.INTERCEPTORS.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server);
    Assert.assertEquals("Model type should be list", ModelType.LIST, interceptors.getType());
    Assert.assertEquals("List should have 0 elements", 0, interceptors.asList().size());
    // this one should be undefined
    ModelNode complexList = MyParser.COMPLEX_LIST.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server);
    Assert.assertTrue("Should be empty", !complexList.isDefined());
    // this one should be undefined
    ModelNode complexListWithDefault = MyParser.COMPLEX_LIST_WITH_DEFAULT.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server);
    Assert.assertEquals("Model type should be list", ModelType.LIST, complexListWithDefault.getType());
    Assert.assertEquals("List should have 1 elements", 1, complexListWithDefault.asList().size());
    /*List<ModelNode> unwrapped = MyParser.COMPLEX_LIST_WITH_DEFAULT.unwrap(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
        Assert.assertEquals("it should contain one element", 1, unwrapped.size());*/
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 10 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testMail.

@Test
public void testMail() throws Exception {
    MyParser parser = new MailParser();
    String xml = readResource("mail-parser.xml");
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);
    ModelNode propsModel = subsystem.get("mail-session", "custom");
    assertTrue("Model should be defined", propsModel.has(MyParser.WRAPPED_PROPERTIES.getName()));
    Map<String, String> props = MyParser.WRAPPED_PROPERTIES.unwrap(ExpressionResolver.TEST_RESOLVER, propsModel);
    Assert.assertEquals(0, props.size());
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Aggregations

XMLExtendedStreamWriter (org.jboss.staxmapper.XMLExtendedStreamWriter)15 ModelNode (org.jboss.dmr.ModelNode)13 StringWriter (java.io.StringWriter)12 StringReader (java.io.StringReader)10 ArrayList (java.util.ArrayList)10 QName (javax.xml.namespace.QName)10 XMLStreamReader (javax.xml.stream.XMLStreamReader)10 StreamSource (javax.xml.transform.stream.StreamSource)10 XMLMapper (org.jboss.staxmapper.XMLMapper)10 Test (org.junit.Test)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 DefaultFilenameTabCompleter (org.jboss.as.cli.handlers.DefaultFilenameTabCompleter)1 FilenameTabCompleter (org.jboss.as.cli.handlers.FilenameTabCompleter)1