Java Code Examples for org.jboss.staxmapper.XMLMapper#deparseDocument()

The following examples show how to use org.jboss.staxmapper.XMLMapper#deparseDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testChildlessResource() throws Exception {
    MyParser parser = new ChildlessParser();
    String xml =
            "<subsystem xmlns=\"" + MyParser.NAMESPACE + "\">" +
                    "   <cluster attr1=\"alice\"/>" +
                    "</subsystem>";
    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);

    ModelNode subsystem = opsToModel(operations);

    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));
}
 
Example 2
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testElementParsers() throws Exception {

    MyParser parser = new MyParser();

    String xml = readResource("elements.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(5, operations.size());
    ModelNode subsystem = opsToModel(operations);

    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));
}
 
Example 3
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testServerWithComplexAttributeParser() throws Exception {
    ServerParser parser = new ServerParser();

    String xml = readResource("server-complex-attribute.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(5, operations.size());
    ModelNode subsystem = opsToModel(operations);

    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));
}
 
Example 4
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testORBSubsystem() throws Exception {
    IIOPSubsystemParser parser = new IIOPSubsystemParser();

    String xml = readResource("orb-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName("urn:jboss:domain:orb-test:1.0", "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(1, operations.size());
    ModelNode subsystem = opsToModel(operations);

    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));
}
 
Example 5
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testComplexAttributesStuff() throws Exception {
    CoreParser parser = new CoreParser();

    String xml = readResource("core-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName("urn:jboss:domain:core:1.0", "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(2, operations.size());
    Assert.assertEquals(2, operations.get(1).get("listeners").asList().size());
    ModelNode subsystem = opsToModel(operations);

    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));
}
 
Example 6
Source File: AbstractConfigurationPersister.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void marshallAsXml(final ModelNode model, final OutputStream output) throws ConfigurationPersistenceException {
    final XMLMapper mapper = XMLMapper.Factory.create();
    final Map<String, XMLElementWriter<SubsystemMarshallingContext>> localSubsystemWriters = new HashMap<>(subsystemWriters);
    try {
        XMLStreamWriter streamWriter = null;
        try {
            streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output);
            final ModelMarshallingContext extensibleModel = new ModelMarshallingContext() {

                @Override
                public ModelNode getModelNode() {
                    return model;
                }

                @Override
                public XMLElementWriter<SubsystemMarshallingContext> getSubsystemWriter(String extensionName) {
                    //lazy create writer, but only once per config serialization
                    XMLElementWriter<SubsystemMarshallingContext> result = localSubsystemWriters.get(extensionName);
                    if (result == null) {
                        Supplier<XMLElementWriter<SubsystemMarshallingContext>> supplier = subsystemWriterSuppliers.get(extensionName);
                        if (supplier != null) {
                            result = supplier.get();
                            localSubsystemWriters.put(extensionName, result);
                        }
                    }
                    return result;
                }
            };
            mapper.deparseDocument(rootDeparser, extensibleModel, streamWriter);
            streamWriter.close();
        } finally {
            safeClose(streamWriter);
        }
    } catch (Exception e) {
        throw ControllerLogger.ROOT_LOGGER.failedToWriteConfiguration(e);
    }
}
 
Example 7
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testWrappersAndGroups() throws Exception {
    MyParser parser = new MyParser();
    String xml = readResource("groups-wrappers-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);

    ModelNode subsystem = opsToModel(operations);

    assertEquals("bar", subsystem.get("resource", "foo", "cluster-attr1").asString());
    assertEquals("baz", subsystem.get("resource", "foo", "cluster-attr2").asString());
    assertEquals("alice", subsystem.get("resource", "foo", "security-my-attr1").asString());
    assertEquals("bob", subsystem.get("resource", "foo", "security-my-attr2").asString());
    assertEquals("bar2", subsystem.get("resource", "foo2", "cluster-attr1").asString());
    assertEquals("baz2", subsystem.get("resource", "foo2", "cluster-attr2").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));

}
 
Example 8
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testGroups() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml = readResource("groups-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);

    ModelNode subsystem = opsToModel(operations);

    assertEquals("bar", subsystem.get("resource", "foo", "cluster-attr1").asString());
    assertEquals("baz", subsystem.get("resource", "foo", "cluster-attr2").asString());
    assertEquals("alice", subsystem.get("resource", "foo", "security-my-attr1").asString());
    assertEquals("bob", subsystem.get("resource", "foo", "security-my-attr2").asString());
    assertEquals("val", subsystem.get("resource", "foo", "props", "prop").asString());
    assertEquals("val", subsystem.get("resource", "foo", "wrapped-properties", "prop").asString());
    assertEquals("bar2", subsystem.get("resource", "foo2", "cluster-attr1").asString());
    assertEquals("baz2", subsystem.get("resource", "foo2", "cluster-attr2").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));

}
 
Example 9
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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));
}
 
Example 10
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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));
}
 
Example 11
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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());

    ModelNode complexList = MyParser.COMPLEX_LIST.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
    Assert.assertTrue("Should be empty", !complexList.isDefined());

    ModelNode complexListWithDefault = MyParser.COMPLEX_LIST_WITH_DEFAULT.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
    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));
}